@@ -82,35 +82,27 @@ type DestroyReplicaInfo struct {
8282// This call issues all writes ordered by key. This is to support a large
8383// variety of uses, including SSTable generation for snapshot application.
8484func DestroyReplica (
85- ctx context.Context ,
86- reader storage.Reader ,
87- writer storage.Writer ,
88- info DestroyReplicaInfo ,
89- next roachpb.ReplicaID ,
85+ ctx context.Context , rw ReadWriter , info DestroyReplicaInfo , next roachpb.ReplicaID ,
9086) error {
91- return destroyReplicaImpl (ctx , reader , writer , info , next )
87+ return destroyReplicaImpl (ctx , rw , info , next )
9288}
9389
9490func destroyReplicaImpl (
95- ctx context.Context ,
96- reader storage.Reader ,
97- writer storage.Writer ,
98- info DestroyReplicaInfo ,
99- next roachpb.ReplicaID ,
91+ ctx context.Context , rw ReadWriter , info DestroyReplicaInfo , next roachpb.ReplicaID ,
10092) error {
10193 if next <= info .ReplicaID {
10294 return errors .AssertionFailedf ("%v must not survive its own tombstone" , info .FullReplicaID )
10395 }
10496 sl := MakeStateLoader (info .RangeID )
10597 // Assert that the ReplicaID in storage matches the one being removed.
106- if loaded , err := sl .LoadRaftReplicaID (ctx , reader ); err != nil {
98+ if loaded , err := sl .LoadRaftReplicaID (ctx , rw . State . RO ); err != nil {
10799 return err
108100 } else if id := loaded .ReplicaID ; id != info .ReplicaID {
109101 return errors .AssertionFailedf ("%v has a mismatching ID %d" , info .FullReplicaID , id )
110102 }
111103 // Assert that the provided tombstone moves the existing one strictly forward.
112104 // A failure would indicate that something is wrong in the replica lifecycle.
113- if ts , err := sl .LoadRangeTombstone (ctx , reader ); err != nil {
105+ if ts , err := sl .LoadRangeTombstone (ctx , rw . State . RO ); err != nil {
114106 return err
115107 } else if next <= ts .NextReplicaID {
116108 return errors .AssertionFailedf ("%v cannot rewind tombstone from %d to %d" ,
@@ -136,40 +128,64 @@ func destroyReplicaImpl(
136128 // First, clear all RangeID-local replicated keys. Also, include all
137129 // RangeID-local unreplicated keys < RangeTombstoneKey as a drive-by, since we
138130 // decided that these (currently none) belong to the state machine engine.
139- tombstoneKey := keys .RangeTombstoneKey (info .RangeID )
131+ span := roachpb.Span {
132+ Key : keys .MakeRangeIDReplicatedPrefix (info .RangeID ),
133+ EndKey : keys .RangeTombstoneKey (info .RangeID ),
134+ }
140135 if err := storage .ClearRangeWithHeuristic (
141- ctx , reader , writer ,
142- keys .MakeRangeIDReplicatedPrefix (info .RangeID ),
143- tombstoneKey ,
144- ClearRangeThresholdPointKeys (),
136+ ctx , rw .State .RO , rw .State .WO ,
137+ span .Key , span .EndKey , ClearRangeThresholdPointKeys (),
145138 ); err != nil {
146139 return err
147140 }
148141 // Save a tombstone to ensure that replica IDs never get reused.
149- if err := sl .SetRangeTombstone (ctx , writer , kvserverpb.RangeTombstone {
142+ if err := sl .SetRangeTombstone (ctx , rw . State . WO , kvserverpb.RangeTombstone {
150143 NextReplicaID : next , // NB: NextReplicaID > 0
151144 }); err != nil {
152145 return err
153146 }
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.
147+
148+ // Clear the rest of the RangeID-local unreplicated keys. These are all raft
149+ // engine keys, except for RaftReplicaIDKey. Make a stop at the latter, and
150+ // clear it manually (note that it always exists for an existing replica, and
151+ // we have asserted that above).
152+ //
153+ // TODO(pav-kv): make a helper for piece-wise clearing, instead of using a
154+ // series of ClearRangeWithHeuristic.
155+ span = roachpb.Span {
156+ Key : span .EndKey .Next (), // RangeTombstoneKey.Next()
157+ EndKey : keys .RaftReplicaIDKey (info .RangeID ),
158+ }
159+ // TODO(#152845): with separated raft storage, clear only the unapplied suffix
160+ // of the raft log, which is in this span.
161+ if err := storage .ClearRangeWithHeuristic (
162+ ctx , rw .Raft .RO , rw .Raft .WO ,
163+ span .Key , span .EndKey , ClearRangeThresholdPointKeys (),
164+ ); err != nil {
165+ return err
166+ }
167+ if err := sl .ClearRaftReplicaID (rw .State .WO ); err != nil {
168+ return err
169+ }
170+ span = roachpb.Span {
171+ Key : span .EndKey .Next (), // RaftReplicaIDKey.Next()
172+ EndKey : keys .MakeRangeIDUnreplicatedPrefix (info .RangeID ).PrefixEnd (),
173+ }
158174 if err := storage .ClearRangeWithHeuristic (
159- ctx , reader , writer ,
160- tombstoneKey .Next (),
161- keys .MakeRangeIDUnreplicatedPrefix (info .RangeID ).PrefixEnd (),
162- ClearRangeThresholdPointKeys (),
175+ ctx , rw .Raft .RO , rw .Raft .WO ,
176+ span .Key , span .EndKey , ClearRangeThresholdPointKeys (),
163177 ); err != nil {
164178 return err
165179 }
180+
166181 // Finally, clear all the user keys (MVCC keyspace and the corresponding
167182 // system and lock table keys), if info.Keys is not empty.
168183 for _ , span := range rditer .Select (info .RangeID , rditer.SelectOpts {
169184 Ranged : rditer .SelectAllRanged (info .Keys ),
170185 }) {
171186 if err := storage .ClearRangeWithHeuristic (
172- ctx , reader , writer , span .Key , span .EndKey , ClearRangeThresholdPointKeys (),
187+ ctx , rw .State .RO , rw .State .WO ,
188+ span .Key , span .EndKey , ClearRangeThresholdPointKeys (),
173189 ); err != nil {
174190 return err
175191 }
@@ -185,14 +201,14 @@ func destroyReplicaImpl(
185201// function clears.
186202// TODO(pav-kv): get rid of SelectOpts.
187203func SubsumeReplica (
188- ctx context.Context , reader storage. Reader , writer storage. Writer , info DestroyReplicaInfo ,
204+ ctx context.Context , rw ReadWriter , info DestroyReplicaInfo ,
189205) (rditer.SelectOpts , error ) {
190206 // Forget about the user keys.
191207 info .Keys = roachpb.RSpan {}
192208 return rditer.SelectOpts {
193209 ReplicatedByRangeID : true ,
194210 UnreplicatedByRangeID : true ,
195- }, destroyReplicaImpl (ctx , reader , writer , info , MergedTombstoneReplicaID )
211+ }, destroyReplicaImpl (ctx , rw , info , MergedTombstoneReplicaID )
196212}
197213
198214// RemoveStaleRHSFromSplit removes all replicated data for the RHS replica of a
0 commit comments