From 12f6c19be0943389278ec5eaa8df6805807f7d5f Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 1 Feb 2023 12:28:06 +0800 Subject: [PATCH] ethdb, core, cmd: polish and fixes --- cmd/utils/flags.go | 7 ++++--- core/rawdb/database.go | 21 +++++++++++++-------- core/rawdb/databases_64bit.go | 16 ++++++++++++++++ core/rawdb/databases_non64bit.go | 16 ++++++++++++++++ ethdb/leveldb/leveldb.go | 1 - ethdb/pebble/pebble.go | 14 +++++++------- ethdb/pebble/pebble_test.go | 2 +- 7 files changed, 57 insertions(+), 20 deletions(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 2d2cac1d7796..5b01d9a35480 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -100,9 +100,10 @@ var ( Category: flags.LoggingCategory, } BackingDBFlag = &cli.StringFlag{ - Name: "backingdb", - Usage: "Backing database implementation to use", - Value: "leveldb", + Name: "backingdb", + Usage: "Backing database implementation to use", + Value: "leveldb", + Category: flags.EthCategory, } AncientFlag = &flags.DirectoryFlag{ Name: "datadir.ancient", diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 1d3daec6a7ab..92eb8b72cdec 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -316,10 +316,10 @@ const ( // instantiated at that location, and if so, returns the type of database (or the // empty string). func hasPreexistingDb(path string) string { - if _, err := os.Stat(path + "/CURRENT"); err != nil { + if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil { return "" // No pre-existing db } - if matches, err := filepath.Glob(path + "/OPTIONS*"); len(matches) > 0 || err != nil { + if matches, err := filepath.Glob(filepath.Join(path, "OPTIONS*")); len(matches) > 0 || err != nil { if err != nil { panic(err) // only possible if the pattern is malformed } @@ -334,14 +334,19 @@ type OpenOptions struct { Type string // "leveldb" | "pebble" Directory string // the datadir AncientsDirectory string // the ancients-dir - Namespace string - Cache int // Cache size in MiB - Handles int + Namespace string // the namespace for database relevant metrics + Cache int // the capacity(in megabytes) of the data caching + Handles int // the capacity of the open files caching ReadOnly bool } -// openKvDb Opens a disk-based key-value database, e.g. leveldb or pebble. -func openKvDb(o OpenOptions) (ethdb.Database, error) { +// openKeyValueDatabase opens a disk-based key-value database, e.g. leveldb or pebble. +// +// type == null type != null +// +---------------------------------------- +// db is non-existent | leveldb default | specified type +// db is existent | from db | specified type (if compatible) +func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) { existingDb := hasPreexistingDb(o.Directory) if len(existingDb) != 0 && len(o.Type) != 0 && o.Type != existingDb { return nil, fmt.Errorf("backingdb choice was %v but found pre-existing %v database in specified data directory", o.Type, existingDb) @@ -366,7 +371,7 @@ func openKvDb(o OpenOptions) (ethdb.Database, error) { // The passed o.AncientDir indicates the path of root ancient directory where // the chain freezer can be opened. func Open(o OpenOptions) (ethdb.Database, error) { - kvdb, err := openKvDb(o) + kvdb, err := openKeyValueDatabase(o) if err != nil { return nil, err } diff --git a/core/rawdb/databases_64bit.go b/core/rawdb/databases_64bit.go index 7b998596ed0a..e468269e9d35 100644 --- a/core/rawdb/databases_64bit.go +++ b/core/rawdb/databases_64bit.go @@ -1,3 +1,19 @@ +// Copyright 2023 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see + //go:build arm64 || amd64 package rawdb diff --git a/core/rawdb/databases_non64bit.go b/core/rawdb/databases_non64bit.go index b3172acacfe5..4fa5bec7866e 100644 --- a/core/rawdb/databases_non64bit.go +++ b/core/rawdb/databases_non64bit.go @@ -1,3 +1,19 @@ +// Copyright 2023 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + //go:build !(arm64 || amd64) package rawdb diff --git a/ethdb/leveldb/leveldb.go b/ethdb/leveldb/leveldb.go index 08bca1b578a2..ce13659d9d97 100644 --- a/ethdb/leveldb/leveldb.go +++ b/ethdb/leveldb/leveldb.go @@ -144,7 +144,6 @@ func NewCustom(file string, namespace string, customize func(options *opt.Option ldb.level0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/level0", nil) ldb.nonlevel0CompGauge = metrics.NewRegisteredGauge(namespace+"compact/nonlevel0", nil) ldb.seekCompGauge = metrics.NewRegisteredGauge(namespace+"compact/seek", nil) - ldb.manualMemAllocGauge = metrics.NewRegisteredGauge(namespace+"memory/manualalloc", nil) // Start up the metrics gathering and return diff --git a/ethdb/pebble/pebble.go b/ethdb/pebble/pebble.go index 38f474e03397..8f50f803da0a 100644 --- a/ethdb/pebble/pebble.go +++ b/ethdb/pebble/pebble.go @@ -1,4 +1,4 @@ -// Copyright 2020 The go-ethereum Authors +// Copyright 2023 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify @@ -299,8 +299,6 @@ type snapshot struct { // data store. func (snap *snapshot) Has(key []byte) (bool, error) { _, closer, err := snap.db.Get(key) - defer closer.Close() - if err != nil { if err != pebble.ErrNotFound { return false, err @@ -308,19 +306,21 @@ func (snap *snapshot) Has(key []byte) (bool, error) { return false, nil } } + closer.Close() return true, nil } // Get retrieves the given key if it's present in the snapshot backing by // key-value data store. func (snap *snapshot) Get(key []byte) ([]byte, error) { - val, closer, err := snap.db.Get(key) - + dat, closer, err := snap.db.Get(key) if err != nil { return nil, err } + ret := make([]byte, len(dat)) + copy(ret, dat) closer.Close() - return val, nil + return ret, nil } // Release releases associated resources. Release should always succeed and can @@ -365,7 +365,7 @@ func (d *Database) Stat(property string) (string, error) { // is treated as a key after all keys in the data store. If both is nil then it // will compact entire data store. func (d *Database) Compact(start []byte, limit []byte) error { - return d.db.Compact(start, limit, false) + return d.db.Compact(start, limit, true) // Parallelization is preferred } // Path returns the path to the database directory. diff --git a/ethdb/pebble/pebble_test.go b/ethdb/pebble/pebble_test.go index 02d55fdded4e..18b800e8aba9 100644 --- a/ethdb/pebble/pebble_test.go +++ b/ethdb/pebble/pebble_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 The go-ethereum Authors +// Copyright 2023 The go-ethereum Authors // This file is part of the go-ethereum library. // // The go-ethereum library is free software: you can redistribute it and/or modify