-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.go
954 lines (830 loc) · 23.9 KB
/
raft.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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
package yaft
import (
"bytes"
cr "crypto/rand"
"errors"
"fmt"
"log"
"math/rand"
"net"
"os"
"sync"
"time"
)
var (
keyLastVoteTerm = []byte("LastVoteTerm")
keyLastVoteCand = []byte("LastVoteCand")
keyCurrentTerm = []byte("CurrentTerm")
// ErrNotFound is used in persistence layer
ErrNotFound = errors.New("not found")
// ErrNotLeader is used when a
ErrNotLeader = fmt.Errorf("node is not the leader")
// ErrLeadershipLost ...
ErrLeadershipLost = fmt.Errorf("leadership lost while committing log")
// ErrRaftShutdown raft is already shutdown
ErrRaftShutdown = fmt.Errorf("raft is already shutdown")
// ErrEnqueueTimeout is timeout for log
ErrEnqueueTimeout = fmt.Errorf("timed out enqueuing operation")
)
type Raft struct {
raftState
// Configuration
conf *Config
// stable is a Store implementation for durable state
stable Store
// logs is a LogStore implementation to keep our logs
logs LogStore
// FSM is a finite state machine handler for logs
fsm FSM
shutdown bool
// shutdownCh is to signal the system wide shutdown
shutdownCh chan struct{}
// shutdownLock is mutex for shutdown operations
shutdownLock sync.Mutex
// Only one conf change may be pending (in the log, but not yet
// applied) at a time. This is enforced via pendingConfIndex, which
// is set to a value >= the log index of the latest pending
// configuration change (if any). Config changes are only allowed to
// be proposed if the leader's applied index is greater than this
// value.
pendingConfIndex uint64
// an estimate of the size of the uncommitted tail of the Raft log. Used to
// prevent unbounded log growth. Only maintained by the leader. Reset on
// term changes.
uncommittedSize uint64
// Transport layer. Most probably TCP.
trans Transport
// Log warnings
logW *log.Logger
// log errors
logE *log.Logger
// log debug + info
logD *log.Logger
// peers ...
peers []net.Addr
peerLock sync.Mutex
peerStore PeerStore
// commitCh is used to provide the newest commit index
// so that changes can be applied to the FSM. This is used
// so the main goroutine can use commitIndex without locking.
commitCh chan commitTuple
// applyCh is used to manage commands to be applied
applyCh chan *DeferLog
// rpcCh for transport layer
rpcCh <-chan RPC
// Stores our local addr
localAddr net.Addr
}
// commitTupel is used to send an index that was committed,
// with an optional associated deferLog that should be invoked
type commitTuple struct {
index uint64
deferLog *DeferLog
}
// NewRaft is used to construct a new Raft node
func NewRaft(conf *Config, store Store, logs LogStore, peerStore PeerStore, fsm FSM, trans Transport) (*Raft, error) {
// Try to restore the current term
currentTerm, err := store.GetUint64(keyCurrentTerm)
if err != nil && errors.Is(err, ErrNotFound) {
return nil, fmt.Errorf("failed to load current term: %w", err)
}
lastLog, err := logs.LastIndex()
if err != nil {
return nil, fmt.Errorf("failed to find last log: %w", err)
}
// Construct the list of peers that excludes us
localAddr := trans.LocalAddr()
peers, err := peerStore.Peers()
if err != nil {
return nil, fmt.Errorf("peer store failed: %w", err)
}
peers = excludePeer(peers, localAddr)
r := &Raft{
conf: conf,
stable: store,
logs: logs,
fsm: fsm,
shutdownCh: make(chan struct{}),
logE: log.New(os.Stdout, "[ERROR]", log.LstdFlags|log.Lshortfile),
logW: log.New(os.Stdout, "[WARN]", log.LstdFlags|log.Lshortfile),
logD: log.New(os.Stdout, "[DEBUG]", log.LstdFlags|log.Lshortfile),
commitCh: make(chan commitTuple, 128),
applyCh: make(chan *DeferLog),
rpcCh: trans.Consume(),
peers: peers,
peerStore: peerStore,
trans: trans,
localAddr: localAddr,
}
// Initialize as a follower
r.setState(Follower)
// Restore the current term and the last log
_ = r.setCurrentTerm(currentTerm)
r.setLastLogIndex(lastLog)
go r.run()
go r.runFSM()
return r, nil
}
func (r *Raft) String() string {
return fmt.Sprintf("Node %s", r.localAddr.String())
}
// run is a long running goroutine that runs the Raft FSM
func (r *Raft) run() {
for {
// Check if we are doing a shutdown
select {
case <-r.shutdownCh:
return
default:
}
// Enter into a sub-FSM
switch r.getState() {
case Follower:
r.runFollower()
case Candidate:
r.runCandidate()
case Leader:
r.runLeader()
}
}
}
// State is used to return the state of current node
func (r *Raft) State() RaftState {
return r.getState()
}
// Apply is used to apply a command to the FSM in a highly consistent
// manner. This returns a defer that can be used to wait on the application.
// An timeout should be provided to limit the amount of time we wait
// for the command to be started.
func (r *Raft) Apply(cmd []byte, timeout time.Duration) ApplyDefer {
if timeout <= 0 {
return &DeferError{err: fmt.Errorf("invalid timeout %v", timeout)}
}
var timer = time.After(timeout)
// Create a log deferLog, no index or term yet
deferLog := &DeferLog{
log: Log{
Type: LogCommand,
Data: cmd,
},
}
deferLog.init()
select {
case <-timer:
return &DeferError{err: ErrEnqueueTimeout}
case <-r.shutdownCh:
return &DeferError{err: ErrRaftShutdown}
case r.applyCh <- deferLog:
return deferLog
}
}
// AddPeer is used to add a new peer into the cluster. This must be
// run on the leader or it will fail.
func (r *Raft) AddPeer(peer net.Addr) ApplyDefer {
deferLog := &DeferLog{
log: Log{
Type: LogAddPeer,
Data: r.trans.EncodePeer(peer),
},
DeferError: DeferError{errCh: make(chan error, 1)},
}
select{
case r.applyCh <- deferLog:
return deferLog
case <-r.shutdownCh:
return &DeferError{err: ErrRaftShutdown}
}
}
// RemovePeer is used to remove a peer from the cluster. If the
// current leader is being removed, it will cause a new election
// to occur. This must be run on the leader or it will fail.
func (r *Raft) RemovePeer(peer net.Addr) ApplyDefer {
logFuture := &DeferLog{
log: Log{
Type: LogRemovePeer,
Data: r.trans.EncodePeer(peer),
},
DeferError: DeferError{errCh: make(chan error, 1)},
}
select {
case r.applyCh <- logFuture:
return logFuture
case <-r.shutdownCh:
return &DeferError{err: ErrRaftShutdown}
}
}
// appendEntries is invoked when we get an append entries RPC call
// Returns true if we transition to a Follower
func (r *Raft) appendEntries(rpc RPC, a *AppendEntriesRequest) (transition bool) {
// Setup a response
resp := &AppendEntriesResponse{
Term: r.getCurrentTerm(),
LastLog: r.getLastLogIndex(),
Success: false,
}
var err error
defer rpc.Respond(resp, err)
// Ignore an older term
if a.Term < r.getCurrentTerm() {
err = errors.New("obsolete term")
return
}
// Increase the term if we see a newer one, also transition to follower
// if we ever get an appendEntries call
if a.Term > r.getCurrentTerm() || r.getState() != Follower {
r.currentTerm = a.Term
resp.Term = a.Term
// Ensure transition to follower
transition = true
r.setState(Follower)
}
// Verify the last log entry
var prevLog Log
if a.PrevLogEntry > 0 {
if err := r.logs.GetLog(a.PrevLogEntry, &prevLog); err != nil {
r.logW.Printf("Failed to get previous log: %d %v",
a.PrevLogEntry, err)
return
}
if a.PrevLogTerm != prevLog.Term {
r.logW.Printf("Previous log term mis-match: ours: %d remote: %d",
prevLog.Term, a.PrevLogTerm)
return
}
}
// Add all the entries
for _, entry := range a.Entries {
// Delete any conflicting entries
if entry.Index <= r.getLastLogIndex() {
r.logW.Printf("Clearing log suffix from %d to %d",
entry.Index, r.getLastLogIndex())
if err := r.logs.DeleteRange(entry.Index, r.getLastLogIndex()); err != nil {
r.logE.Printf("Failed to clear log suffix: %w", err)
return
}
}
// Append the entry
if err := r.logs.StoreLog(entry); err != nil {
r.logE.Printf("Failed to append to log: %w", err)
return
}
// Update the lastLog
r.setLastLogIndex(entry.Index)
}
// Update the commit index
if a.LeaderCommitIndex > 0 && a.LeaderCommitIndex > r.getCommitIndex() {
idx := min(a.LeaderCommitIndex, r.getLastLogIndex())
r.setCommitIndex(idx)
// Trigger applying logs locally
r.commitCh <- commitTuple{idx, nil}
}
// Set success
resp.Success = true
return
}
// requestVote is called when node is in the follower state and
// get an request vote for candidate.
func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) (transition bool) {
r.peerLock.Lock()
defer r.peerLock.Unlock()
// Setup a response
peers := make([][]byte, 0, len(r.peers))
for _, p := range r.peers {
peers = append(peers, []byte(p.String()))
}
resp := &RequestVoteResponse{
Term: r.getCurrentTerm(),
Granted: false,
Peers: peers,
}
var err error
defer rpc.Respond(resp, err)
// Ignore an older term
if req.Term < r.getCurrentTerm() {
err = errors.New("obsolete term")
return
}
// Increase the term if we see a newer one
if req.Term > r.getCurrentTerm() {
if err := r.setCurrentTerm(req.Term); err != nil {
r.logE.Printf("Failed to update current term: %w", err)
return
}
resp.Term = req.Term
// Ensure transition to follower
transition = true
r.setState(Follower)
}
// Check if we have voted yet
lastVoteTerm, err := r.stable.GetUint64(keyLastVoteTerm)
if err != nil && err.Error() != "not found" {
r.logE.Printf("raft: Failed to get last vote term: %w", err)
return
}
lastVoteCandyBytes, err := r.stable.Get(keyLastVoteCand)
if err != nil && err.Error() != "not found" {
r.logE.Printf("raft: Failed to get last vote candidate: %w", err)
return
}
// Check if we've voted in this election before
if lastVoteTerm == req.Term && lastVoteCandyBytes != nil {
r.logW.Printf("raft: Duplicate RequestVote for same term: %d", req.Term)
if bytes.Compare(lastVoteCandyBytes, req.Candidate) == 0 {
r.logW.Printf("raft: Duplicate RequestVote from candidate: %s", req.Candidate)
resp.Granted = true
}
return
}
// Reject if their term is older
if r.getLastLogIndex() > 0 {
var lastLog Log
if err := r.logs.GetLog(r.getLastLogIndex(), &lastLog); err != nil {
r.logE.Printf("Failed to get last log: %d %v",
r.getLastLogIndex(), err)
return
}
if lastLog.Term > req.LastLogTerm {
r.logW.Printf("Rejecting vote since our last term is greater")
return
}
if lastLog.Index > req.LastLogIndex {
r.logW.Printf("Rejecting vote since our last index is greater")
return
}
}
// Persist a vote for safety
if err := r.persistVote(req.Term, req.Candidate); err != nil {
r.logE.Printf("raft: Failed to persist vote: %w", err)
return
}
resp.Granted = true
return
}
// electSelf is used to send a RequestVote RPC to all peers,
// and vote for itself. This has the side affecting of incrementing
// the current term. The response channel returned is used to wait
// for all the responses (including a vote for ourself).
func (r *Raft) electSelf() <-chan *RequestVoteResponse {
r.peerLock.Lock()
defer r.peerLock.Unlock()
// Create a response channel
respCh := make(chan *RequestVoteResponse, len(r.peers)+1)
// Get the last log
var lastLog Log
if r.getLastLogIndex() > 0 {
if err := r.logs.GetLog(r.lastLogIndex, &lastLog); err != nil {
r.logE.Printf("Failed to get last log: %d %v",
r.getLastLogIndex(), err)
return nil
}
}
// Increment the term
if err := r.setCurrentTerm(r.getCurrentTerm() + 1); err != nil {
r.logE.Printf("Failed to update current term: %w", err)
return nil
}
// Construct the request
req := &RequestVoteRequest{
Term: r.getCurrentTerm(),
Candidate: []byte(r.localAddr.String()),
LastLogIndex: lastLog.Index,
LastLogTerm: lastLog.Term,
}
// request peer for a vote
reqPeer := func(peer net.Addr) {
resp := &RequestVoteResponse{
Granted: false,
}
err := r.trans.RequestVote(peer, req, resp)
if err != nil {
r.logE.Printf("Failed to make RequestVote RPC to %v: %v",
peer, err)
resp.Term = req.Term
resp.Granted = false
}
respCh <- resp
}
// For each peer, request a vote
for _, peer := range r.peers {
go reqPeer(peer)
}
// Persist a vote for ourselves
if err := r.persistVote(req.Term, req.Candidate); err != nil {
r.logE.Printf("Failed to persist vote : %w", err)
return nil
}
// Include our own vote
respCh <- &RequestVoteResponse{Term: req.Term, Granted: true}
return respCh
}
// runFollower runs the FSM for a follower
func (r *Raft) runFollower() {
for {
select {
case rpc := <-r.rpcCh:
// Handle the command
switch cmd := rpc.Command.(type) {
case *AppendEntriesRequest:
r.appendEntries(rpc, cmd)
case *RequestVoteRequest:
r.requestVote(rpc, cmd)
default:
r.logE.Printf("In follower state, got unexpected command: %#v", rpc.Command)
rpc.Respond(nil, fmt.Errorf("unexpected command"))
}
case a := <-r.applyCh:
// Reject any operations since we are not the leader
a.response = ErrNotLeader
a.Response()
case <-randomTimeout(r.conf.HeartbeatTimeout, r.conf.ElectionTimeout):
// Heartbeat failed! Go to the candidate state
r.logW.Printf("Heartbeat timeout, start election process")
r.setState(Candidate)
return
case <-r.shutdownCh:
return
}
}
}
// runCandidate runs the FSM for a candidate
func (r *Raft) runCandidate() {
// Start vote for us, and set a timeout
voteCh := r.electSelf()
electionTimeout := randomTimeout(r.conf.ElectionTimeout, 2*r.conf.ElectionTimeout)
// Tally the votes, need a simple majority
grantedVotes := 0
quorum := r.quorumSize()
r.logD.Printf("Cluster size: %d, votes needed: %d", len(r.peers)+1, quorum)
transition := false
for !transition {
select {
case rpc := <-r.rpcCh:
switch cmd := rpc.Command.(type) {
case *AppendEntriesRequest:
transition = r.appendEntries(rpc, cmd)
case *RequestVoteRequest:
transition = r.requestVote(rpc, cmd)
default:
r.logE.Printf("Candidate state, got unexpected command: %#v",
rpc.Command)
rpc.Respond(nil, fmt.Errorf("unexpected command"))
}
// Got response from peers on voting request
case vote := <-voteCh:
// Check if the term is greater than ours, bail
if vote.Term > r.getCurrentTerm() {
r.logD.Printf("Newer term discovered")
r.setState(Follower)
if err := r.setCurrentTerm(vote.Term); err != nil {
r.logE.Printf("Failed to update current term: %w", err)
}
return
}
// Check if the vote is granted
if vote.Granted {
grantedVotes++
r.logD.Printf("Vote granted. Tally: %d", grantedVotes)
}
// Check if we've become the leader
if grantedVotes >= quorum {
r.logD.Printf("Election won. Tally: %d", grantedVotes)
r.setState(Leader)
return
}
case a := <-r.applyCh:
// Reject any operations since we are not the leader
a.response = ErrNotLeader
a.Response()
case <-electionTimeout:
// Election failed! Restart the election. We simply return,
// which will kick us back into runCandidate
r.logW.Printf("Election timeout reached, restarting election")
return
case <-r.shutdownCh:
return
}
}
}
type leaderState struct {
commitCh chan *DeferLog
inflight *inflight
replicationState map[string]*followerReplication
}
func (l *leaderState) Release() {
// Stop replication
for _, p := range l.replicationState {
close(p.stopCh)
}
// Cancel inflight requests
l.inflight.Cancel(ErrLeadershipLost)
}
// runLeader runs the FSM for a leader
func (r *Raft) runLeader() {
state := leaderState{
commitCh: make(chan *DeferLog, 128),
replicationState: make(map[string]*followerReplication),
}
defer state.Release()
// Initialize inflight tracker
state.inflight = NewInflight(state.commitCh)
r.peerLock.Lock()
// Start a replication routine for each peer
for _, peer := range r.peers {
r.startReplication(&state, peer)
}
r.peerLock.Unlock()
// seal leadership
go r.leaderNoop()
transition := false
for !transition {
select {
case applyLog := <-r.applyCh:
// Prepare log
applyLog.log.Index = r.getLastLogIndex() + 1
applyLog.log.Term = r.getCurrentTerm()
// Write the log entry locally
if err := r.logs.StoreLog(&applyLog.log); err != nil {
r.logE.Printf("Failed to commit log: %w", err)
applyLog.response = err
applyLog.Response()
r.setState(Follower)
return
}
// Add this to the inflight logs
state.inflight.Start(applyLog, r.quorumSize())
state.inflight.Commit(applyLog.log.Index)
// Update the last log since it's on disk now
r.setLastLogIndex(applyLog.log.Index)
// Notify the replicators of the new log
for _, f := range state.replicationState {
asyncNotifyCh(f.triggerCh)
}
case commitLog := <-state.commitCh:
// Increment the commit index
idx := commitLog.log.Index
r.setCommitIndex(idx)
// Perform leader-specific processing
transition = r.leaderProcessLog(&state, &commitLog.log)
// Trigger applying logs locally
r.commitCh <- commitTuple{idx, commitLog}
case rpc := <-r.rpcCh:
switch cmd := rpc.Command.(type) {
case *AppendEntriesRequest:
transition = r.appendEntries(rpc, cmd)
case *RequestVoteRequest:
transition = r.requestVote(rpc, cmd)
default:
r.logE.Printf("Leader state, got unexpected command: %#v",
rpc.Command)
rpc.Respond(nil, fmt.Errorf("unexpected command"))
}
case <-r.shutdownCh:
return
}
}
}
// startReplication is a helper to setup state and start async replication to a peer
func (r *Raft) startReplication(state *leaderState, peer net.Addr) {
s := &followerReplication{
peer: peer,
inflight: state.inflight,
stopCh: make(chan struct{}),
triggerCh: make(chan struct{}, 1),
matchIndex: r.getLastLogIndex(),
nextIndex: r.getLastLogIndex() + 1,
}
state.replicationState[peer.String()] = s
go r.replicate(s)
}
// leaderProcessLog is used for leader-specific log handling before we
// hand off to the generic runPostCommit handler. Returns true if there
// should be a state transition.
func (r *Raft) leaderProcessLog(s *leaderState, l *Log) bool {
// Only handle LogAddPeer and LogRemove Peer
if l.Type != LogAddPeer && l.Type != LogRemovePeer {
return false
}
// Process the log immediately to update the peer list
r.processLog(l)
// Decode the peer
peer := r.trans.DecodePeer(l.Data)
isSelf := peer.String() == r.localAddr.String()
// Get the replication state
repl, ok := s.replicationState[peer.String()]
// Start replication for new nodes
if l.Type == LogAddPeer && !ok && !isSelf {
r.startReplication(s, peer)
}
// Stop replication for old nodes
if l.Type == LogRemovePeer && ok {
close(repl.stopCh)
delete(s.replicationState, peer.String())
}
// Step down if we are being removed
if l.Type == LogRemovePeer && isSelf {
r.logD.Printf("Removed ourself, stepping down as leader")
return true
}
return false
}
// leaderNoop is a blocking command that appends a no-op log
// entry. It is used to seal leadership.
func (r *Raft) leaderNoop() {
logFuture := &DeferLog{
log: Log{
Type: LogNoop,
},
}
select {
case r.applyCh <- logFuture:
case <-r.shutdownCh:
}
}
// runFSM is a long running goroutine responsible for the management
// of the local FSM.
func (r *Raft) runFSM() {
for {
select {
case commitTuple := <-r.commitCh:
// obsolete logs
if commitTuple.index <= r.getLastApplied() {
r.logW.Printf("Skipping application of old log: %d",
commitTuple.index)
continue
}
// Apply all the preceding logs
for idx := r.getLastApplied() + 1; idx <= commitTuple.index; idx++ {
// Get the log, either from the future or from our log store
var l *Log
if commitTuple.deferLog != nil && commitTuple.deferLog.log.Index == idx {
l = &commitTuple.deferLog.log
} else {
l = new(Log)
if err := r.logs.GetLog(idx, l); err != nil {
r.logE.Printf("Failed to get log at %d: %v", idx, err)
panic(err)
}
}
r.processLog(l)
}
r.setLastApplied(commitTuple.index)
// Invoke the future if given
if commitTuple.deferLog != nil {
if commitTuple.deferLog.errCh == nil || cap(commitTuple.deferLog.errCh) == 0 {
commitTuple.deferLog.init()
}
commitTuple.deferLog.errCh <- nil
commitTuple.deferLog.Response()
}
case <-r.shutdownCh:
return
}
}
}
// processLog is invoked to process the application of a single committed log
func (r *Raft) processLog(l *Log) {
switch l.Type {
case LogCommand:
r.fsm.Apply(l.Data)
case LogAddPeer:
peer := r.trans.DecodePeer(l.Data)
// Avoid adding ourself as a peer
r.peerLock.Lock()
if peer.String() != r.localAddr.String() {
r.peers = addUniquePeer(r.peers, peer)
}
_ = r.peerStore.SetPeers(append([]net.Addr{r.localAddr}, r.peers...))
r.peerLock.Unlock()
case LogRemovePeer:
peer := r.trans.DecodePeer(l.Data)
// Removing ourself acts like removing all other peers
r.peerLock.Lock()
if peer.String() == r.localAddr.String() {
r.peers = nil
if r.conf.ShutdownOnRemove {
r.logD.Printf("Removed ourself, shutting down")
r.Shutdown()
} else {
r.setState(Follower)
}
} else {
r.peers = excludePeer(r.peers, peer)
}
_ = r.peerStore.SetPeers(append([]net.Addr{r.localAddr}, r.peers...))
r.peerLock.Unlock()
case LogNoop:
// Ignore the no-op
default:
r.logE.Printf("Got unrecognized log type: %#v", l)
}
}
// Shutdown is used to stop the Raft background routines.
// This is not a graceful operation.
func (r *Raft) Shutdown() {
r.shutdownLock.Lock()
defer r.shutdownLock.Unlock()
if !r.shutdown {
close(r.shutdownCh)
r.shutdown = true
r.setState(Shutdown)
}
}
// randomTimeout returns a value that is between the minVal and maxVal
func randomTimeout(minVal, maxVal time.Duration) <-chan time.Time {
extra := time.Duration(rand.Int()) % maxVal
return time.After((minVal + extra) % maxVal)
}
// min returns the minimum.
func min(a, b uint64) uint64 {
if a <= b {
return a
}
return b
}
func max(a, b uint64) uint64 {
if a >= b {
return a
}
return b
}
// persistVote is used to persist our vote for safety
func (r *Raft) persistVote(term uint64, candidate []byte) error {
if err := r.stable.SetUint64(keyLastVoteTerm, term); err != nil {
return err
}
if err := r.stable.Set(keyLastVoteCand, candidate); err != nil {
return err
}
return nil
}
// setCurrentTerm is used to set the current term in a durable manner
func (r *Raft) setCurrentTerm(t uint64) error {
// Make persistence
if err := r.stable.SetUint64(keyCurrentTerm, t); err != nil {
r.logE.Printf("Failed to save current term: %w", err)
return err
}
r.raftState.setCurrentTerm(t)
return nil
}
func (r *Raft) quorumSize() int {
clusterSize := len(r.peers) + 1
votesNeeded := (clusterSize / 2) + 1
return votesNeeded
}
// generateUUID is used to generate a random UUID
func generateUUID() string {
buf := make([]byte, 16)
if _, err := cr.Read(buf); err != nil {
panic(fmt.Errorf("failed to read random bytes: %w", err))
}
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
buf[0:4],
buf[4:6],
buf[6:8],
buf[8:10],
buf[10:16])
}
// asyncNotify is used to do an async channel send to
// a list of channels. This will not block.
func asyncNotify(chans []chan struct{}) {
for _, ch := range chans {
asyncNotifyCh(ch)
}
}
// asyncNotifyCh is used to do an async channel send
// to a single channel without blocking.
func asyncNotifyCh(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}
// excludePeer is used to exclude a single peer from a list of peers
func excludePeer(peers []net.Addr, peer net.Addr) []net.Addr {
otherPeers := make([]net.Addr, 0, len(peers))
for _, p := range peers {
if p.String() != peer.String() {
otherPeers = append(otherPeers, p)
}
}
return otherPeers
}
// peerExists checks if a given peer is contained in a list
func peerExists(peers []net.Addr, peer net.Addr) bool {
for _, p := range peers {
if p.String() == peer.String() {
return true
}
}
return false
}
// addUniquePeer is used to add a peer to a list of existing
// peers only if it is not already contained
func addUniquePeer(peers []net.Addr, peer net.Addr) []net.Addr {
if peerExists(peers, peer) {
return peers
} else {
return append(peers, peer)
}
}