Documentation
¶
Index ¶
- Constants
- type RaftServer
- func (s *RaftServer) Get(cmd *entity.Command, resp *raftrpc.ClientResponse) error
- func (s *RaftServer) HeartbeatEventHandler()
- func (s *RaftServer) LeaderElectionEventHandler()
- func (s *RaftServer) OnReceiveAppendRPC(req *raftrpc.AppendRPCRequest, resp *raftrpc.AppendRPCResponse) error
- func (s *RaftServer) OnReceiveRequestVoteRPC(req *raftrpc.RequestVoteRequest, resp *raftrpc.RequestVoteResponse) error
- func (s *RaftServer) OpenRepo(dirpath string, pageSize int, segmentSize int64)
- func (s *RaftServer) ReplicateLog(cmd *entity.Command) ([]byte, error)
- func (s *RaftServer) Set(cmd *entity.Command, resp *raftrpc.ClientResponse) error
- func (s *RaftServer) Start()
- func (s *RaftServer) Stop()
- func (s *RaftServer) String() string
- type TimerEvent
Examples ¶
Constants ¶
const ( // SUCCESS rpc returns success SUCCESS = 1 // FAIL rpc returns fail FAIL = 2 // TIMEOUT rpc times out TIMEOUT = 3 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RaftServer ¶
type RaftServer struct {
ServerConf *core.ServerConfig
RandomRangeInMs int
// contains filtered or unexported fields
}
RaftServer object
func NewRaftServer ¶
func NewRaftServer(conf *core.ServerConfig) *RaftServer
NewRaftServer returns a raft server with default env
Example ¶
envExample := &core.Env{HeartBeatDurationInMs: 1000, LeaderElectionDurationInMs: 5000, RandomRangeInMs: 3000}
serverConfig, err := core.GetServerConfFromFile("config.yml")
if err != nil {
panic(err)
}
raftNode := NewRaftServerWithEnv(serverConfig, envExample)
raftNode.OpenRepo("test/ExampleNewRaftServer", store.DefaultPageSize, store.SegmentSizeBytes)
raftNode.Start()
func NewRaftServerWithEnv ¶
func NewRaftServerWithEnv(conf *core.ServerConfig, env *core.Env) *RaftServer
NewRaftServerWithEnv returns a new raft server with env
func (*RaftServer) Get ¶
func (s *RaftServer) Get(cmd *entity.Command, resp *raftrpc.ClientResponse) error
Get handles client get command
func (*RaftServer) HeartbeatEventHandler ¶
func (s *RaftServer) HeartbeatEventHandler()
HeartbeatEventHandler is triggered by leaderHeartBeatTicker, leader will send heart beat rpc to followers HeartbeatEventHandler sends heart beat rpc to followers
func (*RaftServer) LeaderElectionEventHandler ¶
func (s *RaftServer) LeaderElectionEventHandler()
LeaderElectionEventHandler is the event triggered by leaderElectionTimer LeaderElectionEventHandler sends request vote rpc to other servers
func (*RaftServer) OnReceiveAppendRPC ¶
func (s *RaftServer) OnReceiveAppendRPC(req *raftrpc.AppendRPCRequest, resp *raftrpc.AppendRPCResponse) error
OnReceiveAppendRPC implementation: 1. Reply false if term < currentTerm (§5.1) 2. Reply false if log doesn’t contain an entry at prevLogIndex whose term matches prevLogTerm (§5.3) 3. If an existing entry conflicts with a new one (same index but different terms), delete the existing entry and all that follow it (§5.3) 4. Append any new entries not already in the log 5. If leaderCommit > commitIndex, set commitIndex = min(leaderCommit, index of last new entry)
func (*RaftServer) OnReceiveRequestVoteRPC ¶
func (s *RaftServer) OnReceiveRequestVoteRPC(req *raftrpc.RequestVoteRequest, resp *raftrpc.RequestVoteResponse) error
OnReceiveRequestVoteRPC will make the election decision: accept or reject 1. Reply false if term < currentTerm (§5.1) 2. If votedFor is null or candidateId, and candidate’s log is at least as up-to-date as receiver’s log, grant vote (§5.2, §5.4)
func (*RaftServer) OpenRepo ¶
func (s *RaftServer) OpenRepo(dirpath string, pageSize int, segmentSize int64)
OpenRepo opens a wal file and read logs/states from it, set to memory
func (*RaftServer) ReplicateLog ¶
func (s *RaftServer) ReplicateLog(cmd *entity.Command) ([]byte, error)
ReplicateLog replicates logs to follower
func (*RaftServer) Set ¶
func (s *RaftServer) Set(cmd *entity.Command, resp *raftrpc.ClientResponse) error
Set handles client set command
func (*RaftServer) String ¶
func (s *RaftServer) String() string
type TimerEvent ¶
type TimerEvent func()
TimerEvent is the callback function triggered by eventTimer