Skip to content

Commit

Permalink
cli: Add 'pebble' debug command to run Pebble tool commands
Browse files Browse the repository at this point in the history
Command-ception: Add a `debug pebble` command similar to `debug
rocksdb` that nests pebble commands like sstable scan, manifest
dump, etc right in the cockroach binary.

Also move the pebble.Compare definition to engine, which is a more
fitting place for it than bulk.

Fixes cockroachdb#40509

Release note: None
  • Loading branch information
itsbilal committed Sep 12, 2019
1 parent 29cb9a6 commit 3e7db32
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 36 deletions.
5 changes: 4 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/sysutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/tool"
"github.com/gogo/protobuf/jsonpb"
"github.com/kr/pretty"
"github.com/pkg/errors"
Expand Down Expand Up @@ -758,6 +760,16 @@ https://github.com/facebook/rocksdb/wiki/Administration-and-Data-Access-Tool#ldb
},
}

var debugPebbleCmd = &cobra.Command{
Use: "pebble [command]",
Short: "run a Pebble tool command",
Long: `
Allows the use of pebble tools, such as to introspect manifests, SSTables, etc.
'cockroach debug pebble' accepts the same arguments and flags as the 'pebble'
binary.
`,
}

var debugSSTDumpCmd = &cobra.Command{
Use: "sst_dump",
Short: "run the RocksDB 'sst_dump' tool",
Expand Down Expand Up @@ -1303,6 +1315,8 @@ var debugCmds = append(DebugCmdsForRocksDB,
debugMergeLogsCommand,
)



// DebugCmd is the root of all debug commands. Exported to allow modification by CCL code.
var DebugCmd = &cobra.Command{
Use: "debug [command]",
Expand All @@ -1318,6 +1332,14 @@ process that has failed and cannot restart.
func init() {
DebugCmd.AddCommand(debugCmds...)

pebbleTool := tool.New()
merger := *pebble.DefaultMerger
merger.Name = "cockroach_merge_operator"
pebbleTool.RegisterMerger(&merger)
pebbleTool.RegisterComparer(engine.MVCCComparer)
debugPebbleCmd.AddCommand(pebbleTool.Commands...)
DebugCmd.AddCommand(debugPebbleCmd)

f := debugSyncBenchCmd.Flags()
f.IntVarP(&syncBenchOpts.Concurrency, "concurrency", "c", syncBenchOpts.Concurrency,
"number of concurrent writers")
Expand Down
35 changes: 1 addition & 34 deletions pkg/storage/bulk/sst_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,6 @@ type SSTWriter struct {
scratch []byte
}

var mvccComparer = &pebble.Comparer{
Compare: engine.MVCCKeyCompare,
AbbreviatedKey: func(k []byte) uint64 {
key, _, ok := enginepb.SplitMVCCKey(k)
if !ok {
return 0
}
return pebble.DefaultComparer.AbbreviatedKey(key)
},

Separator: func(dst, a, b []byte) []byte {
return append(dst, a...)
},

Successor: func(dst, a []byte) []byte {
return append(dst, a...)
},
Split: func(k []byte) int {
if len(k) == 0 {
return len(k)
}
// This is similar to what enginepb.SplitMVCCKey does.
tsLen := int(k[len(k)-1])
keyPartEnd := len(k) - 1 - tsLen
if keyPartEnd < 0 {
return len(k)
}
return keyPartEnd
},

Name: "cockroach_comparator",
}

// timeboundPropCollector implements a property collector for MVCC Timestamps.
// Its behavior matches TimeBoundTblPropCollector in table_props.cc.
type timeboundPropCollector struct {
Expand Down Expand Up @@ -126,7 +93,7 @@ var pebbleOpts = func() *pebble.Options {
merger.Name = "nullptr"
opts := &pebble.Options{
TableFormat: pebble.TableFormatLevelDB,
Comparer: mvccComparer,
Comparer: engine.MVCCComparer,
Merger: &merger,
}
opts.EnsureDefaults()
Expand Down
38 changes: 38 additions & 0 deletions pkg/storage/engine/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -1861,6 +1862,43 @@ func mvccInitPutUsingIter(
})
}

// MVCCComparer is a pebble.Comparer object that implements MVCC-specific
// comparator settings for use with Pebble.
//
// TODO(itsbilal): Move this to a new file pebble.go.
var MVCCComparer = &pebble.Comparer{
Compare: MVCCKeyCompare,
AbbreviatedKey: func(k []byte) uint64 {
key, _, ok := enginepb.SplitMVCCKey(k)
if !ok {
return 0
}
return pebble.DefaultComparer.AbbreviatedKey(key)
},

Separator: func(dst, a, b []byte) []byte {
return append(dst, a...)
},

Successor: func(dst, a []byte) []byte {
return append(dst, a...)
},
Split: func(k []byte) int {
if len(k) == 0 {
return len(k)
}
// This is similar to what enginepb.SplitMVCCKey does.
tsLen := int(k[len(k)-1])
keyPartEnd := len(k) - 1 - tsLen
if keyPartEnd < 0 {
return len(k)
}
return keyPartEnd
},

Name: "cockroach_comparator",
}

// MVCCMerge implements a merge operation. Merge adds integer values,
// concatenates undifferentiated byte slice values, and efficiently
// combines time series observations if the roachpb.Value tag value
Expand Down

0 comments on commit 3e7db32

Please sign in to comment.