Skip to content

Commit 3723231

Browse files
committed
raft: send commit index only if necessary
This commit fixes one case of unnecessary MsgApp sends. The leader now checks that the follower's commit index is behind, and only then sends a commit index update. Signed-off-by: Pavel Kalinnikov <[email protected]>
1 parent 5aaa1f1 commit 3723231

File tree

4 files changed

+3
-70
lines changed

4 files changed

+3
-70
lines changed

raft.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1519,11 +1519,8 @@ func stepLeader(r *raft, m pb.Message) error {
15191519
// to respond to pending read index requests
15201520
releasePendingReadIndexMessages(r)
15211521
r.bcastAppend()
1522-
} else if oldPaused {
1523-
// If we were paused before, this node may be missing the
1524-
// latest commit index, so send it.
1525-
// TODO(pav-kv): remove this branch, and decide on sending the commit
1526-
// index update based on pr.Commit.
1522+
} else if oldPaused && r.id != m.From && pr.Commit < r.raftLog.committed {
1523+
// The node is potentially missing the latest commit index. Send it.
15271524
r.sendAppend(m.From)
15281525
}
15291526
// We've updated flow control information above, which may

raft_paper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func TestFollowerCheckMsgApp(t *testing.T) {
633633

634634
msgs := r.readMessages()
635635
wmsgs := []pb.Message{
636-
{From: 1, To: 2, Type: pb.MsgAppResp, Term: 2, Index: tt.windex, Reject: tt.wreject, RejectHint: tt.wrejectHint, LogTerm: tt.wlogterm},
636+
{From: 1, To: 2, Type: pb.MsgAppResp, Term: 2, Index: tt.windex, Commit: tt.wcommit, Reject: tt.wreject, RejectHint: tt.wrejectHint, LogTerm: tt.wlogterm},
637637
}
638638
if !reflect.DeepEqual(msgs, wmsgs) {
639639
t.Errorf("#%d: msgs = %+v, want %+v", i, msgs, wmsgs)

testdata/confchange_v2_replace_leader.txt

-4
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,10 @@ stabilize
284284
CommittedEntries:
285285
2/5 EntryNormal ""
286286
Messages:
287-
4->1 MsgApp Term:2 Log:2/5 Commit:4
288287
4->1 MsgApp Term:2 Log:2/5 Commit:5
289288
4->2 MsgApp Term:2 Log:2/5 Commit:5
290289
4->3 MsgApp Term:2 Log:2/5 Commit:5
291290
> 1 receiving messages
292-
4->1 MsgApp Term:2 Log:2/5 Commit:4
293291
4->1 MsgApp Term:2 Log:2/5 Commit:5
294292
> 2 receiving messages
295293
4->2 MsgApp Term:2 Log:2/5 Commit:5
@@ -301,7 +299,6 @@ stabilize
301299
CommittedEntries:
302300
2/5 EntryNormal ""
303301
Messages:
304-
1->4 MsgAppResp Term:2 Log:0/5 Commit:4
305302
1->4 MsgAppResp Term:2 Log:0/5 Commit:5
306303
> 2 handling Ready
307304
Ready MustSync=false:
@@ -318,7 +315,6 @@ stabilize
318315
Messages:
319316
3->4 MsgAppResp Term:2 Log:0/5 Commit:5
320317
> 4 receiving messages
321-
1->4 MsgAppResp Term:2 Log:0/5 Commit:4
322318
1->4 MsgAppResp Term:2 Log:0/5 Commit:5
323319
2->4 MsgAppResp Term:2 Log:0/5 Commit:5
324320
3->4 MsgAppResp Term:2 Log:0/5 Commit:5

testdata/probe_and_replicate.txt

-60
Original file line numberDiff line numberDiff line change
@@ -513,18 +513,6 @@ stabilize 1 2
513513
2->1 MsgAppResp Term:8 Log:0/21 Commit:18
514514
> 1 receiving messages
515515
2->1 MsgAppResp Term:8 Log:0/21 Commit:18
516-
> 1 handling Ready
517-
Ready MustSync=false:
518-
Messages:
519-
1->2 MsgApp Term:8 Log:8/21 Commit:18
520-
> 2 receiving messages
521-
1->2 MsgApp Term:8 Log:8/21 Commit:18
522-
> 2 handling Ready
523-
Ready MustSync=false:
524-
Messages:
525-
2->1 MsgAppResp Term:8 Log:0/21 Commit:18
526-
> 1 receiving messages
527-
2->1 MsgAppResp Term:8 Log:0/21 Commit:18
528516

529517
stabilize 1 3
530518
----
@@ -579,18 +567,6 @@ stabilize 1 3
579567
3->1 MsgAppResp Term:8 Log:0/21 Commit:18
580568
> 1 receiving messages
581569
3->1 MsgAppResp Term:8 Log:0/21 Commit:18
582-
> 1 handling Ready
583-
Ready MustSync=false:
584-
Messages:
585-
1->3 MsgApp Term:8 Log:8/21 Commit:18
586-
> 3 receiving messages
587-
1->3 MsgApp Term:8 Log:8/21 Commit:18
588-
> 3 handling Ready
589-
Ready MustSync=false:
590-
Messages:
591-
3->1 MsgAppResp Term:8 Log:0/21 Commit:18
592-
> 1 receiving messages
593-
3->1 MsgAppResp Term:8 Log:0/21 Commit:18
594570

595571
stabilize 1 4
596572
----
@@ -674,18 +650,6 @@ stabilize 1 5
674650
5->1 MsgAppResp Term:8 Log:0/21 Commit:21
675651
> 1 receiving messages
676652
5->1 MsgAppResp Term:8 Log:0/21 Commit:21
677-
> 1 handling Ready
678-
Ready MustSync=false:
679-
Messages:
680-
1->5 MsgApp Term:8 Log:8/21 Commit:21
681-
> 5 receiving messages
682-
1->5 MsgApp Term:8 Log:8/21 Commit:21
683-
> 5 handling Ready
684-
Ready MustSync=false:
685-
Messages:
686-
5->1 MsgAppResp Term:8 Log:0/21 Commit:21
687-
> 1 receiving messages
688-
5->1 MsgAppResp Term:8 Log:0/21 Commit:21
689653

690654
stabilize 1 6
691655
----
@@ -741,18 +705,6 @@ stabilize 1 6
741705
6->1 MsgAppResp Term:8 Log:0/21 Commit:21
742706
> 1 receiving messages
743707
6->1 MsgAppResp Term:8 Log:0/21 Commit:21
744-
> 1 handling Ready
745-
Ready MustSync=false:
746-
Messages:
747-
1->6 MsgApp Term:8 Log:8/21 Commit:21
748-
> 6 receiving messages
749-
1->6 MsgApp Term:8 Log:8/21 Commit:21
750-
> 6 handling Ready
751-
Ready MustSync=false:
752-
Messages:
753-
6->1 MsgAppResp Term:8 Log:0/21 Commit:21
754-
> 1 receiving messages
755-
6->1 MsgAppResp Term:8 Log:0/21 Commit:21
756708

757709
stabilize 1 7
758710
----
@@ -816,15 +768,3 @@ stabilize 1 7
816768
7->1 MsgAppResp Term:8 Log:0/21 Commit:21
817769
> 1 receiving messages
818770
7->1 MsgAppResp Term:8 Log:0/21 Commit:21
819-
> 1 handling Ready
820-
Ready MustSync=false:
821-
Messages:
822-
1->7 MsgApp Term:8 Log:8/21 Commit:21
823-
> 7 receiving messages
824-
1->7 MsgApp Term:8 Log:8/21 Commit:21
825-
> 7 handling Ready
826-
Ready MustSync=false:
827-
Messages:
828-
7->1 MsgAppResp Term:8 Log:0/21 Commit:21
829-
> 1 receiving messages
830-
7->1 MsgAppResp Term:8 Log:0/21 Commit:21

0 commit comments

Comments
 (0)