Skip to content

Commit fb795c4

Browse files
committed
kvserver: add destroy replica to TestReplicaLifecycleDataDriven
This adds a very basic version of ReplicaDestroy to the test suite. Epic: none Release note: None
1 parent 0c7c037 commit fb795c4

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

pkg/kv/kvserver/replica_lifecycle_datadriven_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ import (
8181
//
8282
// Executes the split trigger for the specified range on n1.
8383
//
84+
// destroy-replica range-id=<int>
85+
// ----
86+
//
87+
// Destroys the replica on n1 for the specified range. The replica's state
88+
// must have already been created via create-replica.
89+
//
8490
// print-range-state [sort-keys=<bool>]
8591
// ----
8692
//
@@ -149,7 +155,7 @@ func TestReplicaLifecycleDataDriven(t *testing.T) {
149155
if rs.replica != nil {
150156
return errors.New("initialized replica already exists on n1/s1").Error()
151157
}
152-
repl := rs.getReplicaDescriptor(t, roachpb.NodeID(1))
158+
repl := rs.mustGetReplicaDescriptor(t, roachpb.NodeID(1))
153159

154160
batch := tc.storage.NewBatch()
155161
defer batch.Close()
@@ -210,7 +216,8 @@ func TestReplicaLifecycleDataDriven(t *testing.T) {
210216
replicaNodeID := dd.ScanArg[roachpb.NodeID](t, d, "replica")
211217
leaseType := dd.ScanArgOr(t, d, "lease-type", "leader-lease")
212218
rs := tc.mustGetRangeState(t, rangeID)
213-
targetReplica := rs.getReplicaDescriptor(t, replicaNodeID)
219+
targetReplica := rs.mustGetReplicaDescriptor(t, replicaNodeID)
220+
214221
// NB: The details of the lease are not important to the test;
215222
// only the type is.
216223
var lease roachpb.Lease
@@ -283,6 +290,33 @@ func TestReplicaLifecycleDataDriven(t *testing.T) {
283290
// we manually split things out here.
284291
return strings.ReplaceAll(output, "\n\n", "\n")
285292

293+
case "destroy-replica":
294+
rangeID := dd.ScanArg[roachpb.RangeID](t, d, "range-id")
295+
rs := tc.mustGetRangeState(t, rangeID)
296+
rs.mustGetReplicaDescriptor(t, roachpb.NodeID(1)) // ensure replica exists
297+
298+
batch := tc.storage.NewBatch()
299+
defer batch.Close()
300+
301+
err := kvstorage.DestroyReplica(
302+
ctx,
303+
kvstorage.TODOReadWriter(batch),
304+
kvstorage.DestroyReplicaInfo{
305+
FullReplicaID: rs.replica.FullReplicaID,
306+
Keys: roachpb.RSpan{Key: rs.desc.StartKey, EndKey: rs.desc.EndKey},
307+
},
308+
rs.desc.NextReplicaID,
309+
)
310+
require.NoError(t, err)
311+
output, err := print.DecodeWriteBatch(batch.Repr())
312+
require.NoError(t, err)
313+
err = batch.Commit(true)
314+
require.NoError(t, err)
315+
316+
// Clear the replica from the range state.
317+
rs.replica = nil
318+
return output
319+
286320
case "print-range-state":
287321
var sb strings.Builder
288322
if len(tc.ranges) == 0 {
@@ -440,7 +474,7 @@ func (tc *testCtx) updatePostSplitRangeState(
440474
tc.ranges[split.RightDesc.RangeID] = rhsRangeState
441475
}
442476

443-
func (rs *rangeState) getReplicaDescriptor(
477+
func (rs *rangeState) mustGetReplicaDescriptor(
444478
t *testing.T, nodeID roachpb.NodeID,
445479
) *roachpb.ReplicaDescriptor {
446480
for i, repl := range rs.desc.InternalReplicas {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
create-descriptor start=a end=z replicas=(1,2,3)
2+
----
3+
created descriptor: r1:{a-z} [(n1,s1):1, (n2,s2):2, (n3,s3):3, next=4, gen=0]
4+
5+
create-replica range-id=1 initialized
6+
----
7+
created replica: (n1,s1):1
8+
Put: 0,0 /Local/RangeID/1/r/RangeLease (0x01698972726c6c2d00): <empty>
9+
Put: 0,0 /Local/RangeID/1/r/RangeGCThreshold (0x016989726c67632d00): 0,0
10+
Put: 0,0 /Local/RangeID/1/r/RangeGCHint (0x016989727267636800): latest_range_delete_timestamp:<> gc_timestamp:<> gc_timestamp_next:<>
11+
Put: 0,0 /Local/RangeID/1/r/RangeVersion (0x016989727276657200): 10.8-upgrading-step-007
12+
Put: 0,0 /Local/RangeID/1/r/RangeAppliedState (0x016989727261736b00): raft_applied_index:10 lease_applied_index:10 range_stats:<sys_bytes:142 sys_count:4 > raft_closed_timestamp:<> raft_applied_index_term:5
13+
Put: 0,0 /Local/RangeID/1/u/RaftReplicaID (0x016989757266747200): replica_id:1
14+
Put: 0,0 /Local/RangeID/1/u/RaftHardState (0x016989757266746800): term:5 vote:0 commit:10 lead:0 lead_epoch:0
15+
Put: 0,0 /Local/RangeID/1/u/RaftTruncatedState (0x016989757266747400): index:10 term:5
16+
17+
destroy-replica range-id=1
18+
----
19+
Delete (Sized at 28): 0,0 /Local/RangeID/1/r/RangeGCThreshold (0x016989726c67632d00):
20+
Delete (Sized at 43): 0,0 /Local/RangeID/1/r/RangeAppliedState (0x016989727261736b00):
21+
Delete (Sized at 34): 0,0 /Local/RangeID/1/r/RangeGCHint (0x016989727267636800):
22+
Delete (Sized at 44): 0,0 /Local/RangeID/1/r/RangeLease (0x01698972726c6c2d00):
23+
Delete (Sized at 36): 0,0 /Local/RangeID/1/r/RangeVersion (0x016989727276657200):
24+
Put: 0,0 /Local/RangeID/1/u/RangeTombstone (0x016989757266746200): next_replica_id:4
25+
Delete (Sized at 38): 0,0 /Local/RangeID/1/u/RaftHardState (0x016989757266746800):
26+
Delete: 0,0 /Local/RangeID/1/u/RaftReplicaID (0x016989757266747200):
27+
Delete (Sized at 32): 0,0 /Local/RangeID/1/u/RaftTruncatedState (0x016989757266747400):

0 commit comments

Comments
 (0)