diff --git a/store/storage/pebbledb/batch.go b/store/storage/pebbledb/batch.go index 88a7ec99c3e3..5e9548d97c25 100644 --- a/store/storage/pebbledb/batch.go +++ b/store/storage/pebbledb/batch.go @@ -16,9 +16,10 @@ type Batch struct { storage *pebble.DB batch *pebble.Batch version uint64 + sync bool } -func NewBatch(storage *pebble.DB, version uint64) (*Batch, error) { +func NewBatch(storage *pebble.DB, version uint64, sync bool) (*Batch, error) { var versionBz [VersionSize]byte binary.LittleEndian.PutUint64(versionBz[:], version) @@ -32,6 +33,7 @@ func NewBatch(storage *pebble.DB, version uint64) (*Batch, error) { storage: storage, batch: batch, version: version, + sync: sync, }, nil } @@ -67,5 +69,5 @@ func (b *Batch) Write() (err error) { err = errors.Join(err, b.batch.Close()) }() - return b.batch.Commit(defaultWriteOpts) + return b.batch.Commit(&pebble.WriteOptions{Sync: b.sync}) } diff --git a/store/storage/pebbledb/db.go b/store/storage/pebbledb/db.go index 9203d81c5839..2f3902c09f90 100644 --- a/store/storage/pebbledb/db.go +++ b/store/storage/pebbledb/db.go @@ -21,14 +21,21 @@ const ( tombstoneVal = "TOMBSTONE" ) -var ( - _ store.VersionedDatabase = (*Database)(nil) - - defaultWriteOpts = pebble.Sync -) +var _ store.VersionedDatabase = (*Database)(nil) type Database struct { storage *pebble.DB + + // Sync is whether to sync writes through the OS buffer cache and down onto + // the actual disk, if applicable. Setting Sync is required for durability of + // individual write operations but can result in slower writes. + // + // If false, and the process or machine crashes, then a recent write may be + // lost. This is due to the recently written data being buffered inside the + // process running Pebble. This differs from the semantics of a write system + // call in which the data is buffered in the OS buffer cache and would thus + // survive a process crash. + sync bool } func New(dataDir string) (*Database, error) { @@ -44,12 +51,14 @@ func New(dataDir string) (*Database, error) { return &Database{ storage: db, + sync: true, }, nil } -func NewWithDB(storage *pebble.DB) *Database { +func NewWithDB(storage *pebble.DB, sync bool) *Database { return &Database{ storage: storage, + sync: sync, } } @@ -62,7 +71,7 @@ func (db *Database) Close() error { func (db *Database) SetLatestVersion(version uint64) error { var ts [VersionSize]byte binary.LittleEndian.PutUint64(ts[:], version) - return db.storage.Set([]byte(latestVersionKey), ts[:], defaultWriteOpts) + return db.storage.Set([]byte(latestVersionKey), ts[:], &pebble.WriteOptions{Sync: db.sync}) } func (db *Database) GetLatestVersion() (uint64, error) { @@ -132,7 +141,7 @@ func (db *Database) Get(storeKey string, targetVersion uint64, key []byte) ([]by } func (db *Database) ApplyChangeset(version uint64, cs *store.Changeset) error { - b, err := NewBatch(db.storage, version) + b, err := NewBatch(db.storage, version, db.sync) if err != nil { return err } diff --git a/store/storage/pebbledb/db_test.go b/store/storage/pebbledb/db_test.go index 9465ad55f48f..dde316bf7f12 100644 --- a/store/storage/pebbledb/db_test.go +++ b/store/storage/pebbledb/db_test.go @@ -19,5 +19,6 @@ func TestStorageTestSuite(t *testing.T) { "TestStorageTestSuite/TestDatabase_Prune", }, } + suite.Run(t, s) }