Skip to content

Commit

Permalink
server: Add EngineTypeDefault, resolve it to Pebble if last used
Browse files Browse the repository at this point in the history
Adds a new engine type called EngineTypeDefault, that resolves
at start time to RocksDB, or Pebble if that was last used
to write to the first store. This effectively makes the
--storage-engine flag sticky for future runs where no
engine is specified. If the flag is passed in again, that
specified engine type takes priority over the one used last.

Fixes #42445 .

Release note: None.
  • Loading branch information
itsbilal committed Feb 29, 2020
1 parent 861b4e0 commit 9a17f1a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 28 deletions.
6 changes: 5 additions & 1 deletion pkg/cli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,11 @@ Also, if you use equal signs in the file path to a store, you must use the
Name: "storage-engine",
Description: `
Storage engine to use for all stores on this cockroach node. Options are rocksdb,
or pebble.`,
or pebble.
If left unspecified, the storage engine last used to write to the
first store directory is used (see --store). If unspecified and store directory
is uninitialized, rocksdb is used as the default storage engine.`,
}

Size = FlagInfo{
Expand Down
14 changes: 13 additions & 1 deletion pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/tool"
"github.com/cockroachdb/pebble/vfs"
"github.com/gogo/protobuf/jsonpb"
"github.com/kr/pretty"
"github.com/pkg/errors"
Expand Down Expand Up @@ -140,8 +141,19 @@ func OpenEngine(dir string, stopper *stop.Stopper, opts OpenEngineOptions) (engi
}

var db engine.Engine
storageEngine := engine.DefaultStorageEngine
if storageEngine == enginepb.EngineTypeDefault {
storageEngine = enginepb.EngineTypeRocksDB
// Check if this storage directory was last written to by pebble. In that
// case, default to opening a Pebble engine.
if version, err := pebble.GetVersion(storageConfig.Dir, vfs.Default); err == nil {
if version != "" && !strings.HasPrefix(version, "rocksdb") {
storageEngine = enginepb.EngineTypePebble
}
}
}

switch engine.DefaultStorageEngine {
switch storageEngine {
case enginepb.EngineTypePebble:
cfg := engine.PebbleConfig{
StorageConfig: storageConfig,
Expand Down
16 changes: 16 additions & 0 deletions pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/server/status"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
Expand All @@ -51,6 +52,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/sysutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/vfs"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -530,6 +533,19 @@ func runStart(cmd *cobra.Command, args []string, disableReplication bool) error
if serverCfg.Settings.ExternalIODir, err = initExternalIODir(ctx, serverCfg.Stores.Specs[0]); err != nil {
return err
}
// If the storage engine is set to "default", check the engine type used in
// this store directory in a past run. If this check fails for any reason,
// use RocksDB as the default engine type.
if serverCfg.StorageEngine == enginepb.EngineTypeDefault {
serverCfg.StorageEngine = enginepb.EngineTypeRocksDB
// Only check if the first store is written by pebble. If that's the case,
// set engine type to pebble.
if version, err := pebble.GetVersion(serverCfg.Stores.Specs[0].Path, vfs.Default); err == nil {
if version != "" && !strings.HasPrefix(version, "rocksdb") {
serverCfg.StorageEngine = enginepb.EngineTypePebble
}
}
}
// Find a StoreSpec that has encryption at rest turned on. If can't find
// one, use the first StoreSpec in the list.
var specIdx = 0
Expand Down
5 changes: 4 additions & 1 deletion pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,13 @@ func (cfg *Config) CreateEngines(ctx context.Context) (Engines, error) {
details = append(details, fmt.Sprintf("Pebble cache size: %s", humanizeutil.IBytes(cfg.CacheSize)))
pebbleCache = pebble.NewCache(cfg.CacheSize)
defer pebbleCache.Unref()
} else if cfg.StorageEngine == enginepb.EngineTypeRocksDB || cfg.StorageEngine == enginepb.EngineTypeTeePebbleRocksDB {
}
if cfg.StorageEngine == enginepb.EngineTypeRocksDB || cfg.StorageEngine == enginepb.EngineTypeTeePebbleRocksDB {
details = append(details, fmt.Sprintf("RocksDB cache size: %s", humanizeutil.IBytes(cfg.CacheSize)))
cache = engine.NewRocksDBCache(cfg.CacheSize)
defer cache.Release()
} else if cfg.StorageEngine == enginepb.EngineTypeDefault {
return nil, errors.New("engine type 'default' left unresolved")
}

var physicalStores int
Expand Down
2 changes: 2 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,8 @@ func (s *Server) Start(ctx context.Context) error {
switch s.cfg.StorageEngine {
case enginepb.EngineTypePebble:
nodeStartCounter += "pebble."
case enginepb.EngineTypeDefault:
nodeStartCounter += "default."
case enginepb.EngineTypeRocksDB:
nodeStartCounter += "rocksdb."
case enginepb.EngineTypeTeePebbleRocksDB:
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
var DefaultStorageEngine enginepb.EngineType

func init() {
_ = DefaultStorageEngine.Set(envutil.EnvOrDefaultString("COCKROACH_STORAGE_ENGINE", "rocksdb"))
_ = DefaultStorageEngine.Set(envutil.EnvOrDefaultString("COCKROACH_STORAGE_ENGINE", "default"))
}

// SimpleIterator is an interface for iterating over key/value pairs in an
Expand Down
4 changes: 4 additions & 0 deletions pkg/storage/engine/enginepb/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func (e *EngineType) Type() string { return "string" }
// String implements the pflag.Value interface.
func (e *EngineType) String() string {
switch *e {
case EngineTypeDefault:
return "default"
case EngineTypeRocksDB:
return "rocksdb"
case EngineTypePebble:
Expand All @@ -31,6 +33,8 @@ func (e *EngineType) String() string {
// Set implements the pflag.Value interface.
func (e *EngineType) Set(s string) error {
switch s {
case "default":
*e = EngineTypeDefault
case "rocksdb":
*e = EngineTypeRocksDB
case "pebble":
Expand Down
49 changes: 28 additions & 21 deletions pkg/storage/engine/enginepb/engine.pb.go

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

10 changes: 7 additions & 3 deletions pkg/storage/engine/enginepb/engine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ enum EngineType {
option (gogoproto.goproto_enum_prefix) = false;
option (gogoproto.goproto_enum_stringer) = false;

// Denotes the default engine as the underlying storage engine type. Resolves
// during start to the engine type last used. If left unresolved, it's treated
// the same as EngineTypeRocksDB.
EngineTypeDefault = 0;
// Denotes RocksDB as the underlying storage engine type.
EngineTypeRocksDB = 0;
EngineTypeRocksDB = 1;
// Denotes Pebble as the underlying storage engine type.
EngineTypePebble = 1;
EngineTypePebble = 2;
// Denotes TeePebbleRocksDB as the underlying storage engine type. Only use
// for testing purposes.
EngineTypeTeePebbleRocksDB = 2;
EngineTypeTeePebbleRocksDB = 3;
}
2 changes: 2 additions & 0 deletions pkg/storage/engine/in_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func NewInMem(
return newTeeInMem(ctx, attrs, cacheSize)
case enginepb.EngineTypePebble:
return newPebbleInMem(ctx, attrs, cacheSize)
case enginepb.EngineTypeDefault:
fallthrough
case enginepb.EngineTypeRocksDB:
return newRocksDBInMem(attrs, cacheSize)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/engine/temp_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func NewTempEngine(
fallthrough
case enginepb.EngineTypePebble:
return NewPebbleTempEngine(ctx, tempStorage, storeSpec)
case enginepb.EngineTypeDefault:
fallthrough
case enginepb.EngineTypeRocksDB:
return NewRocksDBTempEngine(tempStorage, storeSpec)
}
Expand Down

0 comments on commit 9a17f1a

Please sign in to comment.