Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterCommand(command Command)
- type AppendEntriesRequest
- type AppendEntriesResponse
- type Command
- type Log
- func (l *Log) AppendEntries(entries []*LogEntry) error
- func (l *Log) AppendEntry(entry *LogEntry) error
- func (l *Log) Close()
- func (l *Log) CommitIndex() uint64
- func (l *Log) CommitInfo() (index uint64, term uint64)
- func (l *Log) Compact(index uint64, term uint64) error
- func (l *Log) ContainsEntry(index uint64, term uint64) bool
- func (l *Log) CreateEntry(term uint64, command Command) *LogEntry
- func (l *Log) CurrentIndex() uint64
- func (l *Log) CurrentTerm() uint64
- func (l *Log) GetEntriesAfter(index uint64) ([]*LogEntry, uint64)
- func (l *Log) GetEntryError(entry *LogEntry) error
- func (l *Log) IsEmpty() bool
- func (l *Log) LastCommandName() string
- func (l *Log) NextIndex() uint64
- func (l *Log) Open(path string) error
- func (l *Log) SetCommitIndex(index uint64) error
- func (l *Log) SetStartIndex(i uint64)
- func (l *Log) SetStartTerm(t uint64)
- func (l *Log) StartIndex() uint64
- func (l *Log) Truncate(index uint64, term uint64) error
- func (l *Log) UpdateCommitIndex(index uint64)
- type LogEntry
- type Peer
- type RequestVoteRequest
- type RequestVoteResponse
- type Server
- func (s *Server) AddPeer(name string) error
- func (s *Server) AppendEntries(req *AppendEntriesRequest) (*AppendEntriesResponse, error)
- func (s *Server) Context() interface{}
- func (s *Server) Do(command Command) error
- func (s *Server) ElectionTimeout() time.Duration
- func (s *Server) HeartbeatTimeout() time.Duration
- func (s *Server) Initialize() error
- func (s *Server) IsLogEmpty() bool
- func (s *Server) LastCommandName() string
- func (s *Server) Leader() string
- func (s *Server) LoadSnapshot() error
- func (s *Server) LogEntries() []*LogEntry
- func (s *Server) LogPath() string
- func (s *Server) MemberCount() int
- func (s *Server) Name() string
- func (s *Server) Path() string
- func (s *Server) QuorumSize() int
- func (s *Server) RemovePeer(name string) error
- func (s *Server) RequestVote(req *RequestVoteRequest) (*RequestVoteResponse, error)
- func (s *Server) Running() bool
- func (s *Server) SetElectionTimeout(duration time.Duration)
- func (s *Server) SetHeartbeatTimeout(duration time.Duration)
- func (s *Server) Snapshot()
- func (s *Server) SnapshotPath(lastIndex uint64, lastTerm uint64) string
- func (s *Server) SnapshotRecovery(req *SnapshotRequest) (*SnapshotResponse, error)
- func (s *Server) Start() error
- func (s *Server) State() string
- func (s *Server) Stop()
- func (s *Server) Transporter() Transporter
- func (s *Server) VotedFor() string
- type Snapshot
- type SnapshotRequest
- type SnapshotResponse
- type StateMachine
- type TestCommand1
- type TestCommand2
- type Timer
- func (t *Timer) C() chan time.Time
- func (t *Timer) MaxDuration() time.Duration
- func (t *Timer) MinDuration() time.Duration
- func (t *Timer) Pause()
- func (t *Timer) Reset()
- func (t *Timer) Running() bool
- func (t *Timer) SetDuration(duration time.Duration)
- func (t *Timer) SetMaxDuration(duration time.Duration)
- func (t *Timer) SetMinDuration(duration time.Duration)
- func (t *Timer) Stop()
- type Transporter
Constants ¶
const ( Stopped = "stopped" Follower = "follower" Candidate = "candidate" Leader = "leader" )
const ( DefaultHeartbeatTimeout = 50 * time.Millisecond DefaultElectionTimeout = 150 * time.Millisecond )
Variables ¶
var DuplicatePeerError = errors.New("raft.Server: Duplicate peer")
var NotLeaderError = errors.New("raft.Server: Not current leader")
Functions ¶
func RegisterCommand ¶
func RegisterCommand(command Command)
Registers a command by storing a reference to an instance of it.
Types ¶
type AppendEntriesRequest ¶
type AppendEntriesRequest struct {
Term uint64 `json:"term"`
LeaderName string `json:"leaderName"`
PrevLogIndex uint64 `json:"prevLogIndex"`
PrevLogTerm uint64 `json:"prevLogTerm"`
Entries []*LogEntry `json:"entries"`
CommitIndex uint64 `json:"commitIndex"`
}
The request sent to a server to append entries to the log.
func NewAppendEntriesRequest ¶
func NewAppendEntriesRequest(term uint64, leaderName string, prevLogIndex uint64, prevLogTerm uint64, entries []*LogEntry, commitIndex uint64) *AppendEntriesRequest
Creates a new AppendEntries request.
type AppendEntriesResponse ¶
type AppendEntriesResponse struct {
Term uint64 `json:"term"`
Success bool `json:"success"`
CommitIndex uint64 `json:"commitIndex"`
}
The response returned from a server appending entries to the log.
func NewAppendEntriesResponse ¶
func NewAppendEntriesResponse(term uint64, success bool, commitIndex uint64) *AppendEntriesResponse
Creates a new AppendEntries response.
type Command ¶
A command represents an action to be taken on the replicated state machine.
func NewCommand ¶
Creates a new instance of a command by name.
type Log ¶
A log is a collection of log entries that are persisted to durable storage.
func (*Log) AppendEntries ¶
Appends a series of entries to the log. These entries are not written to disk until SetCommitIndex() is called.
func (*Log) AppendEntry ¶
Appends a single entry to the log.
func (*Log) CommitInfo ¶
Retrieves the last index and term that has been committed to the log.
func (*Log) ContainsEntry ¶
Checks if the log contains a given index/term combination.
func (*Log) CreateEntry ¶
Creates a log entry associated with this log.
func (*Log) GetEntriesAfter ¶
Retrieves a list of entries after a given index. This function also returns the term of the index provided.
func (*Log) GetEntryError ¶
Retrieves the error returned from an entry. The error can only exist after the entry has been committed.
func (*Log) LastCommandName ¶
The name of the last command in the log.
func (*Log) Open ¶
Opens the log file and reads existing entries. The log can remain open and continue to append entries to the end of the log.
func (*Log) SetCommitIndex ¶
Updates the commit index and writes entries after that index to the stable storage.
func (*Log) SetStartIndex ¶
func (*Log) SetStartTerm ¶
func (*Log) StartIndex ¶
func (*Log) Truncate ¶
Truncates the log to the given index and term. This only works if the log at the index has not been committed.
func (*Log) UpdateCommitIndex ¶
Updates the commit index
type LogEntry ¶
type LogEntry struct {
Index uint64 `json:"index"`
Term uint64 `json:"term"`
Command Command `json:"command"`
// contains filtered or unexported fields
}
A log entry stores a single item in the log.
func NewLogEntry ¶
Creates a new log entry associated with a log.
func (*LogEntry) MarshalJSON ¶
Encodes a log entry into JSON.
func (*LogEntry) UnmarshalJSON ¶
Decodes a log entry from a JSON byte array.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
A peer is a reference to another server involved in the consensus protocol.
func (*Peer) HeartbeatTimeout ¶
Retrieves the heartbeat timeout.
func (*Peer) SetHeartbeatTimeout ¶
Sets the heartbeat timeout.
type RequestVoteRequest ¶
type RequestVoteRequest struct {
Term uint64 `json:"term"`
CandidateName string `json:"candidateName"`
LastLogIndex uint64 `json:"lastLogIndex"`
LastLogTerm uint64 `json:"lastLogTerm"`
// contains filtered or unexported fields
}
The request sent to a server to vote for a candidate to become a leader.
func NewRequestVoteRequest ¶
func NewRequestVoteRequest(term uint64, candidateName string, lastLogIndex uint64, lastLogTerm uint64) *RequestVoteRequest
Creates a new RequestVote request.
type RequestVoteResponse ¶
type RequestVoteResponse struct {
Term uint64 `json:"term"`
VoteGranted bool `json:"voteGranted"`
// contains filtered or unexported fields
}
The response returned from a server after a vote for a candidate to become a leader.
func NewRequestVoteResponse ¶
func NewRequestVoteResponse(term uint64, voteGranted bool) *RequestVoteResponse
Creates a new RequestVote response.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
A server is involved in the consensus protocol and can act as a follower, candidate or a leader.
func NewServer ¶
func NewServer(name string, path string, transporter Transporter, context interface{}) (*Server, error)
Creates a new server with a log at the given path.
func (*Server) AddPeer ¶
Adds a peer to the server. This should be called by a system's join command within the context so that it is within the context of the server lock.
func (*Server) AppendEntries ¶
func (s *Server) AppendEntries(req *AppendEntriesRequest) (*AppendEntriesResponse, error)
Appends a log entry from the leader to this server.
func (*Server) Context ¶
func (s *Server) Context() interface{}
Retrieves the context passed into the constructor.
func (*Server) Do ¶
Attempts to execute a command and replicate it. The function will return when the command has been successfully committed or an error has occurred.
func (*Server) ElectionTimeout ¶
Retrieves the election timeout.
func (*Server) HeartbeatTimeout ¶
Retrieves the heartbeat timeout.
func (*Server) Initialize ¶
Initializes the server to become leader of a new cluster. This function will fail if there is an existing log or the server is already a member in an existing cluster.
func (*Server) IsLogEmpty ¶
Retrieves whether the server's log has no entries.
func (*Server) LastCommandName ¶
A reference to the command name of the last entry.
func (*Server) LogEntries ¶
A list of all the log entries. This should only be used for debugging purposes.
func (*Server) MemberCount ¶
Retrieves the number of member servers in the consensus.
func (*Server) QuorumSize ¶
Retrieves the number of servers required to make a quorum.
func (*Server) RemovePeer ¶
Removes a peer from the server. This should be called by a system's join command within the context so that it is within the context of the server lock.
func (*Server) RequestVote ¶
func (s *Server) RequestVote(req *RequestVoteRequest) (*RequestVoteResponse, error)
Requests a vote from a server. A vote can be obtained if the vote's term is at the server's current term and the server has not made a vote yet. A vote can also be obtained if the term is greater than the server's current term.
func (*Server) SetElectionTimeout ¶
Sets the election timeout.
func (*Server) SetHeartbeatTimeout ¶
Sets the heartbeat timeout.
func (*Server) SnapshotPath ¶
Retrieves the log path for the server.
func (*Server) SnapshotRecovery ¶
func (s *Server) SnapshotRecovery(req *SnapshotRequest) (*SnapshotResponse, error)
func (*Server) Transporter ¶
func (s *Server) Transporter() Transporter
Retrieves the object that transports requests.
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
the in memory SnapShot struct TODO add cluster configuration
type SnapshotRequest ¶
type SnapshotRequest struct {
LeaderName string `json:"leaderName"`
LastIndex uint64 `json:"lastTerm"`
LastTerm uint64 `json:"lastIndex"`
State []byte `json:"state"`
}
The request sent to a server to start from the snapshot.
func NewSnapshotRequest ¶
func NewSnapshotRequest(leaderName string, snapshot *Snapshot) *SnapshotRequest
Creates a new Snapshot request.
type SnapshotResponse ¶
type SnapshotResponse struct {
Term uint64 `json:"term"`
Success bool `json:"success"`
CommitIndex uint64 `json:"commitIndex"`
}
The response returned from a server appending entries to the log.
func NewSnapshotResponse ¶
func NewSnapshotResponse(term uint64, success bool, commitIndex uint64) *SnapshotResponse
Creates a new Snapshot response.
type StateMachine ¶
StateMachine is the interface for allowing the host application to save and recovery the state machine
type TestCommand1 ¶
func (TestCommand1) Apply ¶
func (c TestCommand1) Apply(server *Server) error
func (TestCommand1) CommandName ¶
func (c TestCommand1) CommandName() string
type TestCommand2 ¶
type TestCommand2 struct {
X int `json:"x"`
}
func (TestCommand2) Apply ¶
func (c TestCommand2) Apply(server *Server) error
func (TestCommand2) CommandName ¶
func (c TestCommand2) CommandName() string
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
The timer wraps the internal Go timer and provides the ability to pause, reset and stop. It also allows for the duration of the timer to be a random number between a min and max duration.
func (*Timer) MaxDuration ¶
Retrieves the maximum duration of the timer.
func (*Timer) MinDuration ¶
Retrieves the minimum duration of the timer.
func (*Timer) SetDuration ¶
Sets the minimum and maximum duration of the timer.
func (*Timer) SetMaxDuration ¶
Sets the maximum duration of the timer.
func (*Timer) SetMinDuration ¶
Sets the minimum duration of the timer.
type Transporter ¶
type Transporter interface {
SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) (*RequestVoteResponse, error)
SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) (*AppendEntriesResponse, error)
SendSnapshotRequest(server *Server, peer *Peer, req *SnapshotRequest) (*SnapshotResponse, error)
}
Transporter is the interface for allowing the host application to transport requests to other nodes.