Skip to content

Commit f0f8703

Browse files
authored
core/rawdb, ethdb/pebble: avoid fsync db in tests (#27836)
Adds an option to disable fsync for database operations. This is to make tests faster.
1 parent 5c7136a commit f0f8703

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

core/blockchain_repair_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
17671767
db, err := rawdb.Open(rawdb.OpenOptions{
17681768
Directory: datadir,
17691769
AncientsDirectory: ancient,
1770+
Ephemeral: true,
17701771
})
17711772
if err != nil {
17721773
t.Fatalf("Failed to create persistent database: %v", err)
@@ -1847,6 +1848,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
18471848
db, err = rawdb.Open(rawdb.OpenOptions{
18481849
Directory: datadir,
18491850
AncientsDirectory: ancient,
1851+
Ephemeral: true,
18501852
})
18511853
if err != nil {
18521854
t.Fatalf("Failed to reopen persistent database: %v", err)
@@ -1968,6 +1970,7 @@ func testIssue23496(t *testing.T, scheme string) {
19681970
db, err = rawdb.Open(rawdb.OpenOptions{
19691971
Directory: datadir,
19701972
AncientsDirectory: ancient,
1973+
Ephemeral: true,
19711974
})
19721975
if err != nil {
19731976
t.Fatalf("Failed to reopen persistent database: %v", err)

core/blockchain_sethead_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
19711971
db, err := rawdb.Open(rawdb.OpenOptions{
19721972
Directory: datadir,
19731973
AncientsDirectory: ancient,
1974+
Ephemeral: true,
19741975
})
19751976
if err != nil {
19761977
t.Fatalf("Failed to create persistent database: %v", err)

core/blockchain_snapshot_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo
6868
db, err := rawdb.Open(rawdb.OpenOptions{
6969
Directory: datadir,
7070
AncientsDirectory: ancient,
71+
Ephemeral: true,
7172
})
7273
if err != nil {
7374
t.Fatalf("Failed to create persistent database: %v", err)
@@ -258,6 +259,7 @@ func (snaptest *crashSnapshotTest) test(t *testing.T) {
258259
newdb, err := rawdb.Open(rawdb.OpenOptions{
259260
Directory: snaptest.datadir,
260261
AncientsDirectory: snaptest.ancient,
262+
Ephemeral: true,
261263
})
262264
if err != nil {
263265
t.Fatalf("Failed to reopen persistent database: %v", err)

core/rawdb/database.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ type OpenOptions struct {
352352
Cache int // the capacity(in megabytes) of the data caching
353353
Handles int // number of files to be open simultaneously
354354
ReadOnly bool
355+
// Ephemeral means that filesystem sync operations should be avoided: data integrity in the face of
356+
// a crash is not important. This option should typically be used in tests.
357+
Ephemeral bool
355358
}
356359

357360
// openKeyValueDatabase opens a disk-based key-value database, e.g. leveldb or pebble.
@@ -374,7 +377,7 @@ func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
374377
if o.Type == dbPebble || existingDb == dbPebble {
375378
if PebbleEnabled {
376379
log.Info("Using pebble as the backing database")
377-
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
380+
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
378381
} else {
379382
return nil, errors.New("db.engine 'pebble' not supported on this platform")
380383
}
@@ -387,7 +390,7 @@ func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
387390
// on supported platforms and LevelDB on anything else.
388391
if PebbleEnabled {
389392
log.Info("Defaulting to pebble as the backing database")
390-
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
393+
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
391394
} else {
392395
log.Info("Defaulting to leveldb as the backing database")
393396
return NewLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)

core/rawdb/databases_64bit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const PebbleEnabled = true
2828

2929
// NewPebbleDBDatabase creates a persistent key-value database without a freezer
3030
// moving immutable chain segments into cold storage.
31-
func NewPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly bool) (ethdb.Database, error) {
32-
db, err := pebble.New(file, cache, handles, namespace, readonly)
31+
func NewPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly, ephemeral bool) (ethdb.Database, error) {
32+
db, err := pebble.New(file, cache, handles, namespace, readonly, ephemeral)
3333
if err != nil {
3434
return nil, err
3535
}

ethdb/pebble/pebble.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ type Database struct {
8484
writeDelayStartTime time.Time // The start time of the latest write stall
8585
writeDelayCount atomic.Int64 // Total number of write stall counts
8686
writeDelayTime atomic.Int64 // Total time spent in write stalls
87+
88+
writeOptions *pebble.WriteOptions
8789
}
8890

8991
func (d *Database) onCompactionBegin(info pebble.CompactionInfo) {
@@ -118,7 +120,7 @@ func (d *Database) onWriteStallEnd() {
118120

119121
// New returns a wrapped pebble DB object. The namespace is the prefix that the
120122
// metrics reporting should use for surfacing internal stats.
121-
func New(file string, cache int, handles int, namespace string, readonly bool) (*Database, error) {
123+
func New(file string, cache int, handles int, namespace string, readonly bool, ephemeral bool) (*Database, error) {
122124
// Ensure we have some minimal caching and file guarantees
123125
if cache < minCache {
124126
cache = minCache
@@ -142,9 +144,10 @@ func New(file string, cache int, handles int, namespace string, readonly bool) (
142144
memTableSize = maxMemTableSize
143145
}
144146
db := &Database{
145-
fn: file,
146-
log: logger,
147-
quitChan: make(chan chan error),
147+
fn: file,
148+
log: logger,
149+
quitChan: make(chan chan error),
150+
writeOptions: &pebble.WriteOptions{Sync: !ephemeral},
148151
}
149152
opt := &pebble.Options{
150153
// Pebble has a single combined cache area and the write
@@ -279,7 +282,7 @@ func (d *Database) Put(key []byte, value []byte) error {
279282
if d.closed {
280283
return pebble.ErrClosed
281284
}
282-
return d.db.Set(key, value, pebble.Sync)
285+
return d.db.Set(key, value, d.writeOptions)
283286
}
284287

285288
// Delete removes the key from the key-value store.
@@ -535,7 +538,7 @@ func (b *batch) Write() error {
535538
if b.db.closed {
536539
return pebble.ErrClosed
537540
}
538-
return b.b.Commit(pebble.Sync)
541+
return b.b.Commit(b.db.writeOptions)
539542
}
540543

541544
// Reset resets the batch for reuse.

0 commit comments

Comments
 (0)