node

package
v0.1.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Follower  Role = "Follower"
	Candidate      = "Candidate"
	Leader         = "Leader"
)

A Follower is a read-only member of a cluster A Candidate solicits votes to become a Leader A Leader is a read/write member of a cluster

Variables

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

	// ErrAppendFailed indicates that an append job ran out of retry attempts
	// without successfully appending to a majorit 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
}

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

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 {
	Value           map[string]string
	NodeId          string
	ElectionTimeout time.Duration

	State Role

	Term int64

	CheckForeignNode ForeignNodeChecker

	Log *raft.LogStore

	Store *db.Database
	// 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 one time, its role may be Leader, Candidate, or Follower, and have different responsibilities depending on its role

func NewNode

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

NewNode initializes a Node with a randomized election timeout between 150-300ms, and starts the election timer

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

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

func (n *Node) Halt()

Halt is used to stop all timers (primarily in test)

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

type NodeConfig

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

NodeConfig contains configurable properties for a node

func NewNodeConfig

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

NewNodeConfig creates a config for a Node

type Role

type Role string

A Role is one of Leader, Candidate, or Follower

Jump to

Keyboard shortcuts

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