Skip to content

Commit

Permalink
raft: send commit index eagerly and only if necessary
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kalinnikov <[email protected]>
  • Loading branch information
pav-kv committed Jan 24, 2024
1 parent c9ea2f0 commit 303bd94
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 74 deletions.
8 changes: 2 additions & 6 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,6 @@ func stepLeader(r *raft, m pb.Message) error {
}
} else {
pr.UpdateCommit(m.Commit)
oldPaused := pr.IsPaused()
// We want to update our tracking if the response updates our
// matched index or if the response can move a probing peer back
// into StateReplicate (see heartbeat_rep_recovers_from_probing.txt
Expand Down Expand Up @@ -1500,11 +1499,8 @@ func stepLeader(r *raft, m pb.Message) error {
// to respond to pending read index requests
releasePendingReadIndexMessages(r)
r.bcastAppend()
} else if oldPaused {
// If we were paused before, this node may be missing the
// latest commit index, so send it.
// TODO(pav-kv): remove this branch, and decide on sending the commit
// index update based on pr.Commit.
} else if r.id != m.From && pr.Commit < r.raftLog.committed {
// This node is missing the latest commit index, send it.
r.sendAppend(m.From)
}
// We've updated flow control information above, which may
Expand Down
24 changes: 20 additions & 4 deletions testdata/async_storage_writes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ process-ready 1 2 3
Messages:
1->2 MsgApp Term:1 Log:1/13 Commit:12
1->3 MsgApp Term:1 Log:1/13 Commit:12
1->3 MsgApp Term:1 Log:1/13 Commit:12
1->2 MsgApp Term:1 Log:1/13 Commit:12 Entries:[1/14 EntryNormal "prop_3"]
1->3 MsgApp Term:1 Log:1/13 Commit:12 Entries:[1/14 EntryNormal "prop_3"]
1->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:12 Vote:1 Entries:[1/14 EntryNormal "prop_3"] Responses:[1->1 MsgAppResp Term:1 Log:0/14 Commit:12, AppendThread->1 MsgStorageAppendResp Term:1 Log:1/14]
Expand All @@ -341,6 +342,7 @@ deliver-msgs 1 2 3
1->2 MsgApp Term:1 Log:1/13 Commit:12
1->2 MsgApp Term:1 Log:1/13 Commit:12 Entries:[1/14 EntryNormal "prop_3"]
1->3 MsgApp Term:1 Log:1/13 Commit:12
1->3 MsgApp Term:1 Log:1/13 Commit:12
1->3 MsgApp Term:1 Log:1/13 Commit:12 Entries:[1/14 EntryNormal "prop_3"]

process-ready 1 2 3
Expand All @@ -365,7 +367,7 @@ process-ready 1 2 3
CommittedEntries:
1/12 EntryNormal "prop_1"
Messages:
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:12 Vote:1 Entries:[1/14 EntryNormal "prop_3"] Responses:[3->1 MsgAppResp Term:1 Log:0/13 Commit:12, 3->1 MsgAppResp Term:1 Log:0/14 Commit:12, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/14]
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:12 Vote:1 Entries:[1/14 EntryNormal "prop_3"] Responses:[3->1 MsgAppResp Term:1 Log:0/13 Commit:12, 3->1 MsgAppResp Term:1 Log:0/13 Commit:12, 3->1 MsgAppResp Term:1 Log:0/14 Commit:12, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/14]
3->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/12 EntryNormal "prop_1"] Responses:[ApplyThread->3 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/12 EntryNormal "prop_1"]]

