-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm.go
307 lines (260 loc) · 7.03 KB
/
cm.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"context"
"log"
"math/rand"
"sync"
"time"
pb "github.com/devMYC/raft/proto"
)
// Role is the current role of the server.
type Role int
// Each server can act as exactly one of the following
// roles under normal operation. Each server starts as
// a Follower.
const (
Follower Role = iota
Candidate
Leader
)
// State of Raft algorithm shown in Fig 2 of Raft paper.
type State struct {
// persistent state on all servers
currentTerm int
votedFor int
log []*pb.LogEntry
// volatile state on all servers
commitIndex int
lastApplied int
// volatile state on leaders
nextIndex map[int]int
matchIndex map[int]int
}
// InitState initializes Raft state for a server.
func InitState(peerIds []int) *State {
initState := &State{
currentTerm: 0,
votedFor: -1,
log: make([]*pb.LogEntry, 0),
commitIndex: -1,
lastApplied: -1,
nextIndex: make(map[int]int, 0),
matchIndex: make(map[int]int, 0),
}
for _, id := range peerIds {
initState.nextIndex[id] = len(initState.log)
initState.matchIndex[id] = -1
}
return initState
}
func (s *State) getLastLogEntry() (*pb.LogEntry, int) {
length := len(s.log)
if length <= 0 {
return nil, -1
}
i := length - 1
return s.log[i], i
}
// ConsensusModule encapsulates the state and
// other information used to run Raft algorithm.
type ConsensusModule struct {
// sm *StateMachine
id int
latestUpdateAt time.Time
mu *sync.Mutex
role Role
state *State
}
// NewConsensusModule creates a new consensus module with serverID
// used as seed to prevent livelock of elections.
func NewConsensusModule(id int, peerIds []int) *ConsensusModule {
rand.Seed(int64(id))
return &ConsensusModule{
id: id,
latestUpdateAt: time.Now(),
mu: &sync.Mutex{},
role: Follower,
state: InitState(peerIds),
}
}
func (cm *ConsensusModule) becomeFollower(term int, peers map[int]pb.RpcClient) {
cm.state.currentTerm = term
cm.state.votedFor = -1
cm.role = Follower
cm.latestUpdateAt = time.Now()
log.Printf("[cm.becomeFollower] term=%d\n", term)
go cm.prepareElection(term, peers)
}
func (cm *ConsensusModule) becomeCandidate(peers map[int]pb.RpcClient) {
cm.role = Candidate
cm.state.currentTerm++
currTerm := cm.state.currentTerm
cm.state.votedFor = cm.id
cm.latestUpdateAt = time.Now()
log.Printf("[cm.becomeCandidate] newTerm=%d\n", currTerm)
cm.runElection(currTerm, peers)
}
func (cm *ConsensusModule) becomeLeader(peers map[int]pb.RpcClient) {
cm.latestUpdateAt = time.Now()
cm.role = Leader
for peerID := range peers {
// Reinitialize `nextIndex`s and `matchIndex`s of peers.
cm.state.nextIndex[peerID] = len(cm.state.log)
cm.state.matchIndex[peerID] = -1
}
log.Printf("[cm.becomeLeader] state=%+v\n", cm.state)
go func() {
// Send initial empty AEs to other servers to prevent new elections
cm.sendAE(true, peers)
for {
time.Sleep(50 * time.Millisecond)
cm.mu.Lock()
if cm.role != Leader {
cm.mu.Unlock()
return
}
for _, N := cm.state.getLastLogEntry(); N > cm.state.commitIndex; N-- {
if int(cm.state.log[N].Term) != cm.state.currentTerm {
continue
}
count := 1
for _, matchIdx := range cm.state.matchIndex {
if matchIdx >= N {
count++
}
if 2*count > len(peers)+1 {
// Now it's safe to apply the command at log[N] to the state machine
log.Printf("[cm.becomeLeader] commitIndex advanced to N=%d from %d\n", N, cm.state.commitIndex)
for _, entry := range cm.state.log[cm.state.lastApplied+1 : N+1] {
log.Printf("[cm.becomeLeader] applying log entry=%+v to state machine\n", *entry)
cm.state.lastApplied = int(entry.Idx)
}
cm.state.commitIndex = N
break
}
}
}
cm.mu.Unlock()
cm.sendAE(false, peers)
}
}()
}
func (cm *ConsensusModule) prepareElection(termBefore int, peers map[int]pb.RpcClient) {
// Election timeout 150~300ms as suggested in the Raft paper.
ms := time.Duration(150+rand.Intn(151)) * time.Millisecond
cm.mu.Lock()
cm.latestUpdateAt = time.Now()
cm.mu.Unlock()
log.Printf("[cm.prepareElection] next election will start in %v\n", ms)
for {
time.Sleep(15 * time.Millisecond)
cm.mu.Lock()
if cm.role == Leader || termBefore != cm.state.currentTerm {
cm.mu.Unlock()
return
}
if cm.latestUpdateAt.Add(ms).Before(time.Now()) {
log.Printf("[cm.prepareElection] term=%d timed out\n", termBefore)
cm.becomeCandidate(peers)
cm.mu.Unlock()
return
}
cm.mu.Unlock()
}
}
func (cm *ConsensusModule) runElection(currTerm int, peers map[int]pb.RpcClient) {
log.Printf("[cm.runElection] new election for term=%d starts", currTerm)
voteCount := 1
for _, c := range peers {
go func(rpc pb.RpcClient) {
cm.mu.Lock()
entry, i := cm.state.getLastLogEntry()
cm.mu.Unlock()
args := pb.RequestVoteArgs{
Term: int32(currTerm),
CandidateId: int32(cm.id),
LastLogIndex: int32(i),
LastLogTerm: 0,
}
if entry != nil {
args.LastLogTerm = entry.Term
}
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
resp, err := rpc.RequestVote(ctx, &args)
if err != nil {
return
}
cm.mu.Lock()
defer cm.mu.Unlock()
if cm.role != Candidate {
return
}
respTerm := int(resp.Term)
if respTerm > currTerm {
cm.becomeFollower(respTerm, peers)
} else if respTerm == currTerm && resp.VoteGranted {
voteCount++
if 2*voteCount > len(peers)+1 {
cm.becomeLeader(peers)
}
}
}(c)
}
// In case the votes are split and no new leader is elected.
go cm.prepareElection(currTerm, peers)
}
func (cm *ConsensusModule) sendAE(init bool, peers map[int]pb.RpcClient) {
cm.mu.Lock()
termBefore := cm.state.currentTerm
cm.mu.Unlock()
for peerID, c := range peers {
go func(id int, rpc pb.RpcClient) {
cm.mu.Lock()
_, lastLogIdx := cm.state.getLastLogEntry()
nextIdx := cm.state.nextIndex[id]
prevLogIdx := nextIdx - 1
args := pb.AppendEntriesArgs{
Term: int32(termBefore),
LeaderId: int32(cm.id),
PrevLogIndex: int32(prevLogIdx),
PrevLogTerm: 0,
Entries: make([]*pb.LogEntry, 0, 0),
LeaderCommit: int32(cm.state.commitIndex),
}
if prevLogIdx >= 0 {
args.PrevLogTerm = cm.state.log[prevLogIdx].Term
}
if !init && lastLogIdx >= nextIdx {
args.Entries = cm.state.log[nextIdx:]
}
cm.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond)
defer cancel()
resp, err := rpc.AppendEntries(ctx, &args)
if err != nil {
return
}
cm.mu.Lock()
defer cm.mu.Unlock()
respTerm := int(resp.Term)
if respTerm > cm.state.currentTerm {
cm.becomeFollower(respTerm, peers)
return
}
if init {
return
}
if resp.Success {
i := nextIdx + len(args.Entries)
cm.state.nextIndex[id] = i
cm.state.matchIndex[id] = i - 1
} else {
cm.state.nextIndex[id]--
}
if len(args.Entries) > 0 {
log.Printf("[cm.sendAE] state=%+v\n", cm.state)
}
}(peerID, c)
}
}