1
0
Fork 0
forked from forks/go-ethereum

core, eth, node: use sync write option in pebble (#31519)

Fixes #31499
This commit is contained in:
rjl493456442 2025-04-03 22:03:03 +08:00 committed by GitHub
parent 49f0d49e89
commit 553183e5de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 23 additions and 18 deletions

View file

@ -183,7 +183,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
if !disk {
db = rawdb.NewMemoryDatabase()
} else {
pdb, err := pebble.New(b.TempDir(), 128, 128, "", false)
pdb, err := pebble.New(b.TempDir(), 128, 128, "", false, true)
if err != nil {
b.Fatalf("cannot create temporary database: %v", err)
}
@ -303,7 +303,7 @@ func makeChainForBench(db ethdb.Database, genesis *Genesis, full bool, count uin
func benchWriteChain(b *testing.B, full bool, count uint64) {
genesis := &Genesis{Config: params.AllEthashProtocolChanges}
for i := 0; i < b.N; i++ {
pdb, err := pebble.New(b.TempDir(), 1024, 128, "", false)
pdb, err := pebble.New(b.TempDir(), 1024, 128, "", false, true)
if err != nil {
b.Fatalf("error opening database: %v", err)
}
@ -316,7 +316,7 @@ func benchWriteChain(b *testing.B, full bool, count uint64) {
func benchReadChain(b *testing.B, full bool, count uint64) {
dir := b.TempDir()
pdb, err := pebble.New(dir, 1024, 128, "", false)
pdb, err := pebble.New(dir, 1024, 128, "", false, true)
if err != nil {
b.Fatalf("error opening database: %v", err)
}
@ -332,7 +332,7 @@ func benchReadChain(b *testing.B, full bool, count uint64) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
pdb, err = pebble.New(dir, 1024, 128, "", false)
pdb, err = pebble.New(dir, 1024, 128, "", false, true)
if err != nil {
b.Fatalf("error opening database: %v", err)
}

View file

@ -1765,7 +1765,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := pebble.New(datadir, 0, 0, "", false)
pdb, err := pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@ -1850,7 +1850,7 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
chain.stopWithoutSaving()
// Start a new blockchain back up and see where the repair leads us
pdb, err = pebble.New(datadir, 0, 0, "", false)
pdb, err = pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to reopen persistent key-value database: %v", err)
}
@ -1915,7 +1915,7 @@ func testIssue23496(t *testing.T, scheme string) {
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := pebble.New(datadir, 0, 0, "", false)
pdb, err := pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@ -1973,7 +1973,7 @@ func testIssue23496(t *testing.T, scheme string) {
chain.stopWithoutSaving()
// Start a new blockchain back up and see where the repair leads us
pdb, err = pebble.New(datadir, 0, 0, "", false)
pdb, err = pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to reopen persistent key-value database: %v", err)
}

View file

@ -1969,7 +1969,7 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := pebble.New(datadir, 0, 0, "", false)
pdb, err := pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}

View file

@ -66,7 +66,7 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo
datadir := t.TempDir()
ancient := filepath.Join(datadir, "ancient")
pdb, err := pebble.New(datadir, 0, 0, "", false)
pdb, err := pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}
@ -257,7 +257,7 @@ func (snaptest *crashSnapshotTest) test(t *testing.T) {
chain.triedb.Close()
// Start a new blockchain back up and see where the repair leads us
pdb, err := pebble.New(snaptest.datadir, 0, 0, "", false)
pdb, err := pebble.New(snaptest.datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}

View file

@ -2491,7 +2491,7 @@ func testSideImportPrunedBlocks(t *testing.T, scheme string) {
datadir := t.TempDir()
ancient := path.Join(datadir, "ancient")
pdb, err := pebble.New(datadir, 0, 0, "", false)
pdb, err := pebble.New(datadir, 0, 0, "", false, true)
if err != nil {
t.Fatalf("Failed to create persistent key-value database: %v", err)
}

View file

@ -144,7 +144,7 @@ func (l panicLogger) Fatalf(format string, args ...interface{}) {
// New returns a wrapped pebble DB object. The namespace is the prefix that the
// metrics reporting should use for surfacing internal stats.
func New(file string, cache int, handles int, namespace string, readonly bool) (*Database, error) {
func New(file string, cache int, handles int, namespace string, readonly bool, ephemeral bool) (*Database, error) {
// Ensure we have some minimal caching and file guarantees
if cache < minCache {
cache = minCache
@ -185,7 +185,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool) (
fn: file,
log: logger,
quitChan: make(chan chan error),
writeOptions: &pebble.WriteOptions{Sync: false},
writeOptions: &pebble.WriteOptions{Sync: !ephemeral},
}
opt := &pebble.Options{
// Pebble has a single combined cache area and the write

View file

@ -36,6 +36,11 @@ type openOptions struct {
Cache int // the capacity(in megabytes) of the data caching
Handles int // number of files to be open simultaneously
ReadOnly bool
// Ephemeral means that filesystem sync operations should be avoided:
// data integrity in the face of a crash is not important. This option
// should typically be used in tests.
Ephemeral bool
}
// openDatabase opens both a disk-based key-value database such as leveldb or pebble, but also
@ -78,7 +83,7 @@ func openKeyValueDatabase(o openOptions) (ethdb.Database, error) {
}
if o.Type == rawdb.DBPebble || existingDb == rawdb.DBPebble {
log.Info("Using pebble as the backing database")
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
}
if o.Type == rawdb.DBLeveldb || existingDb == rawdb.DBLeveldb {
log.Info("Using leveldb as the backing database")
@ -86,7 +91,7 @@ func openKeyValueDatabase(o openOptions) (ethdb.Database, error) {
}
// No pre-existing database, no user-requested one either. Default to Pebble.
log.Info("Defaulting to pebble as the backing database")
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
return newPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
}
// 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
// newPebbleDBDatabase creates a persistent key-value database without a freezer
// moving immutable chain segments into cold storage.
func newPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly bool) (ethdb.Database, error) {
db, err := pebble.New(file, cache, handles, namespace, readonly)
func newPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly bool, ephemeral bool) (ethdb.Database, error) {
db, err := pebble.New(file, cache, handles, namespace, readonly, ephemeral)
if err != nil {
return nil, err
}