Skip to content

Commit

Permalink
make more resilient to buffer loss if server crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
DocSavage committed Feb 4, 2024
1 parent cbbd1e2 commit 592ac35
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ func Shutdown() {
}
datastore.Shutdown()
dvid.BlockOnActiveCgo()
storage.Shutdown()
rpc.Shutdown()
dvid.Shutdown()
shutdownCh <- struct{}{}
Expand Down
17 changes: 17 additions & 0 deletions storage/badger/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"hash/fnv"
"os"
"path/filepath"
"time"

"github.com/janelia-flyem/dvid/dvid"
"github.com/janelia-flyem/dvid/storage"
Expand Down Expand Up @@ -103,6 +104,19 @@ func parseConfig(config dvid.StoreConfig) (path string, testing bool, err error)
return
}

// Periodically sync to prevent too many writes from being buffered
// if server crashes.
func syncPeriodically(db *badger.DB) {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
db.Sync()
}
}
}

// newDB returns a Badger backend, creating one at path if it doesn't exist.
func (e Engine) newDB(config dvid.StoreConfig) (*BadgerDB, bool, error) {
path, _, err := parseConfig(config)
Expand Down Expand Up @@ -146,6 +160,8 @@ func (e Engine) newDB(config dvid.StoreConfig) (*BadgerDB, bool, error) {
}
badgerDB.bdp = bdp

go syncPeriodically(bdp)

// if we know it's newly created, just return.
if created {
return badgerDB, created, nil
Expand Down Expand Up @@ -226,6 +242,7 @@ func (db *BadgerDB) Close() {
if db != nil {
if db.bdp != nil {
db.bdp.Close()
dvid.Infof("Closed Badger DB @ %s\n", db.directory)
}
db.bdp = nil
db.options = nil
Expand Down

0 comments on commit 592ac35

Please sign in to comment.