process-append-thread 1 2 3
Expand Down Expand Up @@ -414,6 +416,7 @@ process-ready 1 2 3
Messages:
1->2 MsgApp Term:1 Log:1/14 Commit:13
1->3 MsgApp Term:1 Log:1/14 Commit:13
1->3 MsgApp Term:1 Log:1/14 Commit:13
1->2 MsgApp Term:1 Log:1/14 Commit:13 Entries:[1/15 EntryNormal "prop_4"]
1->3 MsgApp Term:1 Log:1/14 Commit:13 Entries:[1/15 EntryNormal "prop_4"]
1->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:13 Vote:1 Entries:[1/15 EntryNormal "prop_4"] Responses:[1->1 MsgAppResp Term:1 Log:0/15 Commit:13, AppendThread->1 MsgStorageAppendResp Term:1 Log:1/15]
Expand All @@ -428,6 +431,7 @@ deliver-msgs 1 2 3
1->2 MsgApp Term:1 Log:1/14 Commit:13
1->2 MsgApp Term:1 Log:1/14 Commit:13 Entries:[1/15 EntryNormal "prop_4"]
1->3 MsgApp Term:1 Log:1/14 Commit:13
1->3 MsgApp Term:1 Log:1/14 Commit:13
1->3 MsgApp Term:1 Log:1/14 Commit:13 Entries:[1/15 EntryNormal "prop_4"]

process-ready 1 2 3
Expand All @@ -452,7 +456,7 @@ process-ready 1 2 3
CommittedEntries:
1/13 EntryNormal "prop_2"
Messages:
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:13 Vote:1 Entries:[1/15 EntryNormal "prop_4"] Responses:[3->1 MsgAppResp Term:1 Log:0/14 Commit:13, 3->1 MsgAppResp Term:1 Log:0/15 Commit:13, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15]
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:13 Vote:1 Entries:[1/15 EntryNormal "prop_4"] Responses:[3->1 MsgAppResp Term:1 Log:0/14 Commit:13, 3->1 MsgAppResp Term:1 Log:0/14 Commit:13, 3->1 MsgAppResp Term:1 Log:0/15 Commit:13, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15]
3->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/13 EntryNormal "prop_2"] Responses:[ApplyThread->3 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/13 EntryNormal "prop_2"]]

process-append-thread 1 2 3
Expand All @@ -475,6 +479,7 @@ process-append-thread 1 2 3
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:12 Vote:1 Entries:[1/14 EntryNormal "prop_3"]
Responses:
3->1 MsgAppResp Term:1 Log:0/13 Commit:12
3->1 MsgAppResp Term:1 Log:0/13 Commit:12
3->1 MsgAppResp Term:1 Log:0/14 Commit:12
AppendThread->3 MsgStorageAppendResp Term:1 Log:1/14

Expand Down Expand Up @@ -503,6 +508,7 @@ AppendThread->1 MsgStorageAppendResp Term:1 Log:1/14
2->1 MsgAppResp Term:1 Log:0/13 Commit:12
2->1 MsgAppResp Term:1 Log:0/14 Commit:12
3->1 MsgAppResp Term:1 Log:0/13 Commit:12
3->1 MsgAppResp Term:1 Log:0/13 Commit:12
3->1 MsgAppResp Term:1 Log:0/14 Commit:12
ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/12 EntryNormal "prop_1"]
AppendThread->2 MsgStorageAppendResp Term:1 Log:1/14
Expand All @@ -520,6 +526,7 @@ process-ready 1 2 3
Messages:
1->2 MsgApp Term:1 Log:1/15 Commit:14
1->3 MsgApp Term:1 Log:1/15 Commit:14
1->3 MsgApp Term:1 Log:1/15 Commit:14
1->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:14 Vote:1 Responses:[AppendThread->1 MsgStorageAppendResp Term:1 Log:1/15]
1->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/14 EntryNormal "prop_3"] Responses:[ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/14 EntryNormal "prop_3"]]
> 2 handling Ready
Expand All @@ -531,6 +538,7 @@ deliver-msgs 1 2 3
----
1->2 MsgApp Term:1 Log:1/15 Commit:14
1->3 MsgApp Term:1 Log:1/15 Commit:14
1->3 MsgApp Term:1 Log:1/15 Commit:14

