Skip to content

Commit 6458da5

Browse files
committed
kvserver: use SubsumeReplica when merging
Epic: none Release note: none
1 parent bff8e02 commit 6458da5

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

pkg/kv/kvserver/kvstorage/destroy.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,28 @@ func DestroyReplica(
179179
// (and the corresponding system and lock table keys). The latter are inherited
180180
// by the subsuming range.
181181
//
182+
// The forceSortedKeys flag, if true, forces the writes to be generated in the
183+
// sorted order of keys. This is to support feeding the writes from this
184+
// function to SSTables, in the snapshot ingestion path.
185+
//
182186
// Returns SelectOpts which can be used to reflect on the key spans that this
183187
// function clears.
184188
// TODO(pav-kv): get rid of SelectOpts.
185189
func SubsumeReplica(
186-
ctx context.Context, reader storage.Reader, writer storage.Writer, info DestroyReplicaInfo,
190+
ctx context.Context,
191+
reader storage.Reader,
192+
writer storage.Writer,
193+
info DestroyReplicaInfo,
194+
forceSortedKeys bool,
187195
) (rditer.SelectOpts, error) {
188-
// NB: set MustUseClearRange to true because this call is used for generating
189-
// SSTables when ingesting a snapshot, which requires Clears and Puts to be
190-
// written in key order. DestroyReplica sets RangeTombstoneKey after clearing
191-
// the unreplicated span which may contain higher keys.
196+
// NB: if required, set MustUseClearRange to true. This call can be used for
197+
// generating SSTables when ingesting a snapshot, which requires Clears and
198+
// Puts to be written in key order. DestroyReplica sets RangeTombstoneKey
199+
// after clearing the unreplicated span which may contain higher keys.
192200
opts := ClearRangeDataOptions{
193201
ClearReplicatedByRangeID: true,
194202
ClearUnreplicatedByRangeID: true,
195-
MustUseClearRange: true,
203+
MustUseClearRange: forceSortedKeys,
196204
}
197205
return rditer.SelectOpts{
198206
ReplicatedByRangeID: opts.ClearReplicatedByRangeID,

pkg/kv/kvserver/replica_app_batch.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,21 +359,10 @@ func (b *replicaAppBatch) runPostAddTriggersReplicaOnly(
359359
rhsRepl.mu.Unlock()
360360
rhsRepl.readOnlyCmdMu.Unlock()
361361

362-
// Use math.MaxInt32 (MergedTombstoneReplicaID) as the nextReplicaID as an
363-
// extra safeguard against creating new replicas of the RHS. This isn't
364-
// required for correctness, since the merge protocol should guarantee that
365-
// no new replicas of the RHS can ever be created, but it doesn't hurt to
366-
// be careful.
367-
if err := kvstorage.DestroyReplica(
368-
ctx, b.batch, b.batch,
369-
rhsRepl.destroyInfoRaftMuLocked(),
370-
kvstorage.MergedTombstoneReplicaID,
371-
kvstorage.ClearRangeDataOptions{
372-
ClearReplicatedByRangeID: true,
373-
ClearUnreplicatedByRangeID: true,
374-
},
362+
if _, err := kvstorage.SubsumeReplica(
363+
ctx, b.batch, b.batch, rhsRepl.destroyInfoRaftMuLocked(), false, /* forceSortedKeys */
375364
); err != nil {
376-
return errors.Wrapf(err, "unable to destroy replica before merge")
365+
return errors.Wrapf(err, "unable to subsume replica before merge")
377366
}
378367

379368
// Shut down rangefeed processors on either side of the merge.

pkg/kv/kvserver/snapshot_apply_prepare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (s *snapWriteBuilder) clearSubsumedReplicaDiskData(ctx context.Context) err
150150
for _, sub := range s.subsume {
151151
// We have to create an SST for the subsumed replica's range-id local keys.
152152
if err := s.writeSST(ctx, func(ctx context.Context, w storage.Writer) error {
153-
opts, err := kvstorage.SubsumeReplica(ctx, reader, w, sub)
153+
opts, err := kvstorage.SubsumeReplica(ctx, reader, w, sub, true /* forceSortedKeys */)
154154
s.cleared = append(s.cleared, rditer.Select(sub.RangeID, opts)...)
155155
return err
156156
}); err != nil {

0 commit comments

Comments
 (0)