Skip to content

Commit

Permalink
go/badger: enable truncate to recover from corrupted value log file
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Feb 27, 2020
1 parent 470bbd3 commit 88c90e3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/2732.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/badger: enable truncate to recover from corrupted value log file

Apparently badger is not at all resilient to crashes unless the truncate
option is enabled.
3 changes: 3 additions & 0 deletions go/common/persistent/persistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func NewCommonStore(dataDir string) (*CommonStore, error) {
opts := badger.DefaultOptions(filepath.Join(dataDir, dbName))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(true)
// Allow value log truncation if required (this is needed to recover the
// value log file which can get corrupted in crashes).
opts = opts.WithTruncate(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 128 KiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(128 * 1024)
Expand Down
3 changes: 3 additions & 0 deletions go/consensus/tendermint/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func New(fn string, noSuffix bool) (dbm.DB, error) {
opts := badger.DefaultOptions(fn) // This may benefit from LSMOnlyOptions.
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(false)
// Allow value log truncation if required (this is needed to recover the
// value log file which can get corrupted in crashes).
opts = opts.WithTruncate(true)
opts = opts.WithCompression(options.Snappy)
// Reduce cache size to 64 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(64 * 1024 * 1024)
Expand Down
3 changes: 3 additions & 0 deletions go/runtime/history/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ func newDB(fn string, runtimeID common.Namespace) (*DB, error) {
opts := badger.DefaultOptions(fn)
opts = opts.WithLogger(cmnBadger.NewLogAdapter(logger))
opts = opts.WithSyncWrites(true)
// Allow value log truncation if required (this is needed to recover the
// value log file which can get corrupted in crashes).
opts = opts.WithTruncate(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 10 MiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(10 * 1024 * 1024)
Expand Down
3 changes: 3 additions & 0 deletions go/runtime/localstorage/localstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func New(dataDir, fn string, runtimeID common.Namespace) (LocalStorage, error) {
opts := badger.DefaultOptions(filepath.Join(dataDir, fn))
opts = opts.WithLogger(cmnBadger.NewLogAdapter(s.logger))
opts = opts.WithSyncWrites(true)
// Allow value log truncation if required (this is needed to recover the
// value log file which can get corrupted in crashes).
opts = opts.WithTruncate(true)
opts = opts.WithCompression(options.None)
// Reduce cache size to 128 KiB as the default is 1 GiB.
opts = opts.WithMaxCacheSize(128 * 1024)
Expand Down
3 changes: 3 additions & 0 deletions go/storage/mkvs/urkel/db/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func New(cfg *api.Config) (api.NodeDB, error) {
opts := badger.DefaultOptions(cfg.DB)
opts = opts.WithLogger(cmnBadger.NewLogAdapter(db.logger))
opts = opts.WithSyncWrites(!cfg.DebugNoFsync)
// Allow value log truncation if required (this is needed to recover the
// value log file which can get corrupted in crashes).
opts = opts.WithTruncate(true)
opts = opts.WithCompression(options.None)
opts = opts.WithMaxCacheSize(cfg.MaxCacheSize)

Expand Down

0 comments on commit 88c90e3

Please sign in to comment.