From 8162b272647369f42ec424863e8a979dd9a4bc7d Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 26 Jul 2022 12:48:55 +0300 Subject: [PATCH] [#1627] node: Add tree service to the config framework Signed-off-by: Pavel Karpy --- cmd/neofs-node/config/tree/config.go | 53 +++++++++++++++++++++++ cmd/neofs-node/config/tree/config_test.go | 40 +++++++++++++++++ cmd/neofs-node/tree.go | 12 ++--- 3 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 cmd/neofs-node/config/tree/config.go create mode 100644 cmd/neofs-node/config/tree/config_test.go diff --git a/cmd/neofs-node/config/tree/config.go b/cmd/neofs-node/config/tree/config.go new file mode 100644 index 0000000000..9b65700be3 --- /dev/null +++ b/cmd/neofs-node/config/tree/config.go @@ -0,0 +1,53 @@ +package treeconfig + +import "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + +const ( + subsection = "tree" +) + +// TreeConfig is a wrapper over "tree" config section +// which provides access to the configuration of the tree service. +type TreeConfig struct { + cfg *config.Config +} + +// Tree returns structure that provides access to a "tree" +// configuration subsection. +func Tree(c *config.Config) TreeConfig { + return TreeConfig{ + c.Sub(subsection), + } +} + +// Enabled returns the value of "enabled" config parameter +// from the "tree" section. +// +// Returns `false` if config value is not specified. +func (c TreeConfig) Enabled() bool { + return config.BoolSafe(c.cfg, "enabled") +} + +// CacheSize returns the value of "cache_size" config parameter +// from the "tree" section. +// +// Returns `0` if config value is not specified. +func (c TreeConfig) CacheSize() int { + return int(config.IntSafe(c.cfg, "cache_size")) +} + +// ReplicationChannelCapacity returns the value of "replication_channel_capacity" +// config parameter from the "tree" section. +// +// Returns `0` if config value is not specified. +func (c TreeConfig) ReplicationChannelCapacity() int { + return int(config.IntSafe(c.cfg, "replication_channel_capacity")) +} + +// ReplicationWorkerCount returns the value of "replication_worker_count" +// config parameter from the "tree" section. +// +// Returns `0` if config value is not specified. +func (c TreeConfig) ReplicationWorkerCount() int { + return int(config.IntSafe(c.cfg, "replication_worker_count")) +} diff --git a/cmd/neofs-node/config/tree/config_test.go b/cmd/neofs-node/config/tree/config_test.go new file mode 100644 index 0000000000..c65b32c386 --- /dev/null +++ b/cmd/neofs-node/config/tree/config_test.go @@ -0,0 +1,40 @@ +package treeconfig_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" + treeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/tree" + "github.com/stretchr/testify/require" +) + +func TestTreeSection(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + empty := configtest.EmptyConfig() + + treeSec := treeconfig.Tree(empty) + + require.False(t, treeSec.Enabled()) + require.Equal(t, 0, treeSec.CacheSize()) + require.Equal(t, 0, treeSec.ReplicationChannelCapacity()) + require.Equal(t, 0, treeSec.ReplicationWorkerCount()) + }) + + const path = "../../../../config/example/node" + + var fileConfigTest = func(c *config.Config) { + treeSec := treeconfig.Tree(c) + + require.True(t, treeSec.Enabled()) + require.Equal(t, 15, treeSec.CacheSize()) + require.Equal(t, 32, treeSec.ReplicationChannelCapacity()) + require.Equal(t, 32, treeSec.ReplicationWorkerCount()) + } + + configtest.ForEachFileType(path, fileConfigTest) + + t.Run("ENV", func(t *testing.T) { + configtest.ForEnvFileType(path, fileConfigTest) + }) +} diff --git a/cmd/neofs-node/tree.go b/cmd/neofs-node/tree.go index 09c397ac47..91ed24e907 100644 --- a/cmd/neofs-node/tree.go +++ b/cmd/neofs-node/tree.go @@ -3,13 +3,13 @@ package main import ( "context" - "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + treeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/tree" "github.com/nspcc-dev/neofs-node/pkg/services/tree" ) func initTreeService(c *cfg) { - sub := c.appCfg.Sub("tree") - if !config.BoolSafe(sub, "enabled") { + treeConfig := treeconfig.Tree(c.appCfg) + if !treeConfig.Enabled() { c.log.Info("tree service is not enabled, skip initialization") return } @@ -20,9 +20,9 @@ func initTreeService(c *cfg) { tree.WithPrivateKey(&c.key.PrivateKey), tree.WithLogger(c.log), tree.WithStorage(c.cfgObject.cfgLocalStorage.localStorage), - tree.WithContainerCacheSize(int(config.IntSafe(sub, "cache_size"))), - tree.WithReplicationChannelCapacity(int(config.IntSafe(sub, "replication_channel_capacity"))), - tree.WithReplicationWorkerCount(int(config.IntSafe(sub, "replication_worker_count")))) + tree.WithContainerCacheSize(treeConfig.CacheSize()), + tree.WithReplicationChannelCapacity(treeConfig.ReplicationChannelCapacity()), + tree.WithReplicationWorkerCount(treeConfig.ReplicationWorkerCount())) for _, srv := range c.cfgGRPC.servers { tree.RegisterTreeServiceServer(srv, c.treeService)