Documentation
¶
Index ¶
- Constants
- Variables
- type ClusterNode
- func (n *ClusterNode) Close() error
- func (n *ClusterNode) Delete(key string) error
- func (n *ClusterNode) Get(key string) (string, error)
- func (n *ClusterNode) GetAsNode(nodeId int64, key string) (string, error)
- func (n *ClusterNode) GetCluster(key string) (map[int64][]KV, error)
- func (n *ClusterNode) GetLeader() (int64, error)
- func (n *ClusterNode) GetNodeMetadata(id int64) (*NodeMetadata, error)
- func (n *ClusterNode) GetNodes() ([]NodeMetadata, error)
- func (n *ClusterNode) GetSelfMetadata() NodeMetadata
- func (n *ClusterNode) Put(key string, value string) error
- func (n *ClusterNode) Start()
- func (n *ClusterNode) Stop()
- func (n *ClusterNode) WatchKeyCluster(ctx context.Context, key string) (chan StateChangeEvent, error)
- type ClusterNodeOptions
- type EventType
- type FollowerRoutine
- type KV
- type LeaderRoutine
- type Node
- type NodeMetadata
- type NodeRole
- type StandaloneNode
- func (n *StandaloneNode) Close() error
- func (n *StandaloneNode) Delete(key string) error
- func (n *StandaloneNode) Get(key string) (string, error)
- func (n *StandaloneNode) GetAsNode(nodeId int64, key string) (string, error)
- func (n *StandaloneNode) GetCluster(key string) (map[int64][]KV, error)
- func (n *StandaloneNode) GetLeader() (int64, error)
- func (n *StandaloneNode) GetNodeMetadata(id int64) (*NodeMetadata, error)
- func (n *StandaloneNode) GetNodes() ([]NodeMetadata, error)
- func (n *StandaloneNode) GetSelfMetadata() NodeMetadata
- func (n *StandaloneNode) Put(key string, value string) error
- func (n *StandaloneNode) Start()
- func (n *StandaloneNode) Stop()
- func (n *StandaloneNode) WatchKeyCluster(ctx context.Context, key string) (chan StateChangeEvent, error)
- type StateChangeEvent
Constants ¶
View Source
const ( ElectionPrefix = "election" StateDataPrefix = "state-data" NodesPrefix = "nodes" )
Variables ¶
View Source
var ( ErrClusterTickDisagreement = errors.New("cluster tick disagreement") ErrNoLease = errors.New("no lease") )
Functions ¶
This section is empty.
Types ¶
type ClusterNode ¶
type ClusterNode struct { ID int64 Address string Role NodeRole Ttl time.Duration ClusterName string StartTime time.Time // contains filtered or unexported fields }
ClusterNode
ClusterNode in a distributed cluster capable of handling leader elections
func NewClusterNode ¶
func NewClusterNode(opts ClusterNodeOptions) (*ClusterNode, error)
NewClusterNode
Creates a new cluster node with a connection to the etcd cluster.
func (*ClusterNode) Close ¶
func (n *ClusterNode) Close() error
Close
Closes the node and its connection to the etcd cluster. A closed node cannot be restarted.
func (*ClusterNode) Delete ¶
func (n *ClusterNode) Delete(key string) error
Delete
Removes the value associated with a given key from the cluster correlated to the node. If the key does not exist, the method will return nil.
func (*ClusterNode) Get ¶
func (n *ClusterNode) Get(key string) (string, error)
Get
Retrieves the value associated with a given key from the cluster correlated to the node. If the key does not exist, the method will return an empty string.
func (*ClusterNode) GetAsNode ¶
func (n *ClusterNode) GetAsNode(nodeId int64, key string) (string, error)
GetAsNode
Retrieves the value associated with a given key from the cluster correlated to the passed node id. If the key does not exist, the method will return an empty string.
func (*ClusterNode) GetCluster ¶
func (n *ClusterNode) GetCluster(key string) (map[int64][]KV, error)
GetCluster
Retrieves the values associated with a given prefix for all nodes in the cluster and returns the values in a map where each key is the node's id and the value is the value associated with that node. Every node in the cluster will return a key in the map. If a node does not have a value for the key the value will be an empty string for that node's id.
func (*ClusterNode) GetLeader ¶
func (n *ClusterNode) GetLeader() (int64, error)
GetLeader
Retrieves the current leader for the cluster. Returns -1 if there is currently no leader for the cluster.
func (*ClusterNode) GetNodeMetadata ¶
func (n *ClusterNode) GetNodeMetadata(id int64) (*NodeMetadata, error)
GetNodeMetadata
Returns the specified node's metadata
func (*ClusterNode) GetNodes ¶
func (n *ClusterNode) GetNodes() ([]NodeMetadata, error)
GetNodes
Retrieves all the nodes in the cluster.
func (*ClusterNode) GetSelfMetadata ¶
func (n *ClusterNode) GetSelfMetadata() NodeMetadata
GetSelfMetadata
Returns the called node's metadata
func (*ClusterNode) Put ¶
func (n *ClusterNode) Put(key string, value string) error
Put
Adds a new key-value pair to the cluster that is bound to the node. If the node times out, the key-value pair will be dropped from the cluster. If the key already exists, it will be overwritten.
func (*ClusterNode) Start ¶
func (n *ClusterNode) Start()
Start
Initiate a connection to the etcd cluster and begin campaigning to become leader. Once a role in the cluster has been established, the node begins its work as a leader or follower in the background.
func (*ClusterNode) Stop ¶
func (n *ClusterNode) Stop()
Stop
Stops an active node within the cluster gracefully. The node will cease all participation in the cluster at its earliest convenience. If the node is a leader, it will resign its position as leader before stopping. Stopping removes the node from the cluster but preserves the connection to the etcd cluster such that the node can be started again.
func (*ClusterNode) WatchKeyCluster ¶
func (n *ClusterNode) WatchKeyCluster(ctx context.Context, key string) (chan StateChangeEvent, error)
WatchKeyCluster
Watches to changes for a particular key in any of the nodes in the cluster.
type ClusterNodeOptions ¶
type ClusterNodeOptions struct { Ctx context.Context ID int64 Address string Ttl time.Duration ClusterName string EtcdConfig etcd.Config LeaderRoutine LeaderRoutine FollowerRoutine FollowerRoutine RoutineTick time.Duration Logger logging.Logger }
ClusterNodeOptions
Options for a cluster node
type EventType ¶
type EventType int
EventType
Type of event that occurred to the state
const ( // EventTypeAdded value has been added to the state and previously did not exist EventTypeAdded EventType = iota // EventTypeModified value has been modified in the state and previously existed EventTypeModified // EventTypeDeleted value has been removed from the state and previously existed EventTypeDeleted )
type FollowerRoutine ¶
FollowerRoutine
Called by the follower every 50ms. The function should exit as quickly as possible on a context cancel and encompass an entire execution cycle for the follower. Any logic container in this function that should not be executed as frequently as 50ms should track the time between executions independently and skip cycles that are not appropriate for such logic.
type LeaderRoutine ¶
LeaderRoutine
Called by the leader every 50ms. The function should exit as quickly as possible on a context cancel and encompass an entire execution cycle for the leader. Any logic container in this function that should not be executed as frequently as 50ms should track the time between executions independently and skip cycles that are not appropriate for such logic.
type Node ¶
type Node interface { // Start // // Initiate a connection to the etcd cluster and // begin campaigning to become leader. Once a role // in the cluster has been established, the node // begins its work as a leader or follower in the // background. Start() // Stop // // Stops an active node within the cluster gracefully. // The node will cease all participation in the cluster // at its earliest convenience. If the node is a leader, // it will resign its position as leader before stopping. // Stopping removes the node from the cluster but preserves // the connection to the etcd cluster such that the node // can be started again. Stop() // Close // // Closes the node and its connection to the etcd cluster. // A closed node cannot be restarted. Close() error // Put // // Adds a new key-value pair to the cluster that is bound // to the node. If the node times out, the key-value pair // will be dropped from the cluster. If the key already // exists, it will be overwritten. Put(key string, value string) error // Get // // Retrieves the value associated with a given key from // the cluster correlated to the node. If the key does // not exist, the method will return an empty string. Get(key string) (string, error) // GetAsNode // // Retrieves the value associated with a given key from // the cluster correlated to the passed node id. If the // key does not exist, the method will return an empty // string. GetAsNode(nodeId int64, key string) (string, error) // GetCluster // // Retrieves the values associated with a given prefix for // all nodes in the cluster and returns the values in a // map where each key is the node's id and the value is // a slice of key value pairs found with passed prefix // for the node with the node id stripped from the key. // // Every node in the cluster will return a key in the map. // If a node does not have a value for the key the value // will be an empty slice for that node's id. GetCluster(key string) (map[int64][]KV, error) // Delete // // Removes the value associated with a given key from // the cluster correlated to the node. If the key does // not exist, the method will return nil. Delete(key string) error // WatchKeyCluster // // Watches to changes for a particular key in any of the // nodes in the cluster. WatchKeyCluster(ctx context.Context, key string) (chan StateChangeEvent, error) // GetNodes // // Retrieves all the nodes in the cluster. GetNodes() ([]NodeMetadata, error) // GetLeader // // Retrieves the current leader for the cluster. Returns // -1 if there is currently no leader for the cluster. GetLeader() (int64, error) // GetSelfMetadata // // Returns the called node's metadata GetSelfMetadata() NodeMetadata // GetNodeMetadata // // Returns the specified node's metadata and nil if the node is not found GetNodeMetadata(id int64) (*NodeMetadata, error) }
Node
Interface to interact with a cluster node
type NodeMetadata ¶
NodeMetadata
Metadata for a node that is persisted in the cluster state
func UnmarshalNodeMetadata ¶
func UnmarshalNodeMetadata(data []byte) (NodeMetadata, error)
func (*NodeMetadata) Marshal ¶
func (m *NodeMetadata) Marshal() ([]byte, error)
type StandaloneNode ¶
type StandaloneNode struct { ID int64 Address string StartTime time.Time Role NodeRole // contains filtered or unexported fields }
StandaloneNode
StandaloneNode mocks the Node interface but executes the roles of leader and follower itself
func NewStandaloneNode ¶
func NewStandaloneNode(ctx context.Context, id int64, address string, leaderRoutine LeaderRoutine, followerRoutine FollowerRoutine, tick time.Duration, logger logging.Logger) *StandaloneNode
NewStandaloneNode
Creates a new StandaloneNode
func (*StandaloneNode) Close ¶
func (n *StandaloneNode) Close() error
Close
Closes the StandaloneNode This function is a no-op but is needed to satisfy the Node interface
func (*StandaloneNode) Delete ¶
func (n *StandaloneNode) Delete(key string) error
Delete
Removes the value associated with a given key from the cluster correlated to the node. If the key does not exist, the method will return nil.
func (*StandaloneNode) Get ¶
func (n *StandaloneNode) Get(key string) (string, error)
Get
Retrieves the value associated with a given key from the cluster correlated to the node. If the key does not exist, the method will return an empty string.
func (*StandaloneNode) GetAsNode ¶
func (n *StandaloneNode) GetAsNode(nodeId int64, key string) (string, error)
GetAsNode
This is a compliance function for the Node interface. The function will operate the same as Get when provided with the self-id of the node instance and will return an empty string for any other id.
func (*StandaloneNode) GetCluster ¶
func (n *StandaloneNode) GetCluster(key string) (map[int64][]KV, error)
GetCluster
Retrieves the values associated with a given prefix for all nodes in the cluster and returns the values in a map where each key is the node's id and the value is a slice of key value pairs found with passed prefix for the node with the node id stripped from the key. Every node in the cluster will return a key in the map. If a node does not have a value for the key the value will be an empty slice for that node's id.
func (*StandaloneNode) GetLeader ¶
func (n *StandaloneNode) GetLeader() (int64, error)
GetLeader
Retrieves the current leader for the cluster. Returns -1 if there is currently no leader for the cluster.
func (*StandaloneNode) GetNodeMetadata ¶
func (n *StandaloneNode) GetNodeMetadata(id int64) (*NodeMetadata, error)
GetNodeMetadata
Returns the specified node's metadata and nil if the node is not found
func (*StandaloneNode) GetNodes ¶
func (n *StandaloneNode) GetNodes() ([]NodeMetadata, error)
GetNodes
Retrieves all the nodes in the cluster.
func (*StandaloneNode) GetSelfMetadata ¶
func (n *StandaloneNode) GetSelfMetadata() NodeMetadata
GetSelfMetadata
Returns the called node's metadata
func (*StandaloneNode) Put ¶
func (n *StandaloneNode) Put(key string, value string) error
Put
Adds a new key-value pair to the cluster that is bound to the node. If the node times out, the key-value pair will be dropped from the cluster. If the key already exists, it will be overwritten.
func (*StandaloneNode) WatchKeyCluster ¶
func (n *StandaloneNode) WatchKeyCluster(ctx context.Context, key string) (chan StateChangeEvent, error)
WatchKeyCluster
Watches to changes for a particular key in any of the nodes in the cluster.
Click to show internal directories.
Click to hide internal directories.