Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add 'pebble' debug command to run Pebble tool commands #40728

Merged
merged 1 commit into from
Sep 18, 2019
Merged
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
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