From 57ee90bb7314fd05b3387c07da75cd6594757fc1 Mon Sep 17 00:00:00 2001 From: Pavel Kalinnikov Date: Mon, 6 Feb 2023 20:32:47 +0000 Subject: [PATCH] kvserver: avoid duplicate loading in Store.Start Release note: none Epic: none --- pkg/kv/kvserver/kvstorage/init.go | 18 ++++++++++++++++++ pkg/kv/kvserver/replica_init.go | 9 +++++++-- pkg/kv/kvserver/store.go | 10 ++++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pkg/kv/kvserver/kvstorage/init.go b/pkg/kv/kvserver/kvstorage/init.go index 00d76d452460..f0c1356102d2 100644 --- a/pkg/kv/kvserver/kvstorage/init.go +++ b/pkg/kv/kvserver/kvstorage/init.go @@ -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" @@ -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 diff --git a/pkg/kv/kvserver/replica_init.go b/pkg/kv/kvserver/replica_init.go index 54fccc3d135a..2b355ad9ae17 100644 --- a/pkg/kv/kvserver/replica_init.go +++ b/pkg/kv/kvserver/replica_init.go @@ -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 diff --git a/pkg/kv/kvserver/store.go b/pkg/kv/kvserver/store.go index c39bf59708a2..19de32acc82b 100644 --- a/pkg/kv/kvserver/store.go +++ b/pkg/kv/kvserver/store.go @@ -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 }