Skip to content

Commit

Permalink
Merge #57830
Browse files Browse the repository at this point in the history
57830: kvserver: simplify range state initialization r=irfansharif a=irfansharif

This area has seen a bunch of change as of late, now making room for
this tiny cleanup.

Release note: None

Co-authored-by: irfan sharif <[email protected]>
  • Loading branch information
craig[bot] and irfansharif committed Dec 17, 2020
2 parents f4ab896 + 0514fc9 commit 66f4f5d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 50 deletions.
10 changes: 1 addition & 9 deletions pkg/kv/kvserver/replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,7 @@ func (tc *testContext) StartWithStoreConfigAndVersion(
if realRange {
if tc.bootstrapMode == bootstrapRangeOnly {
testDesc := testRangeDescriptor()
if _, err := stateloader.WriteInitialState(
ctx,
tc.store.Engine(),
enginepb.MVCCStats{},
*testDesc,
roachpb.Lease{},
hlc.Timestamp{},
stateloader.TruncatedStateUnreplicated,
); err != nil {
if err := stateloader.WriteInitialRangeState(ctx, tc.store.Engine(), *testDesc); err != nil {
t.Fatal(err)
}
repl, err := newReplica(ctx, testDesc, tc.store, 1)
Expand Down
37 changes: 17 additions & 20 deletions pkg/kv/kvserver/stateloader/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (

// WriteInitialReplicaState sets up a new Range, but without writing an
// associated Raft state (which must be written separately via
// synthesizeRaftState before instantiating a Replica). The main task is to
// SynthesizeRaftState before instantiating a Replica). The main task is to
// persist a ReplicaState which does not start from zero but presupposes a few
// entries already having applied. The supplied MVCCStats are used for the Stats
// field after adjusting for persisting the state itself, and the updated stats
Expand Down Expand Up @@ -82,26 +82,23 @@ func WriteInitialReplicaState(
return newMS, nil
}

// WriteInitialState calls WriteInitialReplicaState followed by
// SynthesizeRaftState. It is typically called during bootstrap. The supplied
// MVCCStats are used for the Stats field after adjusting for persisting the
// state itself, and the updated stats are returned.
func WriteInitialState(
ctx context.Context,
readWriter storage.ReadWriter,
ms enginepb.MVCCStats,
desc roachpb.RangeDescriptor,
lease roachpb.Lease,
gcThreshold hlc.Timestamp,
truncStateType TruncatedStateType,
) (enginepb.MVCCStats, error) {
newMS, err := WriteInitialReplicaState(
ctx, readWriter, ms, desc, lease, gcThreshold, truncStateType)
if err != nil {
return enginepb.MVCCStats{}, err
// WriteInitialRangeState writes the initial range state. It's called during
// bootstrap.
func WriteInitialRangeState(
ctx context.Context, readWriter storage.ReadWriter, desc roachpb.RangeDescriptor,
) error {
initialLease := roachpb.Lease{}
initialGCThreshold := hlc.Timestamp{}
initialTruncStateType := TruncatedStateUnreplicated
initialMS := enginepb.MVCCStats{}

if _, err := WriteInitialReplicaState(
ctx, readWriter, initialMS, desc, initialLease, initialGCThreshold, initialTruncStateType,
); err != nil {
return err
}
if err := Make(desc.RangeID).SynthesizeRaftState(ctx, readWriter); err != nil {
return enginepb.MVCCStats{}, err
return err
}
return newMS, nil
return nil
}
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/stateloader/stateloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (rsl StateLoader) SetHardState(

// SynthesizeRaftState creates a Raft state which synthesizes both a HardState
// and a lastIndex from pre-seeded data in the engine (typically created via
// writeInitialReplicaState and, on a split, perhaps the activity of an
// WriteInitialReplicaState and, on a split, perhaps the activity of an
// uninitialized Raft group)
func (rsl StateLoader) SynthesizeRaftState(
ctx context.Context, readWriter storage.ReadWriter,
Expand Down
13 changes: 1 addition & 12 deletions pkg/kv/kvserver/store_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,9 @@ func WriteInitialClusterData(
}
}

truncStateType := stateloader.TruncatedStateUnreplicated
lease := roachpb.Lease{}
_, err := stateloader.WriteInitialState(
ctx, batch,
enginepb.MVCCStats{},
*desc,
lease,
hlc.Timestamp{}, /* gcThreshold */
truncStateType,
)
if err != nil {
if err := stateloader.WriteInitialRangeState(ctx, batch, *desc); err != nil {
return err
}

computedStats, err := rditer.ComputeStatsForRange(desc, batch, now.WallTime)
if err != nil {
return err
Expand Down
10 changes: 2 additions & 8 deletions pkg/kv/kvserver/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1424,10 +1424,7 @@ func splitTestRange(store *Store, key, splitKey roachpb.RKey, t *testing.T) *Rep
rangeID, splitKey, repl.Desc().EndKey, repl.Desc().Replicas())
// Minimal amount of work to keep this deprecated machinery working: Write
// some required Raft keys.
_, err = stateloader.WriteInitialState(
ctx, store.engine, enginepb.MVCCStats{}, *rhsDesc, roachpb.Lease{},
hlc.Timestamp{}, stateloader.TruncatedStateUnreplicated,
)
err = stateloader.WriteInitialRangeState(ctx, store.engine, *rhsDesc)
require.NoError(t, err)
newRng, err := newReplica(ctx, rhsDesc, store, repl.ReplicaID())
require.NoError(t, err)
Expand Down Expand Up @@ -2932,10 +2929,7 @@ func TestStoreRemovePlaceholderOnRaftIgnored(t *testing.T) {
}

uninitDesc := roachpb.RangeDescriptor{RangeID: repl1.Desc().RangeID}
if _, err := stateloader.WriteInitialState(
ctx, s.Engine(), enginepb.MVCCStats{}, uninitDesc, roachpb.Lease{},
hlc.Timestamp{}, stateloader.TruncatedStateUnreplicated,
); err != nil {
if err := stateloader.WriteInitialRangeState(ctx, s.Engine(), uninitDesc); err != nil {
t.Fatal(err)
}
uninitRepl1, err := newReplica(ctx, &uninitDesc, s, 2)
Expand Down

0 comments on commit 66f4f5d

Please sign in to comment.