process-ready 1 2 3
----
Expand All @@ -550,7 +558,7 @@ process-ready 1 2 3
CommittedEntries:
1/14 EntryNormal "prop_3"
Messages:
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:14 Vote:1 Responses:[3->1 MsgAppResp Term:1 Log:0/15 Commit:14, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15]
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:14 Vote:1 Responses:[3->1 MsgAppResp Term:1 Log:0/15 Commit:14, 3->1 MsgAppResp Term:1 Log:0/15 Commit:14, AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15]
3->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/14 EntryNormal "prop_3"] Responses:[ApplyThread->3 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/14 EntryNormal "prop_3"]]

process-append-thread 1 2 3
Expand All @@ -573,6 +581,7 @@ process-append-thread 1 2 3
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:13 Vote:1 Entries:[1/15 EntryNormal "prop_4"]
Responses:
3->1 MsgAppResp Term:1 Log:0/14 Commit:13
3->1 MsgAppResp Term:1 Log:0/14 Commit:13
3->1 MsgAppResp Term:1 Log:0/15 Commit:13
AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15

Expand Down Expand Up @@ -601,6 +610,7 @@ AppendThread->1 MsgStorageAppendResp Term:1 Log:1/15
2->1 MsgAppResp Term:1 Log:0/14 Commit:13
2->1 MsgAppResp Term:1 Log:0/15 Commit:13
3->1 MsgAppResp Term:1 Log:0/14 Commit:13
3->1 MsgAppResp Term:1 Log:0/14 Commit:13
3->1 MsgAppResp Term:1 Log:0/15 Commit:13
ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/13 EntryNormal "prop_2"]
AppendThread->2 MsgStorageAppendResp Term:1 Log:1/15
Expand All @@ -618,6 +628,7 @@ process-ready 1 2 3
Messages:
1->2 MsgApp Term:1 Log:1/15 Commit:15
1->3 MsgApp Term:1 Log:1/15 Commit:15
1->3 MsgApp Term:1 Log:1/15 Commit:15
1->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:15 Vote:1
1->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"] Responses:[ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"]]
> 2 handling Ready
Expand All @@ -629,6 +640,7 @@ deliver-msgs 1 2 3
----
1->2 MsgApp Term:1 Log:1/15 Commit:15
1->3 MsgApp Term:1 Log:1/15 Commit:15
1->3 MsgApp Term:1 Log:1/15 Commit:15

process-ready 1 2 3
----
Expand All @@ -648,7 +660,7 @@ process-ready 1 2 3
CommittedEntries:
1/15 EntryNormal "prop_4"
Messages:
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:15 Vote:1 Responses:[3->1 MsgAppResp Term:1 Log:0/15 Commit:15]
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:15 Vote:1 Responses:[3->1 MsgAppResp Term:1 Log:0/15 Commit:15, 3->1 MsgAppResp Term:1 Log:0/15 Commit:15]
3->ApplyThread MsgStorageApply Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"] Responses:[ApplyThread->3 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"]]

process-append-thread 2 3
Expand All @@ -664,6 +676,7 @@ process-append-thread 2 3
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:14 Vote:1
Responses:
3->1 MsgAppResp Term:1 Log:0/15 Commit:14
3->1 MsgAppResp Term:1 Log:0/15 Commit:14
AppendThread->3 MsgStorageAppendResp Term:1 Log:1/15

process-apply-thread 1 2 3
Expand All @@ -688,6 +701,7 @@ deliver-msgs 1 2 3
----
2->1 MsgAppResp Term:1 Log:0/15 Commit:14
3->1 MsgAppResp Term:1 Log:0/15 Commit:14
3->1 MsgAppResp Term:1 Log:0/15 Commit:14
ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/14 EntryNormal "prop_3"]
AppendThread->2 MsgStorageAppendResp Term:1 Log:1/15
INFO entry at index 15 missing from unstable log; ignoring
Expand Down Expand Up @@ -717,6 +731,7 @@ process-append-thread 2 3
3->AppendThread MsgStorageAppend Term:1 Log:0/0 Commit:15 Vote:1
Responses:
3->1 MsgAppResp Term:1 Log:0/15 Commit:15
3->1 MsgAppResp Term:1 Log:0/15 Commit:15

