From 51848991bba14f04959f884d0af5ea16c67e02fc Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Wed, 3 Jan 2024 15:53:15 +0200 Subject: [PATCH 1/2] - constant redefinition --- storage/factory/dbConfigHandler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/factory/dbConfigHandler.go b/storage/factory/dbConfigHandler.go index 5dc426ad441..e6066f10c21 100644 --- a/storage/factory/dbConfigHandler.go +++ b/storage/factory/dbConfigHandler.go @@ -12,7 +12,7 @@ const ( dbConfigFileName = "config.toml" defaultType = "LvlDBSerial" defaultBatchDelaySeconds = 2 - defaultMaxBatchSize = 100 + defaultMaxBatchSize = 45000 // TODO: refactor this in next release candidate defaultMaxOpenFiles = 10 ) From 1d84a313cad29b485e56ed142b4b84076c770514 Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Wed, 3 Jan 2024 17:05:55 +0200 Subject: [PATCH 2/2] - refactored solution --- storage/factory/dbConfigHandler.go | 29 +++++++++++++++---------- storage/factory/dbConfigHandler_test.go | 22 ++++++++++++++----- storage/factory/export_test.go | 13 +++-------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/storage/factory/dbConfigHandler.go b/storage/factory/dbConfigHandler.go index e6066f10c21..28ba8b5dcdb 100644 --- a/storage/factory/dbConfigHandler.go +++ b/storage/factory/dbConfigHandler.go @@ -1,6 +1,7 @@ package factory import ( + "fmt" "os" "path/filepath" @@ -9,11 +10,8 @@ import ( ) const ( - dbConfigFileName = "config.toml" - defaultType = "LvlDBSerial" - defaultBatchDelaySeconds = 2 - defaultMaxBatchSize = 45000 // TODO: refactor this in next release candidate - defaultMaxOpenFiles = 10 + dbConfigFileName = "config.toml" + defaultType = "LvlDBSerial" ) type dbConfigHandler struct { @@ -42,7 +40,10 @@ 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 } @@ -50,12 +51,15 @@ func (dh *dbConfigHandler) GetDBConfig(path string) (*config.DBConfig, error) { 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 } @@ -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 } diff --git a/storage/factory/dbConfigHandler_test.go b/storage/factory/dbConfigHandler_test.go index 406218be7dc..039da28ebf9 100644 --- a/storage/factory/dbConfigHandler_test.go +++ b/storage/factory/dbConfigHandler_test.go @@ -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() @@ -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() @@ -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() diff --git a/storage/factory/export_test.go b/storage/factory/export_test.go index 4b5ac54baac..23317b7d4cf 100644 --- a/storage/factory/export_test.go +++ b/storage/factory/export_test.go @@ -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)