Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3ece3a

Browse files
authored
cmd/utils, node: switch to Pebble as the default db if none exists (ethereum#27136)
* cmd/utils, node: switch to Pebble as the default db if none exists * node: fall back to LevelDB on platforms not supporting Pebble * core/rawdb, node: default to Pebble at the node level * cmd/geth: fix some tests explicitly using leveldb * ethdb/pebble: allow double closes, makes tests simpler
1 parent bbc565a commit d3ece3a

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

cmd/geth/dao_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBloc
121121
}
122122
// Retrieve the DAO config flag from the database
123123
path := filepath.Join(datadir, "geth", "chaindata")
124-
db, err := rawdb.NewLevelDBDatabase(path, 0, 0, "", false)
124+
db, err := rawdb.NewPebbleDBDatabase(path, 0, 0, "", false)
125125
if err != nil {
126126
t.Fatalf("test %d: failed to open test database: %v", test, err)
127127
}

cmd/geth/genesis_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func TestCustomBackend(t *testing.T) {
146146
return nil
147147
}
148148
for i, tt := range []backendTest{
149-
{ // When not specified, it should default to leveldb
150-
execArgs: []string{"--db.engine", "leveldb"},
149+
{ // When not specified, it should default to pebble
150+
execArgs: []string{"--db.engine", "pebble"},
151151
execExpect: "0x0000000000001338",
152152
},
153153
{ // Explicit leveldb

cmd/utils/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ var (
101101
}
102102
DBEngineFlag = &cli.StringFlag{
103103
Name: "db.engine",
104-
Usage: "Backing database implementation to use ('leveldb' or 'pebble')",
105-
Value: "leveldb",
104+
Usage: "Backing database implementation to use ('pebble' or 'leveldb')",
105+
Value: node.DefaultConfig.DBEngine,
106106
Category: flags.EthCategory,
107107
}
108108
AncientFlag = &flags.DirectoryFlag{

core/rawdb/database.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,15 @@ type OpenOptions struct {
358358
//
359359
// type == null type != null
360360
// +----------------------------------------
361-
// db is non-existent | leveldb default | specified type
362-
// db is existent | from db | specified type (if compatible)
361+
// db is non-existent | pebble default | specified type
362+
// db is existent | from db | specified type (if compatible)
363363
func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
364+
// Reject any unsupported database type
365+
if len(o.Type) != 0 && o.Type != dbLeveldb && o.Type != dbPebble {
366+
return nil, fmt.Errorf("unknown db.engine %v", o.Type)
367+
}
368+
// Retrieve any pre-existing database's type and use that or the requested one
369+
// as long as there's no conflict between the two types
364370
existingDb := hasPreexistingDb(o.Directory)
365371
if len(existingDb) != 0 && len(o.Type) != 0 && o.Type != existingDb {
366372
return nil, fmt.Errorf("db.engine choice was %v but found pre-existing %v database in specified data directory", o.Type, existingDb)
@@ -373,12 +379,19 @@ func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
373379
return nil, errors.New("db.engine 'pebble' not supported on this platform")
374380
}
375381
}
376-
if len(o.Type) != 0 && o.Type != dbLeveldb {
377-
return nil, fmt.Errorf("unknown db.engine %v", o.Type)
382+
if o.Type == dbLeveldb || existingDb == dbLeveldb {
383+
log.Info("Using leveldb as the backing database")
384+
return NewLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
385+
}
386+
// No pre-existing database, no user-requested one either. Default to Pebble
387+
// on supported platforms and LevelDB on anything else.
388+
if PebbleEnabled {
389+
log.Info("Defaulting to pebble as the backing database")
390+
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
391+
} else {
392+
log.Info("Defaulting to leveldb as the backing database")
393+
return NewLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
378394
}
379-
log.Info("Using leveldb as the backing database")
380-
// Use leveldb, either as default (no explicit choice), or pre-existing, or chosen explicitly
381-
return NewLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
382395
}
383396

384397
// Open opens both a disk-based key-value database such as leveldb or pebble, but also

ethdb/pebble/pebble.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,17 @@ func (d *Database) Close() error {
222222
d.quitLock.Lock()
223223
defer d.quitLock.Unlock()
224224

225-
if d.quitChan != nil {
226-
errc := make(chan error)
227-
d.quitChan <- errc
228-
if err := <-errc; err != nil {
229-
d.log.Error("Metrics collection failed", "err", err)
230-
}
231-
d.quitChan = nil
225+
// Allow double closing, simplifies things
226+
if d.quitChan == nil {
227+
return nil
228+
}
229+
errc := make(chan error)
230+
d.quitChan <- errc
231+
if err := <-errc; err != nil {
232+
d.log.Error("Metrics collection failed", "err", err)
232233
}
234+
d.quitChan = nil
235+
233236
return d.db.Close()
234237
}
235238

node/defaults.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ import (
2828
)
2929

3030
const (
31-
DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
32-
DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server
33-
DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
34-
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
35-
DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server
36-
DefaultGraphQLPort = 8547 // Default TCP port for the GraphQL server
37-
DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
38-
DefaultAuthPort = 8551 // Default port for the authenticated apis
31+
DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
32+
DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server
33+
DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
34+
DefaultWSPort = 8546 // Default TCP port for the websocket RPC server
35+
DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
36+
DefaultAuthPort = 8551 // Default port for the authenticated apis
3937
)
4038

4139
var (
@@ -64,7 +62,7 @@ var DefaultConfig = Config{
6462
MaxPeers: 50,
6563
NAT: nat.Any(),
6664
},
67-
DBEngine: "",
65+
DBEngine: "", // Use whatever exists, will default to Pebble if non-existent and supported
6866
}
6967

7068
// DefaultDataDir is the default data directory to use for the databases and other

0 commit comments

Comments
 (0)