Skip to content

Commit

Permalink
feat(store/v2): Support Custom WriteOpts PebbleDB (cosmos#18195)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Oct 24, 2023
1 parent 5afaeca commit 90723b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
6 changes: 4 additions & 2 deletions store/storage/pebbledb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -32,6 +33,7 @@ func NewBatch(storage *pebble.DB, version uint64) (*Batch, error) {
storage: storage,
batch: batch,
version: version,
sync: sync,
}, nil
}

Expand Down Expand Up @@ -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})
}
25 changes: 17 additions & 8 deletions store/storage/pebbledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions store/storage/pebbledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func TestStorageTestSuite(t *testing.T) {
"TestStorageTestSuite/TestDatabase_Prune",
},
}

suite.Run(t, s)
}

0 comments on commit 90723b7

Please sign in to comment.