Skip to content

Commit

Permalink
keys: move Raft log suffix parsing into pkg/keys
Browse files Browse the repository at this point in the history
This did not belong in stateloader.go.
  • Loading branch information
nvanbenschoten committed Jun 6, 2022
1 parent 8012d6a commit c5497ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
9 changes: 8 additions & 1 deletion pkg/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion pkg/kv/kvserver/stateloader/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions pkg/kv/kvserver/stateloader/stateloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit c5497ba

Please sign in to comment.