forked from happyer/distributed-computing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraft_note
More file actions
775 lines (685 loc) · 24.1 KB
/
Copy pathraft_note
File metadata and controls
775 lines (685 loc) · 24.1 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
package raft
//
// this is an outline of the API that raft must expose to
// the service (or tester). see comments below for
// each of these functions for more details.
//
// rf = Make(...)
// create a new Raft server.
// rf.Start(command interface{}) (index, term, isleader)
// start agreement on a new log entry
// rf.GetState() (term, isLeader)
// ask a Raft for its current term, and whether it thinks it is leader
// ApplyMsg
// each time a new entry is committed to the log, each Raft peer
// should send an ApplyMsg to the service (or tester)
// in the same server.
//
import (
//"fmt"
"sync"
"labrpc"
"bytes"
"encoding/gob"
"time"
"math/rand"
)
const (
LEADER = iota
CANDIDATE
FLLOWER
HBINTERVAL = 50 * time.Millisecond // 50ms
)
//
// as each Raft peer becomes aware that successive log entries are
// committed, the peer should send an ApplyMsg to the service (or
// tester) on the same server, via the applyCh passed to Make().
//
type ApplyMsg struct {
Index int
Command interface{}
UseSnapshot bool // ignore for lab2; only used in lab3
Snapshot []byte // ignore for lab2; only used in lab3
}
// 1, 如果两个entry在不同的服务器中,拥有同样的index和term,则保证他们保存了相同的命令
// 2, 如果两个entry在不同的服务器中,拥有同样的index和term,则保证在两个entry之前的所有entry都相等。
// 第一个属性要保证很简单,leader每次创建entry时,都只会使用新的index,而不会去改写之前index的内容。
// 这也就是在给定index和term的情况下,只会创建一个entry。那么也就保证了相同index和term,相同命令。
// 第二个属性需要在每次AppendEntries的时候进行检查。Leader每次发送entry给follower时,会带上前一个entry的index和term。
// 当Follower收到RPC时,会检查最后一个entry的index和term是不是和AppendEntries中的prevIndex和prevTerm相同,不相同则拒绝。
// 若不相同则说明该entry保存的命令和Leader上保存的命令不同,则自然要拒绝。这个也就是所谓的一致性检查(the consistency check)。
// Raft简单的地方在于,Leader从来不会修改自己log,而是让Follower自己去修改log。
// 方法就是Leader维护一个nextIndex[i]数组,用来保存下个发给Follower[i]的Index,
// 如果consistency check通过,则nextIndex[i]++,不然的话则nextIndex[i]--,直到双方同步log位置。
// 这个感觉有点类似于tcp传数据时的ack,总是返回下一个期望收到的数据包。
// 也就是说这里除了nextIndex的更新之外,Leader不需要做什么额外的动作。这就是Raft可理解的地方了。
type LogEntry struct {
LogIndex int // log的索引
LogTerm int // log的任期。 这说明 Log实体 里面是含有索引和任期属性的,如果保证index和term相同,则保证leader和follower日志都完全相同
LogCommand interface{}
}
//
// A Go object implementing a single Raft peer.
//
type Raft struct {
mu sync.Mutex
peers []*labrpc.ClientEnd
persister *Persister
me int // index into peers[]
// Your data here.
// Look at the paper's Figure 2 for a description of what
// state a Raft server must maintain.
//channel
state int
voteCount int
chanCommit chan bool
chanHeartbeat chan bool
chanGrantVote chan bool
chanLeader chan bool
chanApply chan ApplyMsg
//persistent state on all server
currentTerm int
votedFor int
log []LogEntry
//volatile state on all servers
commitIndex int
lastApplied int
//volatile state on leader
nextIndex []int
matchIndex []int
}
// return currentTerm and whether this server
// believes it is the leader.
func (rf *Raft) GetState() (int, bool) {
return rf.currentTerm, rf.state == LEADER
}
func (rf *Raft) getLastIndex() int {
return rf.log[len(rf.log) - 1].LogIndex
}
func (rf *Raft) getLastTerm() int {
return rf.log[len(rf.log) - 1].LogTerm
}
func (rf *Raft) IsLeader() bool {
return rf.state == LEADER
}
// 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)
data := w.Bytes()
rf.persister.SaveRaftState(data)
}
func (rf *Raft) readSnapshot(data []byte) {
rf.readPersist(rf.persister.ReadRaftState())
if len(data) == 0 {
return
}
r := bytes.NewBuffer(data)
d := gob.NewDecoder(r)
var LastIncludedIndex int
var LastIncludedTerm int
d.Decode(&LastIncludedIndex)
d.Decode(&LastIncludedTerm)
rf.commitIndex = LastIncludedIndex
rf.lastApplied = LastIncludedIndex
rf.log = truncateLog(LastIncludedIndex, LastIncludedTerm, rf.log)
msg := ApplyMsg{UseSnapshot: true, Snapshot: data}
go func() {
rf.chanApply <- msg
}()
}
//
// restore previously persisted state.
//
func (rf *Raft) readPersist(data []byte) {
// Your code here.
// Example:
r := bytes.NewBuffer(data)
d := gob.NewDecoder(r)
d.Decode(&rf.currentTerm)
d.Decode(&rf.votedFor)
d.Decode(&rf.log)
}
//
// example RequestVote RPC arguments structure.
// 请求选举,用于候选人调用获得选票
type RequestVoteArgs struct {
// Your data here.
Term int // 候选人的任期号
CandidateId int // 候选人ID
LastLogTerm int // 候选人的最新log实体的编号.
LastLogIndex int // 候选人的最新log实体的任期号
}
//
// example RequestVote RPC reply structure.
// 请求返回值
type RequestVoteReply struct {
// Your data here.
Term int // 当前任期号(currentTerm), 用于候选人更新自己
VoteGranted bool // 表示投票给该候选人
}
// 复制 log 的 RPC
// 用于领导人调用来复制log实体. 也用于维持心跳.
type AppendEntriesArgs struct {
// Your data here.
Term int // 领导人的任期号
LeaderId int // 领导人 ID, 用于跟随者可以转发请求
PrevLogTerm int // 上一个 log 的任期号. 发送信息需要带上一个索引和任期,是因为需要保证上一个是对上号的,这样递归下去,肯定会保证所有的都正确。
PrevLogIndex int // 上一个 log 的索引号.
Entries []LogEntry // 需要存储的 log (心跳的这个字段为空; 为了提高效率可能会发送多个)
LeaderCommit int // 领导人的 commitIndex
}
type AppendEntriesReply struct {
// Your data here.
Term int
Success bool
NextIndex int
}
//
// example RequestVote RPC handler.
//
func (rf *Raft) RequestVote(args RequestVoteArgs, reply *RequestVoteReply) {
// Your code here.
rf.mu.Lock()
defer rf.mu.Unlock()
defer rf.persist()
reply.VoteGranted = false
// 如果发送方的任期比自己的小,则不给他投票
if args.Term < rf.currentTerm {
reply.Term = rf.currentTerm
return
}
//If RPC request or response contains term T > currentTerm: set currentTerm = T, convert to follower
// 如果发送方的任期比自己大,
if args.Term > rf.currentTerm {
rf.currentTerm = args.Term
rf.state = FLLOWER
rf.votedFor = -1 // 每次更新term后,重置投票器,表示自己可以投票了
}
reply.Term = rf.currentTerm
term := rf.getLastTerm()
index := rf.getLastIndex()
uptoDate := false
//If votedFor is null or candidateId, and candidate’s log is at least as up-to-date as receiver’s log, grant vote
//Raft determines which of two logs is more up-to-date by comparing the index and term of the last entries in the logs.
// If the logs have last entries with different terms,then the log with the later term is more up-to-date.
// If the logs end with the same term, then whichever log is longer is more up-to-date
// 发送者最新log的任期比自己的更大
if args.LastLogTerm > term {
uptoDate = true
}
// 发送者的log编号更大
if args.LastLogTerm == term && args.LastLogIndex >= index {
// at least up to date
uptoDate = true
}
// 发送的log数据更新,同时发送者的任期比自己大或者我已经给他投过票了??,则给他投票
if (rf.votedFor == -1 || rf.votedFor == args.CandidateId) && uptoDate {
rf.chanGrantVote <- true
rf.state = FLLOWER
reply.VoteGranted = true // 投票给他
rf.votedFor = args.CandidateId
}
}
func (rf *Raft) AppendEntries(args AppendEntriesArgs, reply *AppendEntriesReply) {
// Your code here.
rf.mu.Lock()
defer rf.mu.Unlock()
defer rf.persist()
reply.Success = false
//Reply false if term < currentTerm
// 说明这是旧领导发送给自己的心跳包,什么也不处理,返回自己的term给旧领导,让旧领导知道他的term过期了
if args.Term < rf.currentTerm {
reply.Term = rf.currentTerm
reply.NextIndex = rf.getLastIndex() + 1
return
}
// 向状态机通过管道发送消息"接收到来自新的领导人的附加日志RPC",其实也是一种心跳信息
rf.chanHeartbeat <- true
//If RPC request or response contains term T > currentTerm: set currentTerm = T, convert to follower
if args.Term > rf.currentTerm {
rf.currentTerm = args.Term
rf.state = FLLOWER
rf.votedFor = -1
}
reply.Term = args.Term
// 检查对于发送方来说的上一个索引
if args.PrevLogIndex > rf.getLastIndex() {
// 如果不对,则返回我期待的索引号,即我当前的索引加一
reply.NextIndex = rf.getLastIndex() + 1
return
}
baseIndex := rf.log[0].LogIndex
// If a follower’s log is inconsistent with the leader’s, the AppendEntries consis- tency check will fail in the next AppendEntries RPC.
// Af- ter a rejection, the leader decrements nextIndex and retries the AppendEntries RPC
//Eventually nextIndex will reach a point where the leader and follower logs match
//which removes any conflicting entries in the follower’s log and appends entries from the leader’s log (if any).
if args.PrevLogIndex > baseIndex {
term := rf.log[args.PrevLogIndex - baseIndex].LogTerm
// 判断上一个任期是否与现在任期相同
if args.PrevLogTerm != term {
for i := args.PrevLogIndex - 1; i >= baseIndex; i-- {
if rf.log[i - baseIndex].LogTerm != term {
reply.NextIndex = i + 1
break
}
}
return
}
}
if args.PrevLogIndex < baseIndex {
} else {
//Append any new entries not already in the log
rf.log = rf.log[: args.PrevLogIndex + 1 - baseIndex]
rf.log = append(rf.log, args.Entries...)
reply.Success = true
reply.NextIndex = rf.getLastIndex() + 1
}
//If leaderCommit > commitIndex, set commitIndex =min(leaderCommit, index of last new entry)
if args.LeaderCommit > rf.commitIndex {
last := rf.getLastIndex()
if args.LeaderCommit > last {
rf.commitIndex = last
} else {
rf.commitIndex = args.LeaderCommit
}
rf.chanCommit <- true
}
return
}
//
// example code to send a RequestVote RPC to a server.
// server is the index of the target server in rf.peers[].
// expects RPC arguments in args.
// fills in *reply with RPC reply, so caller should
// pass &reply.
// the types of the args and reply passed to Call() must be
// the same as the types of the arguments declared in the
// handler function (including whether they are pointers).
//
// returns true if labrpc says the RPC was delivered.
//
// if you're having trouble getting RPC to work, check that you've
// capitalized all field names in structs passed over RPC, and
// that the caller passes the address of the reply struct with &, not
// the struct itself.
//
func (rf *Raft) sendRequestVote(server int, args RequestVoteArgs, reply *RequestVoteReply) bool {
ok := rf.peers[server].Call("Raft.RequestVote", args, reply)
rf.mu.Lock()
defer rf.mu.Unlock()
if ok {
term := rf.currentTerm
// 在异步/阻塞调用前后,可能身份角色(自身)和选举周期(外界)早已天翻地覆,因此需要进行自检。
if rf.state != CANDIDATE {
return ok
}
// 异步调用后,有可能任期改变了
if args.Term != term {
return ok
}
if reply.Term > term {
rf.currentTerm = reply.Term
rf.state = FLLOWER
rf.votedFor = -1
rf.persist()
}
if reply.VoteGranted {
rf.voteCount++ // 如果其他服务器给自己投票了,则投票数加1
if rf.state == CANDIDATE && rf.voteCount > len(rf.peers) / 2 {
rf.state = FLLOWER
// 如果自己的投票数大于一半,则向状态机通过管道发送消息说"接收到大多数服务器的选票"
rf.chanLeader <- true
}
}
}
return ok
}
func (rf *Raft) sendAppendEntries(server int, args AppendEntriesArgs, reply *AppendEntriesReply) bool {
ok := rf.peers[server].Call("Raft.AppendEntries", args, reply)
rf.mu.Lock()
defer rf.mu.Unlock()
if ok {
// Leader进行心跳检测时候,由于是异步调用,所以需要先检测自己还是不是leader,
// 以及是否还在自己任期(由args保存了当时的任期);
// 当rpc返回后,再一次进行该检查,无误,才能按照自己是leader来进行下一步动作。
if rf.state/*新的状态还是leader吗?*/ != LEADER {
return ok
}
if args.Term/*args保存了阻塞调用Call之前的任期*/ != rf.currentTerm/*新的任期还是之前的任期吗*/ {
return ok
}
if reply.Term > rf.currentTerm {
rf.currentTerm = reply.Term
rf.state = FLLOWER
rf.votedFor = -1
rf.persist()
return ok
}
if reply.Success {
if len(args.Entries) > 0 {
rf.nextIndex[server] = args.Entries[len(args.Entries) - 1].LogIndex + 1
//reply.NextIndex
//rf.nextIndex[server] = reply.NextIndex
rf.matchIndex[server] = rf.nextIndex[server] - 1
}
} else {
rf.nextIndex[server] = reply.NextIndex
}
}
return ok
}
type InstallSnapshotArgs struct {
Term int
LeaderId int
LastIncludedIndex int
LastIncludedTerm int
Data []byte
}
type InstallSnapshotReply struct {
Term int
}
func (rf *Raft) GetPerisistSize() int {
return rf.persister.RaftStateSize()
}
func (rf *Raft) StartSnapshot(snapshot []byte, index int) {
rf.mu.Lock()
defer rf.mu.Unlock()
baseIndex := rf.log[0].LogIndex
lastIndex := rf.getLastIndex()
if index <= baseIndex || index > lastIndex {
// in case having installed a snapshot from leader before snapshotting
// second condition is a hack
return
}
var newLogEntries []LogEntry
newLogEntries = append(newLogEntries, LogEntry{LogIndex: index, LogTerm: rf.log[index - baseIndex].LogTerm})
for i := index + 1; i <= lastIndex; i++ {
newLogEntries = append(newLogEntries, rf.log[i - baseIndex])
}
rf.log = newLogEntries
rf.persist()
w := new(bytes.Buffer)
e := gob.NewEncoder(w)
e.Encode(newLogEntries[0].LogIndex)
e.Encode(newLogEntries[0].LogTerm)
data := w.Bytes()
data = append(data, snapshot...)
rf.persister.SaveSnapshot(data)
}
func truncateLog(lastIncludedIndex int, lastIncludedTerm int, log []LogEntry) []LogEntry {
var newLogEntries []LogEntry
newLogEntries = append(newLogEntries, LogEntry{LogIndex: lastIncludedIndex, LogTerm: lastIncludedTerm})
for index := len(log) - 1; index >= 0; index-- {
if log[index].LogIndex == lastIncludedIndex && log[index].LogTerm == lastIncludedTerm {
newLogEntries = append(newLogEntries, log[index + 1:]...)
break
}
}
return newLogEntries
}
func (rf *Raft) InstallSnapshot(args InstallSnapshotArgs, reply *InstallSnapshotReply) {
// Your code here.
rf.mu.Lock()
defer rf.mu.Unlock()
if args.Term < rf.currentTerm {
reply.Term = rf.currentTerm
return
}
rf.chanHeartbeat <- true
rf.state = FLLOWER
rf.currentTerm = rf.currentTerm
rf.persister.SaveSnapshot(args.Data)
rf.log = truncateLog(args.LastIncludedIndex, args.LastIncludedTerm, rf.log)
msg := ApplyMsg{UseSnapshot: true, Snapshot: args.Data}
rf.lastApplied = args.LastIncludedIndex
rf.commitIndex = args.LastIncludedIndex
rf.persist()
rf.chanApply <- msg
}
func (rf *Raft) sendInstallSnapshot(server int, args InstallSnapshotArgs, reply *InstallSnapshotReply) bool {
ok := rf.peers[server].Call("Raft.InstallSnapshot", args, reply)
if ok {
if reply.Term > rf.currentTerm {
rf.currentTerm = reply.Term
rf.state = FLLOWER
rf.votedFor = -1
return ok
}
rf.nextIndex[server] = args.LastIncludedIndex + 1
rf.matchIndex[server] = args.LastIncludedIndex
}
return ok
}
//
// the service using Raft (e.g. a k/v server) wants to start
// agreement on the next command to be appended to Raft's log. if this
// server isn't the leader, returns false. otherwise start the
// agreement and return immediately. there is no guarantee that this
// command will ever be committed to the Raft log, since the leader
// may fail or lose an election.
//
// the first return value is the index that the command will appear at
// if it's ever committed. the second return value is the current
// term. the third return value is true if this server believes it is
// the leader.
//
func (rf *Raft) Start(command interface{}) (int, int, bool) {
rf.mu.Lock()
defer rf.mu.Unlock()
index := -1
term := rf.currentTerm
isLeader := rf.state == LEADER
if isLeader {
index = rf.getLastIndex() + 1
//fmt.Printf("raft:%d start\n",rf.me)
rf.log = append(rf.log, LogEntry{LogTerm:term, LogCommand:command, LogIndex:index}) // append new entry from client
rf.persist()
}
return index, term, isLeader
}
//
// the tester calls Kill() when a Raft instance won't
// be needed again. you are not required to do anything
// in Kill(), but it might be convenient to (for example)
// turn off debug output from this instance.
//
func (rf *Raft) Kill() {
// Your code here, if desired.
}
func (rf *Raft) broadcastRequestVote() {
var args RequestVoteArgs
rf.mu.Lock()
args.Term = rf.currentTerm
args.CandidateId = rf.me
args.LastLogTerm = rf.getLastTerm()
args.LastLogIndex = rf.getLastIndex()
rf.mu.Unlock()
for i := range rf.peers {
if i != rf.me && rf.state == CANDIDATE {
go func(i int) {
var reply RequestVoteReply
rf.sendRequestVote(i, args, &reply)
}(i)
}
}
}
/**
* Log replication
*/
// 广播日志复制
func (rf *Raft) broadcastAppendEntries() {
rf.mu.Lock()
defer rf.mu.Unlock()
N := rf.commitIndex
last := rf.getLastIndex()
baseIndex := rf.log[0].LogIndex
//If there exists an N such that N > commitIndex, a majority of matchIndex[i] ≥ N, and log[N].term == currentTerm: set commitIndex = N
for i := rf.commitIndex + 1; i <= last; i++ {
num := 1
for j := range rf.peers {
if j != rf.me && rf.matchIndex[j] >= i && rf.log[i - baseIndex].LogTerm == rf.currentTerm {
num++
}
}
if 2 * num > len(rf.peers) {
N = i
}
}
// 检测大多数 logEntry match Index,
// 以决定是否要进行 commit,即前移 Leader 的 commitIndex ,
// 并且在以后的 AppendEntries 的 RPC 中将其同步给各个 Follower。
if N != rf.commitIndex {
rf.commitIndex = N
rf.chanCommit <- true
}
for i := range rf.peers {
if i != rf.me && rf.state == LEADER {
//copy(args.Entries, rf.log[args.PrevLogIndex + 1:])
// 同步过程,我将其分为对 match 位置的 试探阶段 和 match 后的传送阶段;
// 即首先通过每次前移 prevLogIndex+prevLogTerm 匹配上 Follower 中的某个 logEntry,
// 然后 Leader 将匹配到的 logEntry 之后的 entries 一次性送给 Follower。
if rf.nextIndex[i] > baseIndex {
var args AppendEntriesArgs
args.Term = rf.currentTerm
args.LeaderId = rf.me
args.PrevLogIndex = rf.nextIndex[i] - 1
// fmt.Printf("baseIndex:%d PrevLogIndex:%d\n",baseIndex,args.PrevLogIndex )
args.PrevLogTerm = rf.log[args.PrevLogIndex - baseIndex].LogTerm
//args.Entries = make([]LogEntry, len(rf.log[args.PrevLogIndex + 1:]))
args.Entries = make([]LogEntry, len(rf.log[args.PrevLogIndex + 1 - baseIndex:]))
copy(args.Entries, rf.log[args.PrevLogIndex + 1 - baseIndex:])
args.LeaderCommit = rf.commitIndex
go func(i int, args AppendEntriesArgs) {
var reply AppendEntriesReply
rf.sendAppendEntries(i, args, &reply)
}(i, args)
} else {
var args InstallSnapshotArgs
args.Term = rf.currentTerm
args.LeaderId = rf.me
args.LastIncludedIndex = rf.log[0].LogIndex
args.LastIncludedTerm = rf.log[0].LogTerm
args.Data = rf.persister.snapshot
go func(server int, args InstallSnapshotArgs) {
reply := &InstallSnapshotReply{}
rf.sendInstallSnapshot(server, args, reply)
}(i, args)
}
}
}
}
//
// the service or tester wants to create a Raft server. the ports
// of all the Raft servers (including this one) are in peers[]. this
// server's port is peers[me]. all the servers' peers[] arrays
// have the same order. persister is a place for this server to
// save its persistent state, and also initially holds the most
// recent saved state, if any. applyCh is a channel on which the
// tester or service expects Raft to send ApplyMsg messages.
// Make() must return quickly, so it should start goroutines
// for any long-running work.
//
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.
rf.state = FLLOWER
rf.votedFor = -1
rf.log = append(rf.log, LogEntry{LogTerm: 0})
rf.currentTerm = 0
rf.chanCommit = make(chan bool, 100)
rf.chanHeartbeat = make(chan bool, 100)
rf.chanGrantVote = make(chan bool, 100)
rf.chanLeader = make(chan bool, 100)
rf.chanApply = applyCh
// initialize from state persisted before a crash
rf.readPersist(persister.ReadRaftState())
rf.readSnapshot(persister.ReadSnapshot())
go func() {
for {
// fmt.Println("me: ", me, " state: ", rf.state)
switch rf.state {
case FLLOWER:
select {
case <-rf.chanHeartbeat:
case <-rf.chanGrantVote:
case <-time.After(time.Duration(rand.Int63() % 333 + 550) * time.Millisecond):
// 超时一个随机时间后,将自己职位候选人,超时发起选举
rf.state = CANDIDATE
}
case LEADER:
//fmt.Printf("Leader:%v %v\n",rf.me,"boatcastAppendEntries ")
// 定期心跳,有时携带日志同步
rf.broadcastAppendEntries()
time.Sleep(HBINTERVAL)
case CANDIDATE:
rf.mu.Lock()
//To begin an election, a follower increments its current term and transitions to candidate state
// 任期加一
rf.currentTerm++
//It then votes for itself and issues RequestVote RPCs in parallel to each of the other servers in the cluster.
rf.votedFor = rf.me
rf.voteCount = 1
rf.persist() // 防止机器故障,将节点状态持久化
rf.mu.Unlock()
// (a) it wins the election,
// (b) another server establishes itself as leader, or
// (c) a period of time goes by with no winner
go rf.broadcastRequestVote() // 向其他节点拉票
select {
case <-time.After(time.Duration(rand.Int63() % 300 + 510) * time.Millisecond):
case <-rf.chanHeartbeat: // 如果有其他的节点成为了leader则会在这里接收到心跳消息
rf.state = FLLOWER
// fmt.Printf("CANDIDATE %v reveive chanHeartbeat\n",rf.me)
case <-rf.chanLeader: // 如果票数大于一半了,则向状态机通过管道发送消息说"接收到大多数服务器的选票",说明自己是leader了
rf.mu.Lock()
rf.state = LEADER // 设置自己为leader
//fmt.Printf("%v is Leader\n",rf.me)
// 初始化
rf.nextIndex = make([]int, len(rf.peers))
rf.matchIndex = make([]int, len(rf.peers))
for i := range rf.peers {
//The leader maintains a nextIndex for each follower, which is the index of the next log entry the leader will send to that follower.
// When a leader first comes to power, it initializes all nextIndex values to the index just after the last one in its log
rf.nextIndex[i] = rf.getLastIndex() + 1
rf.matchIndex[i] = 0
}
rf.mu.Unlock()
// 此致,立即开始心跳
//rf.boatcastAppendEntries()
}
}
}
}()
go func() {
for {
select {
// 已经超过半数的节点日志复制成功,则可以提交
case <-rf.chanCommit:
rf.mu.Lock()
commitIndex := rf.commitIndex
baseIndex := rf.log[0].LogIndex
for i := rf.lastApplied + 1; i <= commitIndex; i++ {
msg := ApplyMsg{Index: i, Command: rf.log[i - baseIndex].LogCommand}
applyCh <- msg
//fmt.Printf("me:%d %v\n",rf.me,msg)
rf.lastApplied = i
}
rf.mu.Unlock()
}
}
}()
return rf
}