Documentation ¶
Index ¶
- Variables
- func ReadLogs(filename string) *raft.LogStore
- func ReadTerm(filename string) *raft.TermRecord
- func WriteLogs(filename string, logStore *raft.LogStore) error
- func WriteTerm(filename string, termRecord *raft.TermRecord) error
- type ForeignNode
- type ForeignNodeChecker
- type Node
- func (n *Node) AddForeignNode(addr string)
- func (n *Node) Delete(key string) error
- func (n *Node) DoElection() bool
- func (n *Node) HandleAppend(req *raft.AppendRequest) *raft.AppendReply
- func (n *Node) HandleVote(req *raft.VoteRequest) *raft.VoteReply
- func (n *Node) RedirectLeader() string
- func (n *Node) SendAppend(retriesRemaining int, term int64) error
- func (n *Node) Set(key string, value string) error
- func (n *Node) SetTerm(newTerm int64, votedFor *raft.Node) error
- type NodeConfig
- type Role
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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 ¶
AddForeignNode updates the list of known other members of the raft cluster
func (*Node) Delete ¶
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 ¶
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 ¶
RedirectLeader provides the leader which we want to redirect requests to if we are not the leader at present
func (*Node) SendAppend ¶
SendAppend sends out append-logs requests to each other node in the cluster, and updates database state on majority success
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