From adbbad0beb906424008c188f5f42d66953a3fce5 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 18 Jun 2021 09:00:21 +0300 Subject: [PATCH] [#607] network: Do not work with Address pointers `network.Address` structure in most cases created once and used read-only. Replace `AddressFromString` function with `Address.FromString` method with the same purpose and implementation. Make all libraries to work with value. Signed-off-by: Leonard Lyubich --- cmd/neofs-cli/modules/root.go | 8 +++---- cmd/neofs-node/config.go | 4 ++-- cmd/neofs-node/config/node/config.go | 4 ++-- cmd/neofs-node/config/node/config_test.go | 6 +++-- cmd/neofs-node/container.go | 8 ++++--- cmd/neofs-node/object.go | 10 ++++---- cmd/neofs-node/reputation/common/remote.go | 8 ++++--- pkg/innerring/processors/audit/process.go | 4 +++- pkg/innerring/processors/audit/processor.go | 2 +- pkg/innerring/rpc.go | 16 +++++++++---- pkg/network/address.go | 23 ++++++++----------- pkg/network/address_test.go | 14 +++++++---- pkg/network/cache/client.go | 2 +- pkg/network/tls_test.go | 4 +++- pkg/services/object/get/exec.go | 2 +- pkg/services/object/get/get_test.go | 7 +++--- pkg/services/object/get/remote.go | 2 +- pkg/services/object/get/service.go | 4 ++-- pkg/services/object/get/util.go | 2 +- pkg/services/object/head/remote.go | 6 ++--- pkg/services/object/put/distributed.go | 8 +++---- pkg/services/object/put/remote.go | 6 ++--- pkg/services/object/put/service.go | 2 +- pkg/services/object/put/streamer.go | 10 ++++---- pkg/services/object/search/exec.go | 2 +- pkg/services/object/search/remote.go | 2 +- pkg/services/object/search/search_test.go | 7 +++--- pkg/services/object/search/service.go | 4 ++-- pkg/services/object/search/util.go | 2 +- pkg/services/object/util/log.go | 2 +- pkg/services/object/util/placement.go | 12 ++++++---- .../object_manager/placement/traverser.go | 8 +++---- .../placement/traverser_test.go | 14 +++++++---- pkg/services/policer/check.go | 6 +++-- pkg/services/replicator/process.go | 4 +++- 35 files changed, 128 insertions(+), 97 deletions(-) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index e52ba4e7b..4ebcc55c5 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -238,15 +238,15 @@ func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, erro // getEndpointAddress returns network address structure that stores multiaddr // inside, parsed from global arguments. -func getEndpointAddress() (*network.Address, error) { +func getEndpointAddress() (addr network.Address, err error) { endpoint := viper.GetString("rpc") - addr, err := network.AddressFromString(endpoint) + err = addr.FromString(endpoint) if err != nil { - return nil, errInvalidEndpoint + err = errInvalidEndpoint } - return addr, nil + return } // getSDKClient returns default neofs-api-go sdk client. Consider using diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index 179a11048..ddd3f1cb8 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -90,7 +90,7 @@ type cfg struct { cfgNodeInfo cfgNodeInfo - localAddr *network.Address + localAddr network.Address cfgObject cfgObject @@ -308,7 +308,7 @@ func initCfg(path string) *cfg { return c } -func (c *cfg) LocalAddress() *network.Address { +func (c *cfg) LocalAddress() network.Address { return c.localAddr } diff --git a/cmd/neofs-node/config/node/config.go b/cmd/neofs-node/config/node/config.go index d794ba02b..1e1f0bf85 100644 --- a/cmd/neofs-node/config/node/config.go +++ b/cmd/neofs-node/config/node/config.go @@ -67,13 +67,13 @@ func Wallet(c *config.Config) *keys.PrivateKey { // from "node" section as network.Address. // // Panics if value is not a valid NeoFS network address -func BootstrapAddress(c *config.Config) *network.Address { +func BootstrapAddress(c *config.Config) (addr network.Address) { v := config.StringSafe(c.Sub(subsection), "address") if v == "" { panic(errAddressNotSet) } - addr, err := network.AddressFromString(v) + err := addr.FromString(v) if err != nil { panic(fmt.Errorf("could not convert bootstrap address %s to %T: %w", v, addr, err)) } diff --git a/cmd/neofs-node/config/node/config_test.go b/cmd/neofs-node/config/node/config_test.go index 9c89df7bd..d20c1b09f 100644 --- a/cmd/neofs-node/config/node/config_test.go +++ b/cmd/neofs-node/config/node/config_test.go @@ -46,11 +46,13 @@ func TestNodeSection(t *testing.T) { relay := Relay(c) wKey := Wallet(c) - expectedAddr, err := network.AddressFromString("s01.neofs.devenv:8080") + var expectedAddr network.Address + + err := expectedAddr.FromString("s01.neofs.devenv:8080") require.NoError(t, err) require.Equal(t, "NbUgTSFvPmsRxmGeWpuuGeJUoRoi6PErcM", key.Address()) - require.Equal(t, true, addr.Equal(*expectedAddr)) + require.Equal(t, true, addr.Equal(expectedAddr)) require.Equal(t, true, relay) require.Len(t, attributes, 2) diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index 8dd79c58d..c44da3dcd 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -205,7 +205,7 @@ type remoteLoadAnnounceProvider struct { loadAddrSrc network.LocalAddressSource clientCache interface { - Get(*network.Address) (apiClient.Client, error) + Get(network.Address) (apiClient.Client, error) } deadEndProvider loadcontroller.WriterProvider @@ -218,12 +218,14 @@ func (r *remoteLoadAnnounceProvider) InitRemote(srv loadroute.ServerInfo) (loadc addr := srv.Address() - netAddr, err := network.AddressFromString(addr) + var netAddr network.Address + + err := netAddr.FromString(addr) if err != nil { return nil, fmt.Errorf("could not convert address to IP format: %w", err) } - if network.IsLocalAddress(r.loadAddrSrc, *netAddr) { + if network.IsLocalAddress(r.loadAddrSrc, netAddr) { // if local => return no-op writer return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil } diff --git a/cmd/neofs-node/object.go b/cmd/neofs-node/object.go index 0dbd4647c..bd5805d92 100644 --- a/cmd/neofs-node/object.go +++ b/cmd/neofs-node/object.go @@ -408,7 +408,7 @@ type reputationClientConstructor struct { trustStorage *truststorage.Storage basicConstructor interface { - Get(*network.Address) (client.Client, error) + Get(network.Address) (client.Client, error) } } @@ -492,7 +492,7 @@ func (c *reputationClient) SearchObject(ctx context.Context, prm *client.SearchO return ids, err } -func (c *reputationClientConstructor) Get(addr *network.Address) (client.Client, error) { +func (c *reputationClientConstructor) Get(addr network.Address) (client.Client, error) { cl, err := c.basicConstructor.Get(addr) if err != nil { return nil, err @@ -501,9 +501,11 @@ func (c *reputationClientConstructor) Get(addr *network.Address) (client.Client, nm, err := netmap.GetLatestNetworkMap(c.nmSrc) if err == nil { for i := range nm.Nodes { - netAddr, err := network.AddressFromString(nm.Nodes[i].Address()) + var netAddr network.Address + + err := netAddr.FromString(nm.Nodes[i].Address()) if err == nil { - if netAddr.Equal(*addr) { + if netAddr.Equal(addr) { prm := truststorage.UpdatePrm{} prm.SetPeer(reputation.PeerIDFromBytes(nm.Nodes[i].PublicKey())) diff --git a/cmd/neofs-node/reputation/common/remote.go b/cmd/neofs-node/reputation/common/remote.go index 7c11da308..5c4c88d51 100644 --- a/cmd/neofs-node/reputation/common/remote.go +++ b/cmd/neofs-node/reputation/common/remote.go @@ -11,7 +11,7 @@ import ( ) type clientCache interface { - Get(*network.Address) (apiClient.Client, error) + Get(network.Address) (apiClient.Client, error) } // clientKeyRemoteProvider must provide remote writer and take into account @@ -72,12 +72,14 @@ func (rtp *remoteTrustProvider) InitRemote(srv reputationcommon.ServerInfo) (rep addr := srv.Address() - netAddr, err := network.AddressFromString(addr) + var netAddr network.Address + + err := netAddr.FromString(addr) if err != nil { return nil, fmt.Errorf("could not convert address to IP format: %w", err) } - if network.IsLocalAddress(rtp.localAddrSrc, *netAddr) { + if network.IsLocalAddress(rtp.localAddrSrc, netAddr) { // if local => return no-op writer return trustcontroller.SimpleWriterProvider(new(NopReputationWriter)), nil } diff --git a/pkg/innerring/processors/audit/process.go b/pkg/innerring/processors/audit/process.go index 44169dfaf..c3a78fa77 100644 --- a/pkg/innerring/processors/audit/process.go +++ b/pkg/innerring/processors/audit/process.go @@ -118,7 +118,9 @@ func (ap *Processor) findStorageGroups(cid *cid.ID, shuffled netmap.Nodes) []*ob zap.Int("total_tries", ln), ) - netAddr, err := network.AddressFromString(shuffled[i].Address()) + var netAddr network.Address + + err := netAddr.FromString(shuffled[i].Address()) if err != nil { log.Warn("can't parse remote address", zap.String("error", err.Error())) diff --git a/pkg/innerring/processors/audit/processor.go b/pkg/innerring/processors/audit/processor.go index a5d41b86e..23ef5259d 100644 --- a/pkg/innerring/processors/audit/processor.go +++ b/pkg/innerring/processors/audit/processor.go @@ -29,7 +29,7 @@ type ( // NeoFSClientCache is an interface for cache of neofs RPC clients NeoFSClientCache interface { - Get(address *network.Address) (SDKClient.Client, error) + Get(address network.Address) (SDKClient.Client, error) } TaskManager interface { diff --git a/pkg/innerring/rpc.go b/pkg/innerring/rpc.go index c1dbe82f6..ade22dbd0 100644 --- a/pkg/innerring/rpc.go +++ b/pkg/innerring/rpc.go @@ -22,7 +22,7 @@ type ( ClientCache struct { log *zap.Logger cache interface { - Get(address *network.Address) (client.Client, error) + Get(address network.Address) (client.Client, error) CloseAll() } key *ecdsa.PrivateKey @@ -49,7 +49,7 @@ func newClientCache(p *clientCacheParams) *ClientCache { } } -func (c *ClientCache) Get(address *network.Address) (client.Client, error) { +func (c *ClientCache) Get(address network.Address) (client.Client, error) { // Because cache is used by `ClientCache` exclusively, // client will always have valid key. return c.cache.Get(address) @@ -75,7 +75,9 @@ func (c *ClientCache) getSG(ctx context.Context, addr *object.Address, nm *netma getParams.WithAddress(addr) for _, node := range placement.FlattenNodes(nodes) { - netAddr, err := network.AddressFromString(node.Address()) + var netAddr network.Address + + err := netAddr.FromString(node.Address()) if err != nil { c.log.Warn("can't parse remote address", zap.String("address", node.Address()), @@ -137,7 +139,9 @@ func (c *ClientCache) GetHeader(task *audit.Task, node *netmap.Node, id *object. headParams.WithMainFields() headParams.WithAddress(objAddress) - netAddr, err := network.AddressFromString(node.Address()) + var netAddr network.Address + + err := netAddr.FromString(node.Address()) if err != nil { return nil, fmt.Errorf("can't parse remote address %s: %w", node.Address(), err) } @@ -173,7 +177,9 @@ func (c *ClientCache) GetRangeHash(task *audit.Task, node *netmap.Node, id *obje rangeParams.WithRangeList(rng) rangeParams.WithSalt(nil) // it MUST be nil for correct hash concatenation in PDP game - netAddr, err := network.AddressFromString(node.Address()) + var netAddr network.Address + + err := netAddr.FromString(node.Address()) if err != nil { return nil, fmt.Errorf("can't parse remote address %s: %w", node.Address(), err) } diff --git a/pkg/network/address.go b/pkg/network/address.go index 8eb8a0ce7..b18987ffb 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -25,7 +25,7 @@ type Address struct { // LocalAddressSource is an interface of local // network address container with read access. type LocalAddressSource interface { - LocalAddress() *Address + LocalAddress() Address } // String returns multiaddr string. @@ -57,26 +57,21 @@ func (a Address) HostAddr() string { return host } -// AddressFromString restores address from a string representation. +// FromString restores Address from a string representation. // // Supports MultiAddr and HostAddr strings. -func AddressFromString(s string) (*Address, error) { - ma, err := multiaddr.NewMultiaddr(s) +func (a *Address) FromString(s string) error { + var err error + + a.ma, err = multiaddr.NewMultiaddr(s) if err != nil { s, err = multiaddrStringFromHostAddr(s) - if err != nil { - return nil, err - } - - ma, err = multiaddr.NewMultiaddr(s) // don't want recursion there - if err != nil { - return nil, err + if err == nil { + a.ma, err = multiaddr.NewMultiaddr(s) } } - return &Address{ - ma: ma, - }, nil + return err } // multiaddrStringFromHostAddr converts "localhost:8080" to "/dns4/localhost/tcp/8080" diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index ee7167f07..91f606a85 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -20,8 +20,10 @@ func TestAddressFromString(t *testing.T) { {"[2004:eb1::1]:8080", buildMultiaddr("/ip6/2004:eb1::1/tcp/8080", t)}, } + var addr Address + for _, testcase := range testcases { - addr, err := AddressFromString(testcase.inp) + err := addr.FromString(testcase.inp) require.NoError(t, err) require.Equal(t, testcase.exp, addr.ma, testcase.inp) } @@ -68,14 +70,18 @@ func buildMultiaddr(s string, t *testing.T) multiaddr.Multiaddr { func TestAddress_WriteToNodeInfo(t *testing.T) { a := "127.0.0.1:8080" - addr, err := AddressFromString(a) + var addr Address + + err := addr.FromString(a) require.NoError(t, err) var ni netmap.NodeInfo addr.WriteToNodeInfo(&ni) - restored, err := AddressFromString(ni.Address()) + var restored Address + + err = restored.FromString(ni.Address()) require.NoError(t, err) - require.True(t, restored.Equal(*addr)) + require.True(t, restored.Equal(addr)) } diff --git a/pkg/network/cache/client.go b/pkg/network/cache/client.go index bc8152894..225f83bb6 100644 --- a/pkg/network/cache/client.go +++ b/pkg/network/cache/client.go @@ -29,7 +29,7 @@ func NewSDKClientCache(opts ...client.Option) *ClientCache { } // Get function returns existing client or creates a new one. -func (c *ClientCache) Get(netAddr *network.Address) (client.Client, error) { +func (c *ClientCache) Get(netAddr network.Address) (client.Client, error) { // multiaddr is used as a key in client cache since // same host may have different connections(with tls or not), // therefore, host+port pair is not unique diff --git a/pkg/network/tls_test.go b/pkg/network/tls_test.go index 2f00dad63..122568b33 100644 --- a/pkg/network/tls_test.go +++ b/pkg/network/tls_test.go @@ -43,7 +43,9 @@ func TestAddress_AddTLS(t *testing.T) { addr.AddTLS() - netAddr, err := AddressFromString(test.want) + var netAddr Address + + err := netAddr.FromString(test.want) require.NoError(t, err) require.True(t, netAddr.Equal(addr), test.input) diff --git a/pkg/services/object/get/exec.go b/pkg/services/object/get/exec.go index bfe1ebb44..6848aa42f 100644 --- a/pkg/services/object/get/exec.go +++ b/pkg/services/object/get/exec.go @@ -270,7 +270,7 @@ func (exec *execCtx) headChild(id *objectSDK.ID) (*object.Object, bool) { } } -func (exec execCtx) remoteClient(node *network.Address) (getClient, bool) { +func (exec execCtx) remoteClient(node network.Address) (getClient, bool) { log := exec.log.With(zap.Stringer("node", node)) c, err := exec.svc.clientCache.get(node) diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index 5a0c3d902..549afd618 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -82,7 +82,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap return vs, nil } -func (c *testClientCache) get(mAddr *network.Address) (getClient, error) { +func (c *testClientCache) get(mAddr network.Address) (getClient, error) { v, ok := c.clients[mAddr.HostAddr()] if !ok { return nil, errors.New("could not construct client") @@ -406,8 +406,9 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { strconv.Itoa(60000+j), ) - var err error - na, err := network.AddressFromString(a) + var na network.Address + + err := na.FromString(a) require.NoError(t, err) as[j] = na.HostAddr() diff --git a/pkg/services/object/get/remote.go b/pkg/services/object/get/remote.go index 9de21b645..acea51f64 100644 --- a/pkg/services/object/get/remote.go +++ b/pkg/services/object/get/remote.go @@ -10,7 +10,7 @@ import ( "go.uber.org/zap" ) -func (exec *execCtx) processNode(ctx context.Context, addr *network.Address) bool { +func (exec *execCtx) processNode(ctx context.Context, addr network.Address) bool { log := exec.log.With(zap.Stringer("remote node", addr)) log.Debug("processing node...") diff --git a/pkg/services/object/get/service.go b/pkg/services/object/get/service.go index 1244c0063..ab8d51e83 100644 --- a/pkg/services/object/get/service.go +++ b/pkg/services/object/get/service.go @@ -35,7 +35,7 @@ type cfg struct { } clientCache interface { - get(*network.Address) (getClient, error) + get(network.Address) (getClient, error) } traverserGenerator interface { @@ -93,7 +93,7 @@ func WithLocalStorageEngine(e *engine.StorageEngine) Option { } type ClientConstructor interface { - Get(*network.Address) (client.Client, error) + Get(network.Address) (client.Client, error) } // WithClientConstructor returns option to set constructor of remote node clients. diff --git a/pkg/services/object/get/util.go b/pkg/services/object/get/util.go index a8dee7f78..72578458e 100644 --- a/pkg/services/object/get/util.go +++ b/pkg/services/object/get/util.go @@ -72,7 +72,7 @@ func (s *SimpleObjectWriter) Object() *object.Object { return s.obj.Object() } -func (c *clientCacheWrapper) get(addr *network.Address) (getClient, error) { +func (c *clientCacheWrapper) get(addr network.Address) (getClient, error) { clt, err := c.cache.Get(addr) return &clientWrapper{ diff --git a/pkg/services/object/head/remote.go b/pkg/services/object/head/remote.go index 088e4f111..921a35a48 100644 --- a/pkg/services/object/head/remote.go +++ b/pkg/services/object/head/remote.go @@ -13,7 +13,7 @@ import ( ) type ClientConstructor interface { - Get(*network.Address) (client.Client, error) + Get(network.Address) (client.Client, error) } // RemoteHeader represents utility for getting @@ -28,7 +28,7 @@ type RemoteHeader struct { type RemoteHeadPrm struct { commonHeadPrm *Prm - node *network.Address + node network.Address } var ErrNotFound = errors.New("object header not found") @@ -42,7 +42,7 @@ func NewRemoteHeader(keyStorage *util.KeyStorage, cache ClientConstructor) *Remo } // WithNodeAddress sets network address of the remote node. -func (p *RemoteHeadPrm) WithNodeAddress(v *network.Address) *RemoteHeadPrm { +func (p *RemoteHeadPrm) WithNodeAddress(v network.Address) *RemoteHeadPrm { if p != nil { p.node = v } diff --git a/pkg/services/object/put/distributed.go b/pkg/services/object/put/distributed.go index e302cf1a5..eed9c9bdb 100644 --- a/pkg/services/object/put/distributed.go +++ b/pkg/services/object/put/distributed.go @@ -23,9 +23,9 @@ type distributedTarget struct { chunks [][]byte - nodeTargetInitializer func(*network.Address) transformer.ObjectTarget + nodeTargetInitializer func(network.Address) transformer.ObjectTarget - relay func(*network.Address) error + relay func(network.Address) error fmt *object.FormatValidator @@ -68,7 +68,7 @@ func (t *distributedTarget) Close() (*transformer.AccessIdentifiers, error) { return t.iteratePlacement(t.sendObject) } -func (t *distributedTarget) sendObject(addr *network.Address) error { +func (t *distributedTarget) sendObject(addr network.Address) error { if t.relay != nil { err := t.relay(addr) if err == nil || !errors.Is(err, errLocalAddress) { @@ -86,7 +86,7 @@ func (t *distributedTarget) sendObject(addr *network.Address) error { return nil } -func (t *distributedTarget) iteratePlacement(f func(*network.Address) error) (*transformer.AccessIdentifiers, error) { +func (t *distributedTarget) iteratePlacement(f func(network.Address) error) (*transformer.AccessIdentifiers, error) { traverser, err := placement.NewTraverser( append(t.traverseOpts, placement.ForObject(t.obj.ID()))..., ) diff --git a/pkg/services/object/put/remote.go b/pkg/services/object/put/remote.go index 2d1a710f2..33e790b19 100644 --- a/pkg/services/object/put/remote.go +++ b/pkg/services/object/put/remote.go @@ -20,7 +20,7 @@ type remoteTarget struct { commonPrm *util.CommonPrm - addr *network.Address + addr network.Address obj *object.Object @@ -37,7 +37,7 @@ type RemoteSender struct { // RemotePutPrm groups remote put operation parameters. type RemotePutPrm struct { - node *network.Address + node network.Address obj *object.Object } @@ -86,7 +86,7 @@ func NewRemoteSender(keyStorage *util.KeyStorage, cons ClientConstructor) *Remot } // WithNodeAddress sets network address of the remote node. -func (p *RemotePutPrm) WithNodeAddress(v *network.Address) *RemotePutPrm { +func (p *RemotePutPrm) WithNodeAddress(v network.Address) *RemotePutPrm { if p != nil { p.node = v } diff --git a/pkg/services/object/put/service.go b/pkg/services/object/put/service.go index a34d10d0c..d010226d7 100644 --- a/pkg/services/object/put/service.go +++ b/pkg/services/object/put/service.go @@ -30,7 +30,7 @@ type Service struct { type Option func(*cfg) type ClientConstructor interface { - Get(*network.Address) (client.Client, error) + Get(network.Address) (client.Client, error) } type cfg struct { diff --git a/pkg/services/object/put/streamer.go b/pkg/services/object/put/streamer.go index afdbe9467..459fdfca4 100644 --- a/pkg/services/object/put/streamer.go +++ b/pkg/services/object/put/streamer.go @@ -147,10 +147,10 @@ func (p *Streamer) preparePrm(prm *PutInitPrm) error { var errLocalAddress = errors.New("can't relay to local address") func (p *Streamer) newCommonTarget(prm *PutInitPrm) transformer.ObjectTarget { - var relay func(*network.Address) error + var relay func(network.Address) error if p.relay != nil { - relay = func(addr *network.Address) error { - if network.IsLocalAddress(p.localAddrSrc, *addr) { + relay = func(addr network.Address) error { + if network.IsLocalAddress(p.localAddrSrc, addr) { return errLocalAddress } @@ -166,8 +166,8 @@ func (p *Streamer) newCommonTarget(prm *PutInitPrm) transformer.ObjectTarget { return &distributedTarget{ traverseOpts: prm.traverseOpts, workerPool: p.workerPool, - nodeTargetInitializer: func(addr *network.Address) transformer.ObjectTarget { - if network.IsLocalAddress(p.localAddrSrc, *addr) { + nodeTargetInitializer: func(addr network.Address) transformer.ObjectTarget { + if network.IsLocalAddress(p.localAddrSrc, addr) { return &localTarget{ storage: p.localStore, } diff --git a/pkg/services/object/search/exec.go b/pkg/services/object/search/exec.go index 4de46dc89..75fababac 100644 --- a/pkg/services/object/search/exec.go +++ b/pkg/services/object/search/exec.go @@ -117,7 +117,7 @@ func (exec *execCtx) generateTraverser(cid *cid.ID) (*placement.Traverser, bool) } } -func (exec execCtx) remoteClient(node *network.Address) (searchClient, bool) { +func (exec execCtx) remoteClient(node network.Address) (searchClient, bool) { log := exec.log.With(zap.Stringer("node", node)) c, err := exec.svc.clientConstructor.get(node) diff --git a/pkg/services/object/search/remote.go b/pkg/services/object/search/remote.go index c5db7bdbd..cdf8ed706 100644 --- a/pkg/services/object/search/remote.go +++ b/pkg/services/object/search/remote.go @@ -7,7 +7,7 @@ import ( "go.uber.org/zap" ) -func (exec *execCtx) processNode(ctx context.Context, addr *network.Address) { +func (exec *execCtx) processNode(ctx context.Context, addr network.Address) { log := exec.log.With(zap.Stringer("remote node", addr)) log.Debug("processing node...") diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index 1b2aa94de..827d98999 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -84,7 +84,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap return res, nil } -func (c *testClientCache) get(mAddr *network.Address) (searchClient, error) { +func (c *testClientCache) get(mAddr network.Address) (searchClient, error) { v, ok := c.clients[mAddr.HostAddr()] if !ok { return nil, errors.New("could not construct client") @@ -200,8 +200,9 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) { strconv.Itoa(60000+j), ) - var err error - na, err := network.AddressFromString(a) + var na network.Address + + err := na.FromString(a) require.NoError(t, err) as[j] = na.HostAddr() diff --git a/pkg/services/object/search/service.go b/pkg/services/object/search/service.go index 7b2442618..5b03c1057 100644 --- a/pkg/services/object/search/service.go +++ b/pkg/services/object/search/service.go @@ -27,7 +27,7 @@ type searchClient interface { } type ClientConstructor interface { - Get(*network.Address) (client.Client, error) + Get(network.Address) (client.Client, error) } type cfg struct { @@ -38,7 +38,7 @@ type cfg struct { } clientConstructor interface { - get(*network.Address) (searchClient, error) + get(network.Address) (searchClient, error) } traverserGenerator interface { diff --git a/pkg/services/object/search/util.go b/pkg/services/object/search/util.go index ba8c253a1..c1f5c4e9d 100644 --- a/pkg/services/object/search/util.go +++ b/pkg/services/object/search/util.go @@ -68,7 +68,7 @@ func (w *uniqueIDWriter) WriteIDs(list []*objectSDK.ID) error { return w.writer.WriteIDs(list) } -func (c *clientConstructorWrapper) get(addr *network.Address) (searchClient, error) { +func (c *clientConstructorWrapper) get(addr network.Address) (searchClient, error) { clt, err := c.constructor.Get(addr) return &clientWrapper{ diff --git a/pkg/services/object/util/log.go b/pkg/services/object/util/log.go index e01503b38..7f90e3be4 100644 --- a/pkg/services/object/util/log.go +++ b/pkg/services/object/util/log.go @@ -7,7 +7,7 @@ import ( ) // LogServiceError writes debug error message of object service to provided logger. -func LogServiceError(l *logger.Logger, req string, node *network.Address, err error) { +func LogServiceError(l *logger.Logger, req string, node network.Address, err error) { l.Debug("object service error", zap.Stringer("node", node), zap.String("request", req), diff --git a/pkg/services/object/util/placement.go b/pkg/services/object/util/placement.go index 787e7cf29..e7a9b4234 100644 --- a/pkg/services/object/util/placement.go +++ b/pkg/services/object/util/placement.go @@ -50,13 +50,15 @@ func (p *localPlacement) BuildPlacement(addr *object.Address, policy *netmapSDK. for i := range vs { for j := range vs[i] { - addr, err := network.AddressFromString(vs[i][j].Address()) + var addr network.Address + + err := addr.FromString(vs[i][j].Address()) if err != nil { // TODO: log error continue } - if network.IsLocalAddress(p.localAddrSrc, *addr) { + if network.IsLocalAddress(p.localAddrSrc, addr) { return []netmapSDK.Nodes{{vs[i][j]}}, nil } } @@ -82,13 +84,15 @@ func (p *remotePlacement) BuildPlacement(addr *object.Address, policy *netmapSDK for i := range vs { for j := 0; j < len(vs[i]); j++ { - addr, err := network.AddressFromString(vs[i][j].Address()) + var addr network.Address + + err := addr.FromString(vs[i][j].Address()) if err != nil { // TODO: log error continue } - if network.IsLocalAddress(p.localAddrSrc, *addr) { + if network.IsLocalAddress(p.localAddrSrc, addr) { vs[i] = append(vs[i][:j], vs[i][j+1:]...) j-- } diff --git a/pkg/services/object_manager/placement/traverser.go b/pkg/services/object_manager/placement/traverser.go index 336951feb..23511e892 100644 --- a/pkg/services/object_manager/placement/traverser.go +++ b/pkg/services/object_manager/placement/traverser.go @@ -122,7 +122,7 @@ func flatNodes(ns []netmap.Nodes) []netmap.Nodes { // Next returns next unprocessed address of the object placement. // // Returns nil if no nodes left or traversal operation succeeded. -func (t *Traverser) Next() []*network.Address { +func (t *Traverser) Next() []network.Address { t.mtx.Lock() defer t.mtx.Unlock() @@ -139,16 +139,14 @@ func (t *Traverser) Next() []*network.Address { count = len(t.vectors[0]) } - addrs := make([]*network.Address, 0, count) + addrs := make([]network.Address, count) for i := 0; i < count; i++ { - addr, err := network.AddressFromString(t.vectors[0][i].Address()) + err := addrs[i].FromString(t.vectors[0][i].Address()) if err != nil { // TODO: log error return nil } - - addrs = append(addrs, addr) } t.vectors[0] = t.vectors[0][count:] diff --git a/pkg/services/object_manager/placement/traverser_test.go b/pkg/services/object_manager/placement/traverser_test.go index a3bb48a80..182062962 100644 --- a/pkg/services/object_manager/placement/traverser_test.go +++ b/pkg/services/object_manager/placement/traverser_test.go @@ -65,10 +65,12 @@ func testPlacement(t *testing.T, ss, rs []int) ([]netmap.Nodes, *container.Conta return nodes, container.New(container.WithPolicy(policy)) } -func assertSameAddress(t *testing.T, ni *netmap.NodeInfo, addr *network.Address) { - netAddr, err := network.AddressFromString(ni.Address()) +func assertSameAddress(t *testing.T, ni *netmap.NodeInfo, addr network.Address) { + var netAddr network.Address + + err := netAddr.FromString(ni.Address()) require.NoError(t, err) - require.True(t, netAddr.Equal(*addr)) + require.True(t, netAddr.Equal(addr)) } func TestTraverserObjectScenarios(t *testing.T) { @@ -122,10 +124,12 @@ func TestTraverserObjectScenarios(t *testing.T) { require.NotNil(t, tr.Next()) } - n, err := network.AddressFromString(nodes[1][0].Address()) + var n network.Address + + err = n.FromString(nodes[1][0].Address()) require.NoError(t, err) - require.Equal(t, []*network.Address{n}, tr.Next()) + require.Equal(t, []network.Address{n}, tr.Next()) }) t.Run("put scenario", func(t *testing.T) { diff --git a/pkg/services/policer/check.go b/pkg/services/policer/check.go index 6ba2f9cf2..800d28459 100644 --- a/pkg/services/policer/check.go +++ b/pkg/services/policer/check.go @@ -61,14 +61,16 @@ func (p *Policer) processNodes(ctx context.Context, addr *object.Address, nodes log := p.log.With(zap.String("node", netAddr)) - node, err := network.AddressFromString(netAddr) + var node network.Address + + err := node.FromString(netAddr) if err != nil { log.Error("could not parse network address") continue } - if network.IsLocalAddress(p.localAddrSrc, *node) { + if network.IsLocalAddress(p.localAddrSrc, node) { if shortage == 0 { // we can call the redundant copy callback // here to slightly improve the performance diff --git a/pkg/services/replicator/process.go b/pkg/services/replicator/process.go index c68d305fd..693f50ccf 100644 --- a/pkg/services/replicator/process.go +++ b/pkg/services/replicator/process.go @@ -70,7 +70,9 @@ func (p *Replicator) handleTask(ctx context.Context, task *Task) { log := p.log.With(zap.String("node", netAddr)) - node, err := network.AddressFromString(netAddr) + var node network.Address + + err := node.FromString(netAddr) if err != nil { log.Error("could not parse network address")