raft

package
v0.5.6 Latest Latest
Warning

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

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

Documentation

Index

Constants

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

* 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
	// PurgeRequest denotes that the leader would like to propose purging all
	// records older than the specified index. Body is just a change number
	// encoded using a "varint".
	// -2 and -3 was used in an old version
	PurgeRequest = -4
	// ConfigChange denotes a new configuration file that describes various
	// parameters about the implementation
	ConfigChange = -5
)

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. This work is done using a goroutine, which is simpler and faster than the equivalent using a condition variable.

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 (
	UpdateNodeConfiguration LoopCommand = iota
	JoinAsFollower
	JoinAsCandidate
	UpdateRaftConfiguration
)

* 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) MarshalJSON added in v0.5.2

func (n Node) MarshalJSON() ([]byte, error)

MarshalJSON creates the JSON for this object because otherwise the built-in encoder does not encode the NodeID properly to string.

func (Node) String

func (n Node) String() string

type NodeList

type NodeList struct {
	Current []Node `json:"current"`
	Next    []Node `json:"next"`
}

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,
	configFile string) (*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) GetRaftConfig added in v0.5.2

func (r *Service) GetRaftConfig() *config.State

GetRaftConfig returns details about the state of the node, including cluster status.

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) RemoveNodeForcibly added in v0.5.2

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

RemoveNodeForcibly removes knowledge of a node from the local state, with no consideration to what is going on in the rest of the cluster. It can result in an inconsistent cluster configuration which can cause inconsistent data.

This method is useful (and essential) in the event that an attempt to add a new node has failed and the cluster state must be fixed locally because quorum cannot be reached until the cluster state is fixed.

func (*Service) RequestVote

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

func (*Service) UpdateConfiguration added in v0.5.4

func (r *Service) UpdateConfiguration(cfg []byte) (uint64, error)

UpdateConfiguration updates configuration of various aspects of the implementation. The configuration will be pushed to the other nodes just like any other change. It doesn't get actually applied until it gets proposed to the various other nodes. This configuration will replace the configuration files on every node in the cluster. The input is a set of YAML that matches the YAML configuration syntax, as a byte slice.

func (*Service) UpdateLiveConfiguration added in v0.5.4

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

UpdateLiveConfiguration makes the current configuration of this node (as returned by GetRaftConfig) the live one across the cluster. It will also update the local configuration file.

func (*Service) WaitForCommit added in v0.5.2

func (r *Service) WaitForCommit(ix uint64) error

WaitForCommit blocks the caller until the specified index has been applied across the quorum. It is useful for APIs that want to wait for consistency before reporting to the user. It blocks for a maximum of two election timeouts, which means that updates will always work as long as the cluster is capable of electing a leader.

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