forked from hashicorp/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft.go
1262 lines (1074 loc) · 32.1 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
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 (
"bytes"
"fmt"
"io"
"log"
"net"
"sync"
"time"
)
var (
keyCurrentTerm = []byte("CurrentTerm")
keyLastVoteTerm = []byte("LastVoteTerm")
keyLastVoteCand = []byte("LastVoteCand")
NotLeader = fmt.Errorf("node is not the leader")
LeadershipLost = fmt.Errorf("leadership lost while committing log")
RaftShutdown = fmt.Errorf("raft is already shutdown")
EnqueueTimeout = fmt.Errorf("timed out enqueuing operation")
KnownPeer = fmt.Errorf("peer already known")
UnknownPeer = fmt.Errorf("peer is unknown")
)
// commitTupel is used to send an index that was committed,
// with an optional associated future that should be invoked
type commitTuple struct {
log *Log
future *logFuture
}
// leaderState is state that is used while we are a leader
type leaderState struct {
commitCh chan *logFuture
inflight *inflight
replState map[string]*followerReplication
}
type Raft struct {
raftState
// applyCh is used to async send logs to the main thread to
// be committed and applied to the FSM.
applyCh chan *logFuture
// Configuration provided at Raft initialization
conf *Config
// FSM is the client state machine to apply commands to
fsm FSM
// fsmCommitCh is used to trigger async application of logs to the fsm
fsmCommitCh chan commitTuple
// fsmRestoreCh is used to trigger a restore from snapshot
fsmRestoreCh chan *restoreFuture
// fsmSnapshotCh is used to trigger a new snapshot being taken
fsmSnapshotCh chan *reqSnapshotFuture
// Leader is the current cluster leader
leader net.Addr
// leaderState used only while state is leader
leaderState leaderState
// Stores our local addr
localAddr net.Addr
// LogStore provides durable storage for logs
logs LogStore
// Track our known peers
peers []net.Addr
peerStore PeerStore
// RPC chan comes from the transport layer
rpcCh <-chan RPC
// Shutdown channel to exit, protected to prevent concurrent exits
shutdown bool
shutdownCh chan struct{}
shutdownLock sync.Mutex
// snapshots is used to store and retrieve snapshots
snapshots SnapshotStore
// snapshotCh is used for user triggered snapshots
snapshotCh chan *snapshotFuture
// stable is a StableStore implementation for durable state
// It provides stable storage for many fields in raftState
stable StableStore
// The transport layer we use
trans Transport
}
// NewRaft is used to construct a new Raft node
func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps SnapshotStore,
peerStore PeerStore, trans Transport) (*Raft, error) {
// Try to restore the current term
currentTerm, err := stable.GetUint64(keyCurrentTerm)
if err != nil && err.Error() != "not found" {
return nil, fmt.Errorf("Failed to load current term: %v", err)
}
// Read the last log value
lastIdx, err := logs.LastIndex()
if err != nil {
return nil, fmt.Errorf("Failed to find last log: %v", err)
}
// Get the log
var lastLog Log
if lastIdx > 0 {
if err := logs.GetLog(lastIdx, &lastLog); err != nil {
return nil, fmt.Errorf("Failed to get last log: %v", err)
}
}
// Construct the list of peers that excludes us
localAddr := trans.LocalAddr()
peers, err := peerStore.Peers()
if err != nil {
return nil, fmt.Errorf("Failed to get list of peers: %v", err)
}
peers = excludePeer(peers, localAddr)
// Create Raft struct
r := &Raft{
applyCh: make(chan *logFuture),
conf: conf,
fsm: fsm,
fsmCommitCh: make(chan commitTuple, 128),
fsmRestoreCh: make(chan *restoreFuture),
fsmSnapshotCh: make(chan *reqSnapshotFuture),
localAddr: localAddr,
logs: logs,
peers: peers,
peerStore: peerStore,
rpcCh: trans.Consumer(),
snapshots: snaps,
snapshotCh: make(chan *snapshotFuture),
shutdownCh: make(chan struct{}),
stable: stable,
trans: trans,
}
// Initialize as a follower
r.setState(Follower)
// Restore the current term and the last log
r.setCurrentTerm(currentTerm)
r.setLastLogIndex(lastLog.Index)
r.setLastLogTerm(lastLog.Term)
// Attempt to restore a snapshot if there are any
if err := r.restoreSnapshot(); err != nil {
return nil, err
}
// Start the background work
r.goFunc(r.run)
r.goFunc(r.runFSM)
r.goFunc(r.runSnapshots)
return r, nil
}
// Leader is used to return the current leader of the cluster,
// it may return nil if there is no current leader or the leader
// is unknown
func (r *Raft) Leader() net.Addr {
return r.leader
}
// Apply is used to apply a command to the FSM in a highly consistent
// manner. This returns a future that can be used to wait on the application.
// An optional timeout can be provided to limit the amount of time we wait
// for the command to be started. This must be run on the leader or it
// will fail.
func (r *Raft) Apply(cmd []byte, timeout time.Duration) ApplyFuture {
var timer <-chan time.Time
if timeout > 0 {
timer = time.After(timeout)
}
// Create a log future, no index or term yet
logFuture := &logFuture{
log: Log{
Type: LogCommand,
Data: cmd,
},
}
logFuture.init()
select {
case <-timer:
return errorFuture{EnqueueTimeout}
case <-r.shutdownCh:
return errorFuture{RaftShutdown}
case r.applyCh <- logFuture:
return logFuture
}
}
// 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) Future {
logFuture := &logFuture{
log: Log{
Type: LogAddPeer,
peer: peer,
},
}
logFuture.init()
select {
case r.applyCh <- logFuture:
return logFuture
case <-r.shutdownCh:
return errorFuture{RaftShutdown}
}
}
// 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) Future {
logFuture := &logFuture{
log: Log{
Type: LogRemovePeer,
peer: peer,
},
policy: newExcludeNodeQuorum(len(r.peers)+1, peer),
}
logFuture.init()
select {
case r.applyCh <- logFuture:
return logFuture
case <-r.shutdownCh:
return errorFuture{RaftShutdown}
}
}
// Shutdown is used to stop the Raft background routines.
// This is not a graceful operation. Provides a future that
// can be used to block until all background routines have exited.
func (r *Raft) Shutdown() Future {
r.shutdownLock.Lock()
defer r.shutdownLock.Unlock()
if !r.shutdown {
close(r.shutdownCh)
r.shutdown = true
r.setState(Shutdown)
}
return &shutdownFuture{r}
}
// Snapshot is used to manually force Raft to take a snapshot
// Returns a future that can be used to block until complete.
func (r *Raft) Snapshot() Future {
snapFuture := &snapshotFuture{}
snapFuture.init()
select {
case r.snapshotCh <- snapFuture:
return snapFuture
case <-r.shutdownCh:
return errorFuture{RaftShutdown}
}
}
// State is used to return the state raft is currently in
func (r *Raft) State() RaftState {
return r.getState()
}
func (r *Raft) String() string {
return fmt.Sprintf("Node at %s [%v]", r.localAddr.String(), r.getState())
}
// runFSM is a long running goroutine responsible for applying logs
// to the FSM. This is done async of other logs since we don't want
// the FSM to block our internal operations.
func (r *Raft) runFSM() {
var lastIndex, lastTerm uint64
for {
select {
case req := <-r.fsmRestoreCh:
// Open the snapshot
meta, source, err := r.snapshots.Open(req.ID)
if err != nil {
req.respond(fmt.Errorf("Failed to open snapshot %v: %v",
req.ID, err))
continue
}
// Attempt to restore
if err := r.fsm.Restore(source); err != nil {
req.respond(fmt.Errorf("Failed to restore snapshot %v: %v",
req.ID, err))
source.Close()
continue
}
source.Close()
// Update the last index and term
lastIndex = meta.Index
lastTerm = meta.Term
req.respond(nil)
case req := <-r.fsmSnapshotCh:
// Get our peers
peers, err := r.peerStore.Peers()
if err != nil {
req.respond(err)
}
// Start a snapshot
snap, err := r.fsm.Snapshot()
// Respond to the request
req.index = lastIndex
req.term = lastTerm
req.peers = peers
req.snapshot = snap
req.respond(err)
case commitTuple := <-r.fsmCommitCh:
// Apply the log
resp := r.fsm.Apply(commitTuple.log.Data)
// Update the indexes
lastIndex = commitTuple.log.Index
lastTerm = commitTuple.log.Term
// Invoke the future if given
if commitTuple.future != nil {
commitTuple.future.response = resp
commitTuple.future.respond(nil)
}
case <-r.shutdownCh:
return
}
}
}
// 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()
}
}
}
// runFollower runs the FSM for a follower
func (r *Raft) runFollower() {
log.Printf("[INFO] %v entering Follower state", r)
for {
select {
case rpc := <-r.rpcCh:
r.processRPC(rpc)
case a := <-r.applyCh:
// Reject any operations since we are not the leader
a.respond(NotLeader)
case <-randomTimeout(r.conf.HeartbeatTimeout):
// Heartbeat failed! Transition to the candidate state
log.Printf("[WARN] Heartbeat timeout reached, starting election")
r.leader = nil
r.setState(Candidate)
return
case <-r.shutdownCh:
return
}
}
}
// runCandidate runs the FSM for a candidate
func (r *Raft) runCandidate() {
log.Printf("[INFO] %v entering Candidate state", r)
// Start vote for us, and set a timeout
voteCh := r.electSelf()
electionTimer := randomTimeout(r.conf.ElectionTimeout)
// Tally the votes, need a simple majority
grantedVotes := 0
votesNeeded := ((len(r.peers) + 1) / 2) + 1
log.Printf("[DEBUG] Votes needed: %d", votesNeeded)
for r.getState() == Candidate {
select {
case rpc := <-r.rpcCh:
r.processRPC(rpc)
case vote := <-voteCh:
// Check if the term is greater than ours, bail
if vote.Term > r.getCurrentTerm() {
log.Printf("[DEBUG] Newer term discovered")
r.setState(Follower)
r.setCurrentTerm(vote.Term)
return
}
// Check if the vote is granted
if vote.Granted {
grantedVotes++
log.Printf("[DEBUG] Vote granted. Tally: %d", grantedVotes)
}
// Check if we've become the leader
if grantedVotes >= votesNeeded {
log.Printf("[INFO] Election won. Tally: %d", grantedVotes)
r.leader = r.localAddr
r.setState(Leader)
return
}
case a := <-r.applyCh:
// Reject any operations since we are not the leader
a.respond(NotLeader)
case <-electionTimer:
// Election failed! Restart the elction. We simply return,
// which will kick us back into runCandidate
log.Printf("[WARN] Election timeout reached, restarting election")
return
case <-r.shutdownCh:
return
}
}
}
// runLeader runs the FSM for a leader. Do the setup here and drop into
// the leaderLoop for the hot loop
func (r *Raft) runLeader() {
log.Printf("[INFO] %v entering Leader state", r)
// Setup leader state
r.leaderState.commitCh = make(chan *logFuture, 128)
r.leaderState.inflight = newInflight(r.leaderState.commitCh)
r.leaderState.replState = make(map[string]*followerReplication)
// Cleanup state on step down
defer func() {
// Stop replication
for _, p := range r.leaderState.replState {
close(p.stopCh)
}
// Cancel inflight requests
r.leaderState.inflight.Cancel(LeadershipLost)
// Clear all the state
r.leaderState.commitCh = nil
r.leaderState.inflight = nil
r.leaderState.replState = nil
}()
// Start a replication routine for each peer
for _, peer := range r.peers {
r.startReplication(peer)
}
// Dispatch a no-op log first
noop := &logFuture{log: Log{Type: LogNoop}}
r.dispatchLog(noop)
// Sit in the leader loop until we step down
r.leaderLoop()
}
// startReplication is a helper to setup state and start async replication to a peer
func (r *Raft) startReplication(peer net.Addr) {
lastIdx := r.getLastIndex()
s := &followerReplication{
peer: peer,
inflight: r.leaderState.inflight,
stopCh: make(chan uint64, 1),
triggerCh: make(chan struct{}, 1),
currentTerm: r.getCurrentTerm(),
matchIndex: 0,
nextIndex: lastIdx + 1,
}
r.leaderState.replState[peer.String()] = s
r.goFunc(func() { r.replicate(s) })
}
// leaderLoop is the hot loop for a leader, it is invoked
// after all the various leader setup is done
func (r *Raft) leaderLoop() {
for r.getState() == Leader {
select {
case rpc := <-r.rpcCh:
r.processRPC(rpc)
case commitLog := <-r.leaderState.commitCh:
// Increment the commit index
idx := commitLog.log.Index
r.setCommitIndex(idx)
r.processLogs(idx, commitLog)
case newLog := <-r.applyCh:
// Prepare peer set changes
if newLog.log.Type == LogAddPeer || newLog.log.Type == LogRemovePeer {
if !r.preparePeerChange(newLog) {
continue
}
}
r.dispatchLog(newLog)
case <-r.shutdownCh:
return
}
}
}
// preparePeerChange checks if a LogAddPeer or LogRemovePeer should be performed,
// and properly formats the data field on the log before dispatching it.
func (r *Raft) preparePeerChange(l *logFuture) bool {
// Check if this is a known peer
p := l.log.peer
knownPeer := peerContained(r.peers, p) || r.localAddr.String() == p.String()
// Ignore known peers on add
if l.log.Type == LogAddPeer && knownPeer {
l.respond(KnownPeer)
return false
}
// Ignore unknown peers on remove
if l.log.Type == LogRemovePeer && !knownPeer {
l.respond(UnknownPeer)
return false
}
// Construct the peer set
var peerSet []net.Addr
if l.log.Type == LogAddPeer {
peerSet = append([]net.Addr{p, r.localAddr}, r.peers...)
} else {
peerSet = excludePeer(append([]net.Addr{r.localAddr}, r.peers...), p)
}
// Setup the log
l.log.Data = encodePeers(peerSet, r.trans)
return true
}
// dispatchLog is called to push a log to disk, mark it
// as inflight and begin replication of it
func (r *Raft) dispatchLog(applyLog *logFuture) {
// Prepare log
applyLog.log.Index = r.getLastIndex() + 1
applyLog.log.Term = r.getCurrentTerm()
// Write the log entry locally
if err := r.logs.StoreLog(&applyLog.log); err != nil {
log.Printf("[ERR] Failed to commit log: %v", err)
applyLog.respond(err)
r.setState(Follower)
return
}
// Add a quorum policy if none
if applyLog.policy == nil {
applyLog.policy = newMajorityQuorum(len(r.peers) + 1)
}
// Add this to the inflight logs, commit
r.leaderState.inflight.Start(applyLog)
r.leaderState.inflight.Commit(applyLog.log.Index, r.localAddr)
// Update the last log since it's on disk now
r.setLastLogIndex(applyLog.log.Index)
r.setLastLogTerm(applyLog.log.Term)
// Notify the replicators of the new log
for _, f := range r.leaderState.replState {
asyncNotifyCh(f.triggerCh)
}
}
// processLogs is used to process all the logs from the lastApplied
// up to the given index
func (r *Raft) processLogs(index uint64, future *logFuture) {
// Reject logs we've applied already
lastApplied := r.getLastApplied()
if index <= lastApplied {
log.Printf("[WARN] Skipping application of old log: %d", index)
return
}
// Ensure the leader commits contiguous logs (sanity check)
if r.getState() == Leader && index != lastApplied+1 {
log.Printf("[ERR] %v : last %d curr: %d", r, r.getLastApplied(), index)
panic("leader cannot apply log without applying preceeding logs")
}
// Apply all the preceeding logs
for idx := r.getLastApplied() + 1; idx <= index; idx++ {
// Get the log, either from the future or from our log store
if future != nil && future.log.Index == idx {
r.processLog(&future.log, future)
} else {
l := new(Log)
if err := r.logs.GetLog(idx, l); err != nil {
log.Printf("[ERR] Failed to get log at %d: %v", idx, err)
panic(err)
}
r.processLog(l, nil)
}
// Update the lastApplied index and term
r.setLastApplied(idx)
}
}
// processLog is invoked to process the application of a single committed log
func (r *Raft) processLog(l *Log, future *logFuture) {
switch l.Type {
case LogCommand:
// Forward to the fsm handler
select {
case r.fsmCommitCh <- commitTuple{l, future}:
case <-r.shutdownCh:
if future != nil {
future.respond(RaftShutdown)
}
}
// Return so that the future is only responded to
// by the FSM handler when the application is done
return
case LogAddPeer:
peers := decodePeers(l.Data, r.trans)
log.Printf("[DEBUG] Node %v updated peer set (add): %v", r.localAddr, peers)
// Update our peer set
r.peers = excludePeer(peers, r.localAddr)
r.peerStore.SetPeers(peers)
// Handle replication if we are the leader
if r.getState() == Leader {
for _, p := range r.peers {
if _, ok := r.leaderState.replState[p.String()]; !ok {
log.Printf("[INFO] Added peer %v, starting replication", p)
r.startReplication(p)
}
}
}
case LogRemovePeer:
peers := decodePeers(l.Data, r.trans)
log.Printf("[DEBUG] Node %v updated peer set (remove): %v", r.localAddr, peers)
// If the peer set does not include us, remove all other peers
removeSelf := !peerContained(peers, r.localAddr)
if removeSelf {
r.peers = nil
r.peerStore.SetPeers([]net.Addr{r.localAddr})
} else {
r.peers = excludePeer(peers, r.localAddr)
r.peerStore.SetPeers(peers)
}
// Stop replication for old nodes
if r.getState() == Leader {
var toDelete []string
for _, repl := range r.leaderState.replState {
if !peerContained(r.peers, repl.peer) {
log.Printf("[INFO] Removed peer %v, stopping replication", repl.peer)
// Replicate up to this index and stop
repl.stopCh <- l.Index
toDelete = append(toDelete, repl.peer.String())
}
}
for _, name := range toDelete {
delete(r.leaderState.replState, name)
}
}
// Handle removing ourself
if removeSelf {
if r.conf.ShutdownOnRemove {
log.Printf("[INFO] Removed ourself, shutting down")
r.Shutdown()
} else {
log.Printf("[INFO] Removed ourself, transitioning to follower")
r.setState(Follower)
}
}
case LogNoop:
// Ignore the no-op
default:
log.Printf("[ERR] Got unrecognized log type: %#v", l)
}
// Invoke the future if given
if future != nil {
future.respond(nil)
}
}
// processRPC is called to handle an incoming RPC request
func (r *Raft) processRPC(rpc RPC) {
switch cmd := rpc.Command.(type) {
case *AppendEntriesRequest:
r.appendEntries(rpc, cmd)
case *RequestVoteRequest:
r.requestVote(rpc, cmd)
case *InstallSnapshotRequest:
r.installSnapshot(rpc, cmd)
default:
log.Printf("[ERR] Got unexpected command: %#v",
rpc.Command)
rpc.Respond(nil, fmt.Errorf("Unexpected command"))
}
}
// appendEntries is invoked when we get an append entries RPC call
func (r *Raft) appendEntries(rpc RPC, a *AppendEntriesRequest) {
// Setup a response
resp := &AppendEntriesResponse{
Term: r.getCurrentTerm(),
LastLog: r.getLastIndex(),
Success: false,
}
var rpcErr error
defer rpc.Respond(resp, rpcErr)
// Ignore an older term
if a.Term < r.getCurrentTerm() {
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 {
// Ensure transition to follower
r.setState(Follower)
r.setCurrentTerm(a.Term)
resp.Term = a.Term
}
// Save the current leader
r.leader = r.trans.DecodePeer(a.Leader)
// Verify the last log entry
if a.PrevLogEntry > 0 {
lastIdx, lastTerm := r.getLastEntry()
var prevLogTerm uint64
if a.PrevLogEntry == lastIdx {
prevLogTerm = lastTerm
} else {
var prevLog Log
if err := r.logs.GetLog(a.PrevLogEntry, &prevLog); err != nil {
log.Printf("[WARN] Failed to get previous log: %d %v",
a.PrevLogEntry, err)
return
}
prevLogTerm = prevLog.Term
}
if a.PrevLogTerm != prevLogTerm {
log.Printf("[WARN] Previous log term mis-match: ours: %d remote: %d",
prevLogTerm, a.PrevLogTerm)
return
}
}
// Add all the entries
for _, entry := range a.Entries {
// Delete any conflicting entries
lastLogIdx := r.getLastLogIndex()
if entry.Index <= lastLogIdx {
log.Printf("[WARN] Clearing log suffix from %d to %d", entry.Index, lastLogIdx)
if err := r.logs.DeleteRange(entry.Index, lastLogIdx); err != nil {
log.Printf("[ERR] Failed to clear log suffix: %v", err)
return
}
}
// Append the entry
if err := r.logs.StoreLog(entry); err != nil {
log.Printf("[ERR] Failed to append to log: %v", err)
return
}
// Update the lastLog
r.setLastLogIndex(entry.Index)
r.setLastLogTerm(entry.Term)
}
// Update the commit index
if a.LeaderCommitIndex > 0 && a.LeaderCommitIndex > r.getCommitIndex() {
idx := min(a.LeaderCommitIndex, r.getLastIndex())
r.setCommitIndex(idx)
r.processLogs(idx, nil)
}
// Everything went well, set success
resp.Success = true
return
}
// requestVote is invoked when we get an request vote RPC call
func (r *Raft) requestVote(rpc RPC, req *RequestVoteRequest) {
// Setup a response
resp := &RequestVoteResponse{
Term: r.getCurrentTerm(),
Peers: encodePeers(r.peers, r.trans),
Granted: false,
}
var rpcErr error
defer rpc.Respond(resp, rpcErr)
// Ignore an older term
if req.Term < r.getCurrentTerm() {
return
}
// Increase the term if we see a newer one
if req.Term > r.getCurrentTerm() {
// Ensure transition to follower
r.setState(Follower)
r.setCurrentTerm(req.Term)
resp.Term = req.Term
}
// Check if we have voted yet
lastVoteTerm, err := r.stable.GetUint64(keyLastVoteTerm)
if err != nil && err.Error() != "not found" {
log.Printf("[ERR] Failed to get last vote term: %v", err)
return
}
lastVoteCandBytes, err := r.stable.Get(keyLastVoteCand)
if err != nil && err.Error() != "not found" {
log.Printf("[ERR] Failed to get last vote candidate: %v", err)
return
}
// Check if we've voted in this election before
if lastVoteTerm == req.Term && lastVoteCandBytes != nil {
log.Printf("[INFO] Duplicate RequestVote for same term: %d", req.Term)
if bytes.Compare(lastVoteCandBytes, req.Candidate) == 0 {
log.Printf("[WARN] Duplicate RequestVote from candidate: %s", req.Candidate)
resp.Granted = true
}
return
}
// Reject if their term is older
lastIdx, lastTerm := r.getLastEntry()
if lastTerm > req.LastLogTerm {
log.Printf("[WARN] Rejecting vote since our last term is greater")
return
}
if lastIdx > req.LastLogIndex {
log.Printf("[WARN] Rejecting vote since our last index is greater")
return
}
// Persist a vote for safety
if err := r.persistVote(req.Term, req.Candidate); err != nil {
log.Printf("[ERR] Failed to persist vote: %v", err)
return
}
resp.Granted = true
return
}
// installSnapshot is invoked when we get a InstallSnapshot RPC call.
// We must be in the follower state for this, since it means we are
// too far behind a leader for log replay.
func (r *Raft) installSnapshot(rpc RPC, req *InstallSnapshotRequest) {
// Setup a response
resp := &InstallSnapshotResponse{
Term: r.getCurrentTerm(),
Success: false,
}
var rpcErr error
defer rpc.Respond(resp, rpcErr)
// Ignore an older term
if req.Term < r.getCurrentTerm() {
return
}
// Increase the term if we see a newer one
if req.Term > r.getCurrentTerm() {
// Ensure transition to follower
r.setState(Follower)
r.setCurrentTerm(req.Term)
resp.Term = req.Term
}
// Save the current leader
r.leader = r.trans.DecodePeer(req.Leader)
// Create a new snapshot
sink, err := r.snapshots.Create(req.LastLogIndex, req.LastLogTerm, req.Peers)
if err != nil {
log.Printf("[ERR] Failed to create snapshot to install: %v", err)
rpcErr = fmt.Errorf("Failed to create snapshot: %v", err)
return
}
// Spill the remote snapshot to disk
n, err := io.Copy(sink, rpc.Reader)
if err != nil {
sink.Cancel()
log.Printf("[ERR] Failed to copy snapshot: %v", err)
rpcErr = err
return
}
// Check that we received it all
if n != req.Size {
sink.Cancel()
log.Printf("[ERR] Failed to receive whole snapshot: %d / %d", n, req.Size)
rpcErr = fmt.Errorf("short read")
return
}
// Finalize the snapshot
if err := sink.Close(); err != nil {
log.Printf("[ERR] Failed to finalize snapshot: %v", err)
rpcErr = err
return
} else {
log.Printf("[INFO] Copied %d bytes to local snapshot", n)
}
// Restore snapshot
future := &restoreFuture{ID: sink.ID()}
future.init()
select {
case r.fsmRestoreCh <- future:
case <-r.shutdownCh:
future.respond(RaftShutdown)
return
}
// Wait for the restore to happen
if err := future.Error(); err != nil {
log.Printf("[ERR] Failed to restore snapshot: %v", err)
rpcErr = err
return
}
// Update the lastApplied so we don't replay old logs
r.setLastApplied(req.LastLogIndex)
// Update the last stable snapshot info
r.setLastSnapshotIndex(req.LastLogIndex)
r.setLastSnapshotTerm(req.LastLogTerm)
// Restore the peer set
r.peers = excludePeer(decodePeers(req.Peers, r.trans), r.localAddr)
// Compact logs, continue even if this fails
if err := r.compactLogs(req.LastLogIndex); err != nil {
log.Printf("[ERR] Failed to compact logs: %v", err)
}
log.Printf("[INFO] Installed remote snapshot")
resp.Success = true
return
}
// electSelf is used to send a RequestVote RPC to all peers,