node

package
v0.2.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotLeaderRecv indicates that a client attempted to make a write to a
	// node that is not currently the leader of the cluster
	ErrNotLeaderRecv = errors.New("Cannot accept writes if not leader")

	// ErrNotLeaderSend indicates that a server attempted to send an append
	// request while it is not the leader of the cluster
	ErrNotLeaderSend = errors.New("Cannot send log append request if not leader")

	// ErrExpiredTerm indicates that an append request was generated for a past
	// term, so it should not be sent
	ErrExpiredTerm = errors.New("Do not send append requests for expired terms")

	// ErrAppendFailed indicates that an append job ran out of retry attempts
	// without successfully appending to a majority of nodes
	ErrAppendFailed = errors.New("Failed to append logs to a majority of nodes")

	// ErrCommitFailed indicates that the leader's commit index after append
	// is less than the index of the record being added
	ErrCommitFailed = errors.New("Failed to commit record")

	// ErrAppendRangeMet indicates that reverse-iteration has reached the
	// beginning of the log and still not gotten a response--aborting
	ErrAppendRangeMet = errors.New("Append range reached, not trying again")
)

Functions

func ReadLogs

func ReadLogs(filename string) *raft.LogStore

ReadLogs attempts to unmarshal and return a LogStore from the specified file, and if unable to do so returns an empty LogStore

func ReadTerm

func ReadTerm(filename string) *raft.TermRecord

ReadTerm attempts to unmarshal and return a TermRecord from the specified file, and if unable to do so returns an initialized TermRecord

func WriteLogs

func WriteLogs(filename string, logStore *raft.LogStore) error

WriteLogs persists the node's log

func WriteTerm

func WriteTerm(filename string, termRecord *raft.TermRecord) error

WriteTerm persists the node's most recent term and vote

Types

type ForeignNode

type ForeignNode struct {
	Connection *grpc.ClientConn
	Client     raft.RaftClient
	NextIndex  int64
	MatchIndex int64
	Available  bool
}

A ForeignNode is another member of the cluster, with connections needed to manage gRPC interaction with that node and track recent availability

func NewForeignNode

func NewForeignNode(address string) (*ForeignNode, error)

NewForeignNode constructs a ForeignNode from an address ("host:port")

func (*ForeignNode) Close

func (f *ForeignNode) Close()

Close cleans up the gRPC connection with the foreign node

type ForeignNodeChecker

type ForeignNodeChecker func(string, map[string]*ForeignNode) bool

ForeignNodeChecker functions are used to determine if a request comes from a valid participant in a cluster. It should generally check against a configuration file or other canonical record of membership, but can also be mocked out for test to cause a Node to respond to RPC requests without creating a full multi-node deployment.

type Node

type Node struct {
	RaftNode *raft.Node
	State    Role
	Term     int64

	Reset chan bool

	CheckForeignNode ForeignNodeChecker
	AllowVote        bool
	CommitIndex      int64

	Log *raft.LogStore

	Store *db.Database
	sync.Mutex
	// contains filtered or unexported fields
}

A Node is one member of a Raft cluster, with all state needed to operate the algorithm's state machine. At any time, its role may be Leader, Candidate, or Follower, and have different responsibilities depending on its role (note that Candidate is a virtual role--a Candidate does not behave differently from a Follower w.r.t. incoming messages, so the node will remain in the Follower state while an election is in progress)

func NewNode

func NewNode(config NodeConfig, store *db.Database) (*Node, error)

NewNode initializes a Node with a randomized election timeout

func (*Node) AddForeignNode

func (n *Node) AddForeignNode(addr string)

AddForeignNode updates the list of known other members of the raft cluster

func (*Node) Delete

func (n *Node) Delete(key string) error

Delete appends a delete entry to the log record, and returns once the update is applied to the state machine or an error is generated

func (*Node) DoElection

func (n *Node) DoElection() bool

DoElection sends out requests for votes to each other node in the Raft cluster. When a Raft node's role is "candidate", it should send start an election. If it is granted votes from a majority of nodes, its role changes to "leader". If it receives an append-logs message during the election from a node with a term higher than this node's current term, its role changes to "follower". If it does not receive a majority of votes and also does not receive an append-logs from a valid leader, it increments the term and starts another election (repeat until a leader is elected).

func (*Node) HandleAppend

func (n *Node) HandleAppend(req *raft.AppendRequest) *raft.AppendReply

HandleAppend responds to append-log messages from leader nodes

func (*Node) HandleVote

func (n *Node) HandleVote(req *raft.VoteRequest) *raft.VoteReply

HandleVote responds to vote requests from candidate nodes

func (*Node) RedirectLeader

func (n *Node) RedirectLeader() string

RedirectLeader provides the leader which we want to redirect requests to if we are not the leader at present

func (*Node) SendAppend

func (n *Node) SendAppend(retriesRemaining int, term int64) error

SendAppend sends out append-logs requests to each other node in the cluster, and updates database state on majority success

func (*Node) Set

func (n *Node) Set(key string, value string) error

Set appends a write entry to the log record, and returns once the update is applied to the state machine or an error is generated

func (*Node) SetTerm

func (n *Node) SetTerm(newTerm int64, votedFor *raft.Node) error

SetTerm records term and vote in non-volatile state

type NodeConfig

type NodeConfig struct {
	Id         string
	ClientAddr string
	DataDir    string
	TermFile   string
	LogFile    string
	NodeIds    []string
}

NodeConfig contains configurable properties for a node

func NewNodeConfig

func NewNodeConfig(dataDir string, addr, clientAddr string, nodeIds []string) NodeConfig

NewNodeConfig creates a config for a Node

type Role

type Role string

Role is either Leader or Follower

const (
	Leader   Role = "Leader"
	Follower      = "Follower"
)

Follower is a read-only member of a cluster Leader is a read/write member of a cluster

Jump to

Keyboard shortcuts

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