-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raft.go
601 lines (526 loc) · 14.4 KB
/
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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package raft
import (
"bytes"
"encoding/gob"
"labrpc"
"math/rand"
"sync"
"time"
)
type ApplyMsg struct {
Index int
Command interface{}
UseSnapshot bool
Snapshot []byte
}
//
// A Go object implementing a single Raft peer.
//
type Raft struct {
mu sync.Mutex
peers []*labrpc.ClientEnd
persister *Persister
me int // index into peers[]
currentTerm int
votedFor int
state string
myVotes int
lastHeartbeat int
Log []LogEntry
//volatile state on all servers
commitIndex int
lastApplied int
electionTimeout int //timeout in milliseconds
applyCh chan ApplyMsg
signalToCommit chan bool
nextIndex []int
matchIndex []int
isLeader bool
}
type LogEntry struct {
Term int
Command interface{}
}
// return currentTerm and whether this server
// believes it is the leader.
func (rf *Raft) GetState() (int, bool) {
var term int
var isleader bool
// Your code here.
rf.mu.Lock()
if rf.state == "Leader" {
isleader = true
} else {
isleader = false
}
term = rf.currentTerm
rf.mu.Unlock()
return term, isleader
}
//
// save Raft's persistent state to stable storage,
// where it can later be retrieved after a crash and restart.
// see paper's Figure 2 for a description of what should be persistent.
//
func (rf *Raft) persist() {
// Your code here.
// Example:
w := new(bytes.Buffer)
e := gob.NewEncoder(w)
e.Encode(rf.currentTerm)
e.Encode(rf.votedFor)
e.Encode(rf.Log)
e.Encode(rf.state)
data := w.Bytes()
rf.persister.SaveRaftState(data)
}
//
// restore previously persisted state.
//
func (rf *Raft) readPersist(data []byte) {
// Your code here.
// Example:
r := bytes.NewBuffer(data)
d := gob.NewDecoder(r)
rf.mu.Lock()
d.Decode(&rf.currentTerm)
d.Decode(&rf.votedFor)
d.Decode(&rf.Log)
d.Decode(&rf.state)
rf.mu.Unlock()
}
//
// example RequestVote RPC arguments structure.
//
type RequestVoteArgs struct {
// Your data here.
Term int
CandidateID int
LastLogIndex int
LastLogTerm int
}
//
// example RequestVote RPC reply structure.
//
type RequestVoteReply struct {
// Your data here.
Term int
VoteGranted bool
}
type AppendEntriesArgs struct {
Term int
LeaderID int
PrevLogIndex int
PrevLogTerm int
Entries []LogEntry
LeaderCommit int
}
type AppendEntriesReply struct {
// Your data here.
Term int
Success bool
}
//
// example RequestVote RPC handler.
//
func (rf *Raft) RequestVote(args RequestVoteArgs, reply *RequestVoteReply) {
// Your code here.
//logic of how do you process when you recv requestVote RPC
rf.mu.Lock()
//fmt.Println("getting req vote", "me:", rf.me, "args", args, "currentTerm", rf.currentTerm, "log", rf.Log)
myLastLogIndex := len(rf.Log) - 1
myLastLogTerm := 0
if len(rf.Log) == 0 {
myLastLogTerm = -1
} else {
myLastLogTerm = rf.Log[len(rf.Log)-1].Term
}
reply.VoteGranted = false
if args.Term > rf.currentTerm {
//fmt.Println("coming here in 1st if")
rf.currentTerm = args.Term
rf.votedFor = -1
}
if args.Term > rf.currentTerm && args.LastLogTerm == -1 { //for normal cases with no logs
//fmt.Println("coming here in 2nd if")
rf.currentTerm = args.Term
rf.state = "Follower"
rf.votedFor = args.CandidateID
rf.myVotes = 0
rf.persist()
reply.VoteGranted = true
rf.mu.Unlock()
//fmt.Println("i am", rf.me, "i have voted for", args.CandidateID)
return
}
// fmt.Println("before if", "args.Term", args.Term, "args.LastLogTerm", args.LastLogTerm, "rf.votedFor", rf.votedFor, "myLastLogTerm", myLastLogTerm)
if ((args.Term == rf.currentTerm) || (args.Term > rf.currentTerm)) && (rf.votedFor == -1 || rf.votedFor == args.CandidateID) && (args.LastLogTerm > myLastLogTerm ||
(args.LastLogTerm == myLastLogTerm && args.LastLogIndex >= myLastLogIndex)) {
rf.currentTerm = args.Term
rf.state = "Follower"
rf.votedFor = args.CandidateID
rf.myVotes = 0
rf.persist()
reply.VoteGranted = true
rf.lastHeartbeat = int(time.Now().UnixMilli())
//fmt.Println("i am", rf.me, "i have voted for", args.CandidateID)
}
reply.Term = rf.currentTerm
rf.mu.Unlock()
}
func (rf *Raft) sendRequestVote(server int, args RequestVoteArgs, reply *RequestVoteReply) bool {
//fmt.Println("sending request vote to", server, "me:", rf.me, args, "my log is:", rf.Log)
ok := rf.peers[server].Call("Raft.RequestVote", args, reply)
rf.mu.Lock()
if rf.state != "Candidate" {
rf.mu.Unlock()
return ok
//do nothing since u r no longer a candidate
} else {
if ok {
if reply.Term > rf.currentTerm {
rf.currentTerm = reply.Term
//fmt.Println("turning to Follower")
rf.state = "Follower"
rf.votedFor = -1
rf.myVotes = 0
rf.persist()
} else {
if reply.VoteGranted {
rf.myVotes += 1
numOfServers := len(rf.peers)
//fmt.Println("I am", rf.me, "number of votes gotten:", rf.myVotes, "voted for me", server)
if rf.myVotes > (numOfServers / 2) { //here is the leader functionality
//fmt.Println("becoming leader", rf.me)
rf.leader()
}
}
}
}
}
rf.mu.Unlock()
return ok
}
func (rf *Raft) leader() {
rf.state = "Leader"
rf.myVotes = 0
rf.isLeader = true
rf.persist()
/*the leader here has first come to power so initializes all nextIndex values to the index just after the
last one in its log*/
//fmt.Println("log size when leader made is", len(rf.Log))
for i := 0; i < len(rf.peers); i++ {
rf.nextIndex[i] = len(rf.Log)
rf.matchIndex[i] = -1
}
go rf.sendHeartbeats()
}
func (rf *Raft) TakeMin(x1 int, x2 int) int {
if x1 < x2 {
return x1
} else {
return x2
}
}
func (rf *Raft) AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) {
// Your code here.
//logic of how do you process when you recv AppendEntries RPC
rf.mu.Lock()
defer rf.mu.Unlock()
//fmt.Println("called in appendentries", "args are:", args, "i am", rf.state, rf.me, "currentTerm:", rf.currentTerm, "commitIndex", rf.commitIndex, rf.Log)
rf.lastHeartbeat = int(time.Now().UnixMilli())
reply.Success = false
if args.Term > rf.currentTerm {
//leader needs to send my most updated term so I will not give success
rf.state = "Follower"
rf.currentTerm = args.Term
rf.votedFor = -1
rf.persist()
return
} else if args.Term < rf.currentTerm {
reply.Success = false
reply.Term = rf.currentTerm
return
} else if len(args.Entries) == 0 {
//fmt.Println("i am saying TRUE BITCH (lenEntries) 0", rf.me)
//reply.Success = true
reply.Term = rf.currentTerm
return
} else {
rf.Log = args.Entries
//fmt.Println("i am ", rf.state, rf.me, "my log is", rf.Log)
//fmt.Println("i am saying TRUE BITCH", rf.me)
rf.state = "Follower"
rf.currentTerm = args.Term
reply.Term = rf.currentTerm
rf.votedFor = -1
reply.Success = true
if args.LeaderCommit > rf.commitIndex {
rf.commitIndex = rf.TakeMin(args.LeaderCommit, len(rf.Log)-1)
rf.signalToCommit <- true
// fmt.Println("i am ", rf.state, rf.me, "my commit index is", rf.commitIndex)
}
rf.persist()
}
// rf.heartbeat = time.Now().Nanosecond() / 1000000
//fmt.Println("called in appendentries END", "args are:", args, "i am", rf.state, rf.me, "currentTerm:", rf.currentTerm, "commitIndex", rf.commitIndex, rf.Log, "reply", reply)
}
func (rf *Raft) sendAppendEntries(server int, args AppendEntriesArgs, reply *AppendEntriesReply) bool {
//fmt.Println("SENDING APPENDENTRIES TO", server, "args", args)
ok := rf.peers[server].Call("Raft.AppendEntries", args, reply)
rf.mu.Lock()
//fmt.Println("REPLY RECEIVED FROM", server, "REPLY", reply)
//processing reply of heartbeat sendAppendEntries RPC
if reply.Term > rf.currentTerm {
rf.state = "Follower"
rf.currentTerm = reply.Term
rf.votedFor = -1
rf.myVotes = 0
rf.persist()
rf.mu.Unlock()
return ok
}
if rf.state == "Leader" {
if ok {
if reply.Success {
//fmt.Println("OLD match index of:", server, rf.matchIndex[server], "all matchindex", rf.matchIndex)
rf.nextIndex[server] = len(rf.Log) //args.PrevLogIndex + 1 + len(args.Entries)
rf.matchIndex[server] = len(rf.Log) - 1
//fmt.Println("updating match index of:", server, rf.matchIndex[server], "all matchindex", rf.matchIndex)
currentCommitIndex := rf.commitIndex
for n := rf.commitIndex + 1; n < len(rf.Log); n++ {
// make sure that leader only applies if its log entry is replicated in the majority
if rf.Log[n].Term == rf.currentTerm {
count := 0
for j := 0; j < len(rf.peers); j++ {
if j != rf.me {
if rf.matchIndex[j] >= n {
count += 1
}
}
}
if count >= (len(rf.peers))/2 {
rf.commitIndex = n
rf.persist()
// fmt.Println("i am ", rf.state, rf.me, "my commitindex is", rf.commitIndex)
}
}
}
if rf.commitIndex != currentCommitIndex {
rf.signalToCommit <- true
}
} else {
//fmt.Println("decrementing prev logindex")
rf.nextIndex[server] -= 1
}
}
}
rf.mu.Unlock()
return ok
}
func (rf *Raft) Start(command interface{}) (int, int, bool) {
index := -1
term := -1
isLeader := false
rf.mu.Lock()
index = len(rf.Log) + 1
term = rf.currentTerm
if rf.state == "Leader" && rf.isLeader {
isLeader = true
e := LogEntry{
Term: rf.currentTerm,
Command: command,
}
rf.Log = append(rf.Log, e)
// fmt.Println("i am", rf.state, rf.me, "log is", rf.Log)
rf.persist()
} else {
rf.mu.Unlock()
return -1, -1, false
}
rf.mu.Unlock()
return index, term, isLeader
}
func (rf *Raft) Kill() {
// Your code here, if desired.
}
func (rf *Raft) ApplyCommittedLogEntries() {
for {
select {
case <-rf.signalToCommit:
rf.mu.Lock()
lastApplied := rf.lastApplied
var entries []LogEntry
if rf.commitIndex > rf.lastApplied {
entries = rf.Log[rf.lastApplied+1 : rf.commitIndex+1]
rf.lastApplied = rf.commitIndex
}
rf.mu.Unlock()
lastApplied += 1
for i := 0; i < len(entries); i++ {
entry := ApplyMsg{
Index: lastApplied + i + 1,
Command: entries[i].Command,
}
// fmt.Println("applying to state machine", "i am", rf.state, rf.me, "index of apply:", entry.Index)
rf.applyCh <- entry
}
rf.mu.Lock()
rf.persist()
rf.mu.Unlock()
}
}
}
func Make(peers []*labrpc.ClientEnd, me int,
persister *Persister, applyCh chan ApplyMsg) *Raft {
rf := &Raft{}
rf.peers = peers
rf.persister = persister
rf.me = me
// Your initialization code here.
// initialize from state persisted before a crash
// fmt.Println("Wujood mai aaraha hu", me)
rf.currentTerm = 0
rf.myVotes = 0
rf.state = "Follower"
rf.votedFor = -1
rf.electionTimeout = rand.Intn(500-300) + 300 //gives me randomized timeout
rf.commitIndex = -1
rf.lastApplied = -1
rf.nextIndex = make([]int, len(rf.peers))
rf.matchIndex = make([]int, len(rf.peers))
rf.Log = make([]LogEntry, 0)
rf.applyCh = applyCh
rf.signalToCommit = make(chan bool)
rf.isLeader = false
rf.readPersist(persister.ReadRaftState())
go rf.follower() //this go routine implements the follower logic.
go rf.ApplyCommittedLogEntries()
return rf
}
func (rf *Raft) follower() {
rf.isLeader = false
//The follower has to 1. keep track of timeout 2. Respond to RPCs by other servers
for {
time.Sleep(time.Duration(rf.electionTimeout) * time.Millisecond) //wait for election timeout
rf.mu.Lock()
timeout := rf.electionTimeout
hb := rf.lastHeartbeat
state := rf.state
rf.mu.Unlock()
if state == "Leader" {
//fmt.Println("Still leader", rf.me)
}
if state != "Leader" && (int(time.Now().UnixMilli())-hb) > timeout {
rf.startElection() //start election after you did not recv heartbeat in election timeout.
}
}
}
func (rf *Raft) sendHeartbeats() {
for {
rf.mu.Lock()
if rf.state != "Leader" {
rf.mu.Unlock()
return
}
numOfServers := len(rf.peers)
rf.mu.Unlock()
rep := AppendEntriesReply{}
for i := 0; i < numOfServers; i++ {
if i == rf.me {
} else {
//prevLogIndex has to be inidvidually set for each follower in every iteration.
rf.mu.Lock()
//nextIndexFollower := rf.nextIndex[i]
//fmt.Println("the index is", nextIndexFollower)
prevLogIndex := -1
prevLogTerm := -1
// if prevLogIndex < 0 {
// prevLogTerm = rf.currentTerm
// } else {
// prevLogTerm = rf.Log[prevLogIndex].Term
// }
//what if a follower does not have any log?
//nextIndex would be 0 in that case and prevLogIndex would be -1. This is not accessible in rf.Log
entries := rf.Log
//fmt.Println("leader sending entries", entries)
req := AppendEntriesArgs{
Term: rf.currentTerm,
LeaderID: rf.me,
PrevLogIndex: prevLogIndex,
PrevLogTerm: prevLogTerm,
Entries: entries,
LeaderCommit: rf.commitIndex,
}
rf.mu.Unlock()
//fmt.Println("sending heartbeat to", i, req)
go rf.sendAppendEntries(i, req, &rep)
}
}
time.Sleep(100 * time.Millisecond)
// go rf.changeCommitIndex()
}
}
func (rf *Raft) changeCommitIndex() {
rf.mu.Lock()
defer rf.mu.Unlock()
if rf.state != "Leader" {
return
}
currentCommitIndex := rf.commitIndex
for n := rf.commitIndex + 1; n < len(rf.Log); n++ {
// make sure that leader only applies if its log entry is replicated in the majority
if rf.Log[n].Term == rf.currentTerm {
count := 0
for j := 0; j < len(rf.peers); j++ {
if j != rf.me {
if rf.matchIndex[j] >= n {
count += 1
}
}
}
if count > (len(rf.peers))/2 {
rf.commitIndex = n
//fmt.Println("i am ", rf.state, rf.me, "commitindex is", rf.commitIndex)
rf.persist()
}
}
}
if rf.commitIndex != currentCommitIndex {
rf.signalToCommit <- true
}
}
func (rf *Raft) startElection() {
//this routine will send RequestVote RPCs.
//going into candidate state after election timeout
// fmt.Println(int(time.Now().UnixMilli()) - hb)
//fmt.Println("timing out and my name is", rf.me, rf.state, "term:", rf.currentTerm)
rf.mu.Lock()
rf.lastHeartbeat = int(time.Now().UnixMilli())
rf.state = "Candidate"
rf.votedFor = rf.me
rf.isLeader = false
rf.myVotes = 0
rf.currentTerm += 1
rf.myVotes += 1 //vote for self
lastLogTerm := 0
if len(rf.Log) == 0 {
lastLogTerm = -1
} else {
lastLogTerm = rf.Log[len(rf.Log)-1].Term
}
req := RequestVoteArgs{
Term: rf.currentTerm,
CandidateID: rf.me,
LastLogIndex: len(rf.Log) - 1,
LastLogTerm: lastLogTerm,
}
rf.electionTimeout = rand.Intn(500-300) + 300 //reset election timer
rf.mu.Unlock()
for i := 0; i < len(rf.peers); i++ { //sending RequestVoteRPC to all peers
if i != rf.me {
rep := RequestVoteReply{}
go rf.sendRequestVote(i, req, &rep)
}
}
}