Skip to content

Commit

Permalink
Merge pull request #5796 from multiversx/new-constant-value
Browse files Browse the repository at this point in the history
Fixed dbConfigHandler to accommodate different DB configs
  • Loading branch information
iulianpascalau authored Jan 5, 2024
2 parents ea3bf1d + ccbc0c6 commit d51f756
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
29 changes: 18 additions & 11 deletions storage/factory/dbConfigHandler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -9,11 +10,8 @@ import (
)

const (
dbConfigFileName = "config.toml"
defaultType = "LvlDBSerial"
defaultBatchDelaySeconds = 2
defaultMaxBatchSize = 100
defaultMaxOpenFiles = 10
dbConfigFileName = "config.toml"
defaultType = "LvlDBSerial"
)

type dbConfigHandler struct {
Expand Down Expand Up @@ -42,20 +40,26 @@ func (dh *dbConfigHandler) GetDBConfig(path string) (*config.DBConfig, error) {
dbConfigFromFile := &config.DBConfig{}
err := core.LoadTomlFile(dbConfigFromFile, getPersisterConfigFilePath(path))
if err == nil {
log.Debug("GetDBConfig: loaded db config from toml config file", "path", dbConfigFromFile)
log.Debug("GetDBConfig: loaded db config from toml config file",
"config path", path,
"configuration", fmt.Sprintf("%+v", dbConfigFromFile),
)
return dbConfigFromFile, nil
}

empty := checkIfDirIsEmpty(path)
if !empty {
dbConfig := &config.DBConfig{
Type: defaultType,
BatchDelaySeconds: defaultBatchDelaySeconds,
MaxBatchSize: defaultMaxBatchSize,
MaxOpenFiles: defaultMaxOpenFiles,
BatchDelaySeconds: dh.batchDelaySeconds,
MaxBatchSize: dh.maxBatchSize,
MaxOpenFiles: dh.maxOpenFiles,
}

log.Debug("GetDBConfig: loaded default db config")
log.Debug("GetDBConfig: loaded default db config",
"configuration", fmt.Sprintf("%+v", dbConfig),
)

return dbConfig, nil
}

Expand All @@ -68,7 +72,10 @@ func (dh *dbConfigHandler) GetDBConfig(path string) (*config.DBConfig, error) {
NumShards: dh.numShards,
}

log.Debug("GetDBConfig: loaded db config from main config file")
log.Debug("GetDBConfig: loaded db config from main config file",
"configuration", fmt.Sprintf("%+v", dbConfig),
)

return dbConfig, nil
}

Expand Down
22 changes: 17 additions & 5 deletions storage/factory/dbConfigHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ func TestDBConfigHandler_GetDBConfig(t *testing.T) {
require.Nil(t, err)
require.Equal(t, &expectedDBConfig, conf)
})

t.Run("not empty dir, load default db config", func(t *testing.T) {
t.Parallel()

pf := factory.NewDBConfigHandler(createDefaultDBConfig())
testConfig := createDefaultDBConfig()
testConfig.BatchDelaySeconds = 37
testConfig.MaxBatchSize = 38
testConfig.MaxOpenFiles = 39
testConfig.ShardIDProviderType = "BinarySplit"
testConfig.NumShards = 4
pf := factory.NewDBConfigHandler(testConfig)

dirPath := t.TempDir()

Expand All @@ -68,13 +73,21 @@ func TestDBConfigHandler_GetDBConfig(t *testing.T) {
_ = f.Close()
}()

expectedDBConfig := factory.GetDefaultDBConfig()
expectedDBConfig := &config.DBConfig{
FilePath: "",
Type: factory.DefaultType,
BatchDelaySeconds: testConfig.BatchDelaySeconds,
MaxBatchSize: testConfig.MaxBatchSize,
MaxOpenFiles: testConfig.MaxOpenFiles,
UseTmpAsFilePath: false,
ShardIDProviderType: "",
NumShards: 0,
}

conf, err := pf.GetDBConfig(dirPath)
require.Nil(t, err)
require.Equal(t, expectedDBConfig, conf)
})

t.Run("empty dir, load db config from main config", func(t *testing.T) {
t.Parallel()

Expand All @@ -88,7 +101,6 @@ func TestDBConfigHandler_GetDBConfig(t *testing.T) {
require.Nil(t, err)
require.Equal(t, &expectedDBConfig, conf)
})

t.Run("getDBConfig twice, should load from config file if file available", func(t *testing.T) {
t.Parallel()

Expand Down
13 changes: 3 additions & 10 deletions storage/factory/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ import (
"github.com/multiversx/mx-chain-go/storage"
)

// DefaultType exports the defaultType const to be used in tests
const DefaultType = defaultType

// GetPersisterConfigFilePath -
func GetPersisterConfigFilePath(path string) string {
return getPersisterConfigFilePath(path)
}

// GetDefaultDBConfig -
func GetDefaultDBConfig() *config.DBConfig {
return &config.DBConfig{
Type: defaultType,
BatchDelaySeconds: defaultBatchDelaySeconds,
MaxBatchSize: defaultMaxBatchSize,
MaxOpenFiles: defaultMaxOpenFiles,
}
}

// NewPersisterCreator -
func NewPersisterCreator(config config.DBConfig) *persisterCreator {
return newPersisterCreator(config)
Expand Down

0 comments on commit d51f756

Please sign in to comment.