Skip to content

Commit

Permalink
Make rocksdb configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Jul 31, 2023
1 parent 667b6d0 commit 86d1173
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile-rocksdb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ WORKDIR /root
# default home directory is /root

# install rocksdb
ARG rocksdb_version=v7.10.2
ARG rocksdb_version=v8.1.1
ENV ROCKSDB_VERSION=$rocksdb_version

RUN git clone https://github.com/facebook/rocksdb.git \
Expand Down
11 changes: 7 additions & 4 deletions cmd/kava/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/kava-labs/kava/cmd/kava/opendb"
"os"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -105,13 +106,15 @@ func addSubCmds(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, de
encodingConfig: encodingConfig,
}

opts := ethermintserver.StartOptions{
AppCreator: ac.newApp,
DefaultNodeHome: app.DefaultNodeHome,
DBOpener: opendb.OpenDB,
}
// ethermintserver adds additional flags to start the JSON-RPC server for evm support
ethermintserver.AddCommands(
rootCmd,
ethermintserver.NewDefaultStartOptions(
ac.newApp,
app.DefaultNodeHome,
),
opts,
ac.appExport,
ac.addStartCmdFlags,
)
Expand Down
125 changes: 125 additions & 0 deletions cmd/kava/opendb/opendb_rocksdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2021 Kava Labs, Inc.
// Copyright 2020 Cronos Labs, Inc.
//
// Derived from https://github.com/crypto-org-chain/cronos@496ce7e
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opendb

import (
"path/filepath"
"runtime"
"strings"

"github.com/cosmos/cosmos-sdk/server/types"
"github.com/linxGnu/grocksdb"
dbm "github.com/tendermint/tm-db"
)

const BlockCacheSize = 1 << 30

func OpenDB(_ types.AppOptions, home string, backendType dbm.BackendType) (dbm.DB, error) {
dataDir := filepath.Join(home, "data")
if backendType == dbm.RocksDBBackend {
return openRocksdb(filepath.Join(dataDir, "application.db"))
}

return dbm.NewDB("application", backendType, dataDir)
}

func openRocksdb(dir string) (dbm.DB, error) {
opts, err := loadLatestOptions(dir)
if err != nil {
return nil, err
}
// customize rocksdb options
opts = NewRocksdbOptions(opts, false)

// func NewRocksDBWithOptions(name string, dir string, opts *grocksdb.Options) (*RocksDB, error) {
return dbm.NewRocksDBWithOptions("application", dir, opts)
}

// 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
}

cfNames := opts.ColumnFamilyNames()
cfOpts := opts.ColumnFamilyOpts()

for i := 0; i < len(cfNames); i++ {
if cfNames[i] == "default" {
return &cfOpts[i], nil
}
}

return opts.Options(), nil
}

// NewRocksdbOptions build options for `application.db`,
// it overrides existing options if provided, otherwise create new one assuming it's a new database.
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)
opts.SetTargetFileSizeMultiplier(2)

// block based table options
bbto := grocksdb.NewDefaultBlockBasedTableOptions()

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

// http://rocksdb.org/blog/2021/12/29/ribbon-filter.html
bbto.SetFilterPolicy(grocksdb.NewRibbonHybridFilterPolicy(9.9, 1))

// partition index
// http://rocksdb.org/blog/2017/05/12/partitioned-index-filter.html
bbto.SetIndexType(grocksdb.KTwoLevelIndexSearchIndexType)
bbto.SetPartitionFilters(true)
bbto.SetOptimizeFiltersForMemory(true)

// hash index is better for iavl tree which mostly do point lookup.
bbto.SetDataBlockIndexType(grocksdb.KDataBlockIndexTypeBinarySearchAndHash)

opts.SetBlockBasedTableFactory(bbto)

// in iavl tree, we almost always query existing keys
opts.SetOptimizeFiltersForHits(true)

// heavier compression option at bottommost level,
// 110k dict bytes is default in zstd library,
// train bytes is recommended to be set at 100x dict bytes.
opts.SetBottommostCompression(grocksdb.ZSTDCompression)
compressOpts := grocksdb.NewDefaultCompressionOptions()
compressOpts.Level = 12
if !sstFileWriter {
compressOpts.MaxDictBytes = 110 * 1024
opts.SetBottommostCompressionOptionsZstdMaxTrainBytes(compressOpts.MaxDictBytes*100, true)
}
opts.SetBottommostCompressionOptions(compressOpts, true)

return opts
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ require (
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/linxGnu/grocksdb v1.8.0
github.com/pelletier/go-toml/v2 v2.0.6
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.3
github.com/subosito/gotenv v1.4.2
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
Expand Down Expand Up @@ -129,7 +130,6 @@ require (
github.com/klauspost/compress v1.15.15 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.7.15 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,8 @@ 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 h1:AEhP28lkeAybv5UYNYviYISpR6bJejEnKuYbnWAnxx0=
github.com/linxGnu/grocksdb v1.7.15/go.mod h1:pY55D0o+r8yUYLq70QmhdudxYvoDb9F+9puf4m3/W+U=
github.com/linxGnu/grocksdb v1.8.0 h1:H4L/LhP7GOMf1j17oQAElHgVlbEje2h14A8Tz9cM2BE=
github.com/linxGnu/grocksdb v1.8.0/go.mod h1:09CeBborffXhXdNpEcOeZrLKEnRtrZFEpFdPNI9Zjjg=
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 @@ -1130,8 +1130,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
Expand Down

0 comments on commit 86d1173

Please sign in to comment.