Skip to content

Commit

Permalink
Reduce Badger in-memory cache sizes
Browse files Browse the repository at this point in the history
The default is 1 GiB per badger instance and we use a few instances so
this resulted in some nice memory usage.
  • Loading branch information
kostko committed Dec 19, 2019
1 parent ccc2d2d commit f35116e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
2 changes: 2 additions & 0 deletions go/common/persistent/persistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewCommonStore(dataDir string) (*CommonStore, error) {
opts = opts.WithLogger(ekbadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 128 KiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(128 * 1024)

db, err := badger.Open(opts)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions go/consensus/tendermint/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/tendermint/tendermint/node"
dbm "github.com/tendermint/tm-db"

ekbadger "github.com/oasislabs/oasis-core/go/common/badger"
cmnBadger "github.com/oasislabs/oasis-core/go/common/badger"
"github.com/oasislabs/oasis-core/go/common/logging"
)

Expand Down Expand Up @@ -43,7 +43,7 @@ type badgerDBImpl struct {
logger *logging.Logger

db *badger.DB
gc *ekbadger.GCWorker
gc *cmnBadger.GCWorker

closeOnce sync.Once
}
Expand All @@ -61,9 +61,11 @@ func New(fn string, noSuffix bool) (dbm.DB, error) {
logger := baseLogger.With("path", fn)

opts := badger.DefaultOptions(fn) // This may benefit from LSMOnlyOptions.
opts = opts.WithLogger(ekbadger.NewLogAdapter(logger))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(false)
opts = opts.WithCompression(options.Snappy)
// Reduce cache size to 64 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(64 * 1024 * 1024)

db, err := badger.Open(opts)
if err != nil {
Expand All @@ -73,7 +75,7 @@ func New(fn string, noSuffix bool) (dbm.DB, error) {
impl := &badgerDBImpl{
logger: logger,
db: db,
gc: ekbadger.NewGCWorker(logger, db),
gc: cmnBadger.NewGCWorker(logger, db),
}

return impl, nil
Expand Down
2 changes: 2 additions & 0 deletions go/runtime/history/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func newDB(fn string, runtimeID signature.PublicKey) (*DB, error) {
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 10 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(10 * 1024 * 1024)

db, err := badger.Open(opts)
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions go/storage/mkvs/urkel/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/pkg/errors"

"github.com/oasislabs/oasis-core/go/common"
ekbadger "github.com/oasislabs/oasis-core/go/common/badger"
cmnBadger "github.com/oasislabs/oasis-core/go/common/badger"
"github.com/oasislabs/oasis-core/go/common/cbor"
"github.com/oasislabs/oasis-core/go/common/crypto/hash"
"github.com/oasislabs/oasis-core/go/common/keyformat"
Expand Down Expand Up @@ -106,9 +106,11 @@ func New(cfg *api.Config) (api.NodeDB, error) {
db.CheckpointableDB = api.NewCheckpointableDB(db)

opts := badger.DefaultOptions(cfg.DB)
opts = opts.WithLogger(ekbadger.NewLogAdapter(db.logger))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(db.logger))
opts = opts.WithSyncWrites(!cfg.DebugNoFsync)
opts = opts.WithCompression(options.None)
// Reduce cache size to 64 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(64 * 1024 * 1024)

var err error
if db.db, err = badger.Open(opts); err != nil {
Expand All @@ -121,7 +123,7 @@ func New(cfg *api.Config) (api.NodeDB, error) {
return nil, errors.Wrap(err, "urkel/db/badger: failed to load metadata")
}

db.gc = ekbadger.NewGCWorker(db.logger, db.db)
db.gc = cmnBadger.NewGCWorker(db.logger, db.db)

return db, nil
}
Expand All @@ -132,7 +134,7 @@ type badgerNodeDB struct {
logger *logging.Logger

db *badger.DB
gc *ekbadger.GCWorker
gc *cmnBadger.GCWorker
meta metadata

closeOnce sync.Once
Expand Down
10 changes: 6 additions & 4 deletions go/worker/common/host/local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"

ekbadger "github.com/oasislabs/oasis-core/go/common/badger"
cmnBadger "github.com/oasislabs/oasis-core/go/common/badger"
"github.com/oasislabs/oasis-core/go/common/cbor"
"github.com/oasislabs/oasis-core/go/common/crypto/signature"
"github.com/oasislabs/oasis-core/go/common/keyformat"
Expand All @@ -26,7 +26,7 @@ type LocalStorage struct {
logger *logging.Logger

db *badger.DB
gc *ekbadger.GCWorker
gc *cmnBadger.GCWorker
}

func (s *LocalStorage) Get(id signature.PublicKey, key []byte) ([]byte, error) {
Expand Down Expand Up @@ -97,15 +97,17 @@ func NewLocalStorage(dataDir, fn string) (*LocalStorage, error) {
}

opts := badger.DefaultOptions(filepath.Join(dataDir, fn))
opts = opts.WithLogger(ekbadger.NewLogAdapter(s.logger))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(s.logger))
opts = opts.WithSyncWrites(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 128 KiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(128 * 1024)

var err error
if s.db, err = badger.Open(opts); err != nil {
return nil, fmt.Errorf("failed to open local storage database: %w", err)
}
s.gc = ekbadger.NewGCWorker(s.logger, s.db)
s.gc = cmnBadger.NewGCWorker(s.logger, s.db)

// TODO: The file format could be versioned, but it's not like this
// really is subject to change.
Expand Down

0 comments on commit f35116e

Please sign in to comment.