Documentation
¶
Index ¶
- Constants
- Variables
- func LogLevel() int
- func RegisterCommand(command Command)
- func SetLogLevel(level int)
- type AppendEntriesRequest
- type AppendEntriesResponse
- type Command
- type CommandEncoder
- type DefaultJoinCommand
- type DefaultLeaveCommand
- type HTTPMuxer
- type HTTPTransporter
- func (t *HTTPTransporter) AppendEntriesPath() string
- func (t *HTTPTransporter) Install(server *Server, mux HTTPMuxer)
- func (t *HTTPTransporter) Prefix() string
- func (t *HTTPTransporter) RequestVotePath() string
- func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse
- func (t *HTTPTransporter) SendSnapshotRecoveryRequest(server *Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
- func (t *HTTPTransporter) SendSnapshotRequest(server *Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse
- func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse
- type JoinCommand
- type LeaveCommand
- type Log
- type LogEntry
- type NOPCommand
- type Peer
- type RequestVoteRequest
- type RequestVoteResponse
- type Server
- func (s *Server) AddPeer(name string) error
- func (s *Server) AppendEntries(req *AppendEntriesRequest) *AppendEntriesResponse
- func (s *Server) CommitIndex() uint64
- func (s *Server) Context() interface{}
- func (s *Server) Do(command Command) (interface{}, error)
- func (s *Server) ElectionTimeout() time.Duration
- func (s *Server) GetState() string
- func (s *Server) HeartbeatTimeout() time.Duration
- 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) Peers() map[string]*Peer
- func (s *Server) QuorumSize() int
- func (s *Server) RemovePeer(name string) error
- func (s *Server) RequestSnapshot(req *SnapshotRequest) *SnapshotResponse
- func (s *Server) RequestVote(req *RequestVoteRequest) *RequestVoteResponse
- func (s *Server) Running() bool
- func (s *Server) SetElectionTimeout(duration time.Duration)
- func (s *Server) SetHeartbeatTimeout(duration time.Duration)
- func (s *Server) SetTransporter(t Transporter)
- func (s *Server) Snapshot()
- func (s *Server) SnapshotPath(lastIndex uint64, lastTerm uint64) string
- func (s *Server) SnapshotRecoveryRequest(req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
- func (s *Server) Start() error
- func (s *Server) State() string
- func (s *Server) Stop()
- func (s *Server) Term() uint64
- func (s *Server) Transporter() Transporter
- func (s *Server) VotedFor() string
- type Snapshot
- type SnapshotRecoveryRequest
- type SnapshotRecoveryResponse
- type SnapshotRequest
- type SnapshotResponse
- type StateMachine
- type Transporter
Constants ¶
const ( Debug = 1 Trace = 2 )
const ( Stopped = "stopped" Follower = "follower" Candidate = "candidate" Leader = "leader" Snapshotting = "snapshotting" )
const ( MaxLogEntriesPerRequest = 2000 NumberOfLogEntriesAfterSnapshot = 200 )
const ( DefaultHeartbeatTimeout = 50 * time.Millisecond DefaultElectionTimeout = 150 * time.Millisecond )
const ( STOPPED = iota READY RUNNING )
Variables ¶
var CommandTimeoutError = errors.New("raft: Command timeout")
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.
func SetLogLevel ¶
func SetLogLevel(level int)
Types ¶
type AppendEntriesRequest ¶
type AppendEntriesRequest struct {
Term uint64
PrevLogIndex uint64
PrevLogTerm uint64
CommitIndex uint64
LeaderName string
Entries []*LogEntry
}
The request sent to a server to append entries to the log.
type AppendEntriesResponse ¶
type AppendEntriesResponse struct {
Term uint64
// the current index of the server
Index uint64
Success bool
CommitIndex uint64
// contains filtered or unexported fields
}
The response returned from a server appending entries to the log.
type CommandEncoder ¶
type DefaultJoinCommand ¶
type DefaultJoinCommand struct {
Name string `json:"name"`
}
Join command
func (*DefaultJoinCommand) Apply ¶
func (c *DefaultJoinCommand) Apply(server *Server) (interface{}, error)
func (*DefaultJoinCommand) CommandName ¶
func (c *DefaultJoinCommand) CommandName() string
The name of the Join command in the log
func (*DefaultJoinCommand) NodeName ¶
func (c *DefaultJoinCommand) NodeName() string
type DefaultLeaveCommand ¶
type DefaultLeaveCommand struct {
Name string `json:"name"`
}
Leave command
func (*DefaultLeaveCommand) Apply ¶
func (c *DefaultLeaveCommand) Apply(server *Server) (interface{}, error)
func (*DefaultLeaveCommand) CommandName ¶
func (c *DefaultLeaveCommand) CommandName() string
The name of the Leave command in the log
func (*DefaultLeaveCommand) NodeName ¶
func (c *DefaultLeaveCommand) NodeName() string
type HTTPMuxer ¶
type HTTPMuxer interface {
HandleFunc(string, func(http.ResponseWriter, *http.Request))
}
type HTTPTransporter ¶
type HTTPTransporter struct {
DisableKeepAlives bool
// contains filtered or unexported fields
}
An HTTPTransporter is a default transport layer used to communicate between multiple servers.
func NewHTTPTransporter ¶
func NewHTTPTransporter(prefix string) *HTTPTransporter
Creates a new HTTP transporter with the given path prefix.
func (*HTTPTransporter) AppendEntriesPath ¶
func (t *HTTPTransporter) AppendEntriesPath() string
Retrieves the AppendEntries path.
func (*HTTPTransporter) Install ¶
func (t *HTTPTransporter) Install(server *Server, mux HTTPMuxer)
Applies Raft routes to an HTTP router for a given server.
func (*HTTPTransporter) Prefix ¶
func (t *HTTPTransporter) Prefix() string
Retrieves the path prefix used by the transporter.
func (*HTTPTransporter) RequestVotePath ¶
func (t *HTTPTransporter) RequestVotePath() string
Retrieves the RequestVote path.
func (*HTTPTransporter) SendAppendEntriesRequest ¶
func (t *HTTPTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse
Sends an AppendEntries RPC to a peer.
func (*HTTPTransporter) SendSnapshotRecoveryRequest ¶
func (t *HTTPTransporter) SendSnapshotRecoveryRequest(server *Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
Sends a SnapshotRequest RPC to a peer.
func (*HTTPTransporter) SendSnapshotRequest ¶
func (t *HTTPTransporter) SendSnapshotRequest(server *Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse
Sends a SnapshotRequest RPC to a peer.
func (*HTTPTransporter) SendVoteRequest ¶
func (t *HTTPTransporter) SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse
Sends a RequestVote RPC to a peer.
type JoinCommand ¶
type JoinCommand interface {
CommandName() string
Apply(server *Server) (interface{}, error)
NodeName() string
}
Join command interface
type LeaveCommand ¶
type LeaveCommand interface {
CommandName() string
Apply(server *Server) (interface{}, error)
NodeName() string
}
Leave command interface
type Log ¶
type Log struct {
ApplyFunc func(Command) (interface{}, error)
// contains filtered or unexported fields
}
A log is a collection of log entries that are persisted to durable storage.
type LogEntry ¶
type LogEntry struct {
Index uint64
Term uint64
CommandName string
Command []byte
Position int64 // position in the log file
// contains filtered or unexported fields
}
A log entry stores a single item in the log.
type NOPCommand ¶
type NOPCommand struct {
}
NOP command
func (NOPCommand) Apply ¶
func (c NOPCommand) Apply(server *Server) (interface{}, error)
func (NOPCommand) CommandName ¶
func (c NOPCommand) CommandName() string
The name of the NOP command in the log
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
A peer is a reference to another server involved in the consensus protocol.
type RequestVoteRequest ¶
type RequestVoteRequest struct {
Term uint64
LastLogIndex uint64
LastLogTerm uint64
CandidateName string
// contains filtered or unexported fields
}
The request sent to a server to vote for a candidate to become a leader.
type RequestVoteResponse ¶
type RequestVoteResponse struct {
Term uint64
VoteGranted bool
// contains filtered or unexported fields
}
The response returned from a server after a vote for a candidate to become a leader.
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, stateMachine StateMachine, context interface{}) (*Server, error)
Creates a new server with a log at the given path.
func (*Server) AppendEntries ¶
func (s *Server) AppendEntries(req *AppendEntriesRequest) *AppendEntriesResponse
Appends zero or more log entry from the leader to this server.
func (*Server) CommitIndex ¶
Retrieves the current commit index of the server.
func (*Server) Context ¶
func (s *Server) Context() interface{}
Retrieves the context passed into the constructor.
func (*Server) ElectionTimeout ¶
Retrieves the election timeout.
func (*Server) HeartbeatTimeout ¶
Retrieves the heartbeat timeout.
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.
func (*Server) RequestSnapshot ¶
func (s *Server) RequestSnapshot(req *SnapshotRequest) *SnapshotResponse
func (*Server) RequestVote ¶
func (s *Server) RequestVote(req *RequestVoteRequest) *RequestVoteResponse
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) SetTransporter ¶
func (s *Server) SetTransporter(t Transporter)
func (*Server) SnapshotPath ¶
Retrieves the log path for the server.
func (*Server) SnapshotRecoveryRequest ¶
func (s *Server) SnapshotRecoveryRequest(req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
func (*Server) Transporter ¶
func (s *Server) Transporter() Transporter
Retrieves the object that transports requests.
type Snapshot ¶
type Snapshot struct {
LastIndex uint64 `json:"lastIndex"`
LastTerm uint64 `json:"lastTerm"`
// cluster configuration.
Peers []string `json: "peers"`
State []byte `json: "state"`
Path string `json: "path"`
}
the in memory SnapShot struct TODO add cluster configuration
type SnapshotRecoveryRequest ¶
type SnapshotRecoveryRequest struct {
LeaderName string
LastIndex uint64
LastTerm uint64
Peers []string
State []byte
}
The request sent to a server to start from the snapshot.
type SnapshotRecoveryResponse ¶
The response returned from a server appending entries to the log.
type SnapshotRequest ¶
The request sent to a server to start from the snapshot.
type SnapshotResponse ¶
type SnapshotResponse struct {
Success bool `json:"success"`
}
The response returned if the follower entered snapshot state
type StateMachine ¶
StateMachine is the interface for allowing the host application to save and recovery the state machine
type Transporter ¶
type Transporter interface {
SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) *RequestVoteResponse
SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) *AppendEntriesResponse
SendSnapshotRequest(server *Server, peer *Peer, req *SnapshotRequest) *SnapshotResponse
SendSnapshotRecoveryRequest(server *Server, peer *Peer, req *SnapshotRecoveryRequest) *SnapshotRecoveryResponse
}
Transporter is the interface for allowing the host application to transport requests to other nodes.
Source Files
¶
- append_entries_request.go
- append_entries_response.go
- command.go
- debug.go
- http_transporter.go
- join_command.go
- leave_command.go
- log.go
- log_entry.go
- nop_command.go
- peer.go
- request_vote_request.go
- request_vote_response.go
- server.go
- snapshot.go
- snapshot_recovery_request.go
- snapshot_recovery_response.go
- snapshot_request.go
- snapshot_response.go
- sort.go
- statemachine.go
- test.go
- time.go
- timer.go
- transporter.go
