Skip to content

Commit 843e3f7

Browse files
authored
close database on shutdown (#1403)
* bump avago to v1.12.0 * close database on shutdown * update release notes * rename verdb * close only if standalone db * bump avago branch version * bump avago
1 parent 6c98da7 commit 843e3f7

File tree

9 files changed

+55
-14
lines changed

9 files changed

+55
-14
lines changed

RELEASES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Pending Release
44

5+
## Updates
6+
7+
- Changed default write option from `Sync` to `NoSync` in PebbleDB
8+
9+
## Fixes
10+
11+
- Fixed database close on shutdown
12+
513
## [v0.6.11](https://github.com/ava-labs/subnet-evm/releases/tag/v0.6.11)
614

715
This release focuses on Standalone DB and database configs.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.8
55
require (
66
github.com/VictoriaMetrics/fastcache v1.12.1
77
github.com/antithesishq/antithesis-sdk-go v0.3.8
8-
github.com/ava-labs/avalanchego v1.12.0
8+
github.com/ava-labs/avalanchego v1.12.0-config-pebble-sync
99
github.com/cespare/cp v0.1.0
1010
github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233
1111
github.com/davecgh/go-spew v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ github.com/antithesishq/antithesis-sdk-go v0.3.8/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl
6060
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
6161
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
6262
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
63-
github.com/ava-labs/avalanchego v1.12.0 h1:NBx0vSOY1dCT0PeJzojIhNhx0NMQNem4GgTEN+v8Sx4=
64-
github.com/ava-labs/avalanchego v1.12.0/go.mod h1:yhD5dpZyStIVbxQ550EDi5w5SL7DQ/xGE6TIxosb7U0=
63+
github.com/ava-labs/avalanchego v1.12.0-config-pebble-sync h1:HvtadR1VLxsOe7Y2PSnNmLUXPvp1RnTgeWtk5cOuGYA=
64+
github.com/ava-labs/avalanchego v1.12.0-config-pebble-sync/go.mod h1:yhD5dpZyStIVbxQ550EDi5w5SL7DQ/xGE6TIxosb7U0=
6565
github.com/ava-labs/coreth v0.13.9-rc.1 h1:qIICpC/OZGYUP37QnLgIqqwGmxnLwLpZaUlqJNI85vU=
6666
github.com/ava-labs/coreth v0.13.9-rc.1/go.mod h1:7aMsRIo/3GBE44qWZMjnfqdqfcfZ5yShTTm2LObLaYo=
6767
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=

plugin/evm/block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (b *Block) Accept(context.Context) error {
5555

5656
// Although returning an error from Accept is considered fatal, it is good
5757
// practice to cleanup the batch we were modifying in the case of an error.
58-
defer vm.db.Abort()
58+
defer vm.versiondb.Abort()
5959

6060
log.Debug(fmt.Sprintf("Accepting block %s (%s) at height %d", b.ID().Hex(), b.ID(), b.Height()))
6161

@@ -74,7 +74,7 @@ func (b *Block) Accept(context.Context) error {
7474
return fmt.Errorf("failed to put %s as the last accepted block: %w", b.ID(), err)
7575
}
7676

77-
return b.vm.db.Commit()
77+
return b.vm.versiondb.Commit()
7878
}
7979

8080
// handlePrecompileAccept calls Accept on any logs generated with an active precompile address that implements

plugin/evm/database/database.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package database
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"path/filepath"
910

@@ -16,6 +17,7 @@ import (
1617
"github.com/ava-labs/avalanchego/database/versiondb"
1718
avalanchenode "github.com/ava-labs/avalanchego/node"
1819
"github.com/ava-labs/avalanchego/utils/logging"
20+
"github.com/prometheus/client_golang/prometheus"
1921
)
2022

2123
const (
@@ -44,7 +46,7 @@ func NewStandaloneDatabase(dbConfig avalanchenode.DatabaseConfig, gatherer metri
4446
db = memdb.New()
4547
case pebbledb.Name:
4648
dbPath := filepath.Join(dbConfig.Path, pebbledb.Name)
47-
db, err = pebbledb.New(dbPath, dbConfig.Config, logger, dbRegisterer)
49+
db, err = NewPebbleDB(dbPath, dbConfig.Config, logger, dbRegisterer)
4850
if err != nil {
4951
return nil, fmt.Errorf("couldn't create %s at %s: %w", pebbledb.Name, dbPath, err)
5052
}
@@ -77,3 +79,20 @@ func NewStandaloneDatabase(dbConfig avalanchenode.DatabaseConfig, gatherer metri
7779

7880
return db, nil
7981
}
82+
83+
func NewPebbleDB(file string, configBytes []byte, log logging.Logger, dbRegisterer prometheus.Registerer) (database.Database, error) {
84+
cfg := pebbledb.DefaultConfig
85+
// Use no sync for pebble db
86+
cfg.Sync = false
87+
if len(configBytes) > 0 {
88+
if err := json.Unmarshal(configBytes, &cfg); err != nil {
89+
return nil, err
90+
}
91+
}
92+
// Marshal the config back to bytes to ensure that new defaults are applied
93+
newCfgBytes, err := json.Marshal(cfg)
94+
if err != nil {
95+
return nil, err
96+
}
97+
return pebbledb.New(file, newCfgBytes, log, dbRegisterer)
98+
}

plugin/evm/syncervm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func testSyncerVM(t *testing.T, vmSetup *syncVMSetup, test syncTest) {
431431
if test.expectedErr != nil {
432432
require.ErrorIs(err, test.expectedErr)
433433
// Note we re-open the database here to avoid a closed error when the test is for a shutdown VM.
434-
chaindb := database.WrapDatabase(prefixdb.NewNested(ethDBPrefix, syncerVM.db))
434+
chaindb := database.WrapDatabase(prefixdb.NewNested(ethDBPrefix, syncerVM.versiondb))
435435
assertSyncPerformedHeights(t, chaindb, map[uint64]struct{}{})
436436
return
437437
}

plugin/evm/vm.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,20 @@ type VM struct {
195195
blockChain *core.BlockChain
196196
miner *miner.Miner
197197

198-
// [db] is the VM's current database managed by ChainState
199-
db *versiondb.Database
198+
// [versiondb] is the VM's current versioned database
199+
versiondb *versiondb.Database
200+
201+
// [db] is the VM's current database
202+
db database.Database
200203

201204
// metadataDB is used to store one off keys.
202205
metadataDB database.Database
203206

204207
// [chaindb] is the database supplied to the Ethereum backend
205208
chaindb ethdb.Database
206209

210+
usingStandaloneDB bool
211+
207212
// [acceptedBlockDB] is the database to store the last accepted
208213
// block.
209214
acceptedBlockDB database.Database
@@ -630,7 +635,7 @@ func (vm *VM) initializeStateSyncClient(lastAcceptedHeight uint64) error {
630635
chaindb: vm.chaindb,
631636
metadataDB: vm.metadataDB,
632637
acceptedBlockDB: vm.acceptedBlockDB,
633-
db: vm.db,
638+
db: vm.versiondb,
634639
toEngine: vm.toEngine,
635640
})
636641

@@ -880,6 +885,13 @@ func (vm *VM) Shutdown(context.Context) error {
880885
}
881886
vm.eth.Stop()
882887
log.Info("Ethereum backend stop completed")
888+
if vm.usingStandaloneDB {
889+
if err := vm.db.Close(); err != nil {
890+
log.Error("failed to close database: %w", err)
891+
} else {
892+
log.Info("Database closed")
893+
}
894+
}
883895
vm.shutdownWg.Wait()
884896
log.Info("Subnet-EVM Shutdown completed")
885897
return nil

plugin/evm/vm_database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ func (vm *VM) initializeDBs(avaDB avalanchedatabase.Database) error {
4646
if err != nil {
4747
return err
4848
}
49+
vm.usingStandaloneDB = true
4950
}
5051
}
5152
// Use NewNested rather than New so that the structure of the database
5253
// remains the same regardless of the provided baseDB type.
5354
vm.chaindb = rawdb.NewDatabase(database.WrapDatabase(prefixdb.NewNested(ethDBPrefix, db)))
54-
vm.db = versiondb.New(db)
55-
vm.acceptedBlockDB = prefixdb.New(acceptedPrefix, vm.db)
56-
vm.metadataDB = prefixdb.New(metadataPrefix, vm.db)
55+
vm.versiondb = versiondb.New(db)
56+
vm.acceptedBlockDB = prefixdb.New(acceptedPrefix, vm.versiondb)
57+
vm.metadataDB = prefixdb.New(metadataPrefix, vm.versiondb)
58+
vm.db = db
5759
// Note warpDB and validatorsDB are not part of versiondb because it is not necessary
5860
// that they are committed to the database atomically with
5961
// the last accepted block.

scripts/versions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# shellcheck disable=SC2034
55

66
# Don't export them as they're used in the context of other calls
7-
AVALANCHE_VERSION=${AVALANCHE_VERSION:-'v1.12.0'}
7+
AVALANCHE_VERSION=${AVALANCHE_VERSION:-'v1.12.0-config-pebble-sync'}
88
GINKGO_VERSION=${GINKGO_VERSION:-'v2.2.0'}

0 commit comments

Comments
 (0)