Skip to content

Commit

Permalink
kvserver: avoid duplicate loading in Store.Start
Browse files Browse the repository at this point in the history
Release note: none
Epic: none
  • Loading branch information
pav-kv committed Feb 6, 2023
1 parent 17ab9d4 commit 57ee90b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
18 changes: 18 additions & 0 deletions pkg/kv/kvserver/kvstorage/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/logstore"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/stateloader"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
Expand Down Expand Up @@ -312,6 +313,23 @@ func (r Replica) ID() storage.FullReplicaID {
}
}

// Load loads the state necessary to instantiate a replica in memory.
func (r Replica) Load(ctx context.Context, eng storage.Reader) (LoadedReplicaState, error) {
ls := LoadedReplicaState{
ReplicaID: r.ReplicaID,
hardState: r.hardState,
}
sl := stateloader.Make(r.Desc.RangeID)
var err error
if ls.LastIndex, err = sl.LoadLastIndex(ctx, eng); err != nil {
return LoadedReplicaState{}, err
}
if ls.ReplState, err = sl.Load(ctx, eng, r.Desc); err != nil {
return LoadedReplicaState{}, err
}
return ls, nil
}

// A replicaMap organizes a set of Replicas with unique RangeIDs.
type replicaMap map[roachpb.RangeID]Replica

Expand Down
9 changes: 7 additions & 2 deletions pkg/kv/kvserver/replica_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ func loadInitializedReplica(
if err != nil {
return nil, err
}
r := newUninitializedReplica(store, desc.RangeID, replicaID)
return newInitializedReplica(store, state)
}

// newInitializedReplica creates an initialized Replica from its loaded state.
func newInitializedReplica(store *Store, loaded kvstorage.LoadedReplicaState) (*Replica, error) {
r := newUninitializedReplica(store, loaded.ReplState.Desc.RangeID, loaded.ReplicaID)
r.raftMu.Lock()
defer r.raftMu.Unlock()
r.mu.Lock()
defer r.mu.Unlock()
if err := r.initRaftMuLockedReplicaMuLocked(state); err != nil {
if err := r.initRaftMuLockedReplicaMuLocked(loaded); err != nil {
return nil, err
}
return r, nil
Expand Down
10 changes: 6 additions & 4 deletions pkg/kv/kvserver/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1861,10 +1861,12 @@ func (s *Store) Start(ctx context.Context, stopper *stop.Stopper) error {
// Uninitialized Replicas are not currently instantiated at store start.
continue
}
// TODO(sep-raft-log): construct the loaded Replica state directly in
// LoadAndReconcileReplicas, which loads and checks most of it already, then
// feed it to Replica creation, to avoid double-loading.
rep, err := loadInitializedReplica(ctx, s, repl.Desc, repl.ReplicaID)
// TODO(pavelkalinnikov): integrate into kvstorage.LoadAndReconcileReplicas.
state, err := repl.Load(ctx, s.Engine())
if err != nil {
return err
}
rep, err := newInitializedReplica(s, state)
if err != nil {
return err
}
Expand Down

0 comments on commit 57ee90b

Please sign in to comment.