Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ go_test(
"//pkg/kv",
"//pkg/kv/kvpb",
"//pkg/kv/kvserver",
"//pkg/kv/kvserver/concurrency/lock",
"//pkg/kv/kvserver/kvstorage",
"//pkg/kv/kvserver/liveness",
"//pkg/kv/kvserver/liveness/livenesspb",
Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/cli_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
package cli

import (
gohex "encoding/hex"
"fmt"
"path/filepath"
"strings"
"testing"

"github.com/cockroachdb/cockroach/pkg/kv/kvserver/concurrency/lock"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -116,4 +121,21 @@ func TestDebugDecodeKeys(t *testing.T) {
require.Contains(t, out, "\n"+d.result+"\n")
})
}

t.Run("lock-table-engine-key", func(t *testing.T) {
u := uuid.Must(uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
lk := storage.LockTableKey{Key: roachpb.Key("foo"), Strength: lock.Shared, TxnUUID: u}
ek, _ := lk.ToEngineKey(nil)
lockHex := gohex.EncodeToString(ek.Encode())
out, err := TestCLI{}.RunWithCaptureArgs([]string{
"debug",
"decode-key",
"--encoding",
"hex",
fmt.Sprintf("--user-key=%t", false),
lockHex,
})
require.NoError(t, err)
require.Contains(t, out, "\n"+fmt.Sprint(lk)+"\n")
})
}
28 changes: 24 additions & 4 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,8 @@ var debugDecodeKeyCmd = &cobra.Command{
Long: `
Decode encoded keys provided as command arguments and pretty-print them.
Decode command could be used with either encoded engine keys that contain
timestamp or user keys used in range descriptors, range keys etc.
timestamp, lock table keys (engine keys with a lock version suffix), or user
keys used in range descriptors, range keys etc.
Key encoding type could be changed using encoding flag.
For example:

Expand All @@ -663,11 +664,30 @@ For example:
if decodeKeyOptions.userKey {
fmt.Println(roachpb.Key(b))
} else {
k, err := storage.DecodeMVCCKey(b)
if err != nil {
if k, err := storage.DecodeMVCCKey(b); err == nil {
fmt.Println(k)
} else if ek, ok := storage.DecodeEngineKey(b); ok {
switch {
case ek.IsMVCCKey():
mvccKey, err := ek.ToMVCCKey()
if err != nil {
return errors.Wrap(err, "decode engine key as MVCC")
}
fmt.Println(mvccKey)
case ek.IsLockTableKey():
lk, err := ek.ToLockTableKey()
if err != nil {
return errors.Wrap(err, "decode lock table engine key")
}
fmt.Println(lk)
default:
s := print.SprintEngineKey(ek)
s = strings.TrimSuffix(s, ": ")
fmt.Println(s)
}
} else {
return err
}
fmt.Println(k)
}
}
return nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/kv/kvserver/print/debug_print.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func SprintEngineKey(key storage.EngineKey) string {
if mvccKey, err := key.ToMVCCKey(); err == nil {
return SprintMVCCKey(mvccKey)
}
} else if key.IsLockTableKey() {
if lk, err := key.ToLockTableKey(); err == nil {
return fmt.Sprintf("%v (%#x): ", lk, key.Encode())
}
}

return fmt.Sprintf("%s %x (%#x): ", key.Key, key.Version, key.Encode())
Expand Down