state

package
v0.0.0-...-d831435 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2018 License: MIT Imports: 5 Imported by: 2

Documentation

Index

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

type LogEntry struct {
	Key   string
	Value string
	Term  uint32
}

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

func NewNodeState(nodeDataStore DataStore, storageDataStore DataStore) *NodeState

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

func (state *NodeState) CurrentTerm() uint32

CurrentTerm returns the current term recognized by the node.

func (NodeState) Log

func (state NodeState) Log(index uint32) LogEntry

Log returns the LogEntry at the specified index. Note that log indices start at 1, but the slice indices start at 0.

func (NodeState) LogLength

func (state NodeState) LogLength() uint32

LogLength returns the number of entries in the node's log.

func (*NodeState) SetCurrentTerm

func (state *NodeState) SetCurrentTerm(newCurrentTerm uint32)

SetCurrentTerm sets the current term in memory and in the node state machine.

func (*NodeState) SetLogEntry

func (state *NodeState) SetLogEntry(index uint32, entry LogEntry) error

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

func (state *NodeState) SetVotedFor(newVotedFor string)

SetVotedFor sets VotedFor in memory and in the node state machine.

func (*NodeState) VotedFor

func (state *NodeState) VotedFor() string

VotedFor returns the node's VotedFor.

Jump to

Keyboard shortcuts

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