-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraft_engine.go
1134 lines (964 loc) · 38.7 KB
/
raft_engine.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
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
package raft
import (
"context"
"fmt"
"github.com/ccassar/raft/internal/raft_pb"
"github.com/pkg/errors"
bolt "go.etcd.io/bbolt"
"go.uber.org/atomic"
"math/rand"
"sort"
"strings"
"sync"
"time"
)
// NodeState: describes the state of the raft node.
type nodeState int
func (state nodeState) String() string {
switch state {
case follower:
return "follower"
case candidate:
return "candidate"
case leader:
return "leader"
default:
return "uninit"
}
}
func (state nodeState) Code() int {
return int(state)
}
const (
uninit nodeState = iota
// Node is follower according to Raft spec. This is the initial state of the node coming up.
follower
// Node is candidate according to Raft spec, during election phase.
candidate
// Node is leader in current CurrentTerm according to Raft spec.
leader
)
// stateFn variation of state machine; or a variation at least, as described by [email protected] here:
// https://talks.golang.org/2011/lex.slide
// Each state is represented as a function which has access to an input channel which brings in
// external events; namely timers or messages.
type stateFn func(context.Context) stateFn
// raftEngine is the object at the heart of the raft state machine, the spider at the centre of the web.
// Progression through the raft state machine happens from the raftEngine.run() goroutine. The messaging side
// goroutines are not intelligent and simply relieve the raftEngine from the mundane (and, more importantly,
// blocking interactions with other cluster nodes).
type raftEngine struct {
node *Node
state nodeState
// Persisted state on all nodes. We do not embed the protobuf so as to support atomic in-memory access from
// client and raftEngine goroutines.
votedFor atomic.Int32
currentTerm atomic.Int64
// Persisted log
logDB *bolt.DB
// Volatile state on all nodes.
commitIndex *atomic.Int64
lastApplied *atomic.Int64
// Per state content
leaderState *raftEngineLeader
candidateState *raftEngineCandidate
//
// Channels... same channels are used irrespective of state. It is the state function which decides
// what to do with the content in the channel. These channels allow multiple endpoints receiving messages to bring
// those messages or responses to outbound messages to the raftEngine.
//
// There are in fact two groups of channels.
// The first set bring in external requests (inbound prefix). These requests are handled synchronously.
// The second set bring in the asynchronous responses from the gRPC client go routines.
// IMPORTANT NOTE: Pushing to the gRPC client side must always be done with a timeout to avoid livelock.
//
inboundAppendEntryChan chan *appendEntryContainer
inboundRequestVoteChan chan *requestVoteContainer
inboundLogCommandChan chan *logCommandContainer
inboundRequestTimeoutChan chan *requestTimeoutContainer
// The returns channel for AppendEntries is used to signal data arising from client side pertinent to the
// raftEngine side - e.g. discovery of new term, indication of newly acknowledged messages which then require
// committedIndex updates.
returnsAppendEntryResponsesFromClientChan chan *appendEntryResponsesFromClient
// The returns channels are used to return results asynchronously from the gRPC client goroutines.
returnsRequestVoteChan chan *requestVoteContainer
returnsLogCommandChan chan *logCommandContainer
returnsRequestTimeoutChan chan *requestTimeoutContainer
//
// Channel to handle log commands coming from app. These will be gRPCed to the leader which may be ourselves
// or another node in the cluster.
localLogCommandChan chan *logCommandContainer
//
// Who does the local node think is the current leader. This is set in the context of the raftEngine go routine
// but can be accessed from an application accessor.
currentLeader *atomic.Int32
// publisher handles publishing of log commands which have been committed to the local application managing
// its distributed state machine using the log commands.
publisher *raftLogPublisher
}
// raftEngineCandidate tracks state pertinent to when a node is in candidate state. When node transitions to
// candidate state it starts with a fresh copy of the candidate structure.
type raftEngineCandidate struct {
votesReceived map[int32]bool
}
type clientStateAtLeader struct {
nextIndex *atomic.Int64
// matchIndex is set on the client goroutine as it receives results to AppendEntry requests. matchIndex
// is read on the raftEngine side and is used to manage the committedIndex.
matchIndex *atomic.Int64
// termOfOrigin is immutable; set once at clientStateAtLeader creation time to reflect which term it is
// pertinent too. termOfOrigin is accessed both on the client and raftEngine goroutines.
termOfOrigin int64
// Keepalive time is accessed and updated on the raftEngine thread only.
keepaliveDue *time.Timer
// lastAppendEntrySent is set and read on the client side to dampen unnecessary keepalives. Also displayed
// in logKV, and so we protect it with lock.
lastAppendEntrySentMu sync.RWMutex
lastAppendEntrySent time.Time
}
func (cal *clientStateAtLeader) getLastAppendEntrySent() time.Time {
cal.lastAppendEntrySentMu.RLock()
t := cal.lastAppendEntrySent
cal.lastAppendEntrySentMu.RUnlock()
return t
}
func (cal *clientStateAtLeader) setLastAppendEntrySent(t time.Time) {
cal.lastAppendEntrySentMu.Lock()
cal.lastAppendEntrySent = time.Now()
cal.lastAppendEntrySentMu.Unlock()
}
func (l *raftEngineLeader) mustGetClientAtLeaderFromId(clientIndex int32) *clientStateAtLeader {
cal, ok := l.clients[clientIndex]
if !ok {
err := raftErrorf(RaftErrorOutOfBoundsClient, "failed to validate client is known to leader")
l.engine.node.logger.Errorw("client validation failed", append(l.engine.logKV(), raftErrKeyword, err)...)
l.engine.node.signalFatalError(err)
return nil
} // else signal shutdown with error
return cal
}
func (l *raftEngineLeader) resetKeepalive(clientIndex int32, d time.Duration) {
cal := l.mustGetClientAtLeaderFromId(clientIndex)
if cal != nil {
// With AfterFunc, it does not matter if Reset returns true or false. If reset raced with the expiration,
// both winning and losing the race is fine - if we lose the race we schedule the keepalive when we could
// have avoided it, but then we still block the keepalive on dispatch.
cal.keepaliveDue.Reset(d)
}
}
func (l *raftEngineLeader) notifyClientToProduceAppendEntries(ctx context.Context, clientIndex int32) {
client := l.engine.node.mustGetClientFromId(clientIndex)
cal := l.mustGetClientAtLeaderFromId(clientIndex)
if client != nil && cal != nil {
client.eventChan.postMessageWithFlush(
ctx, &appendEntryEvent{client: client, cal: cal})
l.resetKeepalive(client.index, l.engine.node.keepalivePeriod())
}
}
type orderedCandidates []int64
func (a orderedCandidates) Len() int { return len(a) }
func (a orderedCandidates) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a orderedCandidates) Less(i, j int) bool { return a[i] < a[j] }
func (l *raftEngineLeader) maintainCommittedIndexForLeader() {
oc := make(orderedCandidates, len(l.clients))
for i, cal := range l.clients {
oc[i] = cal.matchIndex.Load()
}
count := len(l.engine.node.config.Nodes)
nodeToCheck := (count / 2) + (count % 2)
sort.Sort(oc)
// committed index can be no better than the last acknowledged log entry of the middle remote node. We know
// more than the majority have acknowledged that value.
indexKnownToMajority := oc[nodeToCheck]
//
// Find term for index we are about to try and commit. If it is for this term, then we can go ahead and commit
// it, otherwise we must wait to commit until the first index in this term is committable. Section 5.4.2 of
// ISUCA.
//
le, err := l.engine.logGetEntry(indexKnownToMajority)
if err != nil || le == nil {
return // error will cause shutdown, le == nil means we, as leaders, do not have so we will definitely not commit.
}
if l.engine.currentTerm.Load() == le.Term &&
l.engine.updateCommittedIndexIfNecessary(indexKnownToMajority) {
//
// Wake up acker if necessary. Acker will be scheduled to run and validate whether any pending acks need to be
// issued to local terminated or remote applications.
l.acker.notify()
}
}
// raftEngineLeader tracks state pertinent to when a node is in leader state. When node transitions to
// leader state it starts with a fresh copy of the leader structure.
type raftEngineLeader struct {
engine *raftEngine
clients map[int32]*clientStateAtLeader
acker *raftLogAcknowledger
clientKeepaliveDue chan int32
}
const noLeader = -1
const notVotedThisTerm = -1
const indexNotSet = int64(0)
const termNotSet = int64(0)
type requestVoteContainer struct {
request *raft_pb.RequestVoteRequest
err error
reply *raft_pb.RequestVoteReply
returnChan chan *requestVoteContainer
}
type requestTimeoutContainer struct {
request *raft_pb.RequestTimeoutRequest
err error
reply *raft_pb.RequestTimeoutReply
returnChan chan *requestTimeoutContainer
}
type appendEntryContainer struct {
request *raft_pb.AppendEntryRequest
err error
reply *raft_pb.AppendEntryReply
returnChan chan *appendEntryContainer
}
type appendEntryResponsesFromClient struct {
request *raft_pb.AppendEntryRequest
err error
reply *raft_pb.AppendEntryReply
}
type logCommandContainer struct {
request *raft_pb.LogCommandRequest
err error
reply *raft_pb.LogCommandReply
returnChan chan *logCommandContainer
appCtx context.Context
}
func (msg *appendEntryResponsesFromClient) getIndexRangeForRequest() (int64, int64) {
firstInRange := int64(indexNotSet)
lastInRange := int64(indexNotSet)
leCount := len(msg.request.LogEntry)
if leCount > 0 {
leFirst := msg.request.LogEntry[0]
if leFirst != nil {
firstInRange = leFirst.Sequence
}
leLast := msg.request.LogEntry[leCount-1]
if leLast != nil {
lastInRange = leLast.Sequence
}
}
return firstInRange, lastInRange
}
func initRaftEngine(ctx context.Context, n *Node) error {
n.logger.Debugw("raftEngine, initialising", n.logKV()...)
re := &raftEngine{
node: n,
inboundAppendEntryChan: make(chan *appendEntryContainer, n.config.channelDepth.serverEvents),
inboundRequestVoteChan: make(chan *requestVoteContainer, n.config.channelDepth.serverEvents),
inboundLogCommandChan: make(chan *logCommandContainer, n.config.channelDepth.serverEvents),
inboundRequestTimeoutChan: make(chan *requestTimeoutContainer, n.config.channelDepth.serverEvents),
returnsAppendEntryResponsesFromClientChan: make(chan *appendEntryResponsesFromClient, n.config.channelDepth.serverEvents),
returnsRequestVoteChan: make(chan *requestVoteContainer, n.config.channelDepth.serverEvents),
returnsLogCommandChan: make(chan *logCommandContainer, n.config.channelDepth.serverEvents),
returnsRequestTimeoutChan: make(chan *requestTimeoutContainer, n.config.channelDepth.serverEvents),
localLogCommandChan: make(chan *logCommandContainer, 1),
commitIndex: atomic.NewInt64(0),
lastApplied: atomic.NewInt64(0),
currentLeader: atomic.NewInt32(int32(noLeader)),
state: uninit,
}
n.engine = re
err := re.initLogDB(ctx, n)
if err != nil {
return err
}
return nil
}
func (re *raftEngine) logKV() []interface{} {
return []interface{}{
"currentTerm", re.currentTerm.Load(),
"commitIndex", re.commitIndex.Load(),
"appliedIndex", re.lastApplied.Load(),
"state", re.state,
"VotedFor", re.votedFor.Load(),
"currentLeader", re.currentLeader.Load()}
}
func (re *raftEngine) updateState(state nodeState) {
oldState := re.state
re.state = state
if re.node.metrics != nil {
re.node.metrics.stateGauge.Set(float64(state.Code()))
}
re.node.logger.Infow(fmt.Sprintf("ROLE CHANGE: from %s to %s",
strings.ToUpper(oldState.String()),
strings.ToUpper(state.String())), re.logKV()...)
}
func (re *raftEngine) updateCurrentTerm(t int64) {
re.currentTerm.Store(t)
if re.node.metrics != nil {
re.node.metrics.term.Set(float64(t))
}
}
func (re *raftEngine) updateCurrentLeader(l int32) {
oldLeader := re.currentLeader.Load()
re.currentLeader.Store(l)
if l == oldLeader {
return
} else if l != noLeader && oldLeader != noLeader {
// We never expect a leader transition with one being noLeader. (We always reset to noLeader on new term).
err := raftErrorf(RaftErrorLeaderTransitionInTerm, "leader update from %d to %d, unexpected",
oldLeader, l)
re.node.logger.Errorw("update current leader failure", append(re.logKV(), raftErrKeyword, err)...)
re.node.signalFatalError(err)
return
}
if re.node.metrics != nil {
re.node.metrics.leader.Set(float64(l))
}
re.node.logger.Infow(fmt.Sprintf("LEADER CHANGE: from %d to %d", oldLeader, l),
re.logKV()...)
}
// raftEngine.run runs the core of the raft algorithm. We have an event loop which handles the state of the raft node,
// and which receives events; both timeouts and messages.
func (re *raftEngine) run(ctx context.Context, wg *sync.WaitGroup, n *Node) {
n.logger.Infow("raftEngine, start running", re.logKV()...)
defer wg.Done()
lp := &raftLogPublisher{
engine: re,
updatesAvailable: make(chan struct{}, 1),
logCmdChannel: re.node.config.LogCmds,
}
re.publisher = lp
wg.Add(1)
go lp.run(ctx, wg)
for s := re.followerStateFn(ctx); s != nil; {
s = s(ctx)
n.logger.Debugw(
fmt.Sprintf("ROLE CHANGE: leaving %s", re.state), re.logKV()...)
}
n.logger.Debugw("raftEngine, stop LogDB", re.logKV()...)
re.shutdownLogDB()
n.logger.Infow("raftEngine, stop running", re.logKV()...)
}
type termComparisonResult int
const (
sameTerm termComparisonResult = iota
newTerm
staleTerm
)
func (re *raftEngine) updateVotedFor(candidate int32) {
re.votedFor.Store(candidate)
_ = re.saveNodePersistedData()
}
// replaceTrainIfNewer will test current against received term. There are three possible outcomes;
// rxed term is newer, in which case current term is updated. Otherwise, received term could be older
// or the same as current term. In all cases, return value indicates identified case.
//
// This function could result in persisted data getting updated... (i.e. new CurrentTerm, clear VotedFor).
// If persisted data needs to be updated and for some reason or another fails, we will signal failure
// and come down.
func (re *raftEngine) replaceTermIfNewer(rxTerm int64) termComparisonResult {
currentTerm := re.currentTerm.Load()
switch {
case rxTerm > currentTerm:
// update currentTerm and votedFor - this will result in the persistent data being updated.
re.updateCurrentTerm(rxTerm)
re.updateVotedFor(notVotedThisTerm)
re.updateCurrentLeader(int32(noLeader))
re.node.logger.Debugw("raftEngine declaring new CurrentTerm", re.logKV()...)
return newTerm
case rxTerm < currentTerm:
return staleTerm
default:
return sameTerm
}
}
// candidateStateFn implements the behaviour of the node while in candidate state. While in candidate state
// we do not handle the localLogCommandChan.
func (re *raftEngine) candidateStateFn(ctx context.Context) stateFn {
defer func() {
// Purge candidate state on the way out of this state.
re.candidateState = nil
}()
re.updateState(candidate)
var ltTimer *time.Timer
ltTimerStop := func() {
if !ltTimer.Stop() {
<-ltTimer.C
}
}
for {
// Start with fresh state fro this iteration...
re.candidateState = &raftEngineCandidate{
votesReceived: map[int32]bool{},
}
lastLogTerm, lastLogIndex, err := re.logGetLastTermAndIndex()
if err != nil {
return nil // error logged and we will be shut down.
}
// Time to declare a new CurrentTerm...
re.replaceTermIfNewer(re.currentTerm.Load() + 1)
re.node.logger.Debugw("raftEngine candidate, declared new term", re.logKV()...)
// ship off request votes to clients... whatever clients were sending out in previous state, given the new CurrentTerm,
// can be flushed too. So, what we do here, is turn on flush, post flush-off message, and send our request vote.
for _, client := range re.node.messaging.clients {
client.eventChan.postMessageWithFlush(ctx, &requestVoteEvent{
client: client,
container: requestVoteContainer{
request: &raft_pb.RequestVoteRequest{
Term: re.currentTerm.Load(),
CandidateId: re.node.index,
LastLogIndex: lastLogIndex,
LastLogTerm: lastLogTerm,
To: client.index,
},
reply: nil,
err: nil,
returnChan: re.returnsRequestVoteChan,
},
})
}
// - oh and vote for myself
re.candidateState.votesReceived[re.node.index] = true
re.updateVotedFor(re.node.index)
leaderTimeout := randomiseDuration(re.node.config.timers.leaderTimeout)
if re.node.verboseLogging {
re.node.logger.Debugw("raftEngine candidate, extending leader timeout",
append(re.logKV(), "period", leaderTimeout.String())...)
}
if ltTimer == nil {
ltTimer = time.NewTimer(leaderTimeout)
} else {
ltTimer.Reset(leaderTimeout)
}
innerLoop:
for {
select {
case <-ltTimer.C:
re.node.logger.Debugw("raftEngine candidate, restart election cycle on timeout", re.logKV()...)
break innerLoop
case msg := <-re.inboundAppendEntryChan:
outcome := re.handleRxedAppendEntry(msg, true)
switch outcome {
case senderStale:
// continue here... waiting for outcome of election.
re.node.logger.Debugw(
"raftEngine candidate, ignoring AppendEntry request from stale remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.LeaderId)...)
default: // election completed... return to follower state.
ltTimerStop()
re.updateCurrentLeader(msg.request.LeaderId)
re.node.logger.Debugw(
"raftEngine candidate, AppendEntry request results in new leader for the term",
append(re.logKV(), "remoteNodeIndex", msg.request.LeaderId)...)
return re.followerStateFn
}
case msg := <-re.inboundRequestVoteChan:
if re.handleRxedRequestVote(msg) {
// Vote will only be granted if we moved to a new term (otherwise we'd have voted for ourselves
// already). In this case, we will simply move back down to follower.
ltTimerStop()
re.node.logger.Debugw(
"raftEngine candidate, cast vote in election",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
return re.followerStateFn
} else {
re.node.logger.Debugw(
"raftEngine candidate, did NOT vote on RequestVote from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
}
case msg := <-re.returnsAppendEntryResponsesFromClientChan:
// We should have not be getting AppendEntry replies at this point. We simply discard the replies.
re.node.logger.Debugw(
"raftEngine candidate, ignoring AppendEntry replies from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.To)...)
case msg := <-re.returnsRequestVoteChan:
if msg.err != nil {
// an error was signalled by the sender. This means we believe we failed to reach the specific
// client. We simply continue. We do not retry. If necessary, we will have another election at
// some point (i.e. when ltTimer expires if another node does not make it to leader).
re.node.logger.Debugw(
"raftEngine candidate, AppendEntry reply error from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.To, raftErrKeyword, msg.err)...)
continue
}
switch re.replaceTermIfNewer(msg.request.Term) {
case staleTerm:
re.node.logger.Debugw(
"raftEngine candidate, ignoring RequestVote reply from stale remote client",
append(re.logKV(), "remoteNodeIndex", msg.reply.VoterId)...)
case sameTerm:
case newTerm:
re.node.logger.Debugw(
"raftEngine candidate, received reply from client in a future term",
append(re.logKV(), "remoteNodeIndex", msg.reply.VoterId)...)
ltTimerStop()
return re.followerStateFn
}
// Figure out whether we were granted the vote, and in particular if we have a majority.
if msg.reply.VoteGranted && re.countVote(msg.reply.VoterId) {
ltTimerStop()
return re.leaderStateFn
}
case <-ctx.Done():
ltTimerStop()
re.node.logger.Debugw(
"raftEngine candidate, received shutdown", re.logKV()...)
return nil
}
}
}
}
// leaderStateFn describes the behaviour of the node while in leader state
func (re *raftEngine) leaderStateFn(ctx context.Context) stateFn {
re.updateCurrentLeader(re.node.index)
re.updateState(leader)
leaderCtx, cancel := context.WithCancel(ctx)
var ackerWg sync.WaitGroup
defer func(wg *sync.WaitGroup, cancel context.CancelFunc) {
// Purge leader state on the way out of this state.
cancel()
re.node.logger.Debug("raftEngine waiting for acker to shutdown on the way down from leader")
ackerWg.Wait()
re.leaderState = nil
}(&ackerWg, cancel)
//
// First thing we do when we become leaders is set up fresh state. This includes setting up the acker; independent
// goroutine which pushes acknowledgement to local or remote applications once their log command is committed.
// The lifetime of the acker is tied to this node being leader. When we stop being leader the defer function above
// stops the acker, and discards its state.
ackerWg.Add(1)
re.leaderState = &raftEngineLeader{
engine: re,
clients: map[int32]*clientStateAtLeader{},
clientKeepaliveDue: make(chan int32, re.node.config.channelDepth.serverEvents),
acker: createLogAcknowledgerAndRun(leaderCtx, &ackerWg, re.commitIndex, re.node.logger),
}
_, ourLastIndex, err := re.logGetLastTermAndIndex()
if err != nil { // logged and triggered shutdown in called function on catastrophic failure.
return nil
}
// Setup per-client state (e.g. keepalive ticker, indices etc.)
// Shutdown keepalive timers when we leave this state since we will no longer want to be sending
// keepalives when we leave leader state.
for i := int32(0); i < int32(len(re.node.config.Nodes)); i++ {
cal := &clientStateAtLeader{
termOfOrigin: re.currentTerm.Load(),
nextIndex: atomic.NewInt64(ourLastIndex + 1),
matchIndex: atomic.NewInt64(0),
}
// Initial expiration is quasi-instant. We want to send a Keepalive right away when we expire.
// Of course the function will be periodically rescheduled.
cal.keepaliveDue = time.AfterFunc(
time.Nanosecond,
keepaliveGenerator(leaderCtx, i, re.leaderState.clientKeepaliveDue))
re.leaderState.clients[i] = cal
}
for {
select {
case msg := <-re.inboundAppendEntryChan:
outcome := re.handleRxedAppendEntry(msg, false)
switch outcome {
case senderStale, rejected:
// continue here... waiting for outcome of election.
re.node.logger.Debugw(
"raftEngine leader, ignoring AppendEntry request from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.LeaderId)...)
default: // new term, fall in.
re.node.logger.Debugw(
"raftEngine leader, AppendEntry request results in new term, new leader",
append(re.logKV(), "remoteNodeIndex", msg.request.LeaderId)...)
return re.followerStateFn
}
case msg := <-re.inboundRequestVoteChan:
if re.handleRxedRequestVote(msg) {
// Vote will only be granted if we moved to a new term (otherwise we'd have voted for ourselves
// already). In the case we moved up a term, we will simply move back down to follower.
re.node.logger.Debugw(
"raftEngine leader, cast vote in election with new term",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
return re.followerStateFn
} else {
re.node.logger.Debugw(
"raftEngine leader, did NOT vote on RequestVote from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
}
case msg := <-re.inboundLogCommandChan:
re.node.logger.Debugw(
"raftEngine leader, LogCommand rxed from application (via remote node)",
append(re.logKV(), "origin", msg.request.Origin)...)
re.handleLogCommandContainer(ctx, msg)
case msg := <-re.localLogCommandChan:
re.node.logger.Debugw(
"raftEngine leader, LogCommand rxed from application (local)",
append(re.logKV(), "origin", msg.request.Origin)...)
re.handleLogCommandContainer(ctx, msg)
case clientIndex := <-re.leaderState.clientKeepaliveDue:
client, ok := re.node.messaging.clients[clientIndex]
if ok {
if re.node.verboseLogging {
re.node.logger.Debugw("raftEngine leader, send keepalive",
append(re.logKV(), "remoteNodeIndex", clientIndex)...)
}
client.eventChan.postMessageWithFlush(
ctx, &appendEntryEvent{client: client, cal: re.leaderState.clients[clientIndex]})
}
re.leaderState.resetKeepalive(clientIndex, re.node.keepalivePeriod())
case msg := <-re.returnsAppendEntryResponsesFromClientChan:
// Why are we here? The client go routine handles sending the AppendEntry requests to
// the client. It does forward up results when a) they result in an update to matchIndex which
// should cause us to reevaluate our committedIndex, b) on failure if retry is required...
// going through the raft engine ensures retries are only rescheduled while we remain
// leaders c) in the case it gets terms back which do not match our current term.
//
// Any retries required are scheduled to next keepalive.
if msg.err != nil {
switch errors.Cause(msg.err) {
case raftErrorMismatchedTerm:
switch re.replaceTermIfNewer(msg.reply.Term) {
case staleTerm:
re.node.logger.Debugw(
"raftEngine leader, ignoring AppendEntry reply from stale remote client, will retry on keepalive",
append(re.logKV(), "remoteNodeIndex", msg.request.To)...)
continue
case newTerm:
re.node.logger.Debugw(
"raftEngine leader, received AppendEntry reply from client in a future term",
append(re.logKV(), "remoteNodeIndex", msg.request.To)...)
return re.followerStateFn
}
default:
// In retry territory... simply wait for keepalive if we failed.
re.node.logger.Debugw(
"raftEngine leader, error AppendEntry reply for remote client, will retry on keepalive",
append(re.logKV(), "remoteNodeIndex", msg.request.To, raftErrKeyword, msg.err)...)
}
continue
}
// The only other reason we are poked is if we need to reevaluate our committedIndex (and consequently issue
// local or remote acks to application on the basis that client's matchIndex has been updated.)
re.leaderState.maintainCommittedIndexForLeader()
case msg := <-re.returnsRequestVoteChan:
if msg.err != nil {
// we are getting an error back, but we are leaders already so it does not matter. Presumably
// a timeout on a vote to a client to which we have lost connectivity.
re.node.logger.Debugw(
"raftEngine leader, ignoring RequestVote reply from remote client with error",
append(re.logKV(), "remoteNodeIndex", msg.request.To, raftErrKeyword, msg.err)...)
continue
}
switch re.replaceTermIfNewer(msg.request.Term) {
case staleTerm:
re.node.logger.Debugw(
"raftEngine leader, ignoring RequestVote reply from stale remote client",
append(re.logKV(), "remoteNodeIndex", msg.reply.VoterId)...)
case sameTerm:
// this is fine. We're still receiving vote but we are already leaders so it does not matter.
case newTerm:
re.node.logger.Debugw(
"raftEngine leader, received reply from client in a future term",
append(re.logKV(), "remoteNodeIndex", msg.reply.VoterId)...)
return re.followerStateFn
}
case <-ctx.Done():
re.node.logger.Debugw(
"raftEngine leader, received shutdown", re.logKV()...)
return nil
}
}
}
// followerStateFn describes the behaviour of the node while in follower state.
func (re *raftEngine) followerStateFn(ctx context.Context) stateFn {
re.updateState(follower)
// While in follower state the following can happen...
// - rx append entries from leader
// - timeout leader (implicitly or in request)
//
var ltTimer *time.Timer
ltTimerStop := func() {
if !ltTimer.Stop() {
<-ltTimer.C
}
}
for {
leaderTimeout := randomiseDuration(re.node.config.timers.leaderTimeout)
if re.node.verboseLogging {
re.node.logger.Debugw("raftEngine follower, extending leader timeout",
append(re.logKV(), "period", leaderTimeout.String())...)
}
if ltTimer == nil {
ltTimer = time.NewTimer(leaderTimeout)
} else {
ltTimer.Reset(leaderTimeout)
}
innerLoop:
for {
select {
case <-ltTimer.C:
// Time to switch to candidate mode right here...
ltTimer.Stop()
re.node.logger.Debugw("raftEngine follower, leader timeout", re.logKV()...)
return re.candidateStateFn
case msg := <-re.inboundAppendEntryChan:
outcome := re.handleRxedAppendEntry(msg, true)
switch outcome {
case senderStale:
re.node.logger.Debugw(
"raftEngine follower, ignoring AppendEntry request from stale remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.LeaderId)...)
case rejected, accepted:
// extend timeout for leader... we got some sensible message which is not stale.
ltTimerStop()
break innerLoop
}
case msg := <-re.inboundRequestVoteChan:
if re.handleRxedRequestVote(msg) {
// if we voted, we extend our timeout
ltTimerStop()
re.node.logger.Debugw(
"raftEngine follower, cast vote in election",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
break innerLoop
} else {
re.node.logger.Debugw(
"raftEngine follower, did NOT vote on RequestVote from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.CandidateId)...)
}
case msg := <-re.returnsAppendEntryResponsesFromClientChan:
re.node.logger.Debugw(
"raftEngine follower, ignoring AppendEntry replies from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.To)...)
case msg := <-re.returnsRequestVoteChan:
re.node.logger.Debugw(
"raftEngine follower, ignoring RequestVote replies from remote client",
append(re.logKV(), "remoteNodeIndex", msg.request.To)...)
case msg := <-re.localLogCommandChan:
leader := re.currentLeader.Load()
re.node.logger.Debugw(
"raftEngine follower, LogCommand rxed from application (local)",
append(re.logKV(), "origin", msg.request.Origin, "forwardTo", leader)...)
var pushed bool
clientCount := len(re.node.config.Nodes)
if leader < int32(clientCount) && leader >= 0 {
client := re.node.messaging.clients[leader]
logCmdEv := &logCmdEvent{client: client, container: msg}
pushed = client.eventChan.postMessage(ctx, logCmdEv)
if !pushed {
msg.err = raftErrorf(
RaftErrorLogCommandLocalDrop, "back pressure from gRPC client [%d]", leader)
re.node.logger.Debugw(
"raftEngine candidate, skipped sending logCmd to remote leader, backpressure",
append(re.logKV(), logCmdEv.logKV()...)...)
}
} else {
msg.err = raftErrorf(
RaftErrorLogCommandLocalDrop, "no gRPC client for leader index/client count [%d/%d]",
leader, clientCount)
re.node.logger.Debugw(
"raftEngine candidate, skipped sending logCmd to remote leader",
append(re.logKV(), "leader", leader)...)
}
if !pushed {
msg.returnChan <- msg
}
case <-ctx.Done():
ltTimerStop()
re.node.logger.Debugw(
"raftEngine follower, received shutdown", re.logKV()...)
return nil
}
}
}
}
// keepaliveGenerator is used as AfterFunc to schedule a keepalive for a given node.
func keepaliveGenerator(ctx context.Context, index int32, trigger chan int32) func() {
return func() {
select {
case trigger <- index:
case <-ctx.Done():
// All timers are cancelled when leader state function exists.
}
}
}
// rafteEngine.handleLogCommandContainer is expected to only be called when we are leader.
func (re *raftEngine) handleLogCommandContainer(ctx context.Context, msg *logCommandContainer) {
failed := func(msg *logCommandContainer, why string, err error) {
re.node.logger.Debugw(
"raftEngine leader, error handling log command from application",
append(re.logKV(), "remoteNodeIndex", msg.request.Origin)...)
msg.reply = &raft_pb.LogCommandReply{
Ack: false,
Reason: why}
msg.returnChan <- msg // dedicated response channel will not block.
}
// We have received a new inbound command. We need to start by appending it to the log.
// Then we need to notify the gRPC clients for the remote nodes. The event passed is what
// effectively pulls up the AppendEntries required and pushes.
_, index, err := re.logGetLastTermAndIndex()
if err != nil {
failed(msg, "leader failed fetching index from persisted log, "+
"leader resigning, please retry", err)
return
}
index++
// Now that we have an index, and know our current term, let's add it to the log.
err = re.logAddEntry(&raft_pb.LogEntry{
Term: re.currentTerm.Load(),
Sequence: index,
Data: msg.request.Command})
if err != nil {
failed(msg, "leader failure adding persisted log, "+
"leader resigning, please retry", err)
return
}
// Next we need to track the pending ack with the acker. When the index gets committed, the ack
// will be dispatched.
re.leaderState.acker.trackPendingAck(msg, index)
// We need to potentially notify each client that they may need to wake up to resync.
// To do that we send the notify event which embodies the pulling and syncing necessary.
for _, client := range re.node.messaging.clients {
re.leaderState.notifyClientToProduceAppendEntries(ctx, client.index)
}
}
func (re *raftEngine) handleRxedRequestVote(msg *requestVoteContainer) bool {
var grant bool
var err error
switch re.replaceTermIfNewer(msg.request.Term) {
case staleTerm:
// do not grant vote
case sameTerm, newTerm:
_, ourLastIndex, err := re.logGetLastTermAndIndex()
if err == nil {
if msg.request.LastLogIndex >= ourLastIndex {
if re.votedFor.Load() == msg.request.CandidateId {
// we have already granted this candidate our vote in this term. We regrant it.
grant = true
}
if re.votedFor.Load() == notVotedThisTerm {
re.updateVotedFor(msg.request.CandidateId)
grant = true
}
}
} // else we have logged error in called function already.
}
msg.reply = &raft_pb.RequestVoteReply{
Term: re.currentTerm.Load(),
VoterId: re.node.index,
VoteGranted: grant,
}
msg.err = err
msg.returnChan <- msg
return grant
}
type appendEntryRequestOutcome int
const (
senderStale appendEntryRequestOutcome = iota