Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Aug 31, 2023
1 parent 0604fd6 commit 9e44371
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
40 changes: 21 additions & 19 deletions cmd/kava/opendb/opendb_rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package opendb
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
Expand All @@ -44,6 +43,8 @@ const (

defaultColumnFamilyName = "default"

enableRocksdbMetricsOptName = "enable_rocksdb_metrics"

maxOpenFilesDBOptName = "max_open_files"
maxFileOpeningThreadsDBOptName = "max_file_opening_threads"

Expand All @@ -70,8 +71,9 @@ func openRocksdb(dir string, appOpts types.AppOptions) (dbm.DB, error) {
// customize rocksdb options
dbOpts = overrideDBOpts(dbOpts, appOpts)
cfOpts = overrideCFOpts(cfOpts, appOpts)
enableMetrics := cast.ToBool(appOpts.Get(enableRocksdbMetricsOptName))

return newRocksDBWithOptions("application", dir, dbOpts, cfOpts)
return newRocksDBWithOptions("application", dir, dbOpts, cfOpts, enableMetrics)
}

// loadLatestOptions loads and returns database and column family options
Expand Down Expand Up @@ -130,7 +132,7 @@ func overrideCFOpts(cfOpts *grocksdb.Options, appOpts types.AppOptions) *grocksd

// newRocksDBWithOptions opens rocksdb with provided database and column family options
// newRocksDBWithOptions expects that db has only one column family named default
func newRocksDBWithOptions(name string, dir string, dbOpts, cfOpts *grocksdb.Options) (*dbm.RocksDB, error) {
func newRocksDBWithOptions(name string, dir string, dbOpts, cfOpts *grocksdb.Options, enableMetrics bool) (*dbm.RocksDB, error) {
dbPath := filepath.Join(dir, name+".db")

// Ensure path exists
Expand All @@ -139,29 +141,29 @@ func newRocksDBWithOptions(name string, dir string, dbOpts, cfOpts *grocksdb.Opt
}

dbOpts.EnableStatistics()
//cfOpts.EnableStatistics()
db, _, err := grocksdb.OpenDbColumnFamilies(dbOpts, dbPath, []string{defaultColumnFamilyName}, []*grocksdb.Options{cfOpts})
if err != nil {
return nil, err
}

registerMetrics()

go func() {
ticker := time.NewTicker(time.Second * 15)
for {
select {
case <-ticker.C:
props, stats, err := getPropsAndStats(db)
if err != nil {
log.Fatal(err)
continue
}
if enableMetrics {
registerMetrics()

go func() {
ticker := time.NewTicker(time.Second * 15)
for {
select {
case <-ticker.C:
props, stats, err := getPropsAndStats(db)
if err != nil {
continue
}

rocksdbMetrics.send(props, stats)
rocksdbMetrics.send(props, stats)
}
}
}
}()
}()
}

ro := grocksdb.NewDefaultReadOptions()
wo := grocksdb.NewDefaultWriteOptions()
Expand Down
4 changes: 2 additions & 2 deletions cmd/kava/opendb/opendb_rocksdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestLoadLatestOptions(t *testing.T) {
require.NoError(t, err)
}()

db, err := newRocksDBWithOptions(name, dir, tc.dbOpts, tc.cfOpts)
db, err := newRocksDBWithOptions(name, dir, tc.dbOpts, tc.cfOpts, true)
require.NoError(t, err)
require.NoError(t, db.Close())

Expand Down Expand Up @@ -337,7 +337,7 @@ func TestNewRocksDBWithOptions(t *testing.T) {
cfOpts := newDefaultOptions()
cfOpts.SetWriteBufferSize(999_999)

db, err := newRocksDBWithOptions(name, dir, dbOpts, cfOpts)
db, err := newRocksDBWithOptions(name, dir, dbOpts, cfOpts, true)
require.NoError(t, err)
require.NoError(t, db.Close())

Expand Down

0 comments on commit 9e44371

Please sign in to comment.