From 163c24a2d24c5dbf81e87120892b1d4b612491f0 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 22 Jun 2021 20:35:56 +0300 Subject: [PATCH] [#607] cmd/node: Configure group of bootstrap addresses There is a need to support multiple network addresses of the storage nodes. Make `BootstrapAddress` to return `network.AddressGroup` (and rename). Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config.go | 4 +-- cmd/neofs-node/config/node/config.go | 38 ++++++++++++++--------- cmd/neofs-node/config/node/config_test.go | 16 +++++----- config/example/node.env | 2 +- config/example/node.json | 5 ++- config/example/node.yaml | 4 ++- 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 74716fb3..c3d72a38 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -215,7 +215,7 @@ func initCfg(path string) *cfg { log, err := logger.NewLogger(logPrm) fatalOnErr(err) - netAddr := nodeconfig.BootstrapAddress(appCfg) + netAddr := nodeconfig.BootstrapAddresses(appCfg) maxChunkSize := uint64(maxMsgSize) * 3 / 4 // 25% to meta, 75% to payload maxAddrAmount := uint64(maxChunkSize) / addressSize // each address is about 72 bytes @@ -259,7 +259,7 @@ func initCfg(path string) *cfg { maxChunkSize: maxChunkSize, maxAddrAmount: maxAddrAmount, }, - localAddr: network.GroupFromAddress(netAddr), + localAddr: netAddr, respSvc: response.NewService( response.WithNetworkState(state), ), diff --git a/cmd/neofs-node/config/node/config.go b/cmd/neofs-node/config/node/config.go index 1e1f0bf8..c22fb239 100644 --- a/cmd/neofs-node/config/node/config.go +++ b/cmd/neofs-node/config/node/config.go @@ -18,10 +18,7 @@ const ( attributePrefix = "attribute" ) -var ( - errKeyNotSet = errors.New("empty/not set key address, see `node.key` section") - errAddressNotSet = errors.New("empty/not set bootstrap address, see `node.address` section") -) +var errKeyNotSet = errors.New("empty/not set key address, see `node.key` section") // Key returns value of "key" config parameter // from "node" section. @@ -63,19 +60,30 @@ func Wallet(c *config.Config) *keys.PrivateKey { return acc.PrivateKey() } -// BootstrapAddress returns value of "address" config parameter -// from "node" section as network.Address. -// -// Panics if value is not a valid NeoFS network address -func BootstrapAddress(c *config.Config) (addr network.Address) { - v := config.StringSafe(c.Sub(subsection), "address") - if v == "" { - panic(errAddressNotSet) - } +type stringAddressGroup []string - err := addr.FromString(v) +func (x stringAddressGroup) IterateAddresses(f func(string) bool) { + for i := range x { + if f(x[i]) { + break + } + } +} + +func (x stringAddressGroup) NumberOfAddresses() int { + return len(x) +} + +// BootstrapAddresses returns value of "addresses" config parameter +// from "node" section as network.AddressGroup. +// +// Panics if value is not a string list of valid NeoFS network addresses. +func BootstrapAddresses(c *config.Config) (addr network.AddressGroup) { + v := config.StringSlice(c.Sub(subsection), "addresses") + + err := addr.FromIterator(stringAddressGroup(v)) if err != nil { - panic(fmt.Errorf("could not convert bootstrap address %s to %T: %w", v, addr, err)) + panic(fmt.Errorf("could not parse bootstrap addresses: %w", err)) } return addr diff --git a/cmd/neofs-node/config/node/config_test.go b/cmd/neofs-node/config/node/config_test.go index d20c1b09..00b67e42 100644 --- a/cmd/neofs-node/config/node/config_test.go +++ b/cmd/neofs-node/config/node/config_test.go @@ -22,11 +22,10 @@ func TestNodeSection(t *testing.T) { }, ) - require.PanicsWithError( + require.Panics( t, - errAddressNotSet.Error(), func() { - BootstrapAddress(empty) + BootstrapAddresses(empty) }, ) @@ -41,18 +40,21 @@ func TestNodeSection(t *testing.T) { var fileConfigTest = func(c *config.Config) { key := Key(c) - addr := BootstrapAddress(c) + addr := BootstrapAddresses(c) attributes := Attributes(c) relay := Relay(c) wKey := Wallet(c) - var expectedAddr network.Address + var expectedAddr network.AddressGroup - err := expectedAddr.FromString("s01.neofs.devenv:8080") + err := expectedAddr.FromIterator(stringAddressGroup([]string{ + "s01.neofs.devenv:8080", + "s02.neofs.devenv:8081", + })) require.NoError(t, err) require.Equal(t, "NbUgTSFvPmsRxmGeWpuuGeJUoRoi6PErcM", key.Address()) - require.Equal(t, true, addr.Equal(expectedAddr)) + require.Equal(t, expectedAddr, addr) require.Equal(t, true, relay) require.Len(t, attributes, 2) diff --git a/config/example/node.env b/config/example/node.env index d979f5c5..42e395c4 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -11,7 +11,7 @@ NEOFS_NODE_KEY=./wallet.key NEOFS_NODE_WALLET_PATH=./wallet.json NEOFS_NODE_WALLET_ADDRESS=NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz NEOFS_NODE_WALLET_PASSWORD=password -NEOFS_NODE_ADDRESS=s01.neofs.devenv:8080 +NEOFS_NODE_ADDRESSES=s01.neofs.devenv:8080 s02.neofs.devenv:8081 NEOFS_NODE_ATTRIBUTE_0=Price:11 NEOFS_NODE_ATTRIBUTE_1=UN-LOCODE:RU MSK NEOFS_NODE_RELAY=true diff --git a/config/example/node.json b/config/example/node.json index 9b78d9d5..bd6bacee 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -17,7 +17,10 @@ "address": "NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz", "password": "password" }, - "address": "s01.neofs.devenv:8080", + "addresses": [ + "s01.neofs.devenv:8080", + "s02.neofs.devenv:8081" + ], "attribute_0": "Price:11", "attribute_1": "UN-LOCODE:RU MSK", "relay": true diff --git a/config/example/node.yaml b/config/example/node.yaml index bc79e973..c7c576f3 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -15,7 +15,9 @@ node: path: "./wallet.json" address: "NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz" password: "password" - address: s01.neofs.devenv:8080 + addresses: + - s01.neofs.devenv:8080 + - s02.neofs.devenv:8081 attribute_0: "Price:11" attribute_1: UN-LOCODE:RU MSK relay: true