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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
|
// 角色分类
const (
follower = "Follower"
candidate = "Candidate"
leader = "Leader"
)
const (
unknownLeader = 0
noVote = 0
)
// 选举时间随机范围[MinimumElectionTimeoutMS, maximumElectionTimeoutMS]
var (
MinimumElectionTimeoutMS int32 = 250
maximumElectionTimeoutMS = 2 * MinimumElectionTimeoutMS
)
var (
errNotLeader = errors.New("not the leader")
errUnknownLeader = errors.New("unknown leader")
errDeposed = errors.New("deposed during replication")
errAppendE#008000ntriesRejected = errors.New("appendEntries RPC rejected")
errReplicationFailed = errors.New("command replication failed (but will keep retrying)")
errOutOfSync = errors.New("out of sync")
errAlreadyRunning = errors.New("already running")
)
// 重置选举时间
func resetElectionTimeoutMS(newMin, newMax int) (int, int) {
oldMin := atomic.LoadInt32(&MinimumElectionTimeoutMS)
oldMax := atomic.LoadInt32(&maximumElectionTimeoutMS)
atomic.StoreInt32(&MinimumElectionTimeoutMS, int32(newMin))
atomic.StoreInt32(&maximumElectionTimeoutMS, int32(newMax))
return int(oldMin), int(oldMax)
}
// minimumElectionTimeout returns the current minimum election timeout.
func minimumElectionTimeout() time.Duration {
return time.Duration(MinimumElectionTimeoutMS) * time.Millisecond
}
// maximumElectionTimeout returns the current maximum election time.
func maximumElectionTimeout() time.Duration {
return time.Duration(maximumElectionTimeoutMS) * time.Millisecond
}
// 选举时间随机函数
func electionTimeout() time.Duration {
n := rand.Intn(int(maximumElectionTimeoutMS - MinimumElectionTimeoutMS))
d := int(MinimumElectionTimeoutMS) + n
return time.Duration(d) * time.Millisecond
}
// broadcastInterval returns the interval between heartbeats (AppendEntry RPCs)
// broadcast from the leader. It is the minimum election timeout / 10, as
// dictated by the spec: BroadcastInterval << ElectionTimeout << MTBF.
// 广播时间,用于Leader发送心跳广播,这个时间应小于选举时间;否则,非Leader节点会产生选举操作
func broadcastInterval() time.Duration {
d := MinimumElectionTimeoutMS / 10
return time.Duration(d) * time.Millisecond
}
// protectedString is just a string protected by a mutex.
type protectedString struct {
sync.RWMutex
value string
}
func (s *protectedString) Get() string {
s.RLock()
defer s.RUnlock()
return s.value
}
func (s *protectedString) Set(value string) {
s.Lock()
defer s.Unlock()
s.value = value
}
// protectedBool is just a bool protected by a mutex.
type protectedBool struct {
sync.RWMutex
value bool
}
func (s *protectedBool) Get() bool {
s.RLock()
defer s.RUnlock()
return s.value
}
func (s *protectedBool) Set(value bool) {
s.Lock()
defer s.Unlock()
s.value = value
}
// Server is the agent that performs all of the Raft protocol logic.
// In a typical application, each running process that wants to be part of
// the distributed state machine will contain a server component.
type Server struct {
id uint64 // id of this server
// 节点状态
state *protectedString
// 节点运行状态
running *protectedBool
// Leader节点标示
leader uint64
// 当前节点任期号
term uint64 // "current term number, which increases monotonically"
// 0表示,当前节点还有投出自己的票;
// 非零表示节点已经投票了,值是获票者的标示ID
vote uint64 // who we voted for this term, if applicable
log *raftLog
config *configuration
// 追加日志信道
appendEntriesChan chan appendEntriesTuple
// 投票信道
requestVoteChan chan requestVoteTuple
// 命令信道
commandChan chan commandTuple
// 配置修改信道
configurationChan chan configurationTuple
// 选举信道
electionTick <-chan time.Time
// 退出信道
quit chan chan struct{}
}
// 状态机函数
// 该函数不可并发执行,否则就达不到一致性状态机的效果(执行时间不要超过选举时间)
// 正常来说,只有"共识"达成的时候,才会调用该函数,然后返回给客户端
// 但是,在这里为了简化实现,"共识“算法是放在后台任务操作的,客户端发送命令单Leader时,Leader马上
// 应答客户端,并没有等”共识算法“的共识结果
type ApplyFunc func(commitIndex uint64, cmd []byte) []byte
// 初始化节点
// 1. 构建日志 2.初始化为"follower"角色 3.leader为"unknown"
func NewServer(id uint64, store io.ReadWriter, a ApplyFunc) *Server {
if id <= 0 {
panic("server id must be > 0")
}
// 5.2 Leader election: "the latest term this server has seen is persisted,
// and is initialized to 0 on first boot."
log := newRaftLog(store, a)
latestTerm := log.lastTerm()
s := &Server{
id: id,
state: &protectedString{value: follower}, // "when servers start up they begin as followers"
running: &protectedBool{value: false},
leader: unknownLeader, // unknown at startup
log: log,
term: latestTerm,
config: newConfiguration(peerMap{}),
appendEntriesChan: make(chan appendEntriesTuple),
requestVoteChan: make(chan requestVoteTuple),
commandChan: make(chan commandTuple),
configurationChan: make(chan configurationTuple),
electionTick: nil,
quit: make(chan chan struct{}),
}
s.resetElectionTimeout()
return s
}
type configurationTuple struct {
Peers []Peer
Err chan error
}
// 设置配置
// 1. 服务启动时,先设置配置
// 2. 集群变更时,设置配置
func (s *Server) SetConfiguration(peers ...Peer) error {
// 节点刚启动
if !s.running.Get() {
s.config.directSet(makePeerMap(peers...))
return nil
}
err := make(chan error)
// 节点已经启动了
s.configurationChan <- configurationTuple{peers, err}
return <-err
}
// Start triggers the server to begin communicating with its peers.
func (s *Server) Start() {
go s.loop()
}
// Stop terminates the server. Stopped servers should not be restarted.
func (s *Server) Stop() {
q := make(chan struct{})
s.quit <- q
<-q
s.logGeneric("server stopped")
}
// 命令元组
type commandTuple struct {
// 命令内容
Command []byte
// 命令信道
CommandResponse chan<- []byte
Err chan error
}
// 命令接口
func (s *Server) Command(cmd []byte, response chan<- []byte) error {
err := make(chan error)
s.commandChan <- commandTuple{cmd, response, err}
return <-err
}
// 日志追加
func (s *Server) appendEntries(ae appendEntries) appendEntriesResponse {
t := appendEntriesTuple{
Request: ae,
Response: make(chan appendEntriesResponse),
}
s.appendEntriesChan <- t
return <-t.Response
}
// 投票
func (s *Server) requestVote(rv requestVote) requestVoteResponse {
t := requestVoteTuple{
Request: rv,
Response: make(chan requestVoteResponse),
}
s.requestVoteChan <- t
return <-t.Response
}
// times out,
// new election
// | .-----.
// | | |
// v times out, | v receives votes from
// +----------+ starts election +-----------+ majority of servers +--------+
// | Follower |------------------>| Candidate |---------------------->| Leader |
// +----------+ +-----------+ +--------+
// ^ ^ | |
// | | discovers current leader | |
// | | or new term | |
// | '------------------------------' |
// | |
// | discovers server with higher term |
// '------------------------------------------------------------------'
//
//
func (s *Server) loop() {
s.running.Set(true)
for s.running.Get() {
switch state := s.state.Get(); state {
case follower:
s.followerSelect()
case candidate:
s.candidateSelect()
case leader:
s.leaderSelect()
default:
panic(fmt.Sprintf("unknown Server State '%s'", state))
}
}
}
func (s *Server) resetElectionTimeout() {
s.electionTick = time.NewTimer(electionTimeout()).C
}
func (s *Server) logGeneric(format string, args ...interface{}) {
prefix := fmt.Sprintf("id=%d term=%d state=%s: ", s.id, s.term, s.state.Get())
log.Printf(prefix+format, args...)
}
func (s *Server) logAppendEntriesResponse(req appendEntries, resp appendEntriesResponse, stepDown bool) {
s.logGeneric(
"got appendEntries, sz=%d leader=%d prevIndex/Term=%d/%d commitIndex=%d: responded with success=%v (reason='%s') stepDown=%v",
len(req.Entries),
req.LeaderID,
req.PrevLogIndex,
req.PrevLogTerm,
req.CommitIndex,
resp.Success,
resp.reason,
stepDown,
)
}
func (s *Server) logRequestVoteResponse(req requestVote, resp requestVoteResponse, stepDown bool) {
s.logGeneric(
"got RequestVote, candidate=%d: responded with granted=%v (reason='%s') stepDown=%v",
req.CandidateID,
resp.VoteGranted,
resp.reason,
stepDown,
)
}
func (s *Server) handleQuit(q chan struct{}) {
s.logGeneric("got quit signal")
s.running.Set(false)
close(q)
}
// 命令转发
// 如果当前节点不是Leader节点,并且已存在Leader节点,则其会以"代理“的角色,将命令转发至Leader节点
func (s *Server) forwardCommand(t commandTuple) {
switch s.leader {
case unknownLeader:
s.logGeneric("got command, but don't know leader")
t.Err <- errUnknownLeader
case s.id: // I am the leader
panic("impossible state in forwardCommand")
default:
leader, ok := s.config.get(s.leader)
if !ok {
panic("invalid state in peers")
}
s.logGeneric("got command, forwarding to leader (%d)", s.leader)
// We're blocking our {follower,candidate}Select function in the
// receive-command branch. If we continue to block while forwarding
// the command, the leader won't be able to get a response from us!
go func() { t.Err <- leader.callCommand(t.Command, t.CommandResponse) }()
}
}
// 配置变更
// 转发规则和命令转发一样
func (s *Server) forwardConfiguration(t configurationTuple) {
switch s.leader {
case unknownLeader:
s.logGeneric("got configuration, but don't know leader")
t.Err <- errUnknownLeader
case s.id: // I am the leader
panic("impossible state in forwardConfiguration")
default:
leader, ok := s.config.get(s.leader)
if !ok {
panic("invalid state in peers")
}
s.logGeneric("got configuration, forwarding to leader (%d)", s.leader)
go func() { t.Err <- leader.callSetConfiguration(t.Peers...) }()
}
}
// follower 节点逻辑
func (s *Server) followerSelect() {
for {
select {
case q := <-s.quit:
s.handleQuit(q)
return
// 命令转发
case t := <-s.commandChan:
s.forwardCommand(t)
// 集群变更转发
case t := <-s.configurationChan:
s.forwardConfiguration(t)
// Leader选举
case <-s.electionTick:
// 5.2 Leader election: "A follower increments its current term and
// transitions to candidate state."
if s.config == nil {
s.logGeneric("election timeout, but no configuration: ignoring")
s.resetElectionTimeout()
continue
}
s.logGeneric("election timeout, becoming candidate")
// 提高自己的任期号
s.term++
// 投票置为空
s.vote = noVote
// Leader
s.leader = unknownLeader
// 设置节点角色为"候选人"
s.state.Set(candidate)
// 重置选举时间,防止马上再次出发选举
s.resetElectionTimeout()
return
// 日志追加(除了客户端请求,leader的心跳也会出发这个行为)
case t := <-s.appendEntriesChan:
if s.leader == unknownLeader {
s.leader = t.Request.LeaderID
s.logGeneric("discovered Leader %d", s.leader)
}
// 处理日志最佳操作
resp, stepDown := s.handleAppendEntries(t.Request)
s.logAppendEntriesResponse(t.Request, resp, stepDown)
t.Response <- resp
// 如果节点已经脱离了当前的集群,需要跟新Leader地址
if stepDown {
// stepDown as a Follower means just to reset the leader
if s.leader != unknownLeader {
s.logGeneric("abandoning old leader=%d", s.leader)
}
s.logGeneric("following new leader=%d", t.Request.LeaderID)
s.leader = t.Request.LeaderID
}
// 选举
case t := <-s.requestVoteChan:
// 选举处理
resp, stepDown := s.handleRequestVote(t.Request)
s.logRequestVoteResponse(t.Request, resp, stepDown)
t.Response <- resp
// 如果落后于当前节点了,把当前的Leader修改为"unkownleader",等待讯据成功后,进行切换
if stepDown {
// stepDown as a Follower means just to reset the leader
if s.leader != unknownLeader {
s.logGeneric("abandoning old leader=%d", s.leader)
}
s.logGeneric("new leader unknown")
s.leader = unknownLeader
}
}
}
}
// 候选状态
func (s *Server) candidateSelect() {
if s.leader != unknownLeader {
panic("known leader when entering candidateSelect")
}
if s.vote != 0 {
panic("existing vote when entering candidateSelect")
}
// "[A server entering the candidate stage] issues requestVote RPCs in
// parallel to each of the other servers in the cluster. If the candidate
// receives no response for an RPC, it reissues the RPC repeatedly until a
// response arrives or the election concludes."
// 发起选举RPC
requestVoteResponses, canceler := s.config.allPeers().except(s.id).requestVotes(requestVote{
Term: s.term,
CandidateID: s.id,
LastLogIndex: s.log.lastIndex(),
LastLogTerm: s.log.lastTerm(),
})
defer canceler.Cancel()
// Set up vote tallies (plus, vote for myself)
votes := map[uint64]bool{s.id: true}
s.vote = s.id
s.logGeneric("term=%d election started (configuration state %s)", s.term, s.config.state)
// 如果已经达到了选举“共识”,则成功选举
if s.config.pass(votes) {
s.logGeneric("I immediately won the election")
s.leader = s.id
s.state.Set(leader)
s.vote = noVote
return
}
// "A candidate continues in this state until one of three things happens:
// (a) it wins the election, (b) another server establishes itself as
// leader, or (c) a period of time goes by with no winner."
for {
select {
case q := <-s.quit:
s.handleQuit(q)
return
// 命令转发
case t := <-s.commandChan:
s.forwardCommand(t)
// 配置更新转发,注意和Leader的不同
case t := <-s.configurationChan:
s.forwardConfiguration(t)
// 收到选举的应答
case t := <-requestVoteResponses:
s.logGeneric("got vote: id=%d term=%d granted=%v", t.id, t.response.Term, t.response.VoteGranted)
// "A candidate wins the election if it receives votes from a
// majority of servers in the full cluster for the same term."
// 本节点落后于其他几点
if t.response.Term > s.term {
s.logGeneric("got vote from future term (%d>%d); abandoning election", t.response.Term, s.term)
s.leader = unknownLeader
s.state.Set(follower)
s.vote = noVote
return // lose
}
// 收到了"落后"当前节点的应答,忽略掉它
if t.response.Term < s.term {
s.logGeneric("got vote from past term (%d<%d); ignoring", t.response.Term, s.term)
break
}
// 收到赞同票
if t.response.VoteGranted {
s.logGeneric("%d voted for me", t.id)
votes[t.id] = true
}
// "Once a candidate wins an election, it becomes leader."
// “共识”达成
if s.config.pass(votes) {
s.logGeneric("I won the election")
s.leader = s.id
s.state.Set(leader)
s.vote = noVote
return // win
}
// 收到日志追加(在这里,心跳也当做日志追加的方式发送)
case t := <-s.appendEntriesChan:
// "While waiting for votes, a candidate may receive an
// appendEntries RPC from another server claiming to be leader.
// If the leader's term (included in its RPC) is at least as
// large as the candidate's current term, then the candidate
// recognizes the leader as legitimate and steps down, meaning
// that it returns to follower state."
// 处理日志
resp, stepDown := s.handleAppendEntries(t.Request)
s.logAppendEntriesResponse(t.Request, resp, stepDown)
t.Response <- resp
// candidate节点落后于Leader节点
if stepDown {
s.logGeneric("after an appendEntries, stepping down to Follower (leader=%d)", t.Request.LeaderID)
s.leader = t.Request.LeaderID
s.state.Set(follower)
return // lose
}
// 虽然当前节点是candidate节点,但集群中此时可能存在多个candidate节点
case t := <-s.requestVoteChan:
// We can also be defeated by a more recent candidate
resp, stepDown := s.handleRequestVote(t.Request)
s.logRequestVoteResponse(t.Request, resp, stepDown)
t.Response <- resp
if stepDown {
// 当前candidate节点落后于集群中已存在的candidate节点,将自己的角色变为follower,
// 并且也会投赞同票
s.logGeneric("after a requestVote, stepping down to Follower (leader unknown)")
s.leader = unknownLeader
s.state.Set(follower)
return // lose
}
// 选举
case <-s.electionTick:
// "The third possible outcome is that a candidate neither wins nor
// loses the election: if many followers become candidates at the
// same time, votes could be split so that no candidate obtains a
// majority. When this happens, each candidate will start a new
// election by incrementing its term and initiating another round of
// requestVote RPCs."
s.logGeneric("election ended with no winner; incrementing term and trying again")
s.resetElectionTimeout()
s.term++
s.vote = noVote
return // draw
}
}
}
// Leader 保存的Follower节点的所有最新同步条目
type nextIndex struct {
sync.RWMutex
m map[uint64]uint64 // followerId: nextIndex
}
func newNextIndex(pm peerMap, defaultNextIndex uint64) *nextIndex {
ni := &nextIndex{
m: map[uint64]uint64{},
}
for id := range pm {
ni.m[id] = defaultNextIndex
}
return ni
}
// 找出已经同步Follower的最小日志
func (ni *nextIndex) bestIndex() uint64 {
ni.RLock()
defer ni.RUnlock()
if len(ni.m) <= 0 {
return 0
}
i := uint64(math.MaxUint64)
for _, nextIndex := range ni.m {
if nextIndex < i {
i = nextIndex
}
}
return i
}
// 返回节点(id)最新的同步日志
func (ni *nextIndex) prevLogIndex(id uint64) uint64 {
ni.RLock()
defer ni.RUnlock()
if _, ok := ni.m[id]; !ok {
panic(fmt.Sprintf("peer %d not found", id))
}
return ni.m[id]
}
// 自减节点(id)的最新同步日志,用于同步失败时的回滚
func (ni *nextIndex) decrement(id uint64, prev uint64) (uint64, error) {
ni.Lock()
defer ni.Unlock()
i, ok := ni.m[id]
if !ok {
panic(fmt.Sprintf("peer %d not found", id))
}
if i != prev {
return i, errOutOfSync
}
if i > 0 {
ni.m[id]--
}
return ni.m[id], nil
}
// 更新节点(id)的同步日志
func (ni *nextIndex) set(id, index, prev uint64) (uint64, error) {
ni.Lock()
defer ni.Unlock()
i, ok := ni.m[id]
if !ok {
panic(fmt.Sprintf("peer %d not found", id))
}
if i != prev {
return i, errOutOfSync
}
ni.m[id] = index
return index, nil
}
// 心跳、复制命令都会用到该函数,flush是同步的,如果对端节点不可达,则阻塞
func (s *Server) flush(peer Peer, ni *nextIndex) error {
peerID := peer.id()
// Leader的任期号
currentTerm := s.term
// 节点(peer)的最新同步索引
prevLogIndex := ni.prevLogIndex(peerID)
// 检索出peers节点落后于Leader几点的日志条目,然后进行同步
entries, prevLogTerm := s.log.entriesAfter(prevLogIndex)
// 获取Leader committed的最新索引
commitIndex := s.log.getCommitIndex()
s.logGeneric("flush to %d: term=%d leaderId=%d prevLogIndex/Term=%d/%d sz=%d commitIndex=%d", peerID, currentTerm, s.id, prevLogIndex, prevLogTerm, len(entries), commitIndex)
// 日志追加RPC
resp := peer.callAppendEntries(appendEntries{
Term: currentTerm,
LeaderID: s.id,
PrevLogIndex: prevLogIndex,
PrevLogTerm: prevLogTerm,
Entries: entries,
CommitIndex: commitIndex,
})
if resp.Term > currentTerm {
// 应答的节点比当前节点的任期号大,当前的Leader被罢免
s.logGeneric("flush to %d: responseTerm=%d > currentTerm=%d: deposed", peerID, resp.Term, currentTerm)
return errDeposed
}
if !resp.Success {
// 应答失败,可能是leader RPC等待超时,或者出现了网络错误(包括脑裂),回滚
newPrevLogIndex, err := ni.decrement(peerID, prevLogIndex)
if err != nil {
s.logGeneric("flush to %d: while decrementing prevLogIndex: %s", peerID, err)
return err
}
s.logGeneric("flush to %d: rejected; prevLogIndex(%d) becomes %d", peerID, peerID, newPrevLogIndex)
return errAppendEntriesRejected
}
if len(entries) > 0 {
// 复制成功,更新同步状态
newPrevLogIndex, err := ni.set(peer.id(), entries[len(entries)-1].Index, prevLogIndex)
if err != nil {
s.logGeneric("flush to %d: while moving prevLogIndex forward: %s", peerID, err)
return err
}
s.logGeneric("flush to %d: accepted; prevLogIndex(%d) becomes %d", peerID, peerID, newPrevLogIndex)
return nil
}
s.logGeneric("flush to %d: accepted; prevLogIndex(%d) remains %d", peerID, peerID, ni.prevLogIndex(peerID))
return nil
}
// Leader并发同步日志
func (s *Server) concurrentFlush(pm peerMap, ni *nextIndex, timeout time.Duration) (int, bool) {
type tuple struct {
id uint64
err error
}
responses := make(chan tuple, len(pm))
for _, peer := range pm {
go func(peer Peer) {
errChan := make(chan error, 1)
go func() { errChan <- s.flush(peer, ni) }()
go func() { time.Sleep(timeout); errChan <- errTimeout }()
responses <- tuple{peer.id(), <-errChan} // first responder wins
}(peer)
}
successes, stepDown := 0, false
for i := 0; i < cap(responses); i++ {
switch t := <-responses; t.err {
case nil:
s.logGeneric("concurrentFlush: peer %d: OK (prevLogIndex(%d)=%d)", t.id, t.id, ni.prevLogIndex(t.id))
successes++
case errDeposed:
// 当前的Leder节点落后于其他节点
s.logGeneric("concurrentFlush: peer %d: deposed!", t.id)
stepDown = true
default:
s.logGeneric("concurrentFlush: peer %d: %s (prevLogIndex(%d)=%d)", t.id, t.err, t.id, ni.prevLogIndex(t.id))
// nothing to do but log and continue
}
}
return successes, stepDown
}
// 作为Leader角色运行
func (s *Server) leaderSelect() {
if s.leader != s.id {
panic(fmt.Sprintf("leader (%d) not me (%d) when entering leaderSelect", s.leader, s.id))
}
if s.vote != 0 {
panic(fmt.Sprintf("vote (%d) not zero when entering leaderSelect", s.leader))
}
// 5.3 Log replication: "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."
//
// I changed this from lastIndex+1 to simply lastIndex. Every initial
// communication from leader to follower was being rejected and we were
// doing the decrement. This was just annoying, except if you manage to
// sneak in a command before the first heartbeat. Then, it will never get
// properly replicated (it seemed).
// Leader为每个Follower保存了最新的同步日志索引
ni := newNextIndex(s.config.allPeers().except(s.id), s.log.lastIndex()) // +1)
flush := make(chan struct{})
heartbeat := time.NewTicker(broadcastInterval())
defer heartbeat.Stop()
go func() {
// 发送心跳,除了检测心跳外,还有防止Follower发送选举
for _ = range heartbeat.C {
flush <- struct{}{}
}
}()
for {
select {
case q := <-s.quit:
s.handleQuit(q)
return
// 收到命令
case t := <-s.commandChan:
// Append the command to our (leader) log
s.logGeneric("got command, appending")
currentTerm := s.term
entry := logEntry{
Index: s.log.lastIndex() + 1,
Term: currentTerm,
Command: t.Command,
commandResponse: t.CommandResponse,
}
// 追加日志
if err := s.log.appendEntry(entry); err != nil {
t.Err <- err
continue
}
s.logGeneric(
"after append, commitIndex=%d lastIndex=%d lastTerm=%d",
s.log.getCommitIndex(),
s.log.lastIndex(),
s.log.lastTerm(),
)
// Now that the entry is in the log, we can fall back to the
// normal flushing mechanism to attempt to replicate the entry
// and advance the commit index. We trigger a manual flush as a
// convenience, so our caller might get a response a bit sooner.
// 这里将日志同步放到了同步队列就返回给客户端了,正常来说,需要"共识"达成才返回给客户端
go func() { flush <- struct{}{} }()
t.Err <- nil
// 收到配置变更
case t := <-s.configurationChan:
// Attempt to change our local configuration
if err := s.config.changeTo(makePeerMap(t.Peers...)); err != nil {
t.Err <- err
continue
}
// Serialize the local (C_old,new) configuration
encodedConfiguration, err := s.config.encode()
if err != nil {
t.Err <- err
continue
}
// We're gonna write+replicate that config via log mechanisms.
// Prepare the on-commit callback.
entry := logEntry{
Index: s.log.lastIndex() + 1,
Term: s.term,
Command: encodedConfiguration,
isConfiguration: true,
committed: make(chan bool),
}
go func() {
// 当日志被commited时,committed将被回调
committed := <-entry.committed
if !committed {
s.config.changeAborted()
return
}
// 日志被committed了,说明其他节点都应用了最新的配置,所以当前的节点配置也需要更新
s.config.changeCommitted()
if _, ok := s.config.allPeers()[s.id]; !ok {
// 当前节点已被新集群剔除
s.logGeneric("leader expelled; shutting down")
q := make(chan struct{})
s.quit <- q
// 节点已退出
<-q
}
}()
// 日志追加
if err := s.log.appendEntry(entry); err != nil {
t.Err <- err
continue
}
case <-flush:
// 获取需要同步的节点
recipients := s.config.allPeers().except(s.id)
// Special case: network of 1
if len(recipients) <= 0 {
ourLastIndex := s.log.lastIndex()
if ourLastIndex > 0 {
if err := s.log.commitTo(ourLastIndex); err != nil {
s.logGeneric("commitTo(%d): %s", ourLastIndex, err)
continue
}
s.logGeneric("after commitTo(%d), commitIndex=%d", ourLastIndex, s.log.getCommitIndex())
}
continue
}
// Normal case: network of at-least-2
// 并发同步日志
successes, stepDown := s.concurrentFlush(recipients, ni, 2*broadcastInterval())
if stepDown {
// 节点已被卸任
s.logGeneric("deposed during flush")
s.state.Set(follower)
s.leader = unknownLeader
return
}
// Only when we know all followers accepted the flush can we
// consider incrementing commitIndex and pushing out another
// round of flushes.
if successes == len(recipients) {
// 最小被同步的Index
peersBestIndex := ni.bestIndex()
ourLastIndex := s.log.lastIndex()
ourCommitIndex := s.log.getCommitIndex()
if peersBestIndex > ourLastIndex {
// safety check: we've probably been deposed
s.logGeneric("peers' best index %d > our lastIndex %d", peersBestIndex, ourLastIndex)
s.logGeneric("this is crazy, I'm gonna become a follower")
s.leader = unknownLeader
s.vote = noVote
s.state.Set(follower)
return
}
if peersBestIndex > ourCommitIndex {
// committed Leader Index
if err := s.log.commitTo(peersBestIndex); err != nil {
s.logGeneric("commitTo(%d): %s", peersBestIndex, err)
// 比如某个Follower在同步Index时失败了,
continue // oh well, next time?
}
if s.log.getCommitIndex() > ourCommitIndex {
// 继续同步日志
s.logGeneric("after commitTo(%d), commitIndex=%d -- queueing another flush", peersBestIndex, s.log.getCommitIndex())
go func() { flush <- struct{}{} }()
}
}
}
// 追加日志, 正常来说,Leader节点是不会受到该命令的,出现这种的可能是集群存在一个新的Leader节点,这命令就是该Leader发送过来的
case t := <-s.appendEntriesChan:
resp, stepDown := s.handleAppendEntries(t.Request)
s.logAppendEntriesResponse(t.Request, resp, stepDown)
t.Response <- resp
if stepDown {
s.logGeneric("after an appendEntries, deposed to Follower (leader=%d)", t.Request.LeaderID)
s.leader = t.Request.LeaderID
s.state.Set(follower)
return // deposed
}
// 受到投票请求
case t := <-s.requestVoteChan:
resp, stepDown := s.handleRequestVote(t.Request)
s.logRequestVoteResponse(t.Request, resp, stepDown)
t.Response <- resp
if stepDown {
s.logGeneric("after a requestVote, deposed to Follower (leader unknown)")
s.leader = unknownLeader
s.state.Set(follower)
return // deposed
}
}
}
}
// handleRequestVote will modify s.term and s.vote, but nothing else.
// stepDown means you need to: s.leader=unknownLeader, s.state.Set(Follower).
// 处理投票
// 可能会修改s.term和s.vote 的值; stepDown意味着需要设置s.leader = unkownLeader, s.state.Set(Follower)
func (s *Server) handleRequestVote(rv requestVote) (requestVoteResponse, bool) {
// Spec is ambiguous here; basing this (loosely!) on benbjohnson's impl
// If the request is from an old term, reject
if rv.Term < s.term {
return requestVoteResponse{
Term: s.term,
VoteGranted: false,
reason: fmt.Sprintf("Term %d < %d", rv.Term, s.term),
}, false
}
// If the request is from a newer term, reset our state
stepDown := false
if rv.Term > s.term {
// 本地节点落后于集群的其他节点,需要更新一下自己的任期号
s.logGeneric("requestVote from newer term (%d): we defer", rv.Term)
s.term = rv.Term
s.vote = noVote
s.leader = unknownLeader
stepDown = true
}
// Special case: if we're the leader, and we haven't been deposed by a more
// recent term, then we should always deny the vote
if s.state.Get() == leader && !stepDown {
// 如果本地节点是Leader,并且又不落后于req 节点,则投反对票
return requestVoteResponse{
Term: s.term,
VoteGranted: false,
reason: "already the leader",
}, stepDown
}
// If we've already voted for someone else this term, reject
// 如果已经投过票,则投失败票
if s.vote != 0 && s.vote != rv.CandidateID {
if stepDown {
panic("impossible state in handleRequestVote")
}
return requestVoteResponse{
Term: s.term,
VoteGranted: false,
reason: fmt.Sprintf("already cast vote for %d", s.vote),
}, stepDown
}
// If the candidate log isn't at least as recent as ours, reject
if s.log.lastIndex() > rv.LastLogIndex || s.log.lastTerm() > rv.LastLogTerm {
return requestVoteResponse{
Term: s.term,
VoteGranted: false,
reason: fmt.Sprintf(
"our index/term %d/%d > %d/%d",
s.log.lastIndex(),
s.log.lastTerm(),
rv.LastLogIndex,
rv.LastLogTerm,
),
}, stepDown
}
// We passed all the tests: cast vote in favor
s.vote = rv.CandidateID
s.resetElectionTimeout()
return requestVoteResponse{
Term: s.term,
VoteGranted: true,
}, stepDown
}
// handleAppendEntries will modify s.term and s.vote, but nothing else.
// stepDown means you need to: s.leader=r.LeaderID, s.state.Set(Follower).
// 追加日志,需要注意的是,handleAppendEntries也会修改s.term和s.vote
// stepDown也会修改s.Leader, s,state
// 需要注意的是,本地节点的state不同时,其行为也是不用的
func (s *Server) handleAppendEntries(r appendEntries) (appendEntriesResponse, bool) {
// Spec is ambiguous here; basing this on benbjohnson's impl
// Maybe a nicer way to handle this is to define explicit handler functions
// for each Server state. Then, we won't try to hide too much logic (i.e.
// too many protocol rules) in one code path.
// If the request is from an old term, reject
if r.Term < s.term {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf("Term %d < %d", r.Term, s.term),
}, false
}
// If the request is from a newer term, reset our state
stepDown := false
if r.Term > s.term {
s.term = r.Term
s.vote = noVote
stepDown = true
}
// Special case for candidates: "While waiting for votes, a candidate may
// receive an appendEntries RPC from another server claiming to be leader.
// If the leader’s term (included in its RPC) is at least as large as the
// candidate’s current term, then the candidate recognizes the leader as
// legitimate and steps down, meaning that it returns to follower state."
if s.state.Get() == candidate && r.LeaderID != s.leader && r.Term >= s.term {
s.term = r.Term
s.vote = noVote
stepDown = true
}
// In any case, reset our election timeout
s.resetElectionTimeout()
// Reject if log doesn't contain a matching previous entry
// 如果{PreLogIndex, PreLogTerm} 不是最新的条目,则失败
// [{1, 2},{1, 3}, {1,4},{1,5},{1,6}] => {1,5} => [{1, 2},{1, 3}, {1,4},{1,5}]
if err := s.log.ensureLastIs(r.PrevLogIndex, r.PrevLogTerm); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"while ensuring last log entry had index=%d term=%d: error: %s",
r.PrevLogIndex,
r.PrevLogTerm,
err,
),
}, stepDown
}
// Process the entries
for i, entry := range r.Entries {
// Configuration changes requre special preprocessing
var pm peerMap
// 处理配置
if entry.isConfiguration {
commandBuf := bytes.NewBuffer(entry.Command)
if err := gob.NewDecoder(commandBuf).Decode(&pm); err != nil {
panic("gob decode of peers failed")
}
if s.state.Get() == leader {
// TODO should we instead just ignore this entry?
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed (configuration): %s",
i+1,
len(r.Entries),
"Leader shouldn't receive configurations via appendEntries",
),
}, stepDown
}
// Expulsion recognition
if _, ok := pm[s.id]; !ok {
entry.committed = make(chan bool)
go func() {
if <-entry.committed {
s.logGeneric("non-leader expelled; shutting down")
q := make(chan struct{})
s.quit <- q
<-q
}
}()
}
}
// Append entry to the log
if err := s.log.appendEntry(entry); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed: %s",
i+1,
len(r.Entries),
err,
),
}, stepDown
}
// "Once a given server adds the new configuration entry to its log, it
// uses that configuration for all future decisions (it does not wait
// for the entry to become committed)."
if entry.isConfiguration {
if err := s.config.directSet(pm); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf(
"AppendEntry %d/%d failed (configuration): %s",
i+1,
len(r.Entries),
err,
),
}, stepDown
}
}
}
// Commit up to the commit index.
//
// < ptrb> ongardie: if the new leader sends a 0-entry appendEntries
// with lastIndex=5 commitIndex=4, to a follower that has lastIndex=5
// commitIndex=5 -- in my impl, this fails, because commitIndex is too
// small. shouldn't be?
// <@ongardie> ptrb: i don't think that should fail
// <@ongardie> there are 4 ways an appendEntries request can fail: (1)
// network drops packet (2) caller has stale term (3) would leave gap in
// the recipient's log (4) term of entry preceding the new entries doesn't
// match the term at the same index on the recipient
//
// 出现这种情况的原因可能是本地节点运行到committed逻辑的时候出现了问题,或者说应答给Leader时,网络出现了问题等等。
// 这些情况都会造成数据不同步的情况,也就是本地节点的commiitted情况和Leader节点保存的Follower(本地节点)不一致
if r.CommitIndex > 0 && r.CommitIndex > s.log.getCommitIndex() {
if err := s.log.commitTo(r.CommitIndex); err != nil {
return appendEntriesResponse{
Term: s.term,
Success: false,
reason: fmt.Sprintf("CommitTo(%d) failed: %s", r.CommitIndex, err),
}, stepDown
}
}
// all good
return appendEntriesResponse{
Term: s.term,
Success: true,
}, stepDown
}
|