Skip to content

Commit

Permalink
Add pebble as valid value for --db-type. (#2244)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Laine <[email protected]>
Co-authored-by: Dhruba Basu <[email protected]>
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2023
1 parent 047d493 commit 4957ccb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/ava-labs/avalanchego/database/leveldb"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/pebble"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/snow/consensus/snowball"
"github.com/ava-labs/avalanchego/trace"
Expand Down Expand Up @@ -103,7 +104,7 @@ func addNodeFlags(fs *pflag.FlagSet) {
fs.Uint64(AddSubnetDelegatorFeeKey, genesis.LocalParams.AddSubnetDelegatorFee, "Transaction fee, in nAVAX, for transactions that add new subnet delegators")

// Database
fs.String(DBTypeKey, leveldb.Name, fmt.Sprintf("Database type to use. Should be one of {%s, %s}", leveldb.Name, memdb.Name))
fs.String(DBTypeKey, leveldb.Name, fmt.Sprintf("Database type to use. Must be one of {%s, %s, %s}", leveldb.Name, memdb.Name, pebble.Name))
fs.String(DBPathKey, defaultDBDir, "Path to database directory")
fs.String(DBConfigFileKey, "", fmt.Sprintf("Path to database config file. Ignored if %s is specified", DBConfigContentKey))
fs.String(DBConfigContentKey, "", "Specifies base64 encoded database config content")
Expand Down
20 changes: 13 additions & 7 deletions database/pebble/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import (
"github.com/ava-labs/avalanchego/utils/units"
)

// pebbleByteOverHead is the number of bytes of constant overhead that
// should be added to a batch size per operation.
const pebbleByteOverHead = 8
const (
Name = "pebble"

// pebbleByteOverHead is the number of bytes of constant overhead that
// should be added to a batch size per operation.
pebbleByteOverHead = 8
)

var (
_ database.Database = (*Database)(nil)
Expand Down Expand Up @@ -73,10 +77,12 @@ type Config struct {
}

// TODO: Add metrics
func New(file string, configBytes []byte, log logging.Logger, _ string, _ prometheus.Registerer) (*Database, error) {
var cfg Config
if err := json.Unmarshal(configBytes, &cfg); err != nil {
return nil, err
func New(file string, configBytes []byte, log logging.Logger, _ string, _ prometheus.Registerer) (database.Database, error) {
cfg := DefaultConfig
if len(configBytes) > 0 {
if err := json.Unmarshal(configBytes, &cfg); err != nil {
return nil, err
}
}

opts := &pebble.Options{
Expand Down
2 changes: 1 addition & 1 deletion database/pebble/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func newDB(t testing.TB) *Database {
folder := t.TempDir()
db, err := New(folder, DefaultConfigBytes, logging.NoLog{}, "pebble", prometheus.NewRegistry())
require.NoError(t, err)
return db
return db.(*Database)
}

func TestInterface(t *testing.T) {
Expand Down
15 changes: 13 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/ava-labs/avalanchego/database/leveldb"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/meterdb"
"github.com/ava-labs/avalanchego/database/pebble"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -503,20 +504,30 @@ func (n *Node) initDatabase() error {
// start the db
switch n.Config.DatabaseConfig.Name {
case leveldb.Name:
// Prior to v1.10.15, the only on-disk database was leveldb, and its
// files went to [dbPath]/[networkID]/v1.4.5.
dbPath := filepath.Join(n.Config.DatabaseConfig.Path, version.CurrentDatabase.String())
var err error
n.DB, err = leveldb.New(dbPath, n.Config.DatabaseConfig.Config, n.Log, "db_internal", n.MetricsRegisterer)
if err != nil {
return fmt.Errorf("couldn't create db at %s: %w", dbPath, err)
return fmt.Errorf("couldn't create leveldb at %s: %w", dbPath, err)
}
case memdb.Name:
n.DB = memdb.New()
case pebble.Name:
dbPath := filepath.Join(n.Config.DatabaseConfig.Path, pebble.Name)
var err error
n.DB, err = pebble.New(dbPath, n.Config.DatabaseConfig.Config, n.Log, "db_internal", n.MetricsRegisterer)
if err != nil {
return fmt.Errorf("couldn't create pebbledb at %s: %w", dbPath, err)
}
default:
return fmt.Errorf(
"db-type was %q but should have been one of {%s, %s}",
"db-type was %q but should have been one of {%s, %s, %s}",
n.Config.DatabaseConfig.Name,
leveldb.Name,
memdb.Name,
pebble.Name,
)
}

Expand Down

0 comments on commit 4957ccb

Please sign in to comment.