Skip to content

Commit

Permalink
Make read-async-io configurable (#1732) (#1738)
Browse files Browse the repository at this point in the history
* Make read-async-io configurable

* Added unit-test for read options configuration

(cherry picked from commit 0598b99)

Co-authored-by: Evgeniy Scherbina <evgeniy.shcherbina.es@gmail.com>
  • Loading branch information
2 people authored and pirtleshell committed Oct 25, 2023
1 parent f8ca056 commit e7f598d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
19 changes: 16 additions & 3 deletions cmd/kava/opendb/opendb_rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const (
cacheIndexAndFilterBlocksBBTOOptName = "rocksdb.cache_index_and_filter_blocks"
pinL0FilterAndIndexBlocksInCacheBBTOOptName = "rocksdb.pin_l0_filter_and_index_blocks_in_cache"
formatVersionBBTOOptName = "rocksdb.format_version"

asyncIOReadOptName = "rocksdb.read-async-io"
)

func OpenDB(appOpts types.AppOptions, home string, backendType dbm.BackendType) (dbm.DB, error) {
Expand All @@ -98,14 +100,15 @@ func openRocksdb(dir string, appOpts types.AppOptions) (dbm.DB, error) {
cfOpts.SetBlockBasedTableFactory(bbtoOpts)
dbOpts = overrideDBOpts(dbOpts, appOpts)
cfOpts = overrideCFOpts(cfOpts, appOpts)
readOpts := readOptsFromAppOpts(appOpts)

enableMetrics := cast.ToBool(appOpts.Get(enableMetricsOptName))
reportMetricsIntervalSecs := cast.ToInt64(appOpts.Get(reportMetricsIntervalSecsOptName))
if reportMetricsIntervalSecs == 0 {
reportMetricsIntervalSecs = defaultReportMetricsIntervalSecs
}

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

// loadLatestOptions loads and returns database and column family options
Expand Down Expand Up @@ -237,6 +240,16 @@ func overrideCFOpts(cfOpts *grocksdb.Options, appOpts types.AppOptions) *grocksd
return cfOpts
}

func readOptsFromAppOpts(appOpts types.AppOptions) *grocksdb.ReadOptions {
ro := grocksdb.NewDefaultReadOptions()
asyncIO := appOpts.Get(asyncIOReadOptName)
if asyncIO != nil {
ro.SetAsyncIO(cast.ToBool(asyncIO))
}

return ro
}

func bbtoFromAppOpts(appOpts types.AppOptions) *grocksdb.BlockBasedTableOptions {
bbto := defaultBBTO()

Expand Down Expand Up @@ -282,6 +295,7 @@ func newRocksDBWithOptions(
dir string,
dbOpts *grocksdb.Options,
cfOpts *grocksdb.Options,
readOpts *grocksdb.ReadOptions,
enableMetrics bool,
reportMetricsIntervalSecs int64,
) (*dbm.RocksDB, error) {
Expand All @@ -307,11 +321,10 @@ func newRocksDBWithOptions(
go reportMetrics(db, time.Second*time.Duration(reportMetricsIntervalSecs))
}

ro := grocksdb.NewDefaultReadOptions()
wo := grocksdb.NewDefaultWriteOptions()
woSync := grocksdb.NewDefaultWriteOptions()
woSync.SetSync(true)
return dbm.NewRocksDBWithRawDB(db, ro, wo, woSync), nil
return dbm.NewRocksDBWithRawDB(db, readOpts, wo, woSync), nil
}

// newDefaultOptions returns default tm-db options for RocksDB, see for details:
Expand Down
31 changes: 29 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, true, defaultReportMetricsIntervalSecs)
db, err := newRocksDBWithOptions(name, dir, tc.dbOpts, tc.cfOpts, grocksdb.NewDefaultReadOptions(), true, defaultReportMetricsIntervalSecs)
require.NoError(t, err)
require.NoError(t, db.Close())

Expand Down Expand Up @@ -321,6 +321,33 @@ func TestOverrideCFOpts(t *testing.T) {
}
}

func TestReadOptsFromAppOpts(t *testing.T) {
for _, tc := range []struct {
desc string
mockAppOptions *mockAppOptions
asyncIO bool
}{
{
desc: "default options",
mockAppOptions: newMockAppOptions(map[string]interface{}{}),
asyncIO: false,
},
{
desc: "set asyncIO option to true",
mockAppOptions: newMockAppOptions(map[string]interface{}{
asyncIOReadOptName: true,
}),
asyncIO: true,
},
} {
t.Run(tc.desc, func(t *testing.T) {
readOpts := readOptsFromAppOpts(tc.mockAppOptions)

require.Equal(t, tc.asyncIO, readOpts.IsAsyncIO())
})
}
}

func TestNewRocksDBWithOptions(t *testing.T) {
defaultOpts := newDefaultOptions()

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

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

Expand Down

0 comments on commit e7f598d

Please sign in to comment.