-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
905 lines (822 loc) · 26.7 KB
/
node.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
package node
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
db "github.com/btmorr/leifdb/internal/database"
"github.com/btmorr/leifdb/internal/raft"
)
// Role is either Leader or Follower
type Role string
// Follower is a read-only member of a cluster
// Leader is a read/write member of a cluster
const (
Leader Role = "Leader"
Follower = "Follower"
)
var (
// ErrNotLeaderRecv indicates that a client attempted to make a write to a
// node that is not currently the leader of the cluster
ErrNotLeaderRecv = errors.New("Cannot accept writes if not leader")
// ErrNotLeaderSend indicates that a server attempted to send an append
// request while it is not the leader of the cluster
ErrNotLeaderSend = errors.New("Cannot send log append request if not leader")
// ErrExpiredTerm indicates that an append request was generated for a past
// term, so it should not be sent
ErrExpiredTerm = errors.New("Do not send append requests for expired terms")
// ErrAppendFailed indicates that an append job ran out of retry attempts
// without successfully appending to a majority of nodes
ErrAppendFailed = errors.New("Failed to append logs to a majority of nodes")
// ErrCommitFailed indicates that the leader's commit index after append
// is less than the index of the record being added
ErrCommitFailed = errors.New("Failed to commit record")
// ErrAppendRangeMet indicates that reverse-iteration has reached the
// beginning of the log and still not gotten a response--aborting
ErrAppendRangeMet = errors.New("Append range reached, not trying again")
)
// A ForeignNode is another member of the cluster, with connections needed
// to manage gRPC interaction with that node and track recent availability
type ForeignNode struct {
Connection *grpc.ClientConn
Client raft.RaftClient
NextIndex int64
MatchIndex int64
Available bool
}
// NewForeignNode constructs a ForeignNode from an address ("host:port")
func NewForeignNode(address string) (*ForeignNode, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
conn, err := grpc.DialContext(
ctx,
address,
grpc.WithInsecure())
if err != nil {
log.Error().Err(err).Msgf("Failed to connect to %s", address)
return nil, err
}
client := raft.NewRaftClient(conn)
return &ForeignNode{
Connection: conn,
Client: client,
NextIndex: 0,
MatchIndex: -1,
Available: true,
}, err
}
// Close cleans up the gRPC connection with the foreign node
func (f *ForeignNode) Close() {
f.Connection.Close()
}
// NodeConfig contains configurable properties for a node
type NodeConfig struct {
Id string
ClientAddr string
DataDir string
TermFile string
LogFile string
NodeIds []string
}
// ForeignNodeChecker functions are used to determine if a request comes from
// a valid participant in a cluster. It should generally check against a
// configuration file or other canonical record of membership, but can also
// be mocked out for test to cause a Node to respond to RPC requests without
// creating a full multi-node deployment.
type ForeignNodeChecker func(string, map[string]*ForeignNode) bool
// A Node is one member of a Raft cluster, with all state needed to operate the
// algorithm's state machine. At any time, its role may be Leader, Candidate,
// or Follower, and have different responsibilities depending on its role (note
// that Candidate is a virtual role--a Candidate does not behave differently
// from a Follower w.r.t. incoming messages, so the node will remain in the
// Follower state while an election is in progress)
type Node struct {
RaftNode *raft.Node
State Role
Term int64
votedFor *raft.Node
Reset chan bool
otherNodes map[string]*ForeignNode
CheckForeignNode ForeignNodeChecker
AllowVote bool
CommitIndex int64
lastApplied int64
Log *raft.LogStore
config NodeConfig
Store *db.Database
sync.Mutex
}
// Non-volatile state functions
// `Term`, `votedFor`, and `Log` must persist through application restart, so
// any request that changes these values must be written to disk before
// responding to the request.
// RedirectLeader provides the leader which we want to redirect requests to if
// we are not the leader at present
func (n *Node) RedirectLeader() string {
if n.votedFor == nil {
return ""
}
return n.votedFor.ClientAddr
}
// WriteTerm persists the node's most recent term and vote
func WriteTerm(filename string, termRecord *raft.TermRecord) error {
out, err := proto.Marshal(termRecord)
if err != nil {
log.Fatal().Err(err).Msg("Failed to marshal term record")
return err
}
_, err = os.Stat(filepath.Dir(filename))
if err != nil {
log.Fatal().Err(err).Msg("Failed stat")
return err
}
if err = ioutil.WriteFile(filename, out, 0644); err != nil {
log.Fatal().Err(err).Msg("Failed to write term file")
}
return err
}
// ReadTerm attempts to unmarshal and return a TermRecord from the specified
// file, and if unable to do so returns an initialized TermRecord
func ReadTerm(filename string) *raft.TermRecord {
record := &raft.TermRecord{Term: 0, VotedFor: nil}
_, err := os.Stat(filename)
if err == nil {
termFile, _ := ioutil.ReadFile(filename)
if err = proto.Unmarshal(termFile, record); err != nil {
log.Warn().Err(err).Msg("Failed to unmarshal term file")
}
}
return record
}
// SetTerm records term and vote in non-volatile state
func (n *Node) SetTerm(newTerm int64, votedFor *raft.Node) error {
n.Term = newTerm
n.votedFor = votedFor
vote := &raft.TermRecord{
Term: newTerm,
VotedFor: votedFor}
return WriteTerm(n.config.TermFile, vote)
}
// WriteLogs persists the node's log
func WriteLogs(filename string, logStore *raft.LogStore) error {
out, err := proto.Marshal(logStore)
if err != nil {
log.Fatal().Err(err).Msg("Failed to marshal logs")
}
if err = ioutil.WriteFile(filename, out, 0644); err != nil {
log.Fatal().Err(err).Msg("Failed to write log file")
}
return err
}
// ReadLogs attempts to unmarshal and return a LogStore from the specified
// file, and if unable to do so returns an empty LogStore
func ReadLogs(filename string) *raft.LogStore {
logStore := &raft.LogStore{Entries: make([]*raft.LogRecord, 0, 0)}
_, err := os.Stat(filename)
if err != nil {
} else {
logFile, _ := ioutil.ReadFile(filename)
if err = proto.Unmarshal(logFile, logStore); err != nil {
log.Error().
Err(err).
Msg("Failed to unmarshal log file, creating empty log store")
}
}
return logStore
}
// resetElectionTimer ensures that the node's state is Follower, and sends a
// signal to the reset channel (read by the StateManager, which controls the
// timers used for elections)
func (n *Node) resetElectionTimer() {
n.State = Follower
go func() {
n.Reset <- true
}()
}
// setLog records new log contents in non-volatile state, and returns the index
// of the record in the log, or an error
func (n *Node) setLog(newLogs []*raft.LogRecord) (int64, error) {
record := &raft.LogStore{Entries: newLogs}
idx := int64(len(record.Entries) - 1)
err := WriteLogs(n.config.LogFile, record)
if err == nil {
n.Log = record
}
return idx, err
}
// applyRecord adds a new record to the log, then sends an append-logs request
// to other nodes in the cluster. This method does not return until either the
// log is successfully committed to a majority of nodes, or a majority of
// nodes fail via explicit rejection or timeout (which should generally result
// in an election)
func (n *Node) applyRecord(record *raft.LogRecord) error {
if n.State != Leader {
return ErrNotLeaderRecv
}
newEntries := append(n.Log.Entries, record)
idx, err := n.setLog(newEntries)
if err != nil {
log.Error().Err(err).Msg("applyRecord: Error setting log")
return err
}
// Try appending logs to other nodes, with 3 retries
currentTerm := n.Term
err = n.SendAppend(3, currentTerm)
if err != nil {
log.Error().Err(err).Msg("applyRecord: Error shipping log")
return err
}
// verify that n.CommitIndex >= idx
if n.CommitIndex < idx {
log.Error().Err(ErrCommitFailed).
Int64("recordIndex", idx).
Int64("CommitIndex", n.CommitIndex).
Msg("Commit index failed to update after append")
return ErrCommitFailed
}
// return once entry is applied to state machine or error
return err
}
// Client methods for managing raft state
// Set appends a write entry to the log record, and returns once the update is
// applied to the state machine or an error is generated
func (n *Node) Set(key string, value string) error {
log.Info().
Str("key", key).
Str("value", value).
Msg("Set")
record := &raft.LogRecord{
Term: n.Term,
Action: raft.LogRecord_SET,
Key: key,
Value: value}
n.Lock()
defer n.Unlock()
return n.applyRecord(record)
}
// Delete appends a delete entry to the log record, and returns once the update
// is applied to the state machine or an error is generated
func (n *Node) Delete(key string) error {
log.Info().
Str("key", key).
Msg("Delete")
record := &raft.LogRecord{
Term: n.Term,
Action: raft.LogRecord_DEL,
Key: key}
n.Lock()
defer n.Unlock()
return n.applyRecord(record)
}
// requestVote sends a request for vote to a single other node (see DoElection)
func (n *Node) requestVote(host string) (*raft.VoteReply, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*4)
defer cancel()
lastLogIndex := int64(len(n.Log.Entries)) - 1
var lastLogTerm int64
if lastLogIndex >= 0 {
lastLogTerm = n.Log.Entries[lastLogIndex].Term
} else {
lastLogTerm = 0
}
voteRequest := &raft.VoteRequest{
Term: n.Term,
Candidate: n.RaftNode,
LastLogIndex: lastLogIndex,
LastLogTerm: lastLogTerm}
vote, err := n.otherNodes[host].Client.RequestVote(ctx, voteRequest)
if err != nil {
log.Warn().Err(err).Msgf("Error requesting vote from %s", host)
n.otherNodes[host].Available = false
} else {
n.otherNodes[host].Available = true
}
return vote, err
}
// DoElection sends out requests for votes to each other node in the Raft
// cluster. When a Raft node's role is "candidate", it should send start an
// election. If it is granted votes from a majority of nodes, its role changes
// to "leader". If it receives an append-logs message during the election from
// a node with a term higher than this node's current term, its role changes to
// "follower". If it does not receive a majority of votes and also does not
// receive an append-logs from a valid leader, it increments the term and
// starts another election (repeat until a leader is elected).
func (n *Node) DoElection() bool {
log.Trace().Msg("Starting Election")
n.SetTerm(n.Term+1, n.RaftNode)
numNodes := len(n.otherNodes) + 1
majority := (numNodes / 2) + 1
var success bool
log.Info().
Int64("Term", n.Term).
Int("clusterSize", numNodes).
Int("needed", majority).
Msg("Becoming candidate")
numVotes := 1
maxTermSeen := n.Term
maxTermSeenSource := n.votedFor
var wg sync.WaitGroup
wg.Add(len(n.otherNodes))
for k := range n.otherNodes {
// if needed for performance, figure out how to collect the term responses in a thread-safe way
go func(k string) {
defer wg.Done()
vote, err := n.requestVote(k)
log.Trace().Msg("got a vote")
if err != nil {
return
}
if vote.VoteGranted {
log.Trace().Msg("it's a 'yay'")
numVotes++
} else {
if vote.Term > maxTermSeen {
maxTermSeen = vote.Term
maxTermSeenSource = vote.Node
}
}
}(k)
}
wg.Wait()
voteLog := log.Info().
Int("needed", majority).
Int("got", numVotes)
if numVotes < majority {
voteLog.
Bool("success", false).
Int64("term", n.Term).
Msg("Election failed")
success = false
if maxTermSeen > n.Term {
log.Info().
Int64("max response term", maxTermSeen).
Str("other node", maxTermSeenSource.Id).
Msg("Updating term to max seen")
n.SetTerm(maxTermSeen, maxTermSeenSource)
}
} else {
voteLog.
Bool("success", true).
Int64("term", n.Term).
Msg("Election succeeded")
n.State = Leader
success = true
// StateManager grace window job sets this back to true
n.AllowVote = false
for k := range n.otherNodes {
n.otherNodes[k].MatchIndex = -1
n.otherNodes[k].NextIndex = int64(len(n.Log.Entries))
}
}
return success
}
// commitRecords iterates backward from last index of log entries, and finds
// latest index that has been appended to a majority of nodes, and updates
// the database and node CommitIndex
func (n *Node) commitRecords() {
log.Trace().Msg("commitRecords")
numNodes := len(n.otherNodes)
majority := (numNodes / 2) + 1
log.Trace().Msgf("Need to apply message to %d nodes", majority)
lastIdx := int64(len(n.Log.Entries) - 1)
log.Trace().
Int64("lastIndex", lastIdx).
Int64("CommitIndex", n.CommitIndex).
Msgf("Checking for update to commit index")
for lastIdx > n.CommitIndex {
count := 1
for k := range n.otherNodes {
if n.otherNodes[k].MatchIndex >= lastIdx {
count++
}
}
log.Trace().Msgf("Applied to %d nodes", count)
if count >= majority {
log.Info().
Int64("prevCommitIndex", n.CommitIndex).
Int64("newCommitIndex", lastIdx).
Msgf("Updated commit index")
n.CommitIndex = lastIdx
break
}
lastIdx--
}
// if any records were committed, apply them to the database
log.Trace().
Int64("lastApplied", n.lastApplied).
Msg("Applying records to database")
for n.lastApplied < n.CommitIndex {
n.lastApplied++
action := n.Log.Entries[n.lastApplied].Action
key := n.Log.Entries[n.lastApplied].Key
if action == raft.LogRecord_SET {
value := n.Log.Entries[n.lastApplied].Value
log.Trace().
Str("key", key).
Str("value", value).
Msg("Db set")
n.Store.Set(key, value)
} else if action == raft.LogRecord_DEL {
log.Trace().
Str("key", key).
Msg("Db del")
n.Store.Delete(key)
}
}
}
// requestAppend sends append to one other node with new record(s) and updates
// match index for that node if successful
func (n *Node) requestAppend(host string, term int64) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*12)
defer cancel()
prevLogIndex := n.otherNodes[host].MatchIndex
// make a slice of all entries the other node has not seen (right after
// election, this will be all records--would it be better to query for
// number of entries in other node's log and start there? or is it better
// to deal with this via reasonable log-compaction limits? (need to figure
// out the relationship between log size and message size and make a
// reasonable speculation about desired max message size)
idx := int64(len(n.Log.Entries))
newEntries := n.Log.Entries[prevLogIndex+1 : idx]
var prevLogTerm int64
if prevLogIndex >= 0 {
prevLogTerm = n.Log.Entries[prevLogIndex].Term
} else {
prevLogTerm = 0
}
req := &raft.AppendRequest{
Term: term,
Leader: n.RaftNode,
PrevLogIndex: prevLogIndex,
PrevLogTerm: prevLogTerm,
Entries: newEntries,
LeaderCommit: n.CommitIndex}
if n.State != Leader {
// escape hatch in case this node stepped down in between the call to
// `SendAppend` and this point
log.Trace().Msg("requestAppend not leader, returning")
return ErrNotLeaderSend
}
if term != n.Term {
log.Trace().
Int64("req term", term).
Int64("node term", n.Term).
Str("state", string(n.State)).
Msg("past escape hatch")
return ErrExpiredTerm
}
reply, err := n.otherNodes[host].Client.AppendLogs(ctx, req)
if err == nil {
if reply.Success {
n.otherNodes[host].MatchIndex = idx - 1
n.otherNodes[host].NextIndex = idx
n.otherNodes[host].Available = true
return nil
} else {
if prevLogIndex > 0 {
n.otherNodes[host].MatchIndex--
return n.requestAppend(host, term)
}
n.otherNodes[host].Available = false
return ErrAppendRangeMet
// todo: would it be viable for AppendReply to include the other
// node's log index, so this could fast-forward to the correct
// index, rather than recursing possibly down the whole list?
// This implementation will blow the stack fast with any kind of
// realistic history when you add a fresh node
}
}
n.otherNodes[host].Available = false
return err
}
// SendAppend sends out append-logs requests to each other node in the cluster,
// and updates database state on majority success
func (n *Node) SendAppend(retriesRemaining int, term int64) error {
log.Trace().Msgf("SendAppend(r%d)", retriesRemaining)
if n.State != Leader {
log.Trace().Msg("SendAppend but not leader, returning")
return ErrNotLeaderSend
}
numNodes := len(n.otherNodes)
majority := (numNodes / 2) + 1
log.Trace().Msgf("Number needed for append: %d", majority)
var m sync.Mutex
numAppended := 1
// Send append out to all other nodes with new record(s)
var wg sync.WaitGroup
for k := range n.otherNodes {
// append new entries
// update indices
wg.Add(1)
go func(k string) {
defer wg.Done()
err := n.requestAppend(k, term)
if err != nil {
log.Debug().Err(err).Msgf(
"Error requesting append from %s for term %d", k, term)
} else {
m.Lock()
numAppended++
m.Unlock()
}
}(k)
}
wg.Wait()
log.Trace().Msgf("Appended to %d nodes", numAppended)
if numAppended >= majority {
log.Trace().Msg("majority")
// update commit index on this node and apply newly committed records
// to the database (next automatic append will commit on other nodes)
n.commitRecords()
} else {
log.Trace().Msg("minority")
// did not get a majority
if retriesRemaining > 0 {
return n.SendAppend(retriesRemaining-1, term)
}
return ErrAppendFailed
}
return nil
}
// NewNodeConfig creates a config for a Node
func NewNodeConfig(dataDir string, addr, clientAddr string, nodeIds []string) NodeConfig {
return NodeConfig{
Id: addr,
ClientAddr: clientAddr,
DataDir: dataDir,
TermFile: filepath.Join(dataDir, "term"),
LogFile: filepath.Join(dataDir, "raftlog"),
NodeIds: nodeIds}
}
// checkForeignNode verifies that a node is a known member of the cluster (this
// is the expected checker for a Node, but is extracted so it can be mocked)
func checkForeignNode(addr string, known map[string]*ForeignNode) bool {
_, ok := known[addr]
return ok
}
// NewNode initializes a Node with a randomized election timeout
func NewNode(config NodeConfig, store *db.Database) (*Node, error) {
// Load persistent Node state
termRecord := ReadTerm(config.TermFile)
logStore := ReadLogs(config.LogFile)
// channels used by Node to communicate with StateManager
resetChannel := make(chan bool)
// haltChannel := make(chan bool)
votedForId := "<undetermined>"
if termRecord.VotedFor != nil {
votedForId = termRecord.VotedFor.Id
}
log.Info().
Int64("Term", termRecord.Term).
Str("Vote", votedForId).
Int("nLogs", len(logStore.Entries)).
Msg("On load")
n := Node{
RaftNode: &raft.Node{
Id: config.Id,
ClientAddr: config.ClientAddr,
},
State: Follower,
Term: termRecord.Term,
votedFor: termRecord.VotedFor,
Reset: resetChannel,
otherNodes: make(map[string]*ForeignNode),
CheckForeignNode: checkForeignNode,
AllowVote: true,
CommitIndex: -1,
lastApplied: -1,
Log: logStore,
config: config,
Store: store}
for _, addr := range config.NodeIds {
n.AddForeignNode(addr)
}
return &n, nil
}
// AddForeignNode updates the list of known other members of the raft cluster
func (n *Node) AddForeignNode(addr string) {
log.Trace().Msgf("AddForeignNode: %s", addr)
n.otherNodes[addr], _ = NewForeignNode(addr)
log.Info().Msgf("Added %s to known nodes", addr)
}
// availability returns the number of nodes believed to be currently available
// and the number of total nodes in the current cluster configuration
func (n *Node) availability() (int, int) {
// initialize to 1 to account for the self
available := 1
total := 1
for _, foreignNode := range n.otherNodes {
total++
if foreignNode.Available {
available++
}
}
return available, total
}
// candidateLogUpToDate checks if a candidate's log index is at least as high as
// the node's commit index (e.g.: candidate has all known committed entries), and
// that the
func (n *Node) candidateLogUpToDate(cLogIndex int64, cLogTerm int64) bool {
indexGreater := cLogIndex > n.CommitIndex
indexEqual := cLogIndex == n.CommitIndex
bothEmpty := cLogIndex == -1 && n.CommitIndex == -1
indexPresent := cLogIndex < int64(len(n.Log.Entries))
upToDate := indexGreater ||
bothEmpty ||
(indexEqual && cLogTerm == n.Log.Entries[cLogIndex].Term)
if !upToDate {
failLog := log.Debug().
Int64("CLogIdx", cLogIndex).
Int64("CommitIdx", n.CommitIndex).
Int64("CLogTerm", cLogTerm)
if indexPresent {
failLog.Int64("LogTerm", n.Log.Entries[cLogIndex].Term)
}
failLog.Msg("candidate log not up to date")
}
return upToDate
}
// HandleVote responds to vote requests from candidate nodes
func (n *Node) HandleVote(req *raft.VoteRequest) *raft.VoteReply {
log.Info().Msgf("%s proposed term: %d", req.Candidate.Id, req.Term)
var vote bool
var msg string
if req.Term < n.Term {
vote = false
msg = "Past term vote received"
} else if req.Term == n.Term {
vote = false
msg = "Current term vote received"
// If this node is the leader, and a vote request is received for the
// current term, the current term should be increased because the other
// node will have voted for itself and therefore not accept appends from
// this leader (this happens when a previous leader restarts and then
// comes back online and restarts an election for the current term, having
// not received an append request from this leader in its initial election
// window--shouldn't happen often, but if it does it would otherwise result
// in an otherwise unnecessary election)
if n.State == Leader {
msg = msg + ", incrementing term"
n.SetTerm(n.Term+1, n.RaftNode)
}
} else if !n.CheckForeignNode(req.Candidate.Id, n.otherNodes) {
vote = false
msg = "Unknown foreign node: " + req.Candidate.Id
} else if !n.candidateLogUpToDate(req.LastLogIndex, req.LastLogTerm) {
vote = false
msg = "Candidate log not up to date"
} else if !n.AllowVote {
vote = false
msg = "Leader still in grace period"
} else {
msg = "Voting yay"
vote = true
n.resetElectionTimer()
n.SetTerm(req.Term, req.Candidate)
}
log.Info().
Int64("Term", n.Term).
Bool("Granted", vote).
Msg(msg)
return &raft.VoteReply{
Term: n.Term,
VoteGranted: vote,
Node: n.RaftNode}
}
// validateAppend performs all checks for valid append request
func (n *Node) validateAppend(term int64, leaderId string) bool {
var success bool
success = true
// reply false if req term < current term
if term < n.Term {
success = false
} else if term == n.Term && leaderId != n.votedFor.Id {
log.Error().
Int64("term", n.Term).
Str("got", leaderId).
Str("expected", n.votedFor.Id).
Msgf("Append request leader mismatch")
success = false
}
if success {
n.resetElectionTimer()
}
return success
}
// If an existing entry conflicts with a new one (same idx diff term),
// reconcileLogs deletes the existing entry and any that follow
func reconcileLogs(
logStore *raft.LogStore, body *raft.AppendRequest) *raft.LogStore {
// note: don't memoize length of Entries, it changes multiple times
// during this method--safer to recalculate, and memoizing would
// only save a maximum of one pass so it's not worth it
var mismatchIdx int64
mismatchIdx = -1
if body.PrevLogIndex < int64(len(logStore.Entries)-1) {
overlappingEntries := logStore.Entries[body.PrevLogIndex+1:]
for i, rec := range overlappingEntries {
if i >= len(body.Entries) {
mismatchIdx = body.PrevLogIndex + int64(i)
break
}
if rec.Term != body.Entries[i].Term {
mismatchIdx = body.PrevLogIndex + 1 + int64(i)
break
}
}
}
if mismatchIdx >= 0 {
log.Debug().Msgf("Mismatch index: %d - rewinding log", mismatchIdx)
logStore.Entries = logStore.Entries[:mismatchIdx]
}
// append any entries not already in log
offset := int64(len(logStore.Entries)-1) - body.PrevLogIndex
newLogs := body.Entries[offset:]
log.Info().Msgf("Appending %d entries from %s", len(newLogs), body.Leader.Id)
return &raft.LogStore{Entries: append(logStore.Entries, newLogs...)}
}
// applyCommittedLogs updates the database with actions that have not yet been
// applied, up to the new commit index
func (n *Node) applyCommittedLogs(commitIdx int64) {
log.Debug().
Int64("current", n.CommitIndex).
Int64("leader", commitIdx).
Msg("apply commits")
if commitIdx > n.CommitIndex {
// ensure we don't run over the end of the log
lastIndex := int64(len(n.Log.Entries))
if commitIdx > lastIndex {
commitIdx = lastIndex
}
// apply all entries up to new commit index to store
for n.CommitIndex < commitIdx {
n.CommitIndex++
action := n.Log.Entries[n.CommitIndex].Action
key := n.Log.Entries[n.CommitIndex].Key
if action == raft.LogRecord_SET {
value := n.Log.Entries[n.CommitIndex].Value
n.Store.Set(key, value)
} else if action == raft.LogRecord_DEL {
n.Store.Delete(key)
}
}
log.Info().
Int64("commit", n.CommitIndex).
Msg("Commit updated")
}
}
// checkPrevious returns true if Node.logs contains an entry at the specified
// index with the specified term, otherwise false
func (n *Node) checkPrevious(prevIndex int64, prevTerm int64) bool {
if prevIndex < 0 {
return true
}
inRange := prevIndex < int64(len(n.Log.Entries))
matches := n.Log.Entries[prevIndex].Term == prevTerm
return inRange && matches
}
// HandleAppend responds to append-log messages from leader nodes
func (n *Node) HandleAppend(req *raft.AppendRequest) *raft.AppendReply {
var success bool
valid := n.validateAppend(req.Term, req.Leader.Id)
matched := n.checkPrevious(req.PrevLogIndex, req.PrevLogTerm)
if !valid {
// Invalid request
success = false
} else if !matched {
// Valid request, but earlier entries needed
success = false
} else {
// Valid request, and all required logs present
if len(req.Entries) > 0 {
n.Log = reconcileLogs(n.Log, req)
n.setLog(n.Log.Entries)
}
n.applyCommittedLogs(req.LeaderCommit)
success = true
}
if valid {
// update term if necessary
if req.Term > n.Term {
log.Info().
Int64("newTerm", req.Term).
Str("votedFor", req.Leader.Id).
Msg("Got more recent append, updating term record")
n.SetTerm(req.Term, req.Leader)
}
// reset the election timer on append from a valid leader (even if
// not matched)--this duplicates the reset in `validateAppend`, in order to
// ensure that the time it takes to do all of the operations in this
// handler effectively happened "instantaneously" from the perspective of
// the election timeout
n.resetElectionTimer()
}
// finally
return &raft.AppendReply{Term: n.Term, Success: success}
}