raft

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2016 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CurrentTermKey = "currentTerm"
	VotedForKey    = "votedFor"
	LocalIDKey     = "localID"
	ClusterIDKey   = "clusterID"
	LastAppliedKey = "lastApplied"
	NodeConfig     = "nodeConfig"
	WebHooks       = "webHooks"
)

* Keys for the metadata API -- each goes into the metadata collection in the storage API. * Make these hard-coded rather than "iota" because they go in a database!

View Source
const (
	// MembershipChange denotes a special message type for membership changes.
	MembershipChange = -1
	// WebHookChange denotes a change in the WebHook configuration for the
	// cluster.
	WebHookChange = -2

	// ElectionTimeout is the amount of time a node will wait once it has heard
	// from the current leader before it declares itself a candidate.
	// It must always be a small multiple of HeartbeatTimeout.
	ElectionTimeout = 10 * time.Second
	// HeartbeatTimeout is the amount of time between heartbeat messages from the
	// leader to other nodes.
	HeartbeatTimeout = 2 * time.Second
)
View Source
const (
	// NodeProposalTimeout is how long to wait for config change to work
	NodeProposalTimeout = ElectionTimeout * 2
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeTracker

type ChangeTracker struct {
	// contains filtered or unexported fields
}

A ChangeTracker allows clients to submit a change, and to wait for a change to occur. The overall effect is like a condition variable, in that waiters are notified when something changes.

func CreateTracker

func CreateTracker() *ChangeTracker

CreateTracker creates a new change tracker with "lastChange" set to zero.

func GetNamedTracker

func GetNamedTracker(name string) *ChangeTracker

GetNamedTracker retrieves a tracker from a thread-safe global table of names trackers. If no tracker with the specified name exists, then one is created.

func (*ChangeTracker) Close

func (t *ChangeTracker) Close()

Close stops the change tracker from delivering notifications.

func (*ChangeTracker) TimedWait

func (t *ChangeTracker) TimedWait(curChange uint64, maxWait time.Duration) uint64

TimedWait blocks the current gorouting until either a new value higher than "curChange" has been reached, or "maxWait" has been exceeded.

func (*ChangeTracker) Update

func (t *ChangeTracker) Update(change uint64)

Update indicates that the current sequence has changed. Wake up any waiting waiters and tell them about it.

func (*ChangeTracker) Wait

func (t *ChangeTracker) Wait(curChange uint64) uint64

Wait blocks the calling gorouting forever until the change tracker has reached a value at least as high as "curChange." Return the current value when that happens.

type LoopCommand

type LoopCommand int32

LoopCommand is used to send configuration changes to the main loop

const (
	UpdateConfiguration LoopCommand = iota
	JoinAsFollower
	JoinAsCandidate
)

* Commands to send to the main loop.

func (LoopCommand) String

func (i LoopCommand) String() string

type MembershipChangeMode

type MembershipChangeMode int32

MembershipChangeMode is the state of the current membership change process

const (
	Stable MembershipChangeMode = iota
	ProposedJointConsensus
	ProposedFinalConsensus
)

* State of the current membership change process

func (MembershipChangeMode) String

func (i MembershipChangeMode) String() string

type Node

type Node struct {
	NodeID  common.NodeID
	Address string
}

A Node represents a single node in the cluster. It has a unique ID as well as a network address.

func (Node) String

func (n Node) String() string

type NodeList

type NodeList struct {
	Current []Node
	Next    []Node
}

A NodeList is simply a list of nodes. For the purposes of joint consensus, it a list of "current" nodes (which are currently running) and an optional list of "next" nodes, which are subject to joint consensus.

func (*NodeList) GetNode

func (nl *NodeList) GetNode(id common.NodeID) *Node

GetNode returns information about a single node in the list, or nil if the node does not exist.

func (*NodeList) GetUniqueNodes

func (nl *NodeList) GetUniqueNodes() []Node

GetUniqueNodes returns only the unique nodes. This is helpful when in joint consensus mode.

func (*NodeList) String

func (nl *NodeList) String() string

type ProtocolStatus

type ProtocolStatus struct {
	// If this node is the leader, a map of the indices of each peer.
	// Otherwise nil.
	PeerIndices *map[common.NodeID]uint64
}

ProtocolStatus returns some of the diagnostic information from the raft engine.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is an instance of code that implements the Raft protocol. It relies on the Storage, Discovery, and Communication services to do its work, and invokes the StateMachine when changes are committed.

func StartRaft

func StartRaft(
	comm communication.Communication,
	stor storage.Storage,
	state StateMachine) (*Service, error)

StartRaft starts an instance of the raft implementation running. It will start at least one goroutine for its implementation of the protocol, and others to communicate with other nodes.

func (*Service) AddNode

func (r *Service) AddNode(addr string) error

AddNode starts the process to add a new node to the cluster. It does this by creating a new membership list, and then proposing it to the cluster.

func (*Service) Append

Append is called by the commnunication service when the leader has a new item to append to the index.

func (*Service) Close

func (r *Service) Close()

Close shuts the service down and stops its goroutines. It does not close the database, however.

func (*Service) GetAppliedTracker

func (r *Service) GetAppliedTracker() *ChangeTracker

GetAppliedTracker returns a change tracker that can be used to wait until a particular change number has been applied. This allows a caller who recently proposed a new value to wait until the value has been applied to a quorum of cluster nodes.

func (*Service) GetClusterID

func (r *Service) GetClusterID() common.NodeID

GetClusterID returns the unique identifier of the cluster where this instance of the service runs. If the node is not in a cluster, then the cluster ID will be zero.

func (*Service) GetCommitIndex

func (r *Service) GetCommitIndex() uint64

GetCommitIndex returns the current index that has been committed to a quorum of nodes.

func (*Service) GetCurrentTerm

func (r *Service) GetCurrentTerm() uint64

GetCurrentTerm returns the current Raft term.

func (*Service) GetFirstIndex

func (r *Service) GetFirstIndex() (uint64, error)

GetFirstIndex returns the lowest index that exists in the local raft log.

func (*Service) GetLastApplied

func (r *Service) GetLastApplied() uint64

GetLastApplied returns the current index that has been applied to this local node.

func (*Service) GetLastIndex

func (r *Service) GetLastIndex() (uint64, uint64)

GetLastIndex returns the highest index that exists in the local raft log, and the corresponding term for that index.

func (*Service) GetLeaderID

func (r *Service) GetLeaderID() common.NodeID

GetLeaderID returns the unique ID of the leader node, or zero if there is currently no known leader.

func (*Service) GetMembershipChangeMode

func (r *Service) GetMembershipChangeMode() MembershipChangeMode

GetMembershipChangeMode gives us the status of the current process of changing cluster membership.

func (*Service) GetNodeConfig

func (r *Service) GetNodeConfig() *NodeList

GetNodeConfig returns the current configuration of this raft node, which means the configuration that is currently running (as oppopsed to what has been proposed.

func (*Service) GetRaftStatus

func (r *Service) GetRaftStatus() ProtocolStatus

GetRaftStatus returns some status information about the Raft engine that requires us to access internal state.

func (*Service) GetState

func (r *Service) GetState() State

GetState returns the state of this Raft node in a thread-safe way.

func (*Service) GetWebHooks

func (r *Service) GetWebHooks() []hooks.WebHook

GetWebHooks returns the set of WebHook configuration that is currently configured for this node.

func (*Service) InitializeCluster

func (r *Service) InitializeCluster(addr string) error

InitializeCluster sets the node up to be able to add nodes to a cluster. It should be called once and only once on the first node in a cluster. After it has been called, it is possible to call AddNode to add more nodes.

The "address" parameter is the network address in host:port format that other nodes should use to contact this node. It should not be a "localhost" address unless the whole cluster runs on localhost. The address will be sent to the other nodes in the cluster which is why it needs to be an address that they can reach.

func (*Service) Join

func (r *Service) Join(req communication.JoinRequest) (uint64, error)

Join is called by the communication service when we are being added to a new cluster and we need to catch up.

func (*Service) MyID

func (r *Service) MyID() common.NodeID

MyID returns the unique ID of this Raft node.

func (*Service) Propose

func (r *Service) Propose(e *common.Entry) (uint64, error)

Propose is called by anyone who wants to propose a new change. It will return with the change number of the new change. However, that change number will not necessarily have been committed yet.

func (*Service) RemoveNode

func (r *Service) RemoveNode(nodeID common.NodeID) error

RemoveNode starts the process to remove a node from the cluster. It does this by creating a new membership list, and then proposing it to the cluster.

func (*Service) RequestVote

RequestVote is called from the communication interface when another node requests a vote.

func (*Service) UpdateWebHooks

func (r *Service) UpdateWebHooks(webHooks []hooks.WebHook) (uint64, error)

UpdateWebHooks updates the configuration of web hooks for the cluster by propagating a special change record to all the nodes. A web hook is a particular web service URI that the leader will invoke before trying to commit any new change -- if any one of the hooks fails, the leader will not make the change.

type State

type State int32

State is the current state of the Raft implementation.

const (
	Follower State = iota
	Candidate
	Leader
	Standalone
	Stopping
	Stopped
)

* State of this particular node.

func (State) String

func (i State) String() string

type StateMachine

type StateMachine interface {
	Commit(entry *common.Entry) error
}

A StateMachine is an interface that is notified whenever a new change is committed in the raft log. (A commit only happens when a quorum of nodes have accepted a new proposal, and the leader decides to increment the commit sequence.) Users of this module may implement this interface so that they can take action when a change is committed. For instance, they can update a database.

Jump to

Keyboard shortcuts

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