Skip to content

Commit b6b8560

Browse files
craig[bot]pav-kv
andcommitted
Merge #147267
147267: kvserver: plan for replica creation and destruction r=tbg a=pav-kv Epic: CRDB-49111 Co-authored-by: Pavel Kalinnikov <[email protected]>
2 parents 941632b + 7e1fd22 commit b6b8560

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

pkg/kv/kvserver/kvstorage/destroy.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ func ClearRangeData(
9191
return nil
9292
}
9393

94+
// DestroyReplicaTODO is the plan for splitting DestroyReplica into cross-engine
95+
// writes.
96+
//
97+
// 1. Log storage write (durable):
98+
// 1.1. Write WAG node with the state machine mutation (2).
99+
// 2. State machine mutation:
100+
// 2.1. Clear RangeID-local un-/replicated state.
101+
// 2.2. (optional) Clear replicated MVCC span.
102+
// 2.3. Write RangeTombstone with next ReplicaID/LogID.
103+
// 3. Log engine GC (after state machine mutation 2 is durably applied):
104+
// 3.1. Remove previous LogID.
105+
//
106+
// TODO(sep-raft-log): support the status quo in which 1+2+3 is written
107+
// atomically, and 1.1 is not written.
108+
const DestroyReplicaTODO = 0
109+
94110
// DestroyReplica destroys all or a part of the Replica's state, installing a
95111
// RangeTombstone in its place. Due to merges, splits, etc, there is a need
96112
// to control which part of the state this method actually gets to remove,
@@ -114,6 +130,7 @@ func DestroyReplica(
114130
if diskReplicaID.ReplicaID >= nextReplicaID {
115131
return errors.AssertionFailedf("replica r%d/%d must not survive its own tombstone", rangeID, diskReplicaID)
116132
}
133+
_ = DestroyReplicaTODO // 2.1 + 3.1 + 2.2
117134
if err := ClearRangeData(ctx, rangeID, reader, writer, opts); err != nil {
118135
return err
119136
}
@@ -129,6 +146,7 @@ func DestroyReplica(
129146
return errors.AssertionFailedf(
130147
"cannot rewind tombstone from %d to %d", ts.NextReplicaID, nextReplicaID)
131148
}
149+
_ = DestroyReplicaTODO // 2.3
132150
return sl.SetRangeTombstone(ctx, writer, kvserverpb.RangeTombstone{
133151
NextReplicaID: nextReplicaID, // NB: nextReplicaID > 0
134152
})

pkg/kv/kvserver/kvstorage/replica_state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ func (r LoadedReplicaState) check(storeID roachpb.StoreID) error {
101101
return nil
102102
}
103103

104+
// CreateUninitReplicaTODO is the plan for splitting CreateUninitializedReplica
105+
// into cross-engine writes.
106+
//
107+
// 1. Log storage write (durable):
108+
// 1.1. Write WAG node with the state machine mutation (2).
109+
// 2. State machine mutation:
110+
// 2.1. Write RaftReplicaID with the new ReplicaID/LogID.
111+
//
112+
// TODO(sep-raft-log): support the status quo in which only 2.1 is written.
113+
const CreateUninitReplicaTODO = 0
114+
104115
// CreateUninitializedReplica creates an uninitialized replica in storage.
105116
// Returns kvpb.RaftGroupDeletedError if this replica can not be created
106117
// because it has been deleted.
@@ -134,6 +145,7 @@ func CreateUninitializedReplica(
134145
// the Term and Vote values for that older replica in the context of
135146
// this newer replica is harmless since it just limits the votes for
136147
// this replica.
148+
_ = CreateUninitReplicaTODO
137149
if err := sl.SetRaftReplicaID(ctx, eng, replicaID); err != nil {
138150
return err
139151
}

pkg/kv/kvserver/replica_raftstorage.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,28 @@ func (r *Replica) updateRangeInfo(ctx context.Context, desc *roachpb.RangeDescri
414414
return nil
415415
}
416416

417+
// applySnapshotTODO is the plan for splitting snapshot application into
418+
// cross-engine writes.
419+
//
420+
// 1. Log engine write (durable):
421+
// 1.1. HardState, RaftTruncatedState for new LogID. Log is empty.
422+
// 1.2. WAG node with the state machine mutation (2).
423+
//
424+
// 2. State machine mutation:
425+
// 2.1. For subsumed, clear RangeID-local un-/replicated state.
426+
// 2.2. For subsumed, write RangeTombstone with max NextReplicaID / LogID.
427+
// 2.3. Clear MVCC keyspace for (this + subsumed).
428+
// 2.4. Ingest snapshot SSTs.
429+
// 2.5. Update RaftReplicaID with the new LogID.
430+
//
431+
// 3. Log engine GC (after state machine mutation 2 is durably applied):
432+
// 3.1. Remove previous LogID.
433+
// 3.2. For each subsumed, remove the last LogID.
434+
//
435+
// TODO(sep-raft-log): support the status quo in which 1+2+3 is written
436+
// atomically, and 1.2 is not written.
437+
const applySnapshotTODO = 0
438+
417439
// applySnapshotRaftMuLocked updates the replica and its store based on the given
418440
// (non-empty) snapshot and associated HardState. All snapshots must pass
419441
// through Raft for correctness, i.e. the parameters to this method must be
@@ -577,6 +599,7 @@ func (r *Replica) applySnapshotRaftMuLocked(
577599
subsumedDescs: subsumedDescs,
578600
}
579601

602+
_ = applySnapshotTODO
580603
clearedUnreplicatedSpan, clearedSubsumedSpans, err := prepareSnapApply(ctx, prepInput)
581604
if err != nil {
582605
return err
@@ -611,12 +634,14 @@ func (r *Replica) applySnapshotRaftMuLocked(
611634
var ingestStats pebble.IngestOperationStats
612635
var writeBytes uint64
613636
if applyAsIngest {
637+
_ = applySnapshotTODO // all atomic
614638
exciseSpan := desc.KeySpan().AsRawSpanWithNoLocals()
615639
if ingestStats, err = r.store.TODOEngine().IngestAndExciseFiles(ctx, inSnap.SSTStorageScratch.SSTs(), inSnap.sharedSSTs, inSnap.externalSSTs, exciseSpan); err != nil {
616640
return errors.Wrapf(err, "while ingesting %s and excising %s-%s",
617641
inSnap.SSTStorageScratch.SSTs(), exciseSpan.Key, exciseSpan.EndKey)
618642
}
619643
} else {
644+
_ = applySnapshotTODO // all atomic
620645
err := r.store.TODOEngine().ConvertFilesToBatchAndCommit(
621646
ctx, inSnap.SSTStorageScratch.SSTs(), clearedSpans)
622647
if err != nil {

pkg/kv/kvserver/snapshot_apply_prepare.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,19 @@ func prepareSnapApply(
4444
[]roachpb.Span, // clearedSubsumedSpans
4545
error,
4646
) {
47-
// Step 1: Write unreplicated SST
47+
_ = applySnapshotTODO // 3.1 + 1.1 + 2.5.
4848
unreplicatedSSTFile, clearedUnreplicatedSpan, err := writeUnreplicatedSST(
4949
ctx, input.id, input.st, input.truncState, input.hardState, input.sl,
5050
)
5151
if err != nil {
5252
return roachpb.Span{}, nil, err
5353
}
54+
_ = applySnapshotTODO // add to 2.4.
5455
if err := input.writeSST(ctx, unreplicatedSSTFile.Data()); err != nil {
5556
return roachpb.Span{}, nil, err
5657
}
5758

59+
_ = applySnapshotTODO // 3.2 + 2.1 + 2.2 + 2.3
5860
clearedSubsumedSpans, err := clearSubsumedReplicaDiskData(
5961
ctx, input.st, input.todoEng, input.writeSST,
6062
input.desc, input.subsumedDescs,

pkg/kv/kvserver/store_create_replica.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func (s *Store) tryGetOrCreateReplica(
203203
// be accessed by someone holding a reference to, or currently creating a
204204
// Replica for this rangeID, and that's us.
205205

206+
_ = kvstorage.CreateUninitReplicaTODO
206207
if err := kvstorage.CreateUninitializedReplica(
207208
// TODO(sep-raft-log): needs both engines due to tombstone (which lives on
208209
// statemachine).

0 commit comments

Comments
 (0)