Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: dynamic-level-bytes is not enabled for restored application.db #904

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
### Improvements

- [#890](/~https://github.com/crypto-org-chain/cronos/pull/890) optimize memiavl snapshot format.
- [#904](/~https://github.com/crypto-org-chain/cronos/pull/904) Enable "dynamic-level-bytes" on new `application.db`.

*Feb 09, 2022*

Expand Down
9 changes: 6 additions & 3 deletions cmd/cronosd/cmd/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/crypto-org-chain/cronos/v2/app"
"github.com/crypto-org-chain/cronos/v2/cmd/cronosd/opendb"
versiondbclient "github.com/crypto-org-chain/cronos/versiondb/client"
"github.com/linxGnu/grocksdb"
"github.com/spf13/cobra"
)

Expand All @@ -21,8 +22,10 @@ func ChangeSetCmd() *cobra.Command {
sort.Strings(storeNames)

return versiondbclient.ChangeSetGroupCmd(versiondbclient.Options{
DefaultStores: storeNames,
OpenReadOnlyDB: opendb.OpenReadOnlyDB,
AppRocksDBOptions: opendb.NewRocksdbOptions,
DefaultStores: storeNames,
OpenReadOnlyDB: opendb.OpenReadOnlyDB,
AppRocksDBOptions: func(sstFileWriter bool) *grocksdb.Options {
return opendb.NewRocksdbOptions(nil, sstFileWriter)
},
})
}
42 changes: 37 additions & 5 deletions cmd/cronosd/opendb/opendb_rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ package opendb
import (
"path/filepath"
"runtime"
"strings"

"github.com/linxGnu/grocksdb"
dbm "github.com/tendermint/tm-db"
)

const BlockCacheSize = 1 << 30

func OpenDB(home string, backendType dbm.BackendType) (dbm.DB, error) {
dataDir := filepath.Join(home, "data")
if backendType == dbm.RocksDBBackend {
dir := filepath.Join(dataDir, "application.db")
opts, err := loadLatestOptions(dir)
if err != nil {
return nil, err
}
// customize rocksdb options
db, err := grocksdb.OpenDb(NewRocksdbOptions(false), filepath.Join(dataDir, "application.db"))
db, err := grocksdb.OpenDb(NewRocksdbOptions(opts, false), dir)
if err != nil {
return nil, err
}
Expand All @@ -33,8 +41,13 @@ func OpenDB(home string, backendType dbm.BackendType) (dbm.DB, error) {
func OpenReadOnlyDB(home string, backendType dbm.BackendType) (dbm.DB, error) {
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
dataDir := filepath.Join(home, "data")
if backendType == dbm.RocksDBBackend {
dir := filepath.Join(dataDir, "application.db")
opts, err := loadLatestOptions(dir)
if err != nil {
return nil, err
}
// customize rocksdb options
db, err := grocksdb.OpenDbForReadOnly(NewRocksdbOptions(false), filepath.Join(dataDir, "application.db"), false)
db, err := grocksdb.OpenDbForReadOnly(NewRocksdbOptions(opts, false), dir, false)
if err != nil {
return nil, err
}
Expand All @@ -49,8 +62,27 @@ func OpenReadOnlyDB(home string, backendType dbm.BackendType) (dbm.DB, error) {
return dbm.NewDB("application", backendType, dataDir)
}

func NewRocksdbOptions(sstFileWriter bool) *grocksdb.Options {
opts := grocksdb.NewDefaultOptions()
// loadLatestOptions try to load options from existing db, returns nil if not exists.
func loadLatestOptions(dir string) (*grocksdb.Options, error) {
opts, err := grocksdb.LoadLatestOptions(dir, grocksdb.NewDefaultEnv(), true, grocksdb.NewLRUCache(BlockCacheSize))
if err != nil {
// not found is not an error
if strings.HasPrefix(err.Error(), "NotFound: ") {
return nil, nil
}
return nil, err
}
return opts.Options(), nil
}

// NewRocksdbOptions build options for `application.db`,
// it overrides exisitng options if provided, otherwise create new one assuming it's a new database.
yihuang marked this conversation as resolved.
Show resolved Hide resolved
func NewRocksdbOptions(opts *grocksdb.Options, sstFileWriter bool) *grocksdb.Options {
if opts == nil {
opts = grocksdb.NewDefaultOptions()
// only enable dynamic-level-bytes on new db, don't override for existing db
opts.SetLevelCompactionDynamicLevelBytes(true)
}
opts.SetCreateIfMissing(true)
opts.IncreaseParallelism(runtime.NumCPU())
opts.OptimizeLevelStyleCompaction(512 * 1024 * 1024)
Expand All @@ -60,7 +92,7 @@ func NewRocksdbOptions(sstFileWriter bool) *grocksdb.Options {
bbto := grocksdb.NewDefaultBlockBasedTableOptions()

// 1G block cache
bbto.SetBlockCache(grocksdb.NewLRUCache(1 << 30))
bbto.SetBlockCache(grocksdb.NewLRUCache(BlockCacheSize))

// http://rocksdb.org/blog/2021/12/29/ribbon-filter.html
bbto.SetFilterPolicy(grocksdb.NewRibbonHybridFilterPolicy(9.9, 1))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ replace (
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
// use replace to force update grocksdb dependency in tm-db
github.com/linxGnu/grocksdb => github.com/linxGnu/grocksdb v1.7.15-0.20230222024938-b61261a9193b
github.com/linxGnu/grocksdb => github.com/yihuang/grocksdb v1.7.15-0.20230303073550-e611b544c0cf
yihuang marked this conversation as resolved.
Show resolved Hide resolved
github.com/miguelmota/go-ethereum-hdwallet => github.com/crypto-org-chain/go-ethereum-hdwallet v0.1.2

github.com/peggyjv/gravity-bridge/module/v2 => github.com/crypto-org-chain/gravity-bridge/module/v2 v2.0.1-0.20230126051749-d984b1562242
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,6 @@ github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/linxGnu/grocksdb v1.7.15-0.20230222024938-b61261a9193b h1:Olh1b4gdN5J9ZZyCiudSZ7HLrU+IL4SkYFcgyM9rGsA=
github.com/linxGnu/grocksdb v1.7.15-0.20230222024938-b61261a9193b/go.mod h1:pY55D0o+r8yUYLq70QmhdudxYvoDb9F+9puf4m3/W+U=
github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -1196,6 +1194,8 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/ybbus/jsonrpc v2.1.2+incompatible/go.mod h1:XJrh1eMSzdIYFbM08flv0wp5G35eRniyeGut1z+LSiE=
github.com/yihuang/grocksdb v1.7.15-0.20230303073550-e611b544c0cf h1:wk3zHZQaeDGC1iYY/WjGAuD4dNAfD5vx/MMAlq5PUMw=
github.com/yihuang/grocksdb v1.7.15-0.20230303073550-e611b544c0cf/go.mod h1:pY55D0o+r8yUYLq70QmhdudxYvoDb9F+9puf4m3/W+U=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
6 changes: 3 additions & 3 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ schema = 3
version = "v0.1.0"
hash = "sha256-wQqGTtRWsfR9n0O/SXHVgECebbnNmHddxJIbG63OJBQ="
[mod."github.com/linxGnu/grocksdb"]
version = "v1.7.15-0.20230222024938-b61261a9193b"
hash = "sha256-Hry5mpO8WqCuYZ0zCnOt6kopDGprc7/nI318A2D+Kk0="
replaced = "github.com/linxGnu/grocksdb"
version = "v1.7.15-0.20230303073550-e611b544c0cf"
hash = "sha256-MuH+rIx+r3zirXdKPHFRYeAP7QORr9gSwSiCJST7534="
replaced = "github.com/yihuang/grocksdb"
[mod."github.com/magiconair/properties"]
version = "v1.8.6"
hash = "sha256-xToSfpuePctkTdhJtsuKIEkXwfMZbnkFT98ahIfd4wY="
Expand Down