@@ -216,3 +216,40 @@ func SubsumeReplica(
216216 UnreplicatedByRangeID : opts .ClearUnreplicatedByRangeID ,
217217 }, destroyReplicaImpl (ctx , reader , writer , info , MergedTombstoneReplicaID , opts )
218218}
219+
220+ // RemoveStaleRHSFromSplit removes all data for the RHS replica of a split. This
221+ // is used in a situation when the RHS replica is already known to have been
222+ // removed from our store, so any pending writes that were supposed to
223+ // initialize the RHS replica should be dropped from the write batch.
224+ //
225+ // TODO(#152199): do not remove the unreplicated state which can belong to a
226+ // newer (uninitialized) replica.
227+ func RemoveStaleRHSFromSplit (
228+ ctx context.Context ,
229+ reader storage.Reader ,
230+ writer storage.Writer ,
231+ rangeID roachpb.RangeID ,
232+ keys roachpb.RSpan ,
233+ ) error {
234+ return ClearRangeData (ctx , rangeID , reader , writer , ClearRangeDataOptions {
235+ // Since the RHS replica is uninitalized, we know there isn't anything in
236+ // the two replicated spans below, before the current batch. Setting these
237+ // options will in effect only clear the writes to the RHS replicated state
238+ // staged in the batch.
239+ ClearReplicatedBySpan : keys ,
240+ ClearReplicatedByRangeID : true ,
241+ // TODO(tbg): we don't actually want to touch the raft state of the RHS
242+ // replica since it's absent or a more recent one than in the split. Now
243+ // that we have a bool targeting unreplicated RangeID-local keys, we can set
244+ // it to false and remove the HardState+ReplicaID write-back in the caller.
245+ // However, there can be historical split proposals with the
246+ // RaftTruncatedState key set in splitTriggerHelper[^1]. We must first make
247+ // sure that such proposals no longer exist, e.g. with a below-raft
248+ // migration.
249+ //
250+ // [^1]: https://github.com/cockroachdb/cockroach/blob/f263a765d750e41f2701da0a923a6e92d09159fa/pkg/kv/kvserver/batcheval/cmd_end_transaction.go#L1109-L1149
251+ //
252+ // See also: https://github.com/cockroachdb/cockroach/issues/94933
253+ ClearUnreplicatedByRangeID : true ,
254+ })
255+ }
0 commit comments