From c5497ba4758c55cfb7be120c882a811f187d7513 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Mon, 6 Jun 2022 14:54:33 -0400 Subject: [PATCH] keys: move Raft log suffix parsing into pkg/keys This did not belong in stateloader.go. --- pkg/keys/keys.go | 9 ++++++++- pkg/kv/kvserver/stateloader/BUILD.bazel | 1 - pkg/kv/kvserver/stateloader/stateloader.go | 13 ++++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/keys/keys.go b/pkg/keys/keys.go index 5ab51a93c6e1..1d39b9c0e700 100644 --- a/pkg/keys/keys.go +++ b/pkg/keys/keys.go @@ -337,6 +337,13 @@ func RaftLogKeyFromPrefix(raftLogPrefix []byte, logIndex uint64) roachpb.Key { return encoding.EncodeUint64Ascending(raftLogPrefix, logIndex) } +// DecodeRaftLogKeyFromSuffix parses the suffix of a system-local key for a Raft +// log entry and returns the entry's log index. +func DecodeRaftLogKeyFromSuffix(raftLogSuffix []byte) (uint64, error) { + _, logIndex, err := encoding.DecodeUint64Ascending(raftLogSuffix) + return logIndex, err +} + // RaftReplicaIDKey returns a system-local key for a RaftReplicaID. func RaftReplicaIDKey(rangeID roachpb.RangeID) roachpb.Key { return MakeRangeIDPrefixBuf(rangeID).RaftReplicaIDKey() @@ -453,7 +460,7 @@ func LockTableSingleKey(key roachpb.Key, buf []byte) (roachpb.Key, []byte) { } // DecodeLockTableSingleKey decodes the single-key lock table key to return the key -// that was locked.. +// that was locked. func DecodeLockTableSingleKey(key roachpb.Key) (lockedKey roachpb.Key, err error) { if !bytes.HasPrefix(key, LocalRangeLockTablePrefix) { return nil, errors.Errorf("key %q does not have %q prefix", diff --git a/pkg/kv/kvserver/stateloader/BUILD.bazel b/pkg/kv/kvserver/stateloader/BUILD.bazel index 56f558fb6afb..89250e5f4926 100644 --- a/pkg/kv/kvserver/stateloader/BUILD.bazel +++ b/pkg/kv/kvserver/stateloader/BUILD.bazel @@ -15,7 +15,6 @@ go_library( "//pkg/roachpb", "//pkg/storage", "//pkg/storage/enginepb", - "//pkg/util/encoding", "//pkg/util/hlc", "//pkg/util/log", "//pkg/util/protoutil", diff --git a/pkg/kv/kvserver/stateloader/stateloader.go b/pkg/kv/kvserver/stateloader/stateloader.go index 6c9eb30c91e5..575c3e7117b0 100644 --- a/pkg/kv/kvserver/stateloader/stateloader.go +++ b/pkg/kv/kvserver/stateloader/stateloader.go @@ -19,7 +19,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/storage/enginepb" - "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/protoutil" @@ -45,7 +44,7 @@ type StateLoader struct { keys.RangeIDPrefixBuf } -// Make creates a a StateLoader. +// Make creates a StateLoader. func Make(rangeID roachpb.RangeID) StateLoader { rsl := StateLoader{ RangeIDPrefixBuf: keys.MakeRangeIDPrefixBuf(rangeID), @@ -302,11 +301,15 @@ func (rsl StateLoader) LoadLastIndex(ctx context.Context, reader storage.Reader) var lastIndex uint64 iter.SeekLT(storage.MakeMVCCMetadataKey(keys.RaftLogKeyFromPrefix(prefix, math.MaxUint64))) if ok, _ := iter.Valid(); ok { - key := iter.Key() + key := iter.UnsafeKey().Key + if len(key) < len(prefix) { + log.Fatalf(ctx, "unable to decode Raft log index key: len(%s) < len(%s)", key.String(), prefix.String()) + } + suffix := key[len(prefix):] var err error - _, lastIndex, err = encoding.DecodeUint64Ascending(key.Key[len(prefix):]) + lastIndex, err = keys.DecodeRaftLogKeyFromSuffix(suffix) if err != nil { - log.Fatalf(ctx, "unable to decode Raft log index key: %s", key) + log.Fatalf(ctx, "unable to decode Raft log index key: %s; %v", key.String(), err) } }