Skip to content

Commit

Permalink
feat(db): expose badger num compactors to dymin toml (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwt authored Jan 9, 2025
1 parent 132f916 commit 8b2ab02
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ type DBConfig struct {
SyncWrites bool `mapstructure:"sync_writes"`
// InMemory sets the database to run in-memory, without touching the disk.
InMemory bool `mapstructure:"in_memory"`
// if zero, default is used
BadgerCompactors int `mapstructure:"badger_num_compactors"`
}

func (dbc DBConfig) Validate() error {
Expand Down
2 changes: 2 additions & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ sync_writes = {{ .DBConfig.SyncWrites }}
# When true, the database will run in-memory only (FOR EXPERIMENTAL USE ONLY)
in_memory = {{ .DBConfig.InMemory }}
# When zero/empty, uses default
badger_num_compactors = {{ .DBConfig.BadgerCompactors }}
#######################################################
### Instrumentation Configuration Options ###
Expand Down
3 changes: 2 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func NewNode(
dstore = datastore.NewMapDatastore()
} else {
// TODO(omritoptx): Move dymint to const
baseKV = store.NewKVStore(conf.RootDir, conf.DBPath, "dymint", conf.DBConfig.SyncWrites, logger)
baseKV = store.NewKVStore(conf.RootDir, conf.DBPath, "dymint",
store.BadgerOpts{SyncWrites: conf.DBConfig.SyncWrites, NumCompactors: conf.DBConfig.BadgerCompactors}, logger)
path := filepath.Join(store.Rootify(conf.RootDir, conf.DBPath), "blocksync")
var err error
dstore, err = leveldb.NewDatastore(path, &leveldb.Options{})
Expand Down
24 changes: 18 additions & 6 deletions store/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func NewDefaultInMemoryKVStore() KV {
}
}

func NewKVStore(rootDir, dbPath, dbName string, syncWrites bool, logger types.Logger) KV {
func NewKVStore(rootDir, dbPath, dbName string,
badgerOpts BadgerOpts,

logger types.Logger,
) KV {
path := filepath.Join(Rootify(rootDir, dbPath), dbName)
opts := memoryEfficientBadgerConfig(path, syncWrites)
opts := memoryEfficientBadgerConfig(path, badgerOpts)
db, err := badger.Open(*opts)
if err != nil {
panic(err)
Expand All @@ -60,7 +64,7 @@ func NewKVStore(rootDir, dbPath, dbName string, syncWrites bool, logger types.Lo

// NewDefaultKVStore creates instance of default key-value store.
func NewDefaultKVStore(rootDir, dbPath, dbName string) KV {
return NewKVStore(rootDir, dbPath, dbName, true, log.NewNopLogger())
return NewKVStore(rootDir, dbPath, dbName, BadgerOpts{SyncWrites: true}, log.NewNopLogger())
}

// Rootify is helper function to make config creation independent of root dir
Expand Down Expand Up @@ -228,13 +232,18 @@ func (i *BadgerIterator) Discard() {
i.txn.Discard()
}

type BadgerOpts struct {
SyncWrites bool
NumCompactors int
}

// memoryEfficientBadgerConfig sets badger configuration parameters to reduce memory usage, specially during compactions to avoid memory spikes that causes OOM.
// based on https://github.com/celestiaorg/celestia-node/issues/2905
func memoryEfficientBadgerConfig(path string, syncWrites bool) *badger.Options {
func memoryEfficientBadgerConfig(path string, o BadgerOpts) *badger.Options {
opts := badger.DefaultOptions(path) // this must be copied
// SyncWrites is a configuration option in Badger that determines whether writes are immediately synced to disk or no.
// If set to true it writes to the write-ahead log (value log) are synced to disk before being applied to the LSM tree.
opts.SyncWrites = syncWrites
opts.SyncWrites = o.SyncWrites
// default 64mib => 0 - disable block cache
// BlockCacheSize specifies how much data cache should hold in memory.
// It improves lookup performance but increases memory consumption.
Expand All @@ -254,7 +263,10 @@ func memoryEfficientBadgerConfig(path string, syncWrites bool) *badger.Options {
// default 15 => 5 - this prevents memory growth on CPU constraint systems by blocking all writers
opts.NumLevelZeroTablesStall = 5
// reducing number compactors, makes it slower but reduces memory usage during compaction
opts.NumCompactors = 2
opts.NumCompactors = o.NumCompactors
if opts.NumCompactors == 0 {
opts.NumCompactors = 2 // default
}
// makes sure badger is always compacted on shutdown
opts.CompactL0OnClose = true

Expand Down

0 comments on commit 8b2ab02

Please sign in to comment.