99 "context"
1010 "math"
1111
12+ "github.com/cockroachdb/cockroach/pkg/keys"
1213 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
1314 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/rditer"
1415 "github.com/cockroachdb/cockroach/pkg/roachpb"
@@ -77,14 +78,17 @@ type DestroyReplicaInfo struct {
7778// DestroyReplica destroys the entirety of the replica's state in storage, and
7879// installs a RangeTombstone in its place. It handles both uninitialized and
7980// initialized replicas uniformly.
81+ //
82+ // This call issues all writes ordered by key. This is to support a large
83+ // variety of uses, including SSTable generation for snapshot application.
8084func DestroyReplica (
8185 ctx context.Context ,
8286 reader storage.Reader ,
8387 writer storage.Writer ,
8488 info DestroyReplicaInfo ,
8589 next roachpb.ReplicaID ,
8690) error {
87- return destroyReplicaImpl (ctx , reader , writer , info , next , false /* forceSortedKeys */ )
91+ return destroyReplicaImpl (ctx , reader , writer , info , next )
8892}
8993
9094func destroyReplicaImpl (
@@ -93,7 +97,6 @@ func destroyReplicaImpl(
9397 writer storage.Writer ,
9498 info DestroyReplicaInfo ,
9599 next roachpb.ReplicaID ,
96- forceSortedKeys bool ,
97100) error {
98101 if next <= info .ReplicaID {
99102 return errors .AssertionFailedf ("%v must not survive its own tombstone" , info .FullReplicaID )
@@ -114,62 +117,82 @@ func destroyReplicaImpl(
114117 info .FullReplicaID , ts .NextReplicaID , next )
115118 }
116119
117- _ = DestroyReplicaTODO // 2.1 + 2.2 + 3.1
118- // NB: if forceSortedKeys is true, disable the heuristic and force deletion by
119- // range keys. This call can be used for generating SSTables when ingesting a
120- // snapshot, which requires Clears and Puts to be written in key order. Since
121- // we write RangeTombstoneKey further down, ensure that this is the only point
122- // key here.
123- pointKeyThreshold := ClearRangeThresholdPointKeys ()
124- if forceSortedKeys {
125- pointKeyThreshold = 1
120+ _ = DestroyReplicaTODO
121+ // Clear all range data in sorted order of engine keys. This call can be used
122+ // for generating SSTables when ingesting a snapshot, which requires Clears
123+ // and Puts to be written in key order.
124+ //
125+ // All the code below is equivalent to clearing all the spans that the
126+ // following SelectOpts represents, and leaving only the RangeTombstoneKey
127+ // populated with the specified NextReplicaID.
128+ if buildutil .CrdbTestBuild {
129+ _ = rditer.SelectOpts {
130+ ReplicatedByRangeID : true ,
131+ UnreplicatedByRangeID : true ,
132+ Ranged : rditer .SelectAllRanged (info .Keys ),
133+ }
134+ }
135+
136+ // First, clear all RangeID-local replicated keys. Also, include all
137+ // RangeID-local unreplicated keys < RangeTombstoneKey as a drive-by, since we
138+ // decided that these (currently none) belong to the state machine engine.
139+ tombstoneKey := keys .RangeTombstoneKey (info .RangeID )
140+ if err := storage .ClearRangeWithHeuristic (
141+ ctx , reader , writer ,
142+ keys .MakeRangeIDReplicatedPrefix (info .RangeID ),
143+ tombstoneKey ,
144+ ClearRangeThresholdPointKeys (),
145+ ); err != nil {
146+ return err
147+ }
148+ // Save a tombstone to ensure that replica IDs never get reused.
149+ if err := sl .SetRangeTombstone (ctx , writer , kvserverpb.RangeTombstone {
150+ NextReplicaID : next , // NB: NextReplicaID > 0
151+ }); err != nil {
152+ return err
126153 }
127- // TODO(pav-kv): decompose this loop to delete raft and state machine keys
128- // separately. The main challenge is the unreplicated span because it is
129- // scattered across 2 engines.
154+ // Clear the rest of the RangeID-local unreplicated keys.
155+ // TODO(pav-kv): decompose this further to delete raft and state machine keys
156+ // separately. Currently, all the remaining keys in this span belong to the
157+ // raft engine except the RaftReplicaID.
158+ if err := storage .ClearRangeWithHeuristic (
159+ ctx , reader , writer ,
160+ tombstoneKey .Next (),
161+ keys .MakeRangeIDUnreplicatedPrefix (info .RangeID ).PrefixEnd (),
162+ ClearRangeThresholdPointKeys (),
163+ ); err != nil {
164+ return err
165+ }
166+ // Finally, clear all the user keys (MVCC keyspace and the corresponding
167+ // system and lock table keys), if info.Keys is not empty.
130168 for _ , span := range rditer .Select (info .RangeID , rditer.SelectOpts {
131- UnreplicatedByRangeID : true ,
132- ReplicatedByRangeID : true ,
133- Ranged : rditer .SelectAllRanged (info .Keys ),
169+ Ranged : rditer .SelectAllRanged (info .Keys ),
134170 }) {
135171 if err := storage .ClearRangeWithHeuristic (
136- ctx , reader , writer , span .Key , span .EndKey , pointKeyThreshold ,
172+ ctx , reader , writer , span .Key , span .EndKey , ClearRangeThresholdPointKeys () ,
137173 ); err != nil {
138174 return err
139175 }
140176 }
141-
142- // Save a tombstone to ensure that replica IDs never get reused.
143- _ = DestroyReplicaTODO // 2.3
144- return sl .SetRangeTombstone (ctx , writer , kvserverpb.RangeTombstone {
145- NextReplicaID : next , // NB: NextReplicaID > 0
146- })
177+ return nil
147178}
148179
149180// SubsumeReplica is like DestroyReplica, but it does not delete the user keys
150181// (and the corresponding system and lock table keys). The latter are inherited
151182// by the subsuming range.
152183//
153- // The forceSortedKeys flag, if true, forces the writes to be generated in the
154- // sorted order of keys. This is to support feeding the writes from this
155- // function to SSTables, in the snapshot ingestion path.
156- //
157184// Returns SelectOpts which can be used to reflect on the key spans that this
158185// function clears.
159186// TODO(pav-kv): get rid of SelectOpts.
160187func SubsumeReplica (
161- ctx context.Context ,
162- reader storage.Reader ,
163- writer storage.Writer ,
164- info DestroyReplicaInfo ,
165- forceSortedKeys bool ,
188+ ctx context.Context , reader storage.Reader , writer storage.Writer , info DestroyReplicaInfo ,
166189) (rditer.SelectOpts , error ) {
167190 // Forget about the user keys.
168191 info .Keys = roachpb.RSpan {}
169192 return rditer.SelectOpts {
170193 ReplicatedByRangeID : true ,
171194 UnreplicatedByRangeID : true ,
172- }, destroyReplicaImpl (ctx , reader , writer , info , MergedTombstoneReplicaID , forceSortedKeys )
195+ }, destroyReplicaImpl (ctx , reader , writer , info , MergedTombstoneReplicaID )
173196}
174197
175198// RemoveStaleRHSFromSplit removes all data for the RHS replica of a split. This
0 commit comments