Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Jul 17, 2024
1 parent 03f7174 commit c601250
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 40 deletions.
87 changes: 47 additions & 40 deletions opendb_rocksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,61 @@ const (

DefaultColumnFamilyName = "default"

enableMetricsOptName = "rocksdb.enable-metrics"
reportMetricsIntervalSecsOptName = "rocksdb.report-metrics-interval-secs"
enableMetricsOptName = "enable-metrics"
reportMetricsIntervalSecsOptName = "report-metrics-interval-secs"
defaultReportMetricsIntervalSecs = 15

maxOpenFilesDBOptName = "rocksdb.max-open-files"
maxFileOpeningThreadsDBOptName = "rocksdb.max-file-opening-threads"
tableCacheNumshardbitsDBOptName = "rocksdb.table_cache_numshardbits"
allowMMAPWritesDBOptName = "rocksdb.allow_mmap_writes"
allowMMAPReadsDBOptName = "rocksdb.allow_mmap_reads"
useFsyncDBOptName = "rocksdb.use_fsync"
useAdaptiveMutexDBOptName = "rocksdb.use_adaptive_mutex"
bytesPerSyncDBOptName = "rocksdb.bytes_per_sync"
maxBackgroundJobsDBOptName = "rocksdb.max-background-jobs"

writeBufferSizeCFOptName = "rocksdb.write-buffer-size"
numLevelsCFOptName = "rocksdb.num-levels"
maxWriteBufferNumberCFOptName = "rocksdb.max_write_buffer_number"
minWriteBufferNumberToMergeCFOptName = "rocksdb.min_write_buffer_number_to_merge"
maxBytesForLevelBaseCFOptName = "rocksdb.max_bytes_for_level_base"
maxBytesForLevelMultiplierCFOptName = "rocksdb.max_bytes_for_level_multiplier"
targetFileSizeBaseCFOptName = "rocksdb.target_file_size_base"
targetFileSizeMultiplierCFOptName = "rocksdb.target_file_size_multiplier"
level0FileNumCompactionTriggerCFOptName = "rocksdb.level0_file_num_compaction_trigger"
level0SlowdownWritesTriggerCFOptName = "rocksdb.level0_slowdown_writes_trigger"

blockCacheSizeBBTOOptName = "rocksdb.block_cache_size"
bitsPerKeyBBTOOptName = "rocksdb.bits_per_key"
blockSizeBBTOOptName = "rocksdb.block_size"
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"
maxOpenFilesDBOptName = "max-open-files"
maxFileOpeningThreadsDBOptName = "max-file-opening-threads"
tableCacheNumshardbitsDBOptName = "table_cache_numshardbits"
allowMMAPWritesDBOptName = "allow_mmap_writes"
allowMMAPReadsDBOptName = "allow_mmap_reads"
useFsyncDBOptName = "use_fsync"
useAdaptiveMutexDBOptName = "use_adaptive_mutex"
bytesPerSyncDBOptName = "bytes_per_sync"
maxBackgroundJobsDBOptName = "max-background-jobs"

writeBufferSizeCFOptName = "write-buffer-size"
numLevelsCFOptName = "num-levels"
maxWriteBufferNumberCFOptName = "max_write_buffer_number"
minWriteBufferNumberToMergeCFOptName = "min_write_buffer_number_to_merge"
maxBytesForLevelBaseCFOptName = "max_bytes_for_level_base"
maxBytesForLevelMultiplierCFOptName = "max_bytes_for_level_multiplier"
targetFileSizeBaseCFOptName = "target_file_size_base"
targetFileSizeMultiplierCFOptName = "target_file_size_multiplier"
level0FileNumCompactionTriggerCFOptName = "level0_file_num_compaction_trigger"
level0SlowdownWritesTriggerCFOptName = "level0_slowdown_writes_trigger"

blockCacheSizeBBTOOptName = "block_cache_size"
bitsPerKeyBBTOOptName = "bits_per_key"
blockSizeBBTOOptName = "block_size"
cacheIndexAndFilterBlocksBBTOOptName = "cache_index_and_filter_blocks"
pinL0FilterAndIndexBlocksInCacheBBTOOptName = "pin_l0_filter_and_index_blocks_in_cache"
formatVersionBBTOOptName = "format_version"

asyncIOReadOptName = "read-async-io"
)

type RocksDBOptions struct {
// AppOptions is the same interface as provided by cosmos-sdk, see for details:
// https://github.com/cosmos/cosmos-sdk/blob/10465a6aabdfc9119ff187ac3ef229f33c06ab45/server/types/app.go#L29-L31
// We added it here to avoid cosmos-sdk dependency.
type AppOptions interface {
Get(string) interface{}
}

type rocksDBOptions struct {
appOpts AppOptions
dbName string
}

func (opts *RocksDBOptions) Get(key string) interface{} {
func newRocksDBOptions(appOpts AppOptions, dbName string) *rocksDBOptions {
return &rocksDBOptions{
appOpts: appOpts,
dbName: dbName,
}
}

func (opts *rocksDBOptions) Get(key string) interface{} {
dbSpecificKey := fmt.Sprintf("rocksdb.%v.%v", opts.dbName, key)
if opts.appOpts.Get(dbSpecificKey) != nil {
return opts.appOpts.Get(dbSpecificKey)
Expand All @@ -92,13 +106,6 @@ func (opts *RocksDBOptions) Get(key string) interface{} {
return opts.appOpts.Get(fallbackKey)
}

// AppOptions is the same interface as provided by cosmos-sdk, see for details:
// https://github.com/cosmos/cosmos-sdk/blob/10465a6aabdfc9119ff187ac3ef229f33c06ab45/server/types/app.go#L29-L31
// We added it here to avoid cosmos-sdk dependency.
type AppOptions interface {
Get(string) interface{}
}

func OpenDB(appOpts AppOptions, dataDir string, dbName string, backendType dbm.BackendType) (dbm.DB, error) {
if backendType == dbm.RocksDBBackend {
return openRocksdb(dataDir, dbName, appOpts)
Expand Down
22 changes: 22 additions & 0 deletions opendb_rocksdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ func (m *mockAppOptions) Get(key string) interface{} {
return m.opts[key]
}

func TestRocksDBOptions(t *testing.T) {
mockAppOptions := newMockAppOptions(map[string]interface{}{
// fallback configuration
"rocksdb.max-open-files": 16_384,
"rocksdb.block_size": 16_384,

// database-specific configuration
"rocksdb.tx_index.max-open-files": 4096,
"rocksdb.tx_index.block_size": 4096,
})

// there isn't database-specific configuration for application database, so fallback to fallback configuration
appDBOpts := newRocksDBOptions(mockAppOptions, "application")
require.Equal(t, 16_384, appDBOpts.Get("max-open-files"))
require.Equal(t, 16_384, appDBOpts.Get("block_size"))

// there is database-specific configuration for tx_index database, so use it instead of fallback configuration
txIndexDBOpts := newRocksDBOptions(mockAppOptions, "tx_index")
require.Equal(t, 4096, txIndexDBOpts.Get("max-open-files"))
require.Equal(t, 4096, txIndexDBOpts.Get("block_size"))
}

func TestOpenRocksdb(t *testing.T) {
t.Run("db already exists", func(t *testing.T) {
defaultOpts := newDefaultOptions()
Expand Down

0 comments on commit c601250

Please sign in to comment.