cluster

package
v0.0.0-...-560f09a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2023 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

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
)

func (EventType) String

func (t EventType) String() string

type FollowerRoutine

type FollowerRoutine func(ctx context.Context) error

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 KV

type KV struct {
	Key   string
	Value string
}

KV

Simple key-value pair

type LeaderRoutine

type LeaderRoutine func(ctx context.Context) error

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

type NodeMetadata struct {
	ID      int64
	Address string
	Start   time.Time
	Role    NodeRole
}

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 NodeRole

type NodeRole int
const (
	NodeRoleUnknown NodeRole = iota
	NodeRoleLeader
	NodeRoleFollower
)

func (NodeRole) String

func (s NodeRole) String() string

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) Start

func (n *StandaloneNode) Start()

Start

Starts the StandaloneNode

func (*StandaloneNode) Stop

func (n *StandaloneNode) Stop()

Stop

Stops the StandaloneNode

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.

type StateChangeEvent

type StateChangeEvent struct {
	Type     EventType
	NodeID   int64
	Key      string
	Value    string
	OldKey   string
	OldValue string
}

StateChangeEvent

An event in which the state of state kv pair changes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL