Skip to content

Commit

Permalink
Accept any snapshot that allows replication
Browse files Browse the repository at this point in the history
A leader will not take into account snapshots reported by a follower
unless they match or exceed the tracked PendingSnapshot index (which is
the leader's last indexat the time of requesting the snapshot). This is
too inflexible: the leader should take into account any snapshot that
reconnects the follower to its log. This PR makes that change.

In doing so, it addresses long-standing problems that we've encountered
in CockroachDB. Unless you create the snapshot immediately and locally
when raft emits an MsgSnap, it's difficult/impossible to later
synthesize a snapshot at the requested index. It is possible to get one
above the requested index which raft always accepted, but CockroachDB
delegates snapshots to followers who might be behind on applying the
log, and it is awkward to have to wait for log application to send the
snapshot just to satisfy an overly strict condition in raft.
Additionally, CockroachDB also sends snapshots preemptively when adding
a new replica since there are qualitative differences between an initial
snapshot and one needed to reconnect to the log and one does not want to
wait for raft to round-trip to the follower to realize that a snapshot
is needed. In this case, the sent snapshot is commonly behind the
PendingSnapshot since the leader transitions the follower into
StateProbe when a snapshot is already in flight.

Touches cockroachdb/cockroach#84242.
Touches cockroachdb/cockroach#87553.
Touches cockroachdb/cockroach#87554.
Touches cockroachdb/cockroach#97971.
Touches cockroachdb/cockroach#114349.
See also https://github.com/cockroachdb/cockroach/blob/2b91c3829270eb512c5380201c26a3d838fc567a/pkg/kv/kvserver/raft_snapshot_queue.go#L131-L143.

Signed-off-by: Erik Grinaker <[email protected]>
Signed-off-by: Tobias Grieger <[email protected]>
  • Loading branch information
erikgrinaker committed Jan 9, 2024
1 parent 5e2260f commit d87942f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
15 changes: 7 additions & 8 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,10 +1478,13 @@ func stepLeader(r *raft, m pb.Message) error {
switch {
case pr.State == tracker.StateProbe:
pr.BecomeReplicate()
case pr.State == tracker.StateSnapshot && pr.Match >= pr.PendingSnapshot:
// TODO(tbg): we should also enter this branch if a snapshot is
// received that is below pr.PendingSnapshot but which makes it
// possible to use the log again.
case pr.State == tracker.StateSnapshot && pr.Match+1 >= r.raftLog.firstIndex():
// Note that we don't take into account PendingSnapshot to
// enter this branch. No matter at which index a snapshot
// was actually applied, as long as this allows catching up
// the follower from the log, we will accept it. This gives
// systems more flexibility in how they implement snapshots;
// see the comments on PendingSnapshot.
r.logger.Debugf("%x recovered from needing snapshot, resumed sending replication messages to %x [%s]", r.id, m.From, pr)
// Transition back to replicating state via probing state
// (which takes the snapshot into account). If we didn't
Expand Down Expand Up @@ -1560,10 +1563,6 @@ func stepLeader(r *raft, m pb.Message) error {
if pr.State != tracker.StateSnapshot {
return nil
}
// TODO(tbg): this code is very similar to the snapshot handling in
// MsgAppResp above. In fact, the code there is more correct than the
// code here and should likely be updated to match (or even better, the
// logic pulled into a newly created Progress state machine handler).
if !m.Reject {
pr.BecomeProbe()
r.logger.Debugf("%x snapshot succeeded, resumed sending replication messages to %x [%s]", r.id, m.From, pr)
Expand Down
14 changes: 8 additions & 6 deletions testdata/snapshot_succeed_via_app_resp_behind.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# This is a variant of snapshot_succeed_via_app_resp in which the snapshot
# that is being sent is behind the PendingSnapshot index tracked by the leader.
#
# In this case, the leader keeps the follower in StateSnapshot even though it
# could move it back to StateReplicate.

# Turn off output during the setup of the test.
log-level none
Expand Down Expand Up @@ -151,11 +148,16 @@ stabilize 1
----
> 1 receiving messages
3->1 MsgAppResp Term:1 Log:0/11
DEBUG 1 recovered from needing snapshot, resumed sending replication messages to 3 [StateSnapshot match=11 next=12 paused pendingSnap=12]
> 1 handling Ready
Ready MustSync=false:
Messages:
1->3 MsgApp Term:1 Log:1/11 Commit:12 Entries:[1/12 EntryNormal "\"foo\""]

# 3 is still pending a snapshot, even though its match is 11 which
# the leader could catch the follower up from.
# 3 is in StateReplicate thanks to receiving the snapshot at index 11.
# This is despite its PendingSnapshot having been 12.
status 1
----
1: StateReplicate match=12 next=13
2: StateReplicate match=12 next=13
3: StateSnapshot match=11 next=12 paused pendingSnap=12
3: StateReplicate match=11 next=13 inflight=1
26 changes: 21 additions & 5 deletions tracker/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,27 @@ type Progress struct {
// before and stops sending any replication message.
State StateType

// PendingSnapshot is used in StateSnapshot.
// If there is a pending snapshot, the pendingSnapshot will be set to the
// index of the snapshot. If pendingSnapshot is set, the replication process of
// this Progress will be paused. raft will not resend snapshot until the pending one
// is reported to be failed.
// PendingSnapshot is used in StateSnapshot and tracks the last index of the
// leader at the time at which it realized a snapshot was necessary. This
// matches the index in the MsgSnap message emitted from raft.
//
// While there is a pending snapshot, replication to the follower is paused.
// The follower will transition back to StateReplicate if the leader
// receives an MsgAppResp from it that reconnects the follower to the
// leader's log (such an MsgAppResp is emitted when the follower applies a
// snapshot). It may be surprising that PendingSnapshot is not taken into
// account here, but consider that complex systems may delegate the sending
// of snapshots to alternative datasources (i.e. not the leader). In such
// setups, it is difficult to manufacture a snapshot at a particular index
// requested by raft and the actual index may be ahead or behind. This
// should be okay, as long as the snapshot allows replication to resume.
//
// The follower will transition to StateProbe if ReportSnapshot is called on
// the leader; if SnapshotFinish is passed then PendingSnapshot becomes the
// basis for the next attempt to append. In practice, the first mechanism is
// the one that is relevant in most cases. However, if this MsgAppResp is
// lost (fallible network) then the second mechanism ensures that in this
// case the follower does not erroneously remain in StateSnapshot.
PendingSnapshot uint64

// RecentActive is true if the progress is recently active. Receiving any messages
Expand Down

0 comments on commit d87942f

Please sign in to comment.