Skip to content

Commit c0ae46f

Browse files
craig[bot]tbg
andcommitted
Merge #145328
145328: kvserver: extract unreplicated SST writing in applySnapshot r=tbg a=tbg The engine (pebble) interactions in applySnapshot are complex and difficult to test in isolation. By extracting these responsibilities into a standalone function, we make it easier to unit test and further refactor the snapshot application process. To this end, this PR introduces a new prepareSnapshot function in snapshot_apply_prepare.go, and moves the construction of the SSTs (and generally all engine interactions prior to the commit) there. Epic: CRDB-46488 Release note: None Co-authored-by: Tobias Grieger <[email protected]>
2 parents 6b5994c + 19155eb commit c0ae46f

File tree

9 files changed

+358
-270
lines changed

9 files changed

+358
-270
lines changed

pkg/kv/kvserver/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ go_library(
8585
"replicate_queue.go",
8686
"scanner.go",
8787
"scheduler.go",
88+
"snapshot_apply_prepare.go",
8889
"snapshot_settings.go",
8990
"split_delay_helper.go",
9091
"split_queue.go",

pkg/kv/kvserver/client_replica_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,7 @@ func TestClearRange(t *testing.T) {
30693069
// significantly more rare. This test uses a knob to disable the new protection
30703070
// so that it can create the scenario where a replica learns that it holds the
30713071
// lease through a snapshot. We'll want to keep the test and the corresponding
3072-
// logic in applySnapshot around until we can eliminate the scenario entirely.
3072+
// logic in applySnapshotRaftMuLocked around until we can eliminate the scenario entirely.
30733073
// See the commentary in github.com/cockroachdb/cockroach/issues/81561 about
30743074
// sending Raft logs in Raft snapshots for a discussion about why this may not
30753075
// be worth eliminating.

pkg/kv/kvserver/replica_app_batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func (b *replicaAppBatch) runPostAddTriggersReplicaOnly(
449449

450450
// Delete all of the Replica's data. We're going to delete the hard state too.
451451
// We've set the replica's in-mem status to reflect the pending destruction
452-
// above, and preDestroyRaftMuLocked will also add a range tombstone to the
452+
// above, and DestroyReplica will also add a range tombstone to the
453453
// batch, so that when we commit it, the removal is finalized.
454454
if err := kvstorage.DestroyReplica(ctx, b.r.RangeID, b.batch, b.batch, change.NextReplicaID(), kvstorage.ClearRangeDataOptions{
455455
ClearReplicatedBySpan: span,

pkg/kv/kvserver/replica_raft.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ func (r *Replica) handleRaftReadyRaftMuLocked(
11881188
defer releaseMergeLock()
11891189

11901190
stats.tSnapBegin = crtime.NowMono()
1191-
if err := r.applySnapshot(ctx, inSnap, snap, app.HardState, subsumedRepls); err != nil {
1191+
if err := r.applySnapshotRaftMuLocked(ctx, inSnap, snap, app.HardState, subsumedRepls); err != nil {
11921192
return stats, errors.Wrap(err, "while applying snapshot")
11931193
}
11941194
for _, msg := range app.Responses {
@@ -1206,7 +1206,7 @@ func (r *Replica) handleRaftReadyRaftMuLocked(
12061206
stats.tSnapEnd = crtime.NowMono()
12071207
stats.snap.applied = true
12081208

1209-
// The raft log state was updated in applySnapshot, but we also want to
1209+
// The raft log state was updated in applySnapshotRaftMuLocked, but we also want to
12101210
// reflect these changes in the state variable here.
12111211
// TODO(pav-kv): this is unnecessary. We only do it because there is an
12121212
// unconditional storing of this state below. Avoid doing it twice.

pkg/kv/kvserver/replica_raftlog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (r *replicaLogStorage) LogSnapshot() raft.LogStorageSnapshot {
195195
//
196196
// This would require auditing and integrating with the write paths. Today, this
197197
// type implements only reads, and writes are in various places like the
198-
// logstore.LogStore type, or applySnapshot.
198+
// logstore.LogStore type, or applySnapshotRaftMuLocked.
199199
type replicaRaftMuLogSnap replicaLogStorage
200200

201201
// Entries implements the raft.LogStorageSnapshot interface.

0 commit comments

Comments
 (0)