Documentation ¶
Index ¶
- Constants
- Variables
- type Cluster
- type DynamicCluster
- func (c *DynamicCluster) AllNodes() map[uint64]Node
- func (c *DynamicCluster) GetNode(id uint64) (Node, error)
- func (c *DynamicCluster) Join(otherAddr string, raftNode Node) error
- func (c *DynamicCluster) Leave() error
- func (c *DynamicCluster) OnMembershipChange(peer memlist.Node)
- func (c *DynamicCluster) Quorum() int
- type FSM
- type InMemStore
- func (m *InMemStore) AllLogs() ([]*Log, error)
- func (m *InMemStore) AppendLogs(logs []*Log) error
- func (m *InMemStore) DeleteRange(min, max int64) error
- func (m *InMemStore) Get(key []byte) ([]byte, error)
- func (m *InMemStore) GetLog(index int64) (*Log, error)
- func (m *InMemStore) LastIndex() int64
- func (m *InMemStore) LastTerm() uint64
- func (m *InMemStore) Set(key, value []byte) error
- type LeaderError
- type Log
- type LogStore
- type Node
- type Options
- type Raft
- type StableStore
- type StaticCluster
- type Task
Constants ¶
const ( Follower raftState = 'F' Candidate raftState = 'C' Leader raftState = 'L' )
Variables ¶
var ( Entry logType = 'E' Snapshot logType = 'S' )
var ( // ErrRaftShutdown is thrown when a client operation has been issued after // the raft instance has shutdown. ErrRaftShutdown = errors.New("raft has already shutdown") // DefaultOpts provide a general baseline configuration setting for the raft // node such as election timeouts and log threshold. DefaultOpts = Options{ MinElectionTimeout: 150 * time.Millisecond, MaxElectionTimout: 300 * time.Millisecond, HeartBeatTimout: 100 * time.Millisecond, SnapshotTimer: 1 * time.Second, LogThreshold: 200, } SlowOpts = Options{ MinElectionTimeout: 1 * time.Second, MaxElectionTimout: 3 * time.Second, HeartBeatTimout: 500 * time.Millisecond, SnapshotTimer: 8 * time.Second, LogThreshold: 5, } )
var ( ErrLogNotFound = errors.New("the log could not be found in the storage") ErrFailedToStore = errors.New("new log failed to properly be stored") )
Functions ¶
This section is empty.
Types ¶
type Cluster ¶
Cluster keeps track of all other nodes and their addresses. It also holds agreed upon constants such as heart beat time and election timeout.
type DynamicCluster ¶
type DynamicCluster struct {
// contains filtered or unexported fields
}
func NewDynamicCluster ¶
func NewDynamicCluster(port uint16) (*DynamicCluster, error)
func (*DynamicCluster) AllNodes ¶
func (c *DynamicCluster) AllNodes() map[uint64]Node
func (*DynamicCluster) Leave ¶
func (c *DynamicCluster) Leave() error
func (*DynamicCluster) OnMembershipChange ¶
func (c *DynamicCluster) OnMembershipChange(peer memlist.Node)
func (*DynamicCluster) Quorum ¶
func (c *DynamicCluster) Quorum() int
type FSM ¶
type FSM interface { // Apply will be invoked when a log has been successfully committed and // should then be applied upon the state of the fsm. Apply(data []byte) error // Snapshot will create a byte slice representation of all the required data // to represent the current state of the machine. Snapshot() ([]byte, error) // Restore the entire state of the FSM to a starting state. Restore(cmd []byte) error }
FSM (finite state machine) defines an interface that must be implemented by the client to receive commands sent by the raft Cluster.
type InMemStore ¶
type InMemStore struct {
// contains filtered or unexported fields
}
InMemStore is an implementation of the StableStore and LogStore interface. Since it is in-memory, all data is lost on shutdown.
NOTE: This implementation is meant for testing and example use-cases and is NOT meant to be used in any production environment. It is up to the client to create the persistence store.
func NewMemStore ¶
func NewMemStore() *InMemStore
func (*InMemStore) AllLogs ¶
func (m *InMemStore) AllLogs() ([]*Log, error)
func (*InMemStore) AppendLogs ¶
func (m *InMemStore) AppendLogs(logs []*Log) error
func (*InMemStore) DeleteRange ¶
func (m *InMemStore) DeleteRange(min, max int64) error
func (*InMemStore) LastIndex ¶
func (m *InMemStore) LastIndex() int64
func (*InMemStore) LastTerm ¶
func (m *InMemStore) LastTerm() uint64
func (*InMemStore) Set ¶
func (m *InMemStore) Set(key, value []byte) error
type LeaderError ¶
LeaderError is an error that is returned when a request that is only meant for the leader is sent to a follower or candidate.
func NewLeaderError ¶
func NewLeaderError(id uint64, addr string) *LeaderError
func (*LeaderError) Error ¶
func (l *LeaderError) Error() string
type Log ¶
type Log struct { // Type is the kind of log that this represents. Type logType // Index represents the index in the list of log entries. Index int64 // Term contains the election term it was added. Term uint64 // Cmd represents the command applied to the FSM. Cmd []byte }
Log entries represent commands that alter the state of the FSM. These entries are replicated across a majority of raft instances before being considered as committed.
type LogStore ¶
type LogStore interface { // LastIndex will return the index of the last log entry that has // been added to the log storage. LastIndex() int64 // LastTerm will return the last log term found in the list of log entries. LastTerm() uint64 // GetLog will return the log found at the given index. An error will // be returned if the index exceeds the maximum index in the log. // // If the index is less than the minimum index, than the log at minimum // index will be returned instead. GetLog(index int64) (*Log, error) // AllLogs retrieves every log entry in the store and returns the result. AllLogs() ([]*Log, error) // AppendLogs will add the slice of logs to the current list of log entries. AppendLogs(logs []*Log) error // DeleteRange will remove all log entries starting from the min index all // the way to the max index (inclusive). DeleteRange(min, max int64) error }
LogStore defines how a raft's log persistence is handled and the required operations for log replication to be successful.
type Options ¶
type Options struct { // Range of possible timeouts for elections or for // no heartbeats from the leader. MinElectionTimeout time.Duration MaxElectionTimout time.Duration // Set time between heart beats (append entries) that the leader // should send out. HeartBeatTimout time.Duration // SnapshotTimer is the period of time between the raft's attempts at making a // snapshot of the current state of the FSM. Although a snapshot is attempted periodically // it is not guaranteed that a snapshot will be completed unless the LogThreshold is met. SnapshotTimer time.Duration // LogThreshold represents the total number of log entries that should be reached // before log compaction (snapshot) is triggered. LogThreshold uint64 }
Options defines required constants that the raft will use while running.
This library provides about some predefined options to use instead of defining your own options configurations.
type Raft ¶
type Raft struct {
// contains filtered or unexported fields
}
Raft represents a node within the entire raft static. It contains the core logic of the consensus algorithm such as keeping track of leaders, replicated logs and other important state.
func New ¶
func New(c Cluster, id uint64, opts Options, fsm FSM, logStr LogStore, stableStr StableStore) (*Raft, error)
New creates a new raft node and registers it with the provided Cluster.
func (*Raft) Apply ¶
Apply takes a command and attempts to propagate it to the FSM and all other replicas in the raft Cluster. A Task is returned which can be used to wait on the completion of the task.
func (*Raft) ListenAndServe ¶
ListenAndServe will start the raft instance and listen using TCP. The listening on the address that is provided as an argument. Note that serving the raft instance is the same as Serve, so it is best to look into that method as well.
type StableStore ¶
type StableStore interface { Set(key, value []byte) error // Get returns the value related to that key. An empty slice is returned if // there is no value with that key found. Get(key []byte) ([]byte, error) }
StableStore is used to provide persistence to vital information related to the raft's state.
type StaticCluster ¶
type StaticCluster struct { // AllLogs the nodes within the raft Cluster. Key is a raft id. Nodes map[uint64]Node // contains filtered or unexported fields }
StaticCluster is a static definition of all members of the cluster. As such new members cannot be dynamically discovered. All members must be known from the start.
func NewCluster ¶
func NewCluster() *StaticCluster
NewCluster will create an entirely new static that doesn't contain any nodes.
func NewClusterWithConfig ¶
func NewClusterWithConfig(conf io.Reader) (*StaticCluster, error)
NewClusterWithConfig similarly creates a static and adds all the nodes that are defined by configuration reader. The config file formatting is expected to be a json format.
func (*StaticCluster) AllNodes ¶
func (c *StaticCluster) AllNodes() map[uint64]Node
func (*StaticCluster) Quorum ¶
func (c *StaticCluster) Quorum() int
type Task ¶
type Task interface { // Error is a blocking operation that will wait until the task has finished // before return the result of the task. // // A non-nil error will be returned if the task failed to be committed. Error() error }
Task represents an operation that has been sent to the raft Cluster. Every task represents a future operation that returns when all operations have been applied to other raft replications.