process-apply-thread 1 2 3
----
Expand All @@ -740,6 +755,7 @@ deliver-msgs 1 2 3
----
2->1 MsgAppResp Term:1 Log:0/15 Commit:15
3->1 MsgAppResp Term:1 Log:0/15 Commit:15
3->1 MsgAppResp Term:1 Log:0/15 Commit:15
ApplyThread->1 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"]
ApplyThread->2 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"]
ApplyThread->3 MsgStorageApplyResp Term:0 Log:0/0 Entries:[1/15 EntryNormal "prop_4"]
Expand Down
19 changes: 19 additions & 0 deletions testdata/confchange_v1_remove_leader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,26 @@ stabilize 1
2->1 MsgAppResp Term:1 Log:0/6 Commit:3
2->1 MsgAppResp Term:1 Log:0/6 Commit:4
2->1 MsgAppResp Term:1 Log:0/6 Commit:5
> 1 handling Ready
Ready MustSync=false:
Messages:
1->2 MsgApp Term:1 Log:1/6 Commit:5

# When n3 responds, quorum is reached and everything falls into place.
stabilize
----
> 2 receiving messages
1->2 MsgApp Term:1 Log:1/6 Commit:5
> 3 receiving messages
1->3 MsgApp Term:1 Log:1/3 Commit:3 Entries:[1/4 EntryConfChange r1]
1->3 MsgApp Term:1 Log:1/4 Commit:3 Entries:[1/5 EntryNormal "foo"]
1->3 MsgApp Term:1 Log:1/5 Commit:3 Entries:[1/6 EntryNormal "bar"]
1->3 MsgApp Term:1 Log:1/6 Commit:4
1->3 MsgApp Term:1 Log:1/6 Commit:5
> 2 handling Ready
Ready MustSync=false:
Messages:
2->1 MsgAppResp Term:1 Log:0/6 Commit:5
> 3 handling Ready
Ready MustSync=true:
HardState Term:1 Vote:1 Commit:5
Expand All @@ -169,6 +179,7 @@ stabilize
3->1 MsgAppResp Term:1 Log:0/6 Commit:5
INFO 3 switched to configuration voters=(2 3)
> 1 receiving messages
2->1 MsgAppResp Term:1 Log:0/6 Commit:5
3->1 MsgAppResp Term:1 Log:0/4 Commit:3
3->1 MsgAppResp Term:1 Log:0/5 Commit:3
3->1 MsgAppResp Term:1 Log:0/6 Commit:3
Expand All @@ -180,11 +191,15 @@ stabilize
CommittedEntries:
1/6 EntryNormal "bar"
Messages:
1->3 MsgApp Term:1 Log:1/6 Commit:5
1->3 MsgApp Term:1 Log:1/6 Commit:5
1->2 MsgApp Term:1 Log:1/6 Commit:6
1->3 MsgApp Term:1 Log:1/6 Commit:6
> 2 receiving messages
1->2 MsgApp Term:1 Log:1/6 Commit:6
> 3 receiving messages
1->3 MsgApp Term:1 Log:1/6 Commit:5
1->3 MsgApp Term:1 Log:1/6 Commit:5
1->3 MsgApp Term:1 Log:1/6 Commit:6
> 2 handling Ready
Ready MustSync=false:
Expand All @@ -199,9 +214,13 @@ stabilize
CommittedEntries:
1/6 EntryNormal "bar"
Messages:
3->1 MsgAppResp Term:1 Log:0/6 Commit:5
3->1 MsgAppResp Term:1 Log:0/6 Commit:5
3->1 MsgAppResp Term:1 Log:0/6 Commit:6
> 1 receiving messages
2->1 MsgAppResp Term:1 Log:0/6 Commit:6
3->1 MsgAppResp Term:1 Log:0/6 Commit:5
3->1 MsgAppResp Term:1 Log:0/6 Commit:5
3->1 MsgAppResp Term:1 Log:0/6 Commit:6

