-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.go
43 lines (35 loc) · 837 Bytes
/
raft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package raft
// ConsensusModule has a State Machine with 3 States
// 1 - Follower
// 2 - Candidate (timeout start an election)
// 3 - Leader (receive votes from majority)
// By default CMState is follower
type CMState int
const (
Follower CMState = 0
Candidate = 1
Leader = 2
Dead = 3
)
func (state CMState) String() string {
switch state {
case Follower:
return "Follower"
case Candidate:
return "Candidate"
case Leader:
return "Leader"
case Dead:
return "Dead"
default:
panic("unreachable")
}
}
type ConsensusModule struct {
id int // server id of this CM
peersIds []int // lists the IDs of our peers in the cluster
}
// Election timer
// Timer every follower runs continuously, restarting it every time it hears from the
// current leader.
// Leader must sent period heartbeats to followers