Skip to content

Commit 7833656

Browse files
craig[bot]arulajmani
andcommitted
Merge #155408
155408: kvserver: refactors to enable replica lifecycle testing r=pav-kv a=arulajmani See individual commits. Co-authored-by: Arul Ajmani <arulajmani@gmail.com>
2 parents 2c1b868 + 9cdf925 commit 7833656

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

pkg/kv/kvserver/batcheval/cmd_end_transaction.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -930,11 +930,11 @@ func RunCommitTrigger(
930930
ctx, errors.Wrap(err, "unable to load replica version"),
931931
)
932932
}
933-
in := splitTriggerHelperInput{
934-
leftLease: lhsLease,
935-
gcThreshold: gcThreshold,
936-
gcHint: gcHint,
937-
replicaVersion: replicaVersion,
933+
in := SplitTriggerHelperInput{
934+
LeftLease: lhsLease,
935+
GCThreshold: gcThreshold,
936+
GCHint: gcHint,
937+
ReplicaVersion: replicaVersion,
938938
}
939939

940940
newMS, res, err := splitTrigger(
@@ -1164,7 +1164,7 @@ func splitTrigger(
11641164
batch storage.Batch,
11651165
bothDeltaMS enginepb.MVCCStats,
11661166
split *roachpb.SplitTrigger,
1167-
in splitTriggerHelperInput,
1167+
in SplitTriggerHelperInput,
11681168
ts hlc.Timestamp,
11691169
) (enginepb.MVCCStats, result.Result, error) {
11701170
desc := rec.Desc()
@@ -1243,6 +1243,20 @@ func splitTrigger(
12431243
return splitTriggerHelper(ctx, rec, batch, in, h, split, ts)
12441244
}
12451245

1246+
// TestingSplitTrigger is a wrapper around splitTrigger that is exported for
1247+
// testing purposes.
1248+
func TestingSplitTrigger(
1249+
ctx context.Context,
1250+
rec EvalContext,
1251+
batch storage.Batch,
1252+
bothDeltaMS enginepb.MVCCStats,
1253+
split *roachpb.SplitTrigger,
1254+
in SplitTriggerHelperInput,
1255+
ts hlc.Timestamp,
1256+
) (enginepb.MVCCStats, result.Result, error) {
1257+
return splitTrigger(ctx, rec, batch, bothDeltaMS, split, in, ts)
1258+
}
1259+
12461260
// splitScansRightForStatsFirst controls whether the left hand side or the right
12471261
// hand side of the split is scanned first on the leaseholder when evaluating
12481262
// the split trigger. In practice, the splitQueue wants to scan the left hand
@@ -1276,13 +1290,13 @@ func makeScanStatsFn(
12761290
}
12771291
}
12781292

1279-
// splitTriggerHelperInput contains metadata needed by the RHS when running the
1293+
// SplitTriggerHelperInput contains metadata needed by the RHS when running the
12801294
// splitTriggerHelper.
1281-
type splitTriggerHelperInput struct {
1282-
leftLease roachpb.Lease
1283-
gcThreshold *hlc.Timestamp
1284-
gcHint *roachpb.GCHint
1285-
replicaVersion roachpb.Version
1295+
type SplitTriggerHelperInput struct {
1296+
LeftLease roachpb.Lease
1297+
GCThreshold *hlc.Timestamp
1298+
GCHint *roachpb.GCHint
1299+
ReplicaVersion roachpb.Version
12861300
}
12871301

12881302
// splitTriggerHelper continues the work begun by splitTrigger, but has a
@@ -1292,7 +1306,7 @@ func splitTriggerHelper(
12921306
ctx context.Context,
12931307
rec EvalContext,
12941308
batch storage.Batch,
1295-
in splitTriggerHelperInput,
1309+
in SplitTriggerHelperInput,
12961310
statsInput splitStatsHelperInput,
12971311
split *roachpb.SplitTrigger,
12981312
ts hlc.Timestamp,
@@ -1434,22 +1448,22 @@ func splitTriggerHelper(
14341448
// - node two becomes the lease holder for [c,e). Its timestamp cache does
14351449
// not know about the read at 'd' which happened at the beginning.
14361450
// - node two can illegally propose a write to 'd' at a lower timestamp.
1437-
if in.leftLease.Empty() {
1451+
if in.LeftLease.Empty() {
14381452
log.KvExec.Fatalf(ctx, "LHS of split has no lease")
14391453
}
14401454

14411455
// Copy the lease from the left-hand side of the split over to the
14421456
// right-hand side so that it can immediately start serving requests.
14431457
// When doing so, we need to make a few modifications.
1444-
rightLease := in.leftLease
1458+
rightLease := in.LeftLease
14451459
// Rebind the lease to the existing leaseholder store's replica from the
14461460
// right-hand side's descriptor.
14471461
var ok bool
1448-
rightLease.Replica, ok = split.RightDesc.GetReplicaDescriptor(in.leftLease.Replica.StoreID)
1462+
rightLease.Replica, ok = split.RightDesc.GetReplicaDescriptor(in.LeftLease.Replica.StoreID)
14491463
if !ok {
14501464
return enginepb.MVCCStats{}, result.Result{}, errors.Errorf(
14511465
"pre-split lease holder %+v not found in post-split descriptor %+v",
1452-
in.leftLease.Replica, split.RightDesc,
1466+
in.LeftLease.Replica, split.RightDesc,
14531467
)
14541468
}
14551469
// Convert leader leases into expiration-based leases. A leader lease is
@@ -1464,7 +1478,7 @@ func splitTriggerHelper(
14641478
rightLease.Term = 0
14651479
rightLease.MinExpiration = hlc.Timestamp{}
14661480
}
1467-
if in.gcThreshold.IsEmpty() {
1481+
if in.GCThreshold.IsEmpty() {
14681482
log.VEventf(ctx, 1, "LHS's GCThreshold of split is not set")
14691483
}
14701484

@@ -1499,7 +1513,7 @@ func splitTriggerHelper(
14991513
// only.
15001514
if *h.AbsPostSplitRight(), err = stateloader.WriteInitialReplicaState(
15011515
ctx, batch, *h.AbsPostSplitRight(), split.RightDesc, rightLease,
1502-
*in.gcThreshold, *in.gcHint, in.replicaVersion,
1516+
*in.GCThreshold, *in.GCHint, in.ReplicaVersion,
15031517
); err != nil {
15041518
return enginepb.MVCCStats{}, result.Result{}, errors.Wrap(err, "unable to write initial Replica state")
15051519
}

pkg/kv/kvserver/batcheval/cmd_end_transaction_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,11 +2203,11 @@ func TestSplitTriggerWritesInitialReplicaState(t *testing.T) {
22032203
err = sl.SetVersion(ctx, batch, nil, &version)
22042204
require.NoError(t, err)
22052205

2206-
in := splitTriggerHelperInput{
2207-
leftLease: lease,
2208-
gcThreshold: &gcThreshold,
2209-
gcHint: &gcHint,
2210-
replicaVersion: version,
2206+
in := SplitTriggerHelperInput{
2207+
LeftLease: lease,
2208+
GCThreshold: &gcThreshold,
2209+
GCHint: &gcHint,
2210+
ReplicaVersion: version,
22112211
}
22122212

22132213
// Run the split trigger, which is normally run as a subset of EndTxn request

pkg/kv/kvserver/kvstorage/replica_state.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,18 @@ const CreateUninitReplicaTODO = 0
120120
// Returns kvpb.RaftGroupDeletedError if this replica can not be created
121121
// because it has been deleted.
122122
func CreateUninitializedReplica(
123-
ctx context.Context, eng storage.Engine, storeID roachpb.StoreID, id roachpb.FullReplicaID,
123+
ctx context.Context,
124+
reader storage.Reader,
125+
writer storage.Writer,
126+
storeID roachpb.StoreID,
127+
id roachpb.FullReplicaID,
124128
) error {
125129
sl := stateloader.Make(id.RangeID)
126130
// Before creating the replica, see if there is a tombstone which would
127131
// indicate that this replica has been removed.
128132
// TODO(pav-kv): should also check that there is no existing replica, i.e.
129133
// ReplicaID load should find nothing.
130-
if ts, err := sl.LoadRangeTombstone(ctx, eng); err != nil {
134+
if ts, err := sl.LoadRangeTombstone(ctx, reader); err != nil {
131135
return err
132136
} else if id.ReplicaID < ts.NextReplicaID {
133137
return &kvpb.RaftGroupDeletedError{}
@@ -140,12 +144,12 @@ func CreateUninitializedReplica(
140144
// non-existent. The only RangeID-specific key that can be present is the
141145
// RangeTombstone inspected above.
142146
_ = CreateUninitReplicaTODO
143-
if err := sl.SetRaftReplicaID(ctx, eng, id.ReplicaID); err != nil {
147+
if err := sl.SetRaftReplicaID(ctx, writer, id.ReplicaID); err != nil {
144148
return err
145149
}
146150

147151
// Make sure that storage invariants for this uninitialized replica hold.
148152
uninitDesc := roachpb.RangeDescriptor{RangeID: id.RangeID}
149-
_, err := LoadReplicaState(ctx, eng, storeID, &uninitDesc, id.ReplicaID)
153+
_, err := LoadReplicaState(ctx, reader, storeID, &uninitDesc, id.ReplicaID)
150154
return err
151155
}

pkg/kv/kvserver/store_create_replica.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func (s *Store) tryGetOrCreateReplica(
187187
// TODO(sep-raft-log): needs both engines due to tombstone (which lives on
188188
// statemachine).
189189
if err := kvstorage.CreateUninitializedReplica(
190-
ctx, s.TODOEngine(), s.StoreID(), id,
190+
ctx, s.TODOEngine(), s.TODOEngine(), s.StoreID(), id,
191191
); err != nil {
192192
return nil, false, err
193193
}

0 commit comments

Comments
 (0)