From 91137936880b9c88ad9c29281dd05d190233ea74 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 20 Sep 2022 15:33:59 +0300 Subject: [PATCH] [#1764] neofs-node: Allow to return error from `IterateShards` Signed-off-by: Evgenii Stratonikov --- cmd/neofs-node/config.go | 8 ++++++-- cmd/neofs-node/config/engine/config.go | 13 ++++++++++--- cmd/neofs-node/config/engine/config_test.go | 19 ++++++++++--------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index c11322e3b..3b2583ee4 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -409,7 +409,7 @@ func initShardOptions(c *cfg) { require := !nodeconfig.Relay(c.appCfg) // relay node does not require shards - engineconfig.IterateShards(c.appCfg, require, func(sc *shardconfig.Config) { + err := engineconfig.IterateShards(c.appCfg, require, func(sc *shardconfig.Config) error { var writeCacheOpts []writecache.Option writeCacheCfg := sc.WriteCache() @@ -474,7 +474,7 @@ func initShardOptions(c *cfg) { }, }) default: - panic(fmt.Errorf("invalid storage type: %s", storages[i].Type())) + return fmt.Errorf("invalid storage type: %s", storages[i].Type()) } } @@ -521,7 +521,11 @@ func initShardOptions(c *cfg) { }), shard.WithGCEventChannel(gcEventChannel), }) + return nil }) + if err != nil { + panic(err) + } c.cfgObject.cfgLocalStorage.shardOpts = opts } diff --git a/cmd/neofs-node/config/engine/config.go b/cmd/neofs-node/config/engine/config.go index 37b6f1c33..0fc9e01eb 100644 --- a/cmd/neofs-node/config/engine/config.go +++ b/cmd/neofs-node/config/engine/config.go @@ -1,6 +1,7 @@ package engineconfig import ( + "errors" "strconv" "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" @@ -15,13 +16,16 @@ const ( ShardPoolSizeDefault = 20 ) +// ErrNoShardConfigured is returned when at least 1 shard is required but none are found. +var ErrNoShardConfigured = errors.New("no shard configured") + // IterateShards iterates over subsections of "shard" subsection of "storage" section of c, // wrap them into shardconfig.Config and passes to f. // // Section names are expected to be consecutive integer numbers, starting from 0. // // Panics if N is not a positive number while shards are required. -func IterateShards(c *config.Config, required bool, f func(*shardconfig.Config)) { +func IterateShards(c *config.Config, required bool, f func(*shardconfig.Config) error) error { c = c.Sub(subsection) c = c.Sub("shard") @@ -44,11 +48,14 @@ func IterateShards(c *config.Config, required bool, f func(*shardconfig.Config)) } (*config.Config)(sc).SetDefault(def) - f(sc) + if err := f(sc); err != nil { + return err + } } if i == 0 && required { - panic("no shard configured") + return ErrNoShardConfigured } + return nil } // ShardPoolSize returns the value of "shard_pool_size" config parameter from "storage" section. diff --git a/cmd/neofs-node/config/engine/config_test.go b/cmd/neofs-node/config/engine/config_test.go index 7eef4eff2..fa0f656d1 100644 --- a/cmd/neofs-node/config/engine/config_test.go +++ b/cmd/neofs-node/config/engine/config_test.go @@ -20,17 +20,17 @@ func TestEngineSection(t *testing.T) { t.Run("defaults", func(t *testing.T) { empty := configtest.EmptyConfig() - require.Panics(t, func() { - engineconfig.IterateShards(empty, true, nil) - }) + require.ErrorIs(t, + engineconfig.IterateShards(empty, true, nil), + engineconfig.ErrNoShardConfigured) handlerCalled := false - require.NotPanics(t, func() { - engineconfig.IterateShards(empty, false, func(_ *shardconfig.Config) { + require.NoError(t, + engineconfig.IterateShards(empty, false, func(_ *shardconfig.Config) error { handlerCalled = true - }) - }) + return nil + })) require.False(t, handlerCalled) @@ -47,7 +47,7 @@ func TestEngineSection(t *testing.T) { require.EqualValues(t, 100, engineconfig.ShardErrorThreshold(c)) require.EqualValues(t, 15, engineconfig.ShardPoolSize(c)) - engineconfig.IterateShards(c, true, func(sc *shardconfig.Config) { + err := engineconfig.IterateShards(c, true, func(sc *shardconfig.Config) error { defer func() { num++ }() @@ -145,8 +145,9 @@ func TestEngineSection(t *testing.T) { require.Equal(t, true, sc.RefillMetabase()) require.Equal(t, mode.ReadWrite, sc.Mode()) } + return nil }) - + require.NoError(t, err) require.Equal(t, 2, num) }