Skip to content

Commit

Permalink
Merge #40728
Browse files Browse the repository at this point in the history
40728: cli: Add 'pebble' debug command to run Pebble tool commands r=itsbilal a=itsbilal

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 #40509

Release note: None

Release justification: Adds debug tools for internal use, no impact
on general operation.

Co-authored-by: Bilal Akhtar <bilal@cockroachlabs.com>
  • Loading branch information
craig[bot] and itsbilal committed Sep 18, 2019
2 parents 7b16a15 + 70cca6d commit 6c508db
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 38 deletions.
8 changes: 6 additions & 2 deletions Gopkg.lock

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

26 changes: 26 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,14 @@ https://github.com/facebook/rocksdb/wiki/Administration-and-Data-Access-Tool#ldb
},
}

var debugPebbleCmd = &cobra.Command{
Use: "pebble [command]",
Short: "run a Pebble introspection tool command",
Long: `
Allows the use of pebble tools, such as to introspect manifests, SSTables, etc.
`,
}

var debugSSTDumpCmd = &cobra.Command{
Use: "sst_dump",
Short: "run the RocksDB 'sst_dump' tool",
Expand Down Expand Up @@ -1318,6 +1328,22 @@ process that has failed and cannot restart.
func init() {
DebugCmd.AddCommand(debugCmds...)

pebbleTool := tool.New()
// To be able to read Cockroach-written RocksDB manifests/SSTables, comparator
// and merger functions must be specified to pebble that match the ones used
// to write those files.
//
// TODO(itsbilal): Port the Cockroach merger over from libroach/merge.cc to go
// and use that here. Until this happens, some data (eg. timeseries) will be
// printed incorrectly by this tool: it will be concatenated instead of being
// properly merged.
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
70 changes: 69 additions & 1 deletion pkg/storage/engine/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/pkg/errors"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble"
)

const (
Expand Down Expand Up @@ -123,6 +124,11 @@ func (k MVCCKey) String() string {
return fmt.Sprintf("%s/%s", k.Key, k.Timestamp)
}

// Format implements the fmt.Formatter interface.
func (k MVCCKey) Format(f fmt.State, c rune) {
fmt.Fprintf(f, "%s/%s", k.Key, k.Timestamp)
}

// Len returns the size of the MVCCKey when encoded. Implements the
// pebble.Encodeable interface.
//
Expand Down Expand Up @@ -1861,6 +1867,68 @@ func mvccInitPutUsingIter(
})
}

// mvccKeyFormatter is an fmt.Formatter for MVCC Keys.
type mvccKeyFormatter struct {
key MVCCKey
err error
}

var _ fmt.Formatter = mvccKeyFormatter{}

// Format implements the fmt.Formatter interface.
func (m mvccKeyFormatter) Format(f fmt.State, c rune) {
if m.err != nil {
errors.FormatError(m.err, f, c)
return
}
m.key.Format(f, c)
}

// 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)
},

Format: func(k []byte) fmt.Formatter {
decoded, err := DecodeMVCCKey(k)
if err != nil {
return mvccKeyFormatter{err: err}
}
return mvccKeyFormatter{key: decoded}
},

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
2 changes: 1 addition & 1 deletion vendor
Submodule vendor updated 27 files
+53 −27 github.com/cockroachdb/pebble/batch.go
+0 −405 github.com/cockroachdb/pebble/bench.txt
+224 −0 github.com/cockroachdb/pebble/bloom/bloom.go
+122 −116 github.com/cockroachdb/pebble/compaction.go
+29 −23 github.com/cockroachdb/pebble/compaction_iter.go
+15 −25 github.com/cockroachdb/pebble/db.go
+57 −28 github.com/cockroachdb/pebble/ingest.go
+0 −285 github.com/cockroachdb/pebble/internal/arenaskl/bench.txt
+58 −31 github.com/cockroachdb/pebble/internal/base/comparer.go
+46 −4 github.com/cockroachdb/pebble/internal/base/event.go
+13 −4 github.com/cockroachdb/pebble/internal/base/internal.go
+11 −0 github.com/cockroachdb/pebble/internal/batch/batch.go
+0 −135 github.com/cockroachdb/pebble/internal/batchskl/bench.txt
+83 −50 github.com/cockroachdb/pebble/internal/manifest/version.go
+2 −2 github.com/cockroachdb/pebble/internal/manifest/version_edit.go
+17 −0 github.com/cockroachdb/pebble/internal/rangedel/tombstone.go
+0 −55 github.com/cockroachdb/pebble/internal/record/bench.txt
+0 −95 github.com/cockroachdb/pebble/sstable/bench.txt
+13 −8 github.com/cockroachdb/pebble/sstable/writer.go
+193 −0 github.com/cockroachdb/pebble/tool/db.go
+44 −0 github.com/cockroachdb/pebble/tool/make_test_sstables.go
+149 −0 github.com/cockroachdb/pebble/tool/manifest.go
+318 −0 github.com/cockroachdb/pebble/tool/sstable.go
+78 −0 github.com/cockroachdb/pebble/tool/tool.go
+195 −0 github.com/cockroachdb/pebble/tool/util.go
+141 −0 github.com/cockroachdb/pebble/tool/wal.go
+13 −17 github.com/cockroachdb/pebble/version_set.go

0 comments on commit 6c508db

Please sign in to comment.