# However not all is well. n1 is still leader but unconditionally drops all
Expand Down
19 changes: 19 additions & 0 deletions testdata/confchange_v2_add_double_auto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ stabilize 1
Messages:
1->2 MsgApp Term:1 Log:1/8 Commit:6
1->3 MsgApp Term:1 Log:1/8 Commit:6
1->3 MsgApp Term:1 Log:1/8 Commit:6
INFO 1 switched to configuration voters=(1)&&(1 2 3) autoleave
INFO initiating automatic transition out of joint configuration voters=(1)&&(1 2 3) autoleave
> 1 handling Ready
Expand All @@ -304,6 +305,7 @@ stabilize 2 3
1->3 MsgApp Term:1 Log:1/6 Commit:5 Entries:[1/7 EntryNormal "foo"]
1->3 MsgApp Term:1 Log:1/7 Commit:5 Entries:[1/8 EntryNormal "bar"]
1->3 MsgApp Term:1 Log:1/8 Commit:6
1->3 MsgApp Term:1 Log:1/8 Commit:6
1->3 MsgApp Term:1 Log:1/8 Commit:6 Entries:[1/9 EntryConfChangeV2]
> 2 handling Ready
Ready MustSync=true:
Expand Down Expand Up @@ -333,6 +335,7 @@ stabilize 2 3
3->1 MsgAppResp Term:1 Log:0/7 Commit:5
3->1 MsgAppResp Term:1 Log:0/8 Commit:5
3->1 MsgAppResp Term:1 Log:0/8 Commit:6
3->1 MsgAppResp Term:1 Log:0/8 Commit:6
3->1 MsgAppResp Term:1 Log:0/9 Commit:6
INFO 3 switched to configuration voters=(1)&&(1 2 3) autoleave

Expand All @@ -349,6 +352,7 @@ stabilize
3->1 MsgAppResp Term:1 Log:0/7 Commit:5
3->1 MsgAppResp Term:1 Log:0/8 Commit:5
3->1 MsgAppResp Term:1 Log:0/8 Commit:6
3->1 MsgAppResp Term:1 Log:0/8 Commit:6
3->1 MsgAppResp Term:1 Log:0/9 Commit:6
> 1 handling Ready
Ready MustSync=false:
Expand All @@ -364,6 +368,9 @@ stabilize
1->3 MsgApp Term:1 Log:1/9 Commit:8
1->2 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
INFO 1 switched to configuration voters=(1)
> 2 receiving messages
1->2 MsgApp Term:1 Log:1/9 Commit:7
Expand All @@ -373,6 +380,9 @@ stabilize
1->3 MsgApp Term:1 Log:1/9 Commit:7
1->3 MsgApp Term:1 Log:1/9 Commit:8
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
1->3 MsgApp Term:1 Log:1/9 Commit:9
> 2 handling Ready
Ready MustSync=false:
HardState Term:1 Commit:9
Expand All @@ -396,6 +406,9 @@ stabilize
3->1 MsgAppResp Term:1 Log:0/9 Commit:7
3->1 MsgAppResp Term:1 Log:0/9 Commit:8
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
INFO 3 switched to configuration voters=(1)
> 1 receiving messages
2->1 MsgAppResp Term:1 Log:0/9 Commit:7
Expand All @@ -410,3 +423,9 @@ stabilize
raft: cannot step as peer not found
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
raft: cannot step as peer not found
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
raft: cannot step as peer not found
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
raft: cannot step as peer not found
3->1 MsgAppResp Term:1 Log:0/9 Commit:9
raft: cannot step as peer not found
Loading

0 comments on commit 303bd94

Please sign in to comment.