[#1764] neofs-node: Allow to return error from IterateShards

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-09-20 15:33:59 +03:00 committed by fyrchik
parent 898689ec14
commit 9113793688
3 changed files with 26 additions and 14 deletions

View file

@ -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
}

View file

@ -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.

View file

@ -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)
}