Skip to content

Commit 46b6f58

Browse files
authored
Merge pull request #2316 from CortexFoundation/dev
use sync write option in pebble
2 parents 2bd7a0c + 3b327aa commit 46b6f58

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

core/bench_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
151151
if !disk {
152152
db = rawdb.NewMemoryDatabase()
153153
} else {
154-
pdb, err := pebble.New(b.TempDir(), 128, 128, "", false)
154+
pdb, err := pebble.New(b.TempDir(), 128, 128, "", false, true)
155155
if err != nil {
156156
b.Fatalf("cannot create temporary database: %v", err)
157157
}
@@ -249,7 +249,7 @@ func benchWriteChain(b *testing.B, full bool, count uint64) {
249249
if err != nil {
250250
b.Fatalf("cannot create temporary directory: %v", err)
251251
}
252-
pdb, err := pebble.New(b.TempDir(), 1024, 128, "", false)
252+
pdb, err := pebble.New(b.TempDir(), 1024, 128, "", false, true)
253253
if err != nil {
254254
b.Fatalf("error opening database: %v", err)
255255
}
@@ -267,7 +267,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
267267
}
268268
defer os.RemoveAll(dir)
269269

270-
pdb, err := pebble.New(dir, 1024, 128, "", false)
270+
pdb, err := pebble.New(dir, 1024, 128, "", false, true)
271271
if err != nil {
272272
b.Fatalf("error opening database: %v", err)
273273
}
@@ -280,7 +280,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
280280
b.ResetTimer()
281281

282282
for i := 0; i < b.N; i++ {
283-
pdb, err = pebble.New(dir, 1024, 128, "", false)
283+
pdb, err = pebble.New(dir, 1024, 128, "", false, true)
284284
if err != nil {
285285
b.Fatalf("error opening database: %v", err)
286286
}

ctxcdb/pebble/pebble.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (l panicLogger) Fatalf(format string, args ...any) {
141141

142142
// New returns a wrapped pebble DB object. The namespace is the prefix that the
143143
// metrics reporting should use for surfacing internal stats.
144-
func New(file string, cache int, handles int, namespace string, readonly bool) (*Database, error) {
144+
func New(file string, cache int, handles int, namespace string, readonly bool, ephemeral bool) (*Database, error) {
145145
// Ensure we have some minimal caching and file guarantees
146146
if cache < minCache {
147147
cache = minCache
@@ -175,7 +175,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool) (
175175
fn: file,
176176
log: logger,
177177
quitChan: make(chan chan error),
178-
writeOptions: &pebble.WriteOptions{Sync: false},
178+
writeOptions: &pebble.WriteOptions{Sync: !ephemeral},
179179
}
180180
opt := &pebble.Options{
181181
// Pebble has a single combined cache area and the write

node/database.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type openOptions struct {
3636
Cache int // the capacity(in megabytes) of the data caching
3737
Handles int // number of files to be open simultaneously
3838
ReadOnly bool
39+
40+
// Ephemeral means that filesystem sync operations should be avoided:
41+
// data integrity in the face of a crash is not important. This option
42+
// should typically be used in tests.
43+
Ephemeral bool
3944
}
4045

4146
// openDatabase opens both a disk-based key-value database such as leveldb or pebble, but also
@@ -78,15 +83,15 @@ func openKeyValueDatabase(o openOptions) (ctxcdb.Database, error) {
7883
}
7984
if o.Type == rawdb.DBPebble || existingDb == rawdb.DBPebble {
8085
log.Info("Using pebble as the backing database")
81-
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
86+
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
8287
}
8388
if o.Type == rawdb.DBLeveldb || existingDb == rawdb.DBLeveldb {
8489
log.Info("Using leveldb as the backing database")
8590
return newLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
8691
}
8792
// No pre-existing database, no user-requested one either. Default to Pebble.
8893
log.Info("Defaulting to pebble as the backing database")
89-
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
94+
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
9095
}
9196

9297
// newLevelDBDatabase creates a persistent key-value database without a freezer
@@ -102,8 +107,8 @@ func newLevelDBDatabase(file string, cache int, handles int, namespace string, r
102107

103108
// newPebbleDBDatabase creates a persistent key-value database without a freezer
104109
// moving immutable chain segments into cold storage.
105-
func newPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly bool) (ctxcdb.Database, error) {
106-
db, err := pebble.New(file, cache, handles, namespace, readonly)
110+
func newPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly bool, ephemeral bool) (ctxcdb.Database, error) {
111+
db, err := pebble.New(file, cache, handles, namespace, readonly, ephemeral)
107112
if err != nil {
108113
return nil, err
109114
}

0 commit comments

Comments
 (0)