Skip to content

Commit 19155eb

Browse files
committed
kvserver: simplify clearSubsumedReplicaDiskData
It may be a little easier on the eyes (and brain) now.
1 parent 439431f commit 19155eb

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

pkg/kv/kvserver/snapshot_apply_prepare.go

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ func rewriteRaftState(
147147
// method requires that each of the subsumed replicas raftMu is held, and that
148148
// the Reader reflects the latest I/O each of the subsumed replicas has done
149149
// (i.e. Reader was instantiated after all raftMu were acquired).
150+
//
151+
// NB: does nothing if subsumedDescs is empty.
150152
func clearSubsumedReplicaDiskData(
151153
ctx context.Context,
152154
st *cluster.Settings,
@@ -236,35 +238,56 @@ func clearSubsumedReplicaDiskData(
236238
keySpans[i], totalKeySpans[i])
237239
}
238240

239-
if totalKeySpans[i].EndKey.Compare(keySpans[i].EndKey) > 0 {
240-
subsumedReplSSTFile := &storage.MemObject{}
241-
subsumedReplSST := storage.MakeIngestionSSTWriter(
242-
ctx, st, subsumedReplSSTFile,
243-
)
244-
if err := storage.ClearRangeWithHeuristic(
245-
ctx,
246-
reader,
247-
&subsumedReplSST,
248-
keySpans[i].EndKey,
249-
totalKeySpans[i].EndKey,
250-
kvstorage.ClearRangeThresholdPointKeys,
251-
kvstorage.ClearRangeThresholdRangeKeys,
252-
); err != nil {
253-
subsumedReplSST.Close()
254-
return nil, err
255-
}
256-
clearedSpans = append(clearedSpans,
257-
roachpb.Span{Key: keySpans[i].EndKey, EndKey: totalKeySpans[i].EndKey})
258-
if err := subsumedReplSST.Finish(); err != nil {
241+
// In the comments below, s1, ..., sn are the end keys for the subsumed
242+
// replicas (for the current keySpan).
243+
// Note that if there aren't any subsumed replicas (the common case), the
244+
// next comparison is always zero and this loop is a no-op.
245+
246+
if totalKeySpans[i].EndKey.Compare(keySpans[i].EndKey) <= 0 {
247+
// The subsumed replicas are fully contained in the snapshot:
248+
//
249+
// [a---s1---...---sn)
250+
// [a---------------------b)
251+
//
252+
// We don't need to clear additional keyspace here, since clearing `[a,b)`
253+
// will also clear all subsumed replicas.
254+
continue
255+
}
256+
257+
// The subsumed replicas extend past the snapshot:
258+
//
259+
// [a----------------s1---...---sn)
260+
// [a---------------------b)
261+
//
262+
// We need to additionally clear [b,sn).
263+
264+
subsumedReplSSTFile := &storage.MemObject{}
265+
subsumedReplSST := storage.MakeIngestionSSTWriter(
266+
ctx, st, subsumedReplSSTFile,
267+
)
268+
if err := storage.ClearRangeWithHeuristic(
269+
ctx,
270+
reader,
271+
&subsumedReplSST,
272+
keySpans[i].EndKey,
273+
totalKeySpans[i].EndKey,
274+
kvstorage.ClearRangeThresholdPointKeys,
275+
kvstorage.ClearRangeThresholdRangeKeys,
276+
); err != nil {
277+
subsumedReplSST.Close()
278+
return nil, err
279+
}
280+
clearedSpans = append(clearedSpans,
281+
roachpb.Span{Key: keySpans[i].EndKey, EndKey: totalKeySpans[i].EndKey})
282+
if err := subsumedReplSST.Finish(); err != nil {
283+
return nil, err
284+
}
285+
if subsumedReplSST.DataSize > 0 {
286+
// TODO(itsbilal): Write to SST directly in subsumedReplSST rather than
287+
// buffering in a MemObject first.
288+
if err := writeSST(ctx, subsumedReplSSTFile.Data()); err != nil {
259289
return nil, err
260290
}
261-
if subsumedReplSST.DataSize > 0 {
262-
// TODO(itsbilal): Write to SST directly in subsumedReplSST rather than
263-
// buffering in a MemObject first.
264-
if err := writeSST(ctx, subsumedReplSSTFile.Data()); err != nil {
265-
return nil, err
266-
}
267-
}
268291
}
269292
}
270293
return clearedSpans, nil

0 commit comments

Comments
 (0)