Skip to content

Commit 7449883

Browse files
committed
print: pretty print WAG node contents
Epic: none Release note: none
1 parent 15bea86 commit 7449883

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

pkg/kv/kvserver/kvstorage/wag/store_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package wag
88
import (
99
"fmt"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213

1314
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
@@ -45,6 +46,9 @@ func TestWrite(t *testing.T) {
4546
write("init", func(w storage.Writer) error { return initReplica(&s, w, id, 10) })
4647
write("split", func(w storage.Writer) error { return splitReplica(&s, w, id, 200) })
4748

49+
// TODO(pav-kv): the trailing \n in DecodeWriteBatch is duplicated with
50+
// recursion. Remove it, and let the caller handle new lines.
51+
out = strings.ReplaceAll(out, "\n\n", "\n")
4852
echotest.Require(t, out, filepath.Join("testdata", t.Name()+".txt"))
4953
}
5054

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
echo
22
----
33
>> create
4-
Put: 0,0 /Local/Store/wag/1 (0x01737761676e000000000000000100): "\n\x04\b{\x10\x04\x10\x01\x1a(\n&\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x12state-machine-key\x00\x05state"
4+
Put: 0,0 /Local/Store/wag/1 (0x01737761676e000000000000000100): NodeCreate r123/4:0
5+
> Put: 0,0 "state-machine-key" (0x73746174652d6d616368696e652d6b657900): "state"
56
>> init
6-
Put: 0,0 /Local/Store/wag/2 (0x01737761676e000000000000000200): "\n\x06\b{\x10\x04 \n\x10\x02\x1a\x18\x12\x16\n\ttmp/1.sst\n\ttmp/2.sst"
7+
Put: 0,0 /Local/Store/wag/2 (0x01737761676e000000000000000200): NodeSnap r123/4:10
8+
ingestion: SSTs:"tmp/1.sst" SSTs:"tmp/2.sst"
79
>> split
8-
Put: 0,0 /Local/Store/wag/3 (0x01737761676e000000000000000300): "\n\a\b{\x10\x04 \xc7\x01\x10\x03\x1a\x00"
9-
Put: 0,0 /Local/Store/wag/4 (0x01737761676e000000000000000400): "\n\a\b{\x10\x04 \xc8\x01\x10\x04\x1a6\n4\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\blhs-key\x00\tlhs-state\x01\brhs-key\x00\trhs-state \xb7\x04"
10+
Put: 0,0 /Local/Store/wag/3 (0x01737761676e000000000000000300): NodeApply r123/4:199
11+
Put: 0,0 /Local/Store/wag/4 (0x01737761676e000000000000000400): NodeSplit r123/4:200 create:567
12+
> Put: 0,0 "lhs-key" (0x6c68732d6b657900): "lhs-state"
13+
> Put: 0,0 "rhs-key" (0x7268732d6b657900): "rhs-state"

pkg/kv/kvserver/kvstorage/wag/wagpb/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ go_library(
3232
embed = [":wagpb_go_proto"],
3333
importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/wag/wagpb",
3434
visibility = ["//visibility:public"],
35-
deps = ["//pkg/kv/kvpb"],
35+
deps = [
36+
"//pkg/kv/kvpb",
37+
"@com_github_cockroachdb_redact//:redact",
38+
],
3639
)

pkg/kv/kvserver/kvstorage/wag/wagpb/wag.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
package wagpb
77

8-
// TODO(pav-kv): remove this once the dev gen bazel picks up this dependency
9-
// from the .pb.go file.
10-
import _ "github.com/cockroachdb/cockroach/pkg/kv/kvpb"
8+
import (
9+
// TODO(pav-kv): remove this once the dev gen bazel picks up this dependency
10+
// from the .pb.go file.
11+
_ "github.com/cockroachdb/cockroach/pkg/kv/kvpb"
12+
"github.com/cockroachdb/redact"
13+
)
14+
15+
// String implements the fmt.Stringer interface.
16+
func (a Addr) String() string {
17+
return redact.StringWithoutMarkers(a)
18+
}
19+
20+
// SafeFormat implements the redact.SafeFormatter interface.
21+
func (a Addr) SafeFormat(w redact.SafePrinter, _ rune) {
22+
w.Printf("r%d/%d:%d", a.RangeID, a.ReplicaID, a.Index)
23+
}

pkg/kv/kvserver/kvstorage/wag/wagpb/wag.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ enum NodeType {
4545
// with lower ReplicaIDs (including their destruction), or same ReplicaID and
4646
// lower Index have been applied.
4747
message Addr {
48+
option (gogoproto.goproto_stringer) = false;
49+
4850
// RangeID is the ID of the range that the WAG node pertains to.
4951
int64 range_id = 1 [
5052
(gogoproto.customname) = "RangeID",

pkg/kv/kvserver/print/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
deps = [
1212
"//pkg/keys",
1313
"//pkg/kv/kvserver/kvserverpb",
14+
"//pkg/kv/kvserver/kvstorage/wag/wagpb",
1415
"//pkg/kv/kvserver/raftlog",
1516
"//pkg/raft/raftpb",
1617
"//pkg/roachpb",
@@ -22,6 +23,7 @@ go_library(
2223
"@com_github_cockroachdb_errors//:errors",
2324
"@com_github_cockroachdb_pebble//:pebble",
2425
"@com_github_gogo_protobuf//proto",
26+
"@com_github_kr_text//:text",
2527
],
2628
)
2729

pkg/kv/kvserver/print/debug_print.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/cockroachdb/cockroach/pkg/keys"
1515
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
16+
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage/wag/wagpb"
1617
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/raftlog"
1718
"github.com/cockroachdb/cockroach/pkg/raft/raftpb"
1819
"github.com/cockroachdb/cockroach/pkg/roachpb"
@@ -23,6 +24,7 @@ import (
2324
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
2425
"github.com/cockroachdb/errors"
2526
"github.com/cockroachdb/pebble"
27+
"github.com/kr/text"
2628
)
2729

2830
// PrintEngineKeyValue attempts to print the given key-value pair to
@@ -136,14 +138,16 @@ func SprintMVCCKeyValue(kv storage.MVCCKeyValue, printKey bool) string {
136138

137139
// TODO(pav-kv): some functions here do not check the key, so can accidentally
138140
// succeed parsing values that have a different "type", and print them in a
139-
// misleading way. Make all these functions key-aware.
141+
// misleading way. Make all these functions key-aware, and use lookups for
142+
// efficiency.
140143
decoders := append(DebugSprintMVCCKeyValueDecoders,
141144
tryRangeIDKey,
142145
tryRangeDescriptor,
143146
tryMeta,
144147
tryTxn,
145148
tryTimeSeries,
146149
tryIntent,
150+
tryWAGKey,
147151
func(kv storage.MVCCKeyValue) (string, error) {
148152
// No better idea, just print raw bytes and hope that folks use `less -S`.
149153
return fmt.Sprintf("%q", kv.Value), nil
@@ -465,6 +469,40 @@ func tryRangeIDKey(kv storage.MVCCKeyValue) (string, error) {
465469
return msg.String(), nil
466470
}
467471

472+
func tryWAGKey(kv storage.MVCCKeyValue) (string, error) {
473+
if !bytes.HasPrefix(kv.Key.Key, keys.StoreWAGPrefix()) {
474+
return "", errors.New("not a WAG node key")
475+
}
476+
var node wagpb.Node
477+
if err := node.Unmarshal(kv.Value); err != nil { // nolint:protounmarshal
478+
return "", err
479+
}
480+
481+
str := fmt.Appendf(nil, "%v %s", node.Type, node.Addr)
482+
if c, d := node.Create, node.Destroy; c != 0 || len(d) != 0 {
483+
if c != 0 {
484+
str = fmt.Appendf(str, " create:%d", c)
485+
}
486+
if len(d) != 0 {
487+
str = fmt.Appendf(str, " destroy:%v", d)
488+
}
489+
}
490+
491+
if b := node.Mutation.Batch; len(b) > 0 {
492+
bs, err := DecodeWriteBatch(b)
493+
if err != nil {
494+
bs = "failed to decode write batch: " + err.Error()
495+
}
496+
str = fmt.Appendf(str, "\n%s", text.Indent(bs, "> "))
497+
}
498+
if ing := node.Mutation.Ingestion; ing != nil {
499+
// TODO(pav-kv): make this format nicely. Currently, this proto is too big.
500+
str = fmt.Appendf(str, "\ningestion: %v", ing)
501+
}
502+
503+
return string(str), nil
504+
}
505+
468506
func tryMeta(kv storage.MVCCKeyValue) (string, error) {
469507
if !bytes.HasPrefix(kv.Key.Key, keys.Meta1Prefix) && !bytes.HasPrefix(kv.Key.Key, keys.Meta2Prefix) {
470508
return "", errors.New("not a meta key")

0 commit comments

Comments
 (0)