Documentation ¶
Index ¶
- type BoltDataStore
- type DataStore
- type LogEntry
- type MemoryDataStore
- type NodeState
- func (state *NodeState) CurrentTerm() uint32
- func (state NodeState) Log(index uint32) LogEntry
- func (state NodeState) LogLength() uint32
- func (state *NodeState) SetCurrentTerm(newCurrentTerm uint32)
- func (state *NodeState) SetLogEntry(index uint32, entry LogEntry) error
- func (state *NodeState) SetVotedFor(newVotedFor string)
- func (state *NodeState) VotedFor() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoltDataStore ¶
type BoltDataStore struct {
// contains filtered or unexported fields
}
BoltDataStore has a Bolt database that it uses to store key-value pairs and retrieve values by their keys.
func NewBoltDataStore ¶
func NewBoltDataStore(dbFile string) (boltSM *BoltDataStore, err error)
NewBoltDataStore returns a new instance of the BoltDataStore type.
func (BoltDataStore) CreateBucketIfNotExists ¶
func (boltSM BoltDataStore) CreateBucketIfNotExists(name string) error
CreateBucketIfNotExists ensures that a bucket with the given name exists by creating it if it does not already exist.
func (BoltDataStore) Get ¶
func (boltSM BoltDataStore) Get(key string) (string, error)
Get returns the value of the specified key stored in the Bolt database.
func (BoltDataStore) Put ¶
func (boltSM BoltDataStore) Put(key string, value string) error
Put writes a key-value pair to the Bolt database.
func (BoltDataStore) RetrieveLogEntries ¶
func (boltSM BoltDataStore) RetrieveLogEntries(firstIndex int, lastIndex int) ([]LogEntry, error)
RetrieveLogEntries returns log entries within the specified key range.
TODO: Use a more efficient method than querying each index one at a time.
type DataStore ¶
type DataStore interface { Put(string, string) error Get(string) (string, error) RetrieveLogEntries(int, int) ([]LogEntry, error) }
DataStore represents any kind of key-value database.
type LogEntry ¶
LogEntry represents a state machine command (Put {Key},{Value}) and the term when the entry was received by the leader.
type MemoryDataStore ¶
type MemoryDataStore struct {
// contains filtered or unexported fields
}
MemoryDataStore stores key-value pairs in memory.
func NewMemoryDataStore ¶
func NewMemoryDataStore() MemoryDataStore
NewMemoryDataStore constructs a new empty MemoryDataStore.
func (MemoryDataStore) Get ¶
func (sm MemoryDataStore) Get(key string) (string, error)
Get retrieves a value based on its key.
func (MemoryDataStore) Put ¶
func (sm MemoryDataStore) Put(key string, value string) error
Put adds a new key-value pair to the data store.
func (MemoryDataStore) RetrieveLogEntries ¶
func (sm MemoryDataStore) RetrieveLogEntries(firstIndex int, lastIndex int) ([]LogEntry, error)
RetrieveLogEntries returns the LogEntries found between the specified indices.
type NodeState ¶
type NodeState struct { // The node id of the current leader. LeaderId string // Node state and stored state are stored by different state machines. NodeDataStore DataStore StorageDataStore DataStore // Index of the highest log entry known to be committed. CommitIndex uint32 // Index of the highest log entry applied to this node's storage state machine. LastApplied uint32 // (Leader only) For each node, the index of the next log entry to send to. NextIndex map[string]uint32 // (Leader only) For each node, the index of the highest log entry known to be replicated on // the node. MatchIndex map[string]uint32 // contains filtered or unexported fields }
NodeState contains both the persistent and volatile state that a node needs to function in a Raft cluster.
Note that currentTerm, votedFor, and log are not exported because they need to be persistent, so to preserve consistency, they should be updated and retrieved using the respective NodeState methods.
var Node *NodeState
Node contains the state of the currently running host node.
func GetNodeState ¶
func GetNodeState() *NodeState
GetNodeState returns the global NodeState variable, initializing it if it has not yet been instantiated.
func NewNodeState ¶
NewNodeState returns a NodeState based on values in the node state Bolt database, using default values if the database does not exist or have any values in it.
func (*NodeState) CurrentTerm ¶
CurrentTerm returns the current term recognized by the node.
func (NodeState) Log ¶
Log returns the LogEntry at the specified index. Note that log indices start at 1, but the slice indices start at 0.
func (*NodeState) SetCurrentTerm ¶
SetCurrentTerm sets the current term in memory and in the node state machine.
func (*NodeState) SetLogEntry ¶
SetLogEntry sets the log entry in the NodeState's log at the given index. Note that this method does not do any safety checking to prevent overwriting existing entries; that check should be done by the caller beforehand.
func (*NodeState) SetVotedFor ¶
SetVotedFor sets VotedFor in memory and in the node state machine.