From a5ebdd1891ba211a6cc748ce766262449e8b5c3e Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 22 Sep 2020 09:48:06 +0300 Subject: [PATCH] [#34] services/object: Replace local placement to util Replace local placement tool in order to reuse it in different object services. Signed-off-by: Leonard Lyubich --- pkg/services/object/put/local.go | 33 --------------------- pkg/services/object/put/streamer.go | 6 ++-- pkg/services/object/util/local.go | 45 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 pkg/services/object/util/local.go diff --git a/pkg/services/object/put/local.go b/pkg/services/object/put/local.go index efa7ffb53..cc0616485 100644 --- a/pkg/services/object/put/local.go +++ b/pkg/services/object/put/local.go @@ -1,45 +1,12 @@ package putsvc import ( - "github.com/nspcc-dev/neofs-api-go/pkg/netmap" - objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore" - "github.com/nspcc-dev/neofs-node/pkg/network" - "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer" "github.com/pkg/errors" ) -type localPlacement struct { - builder placement.Builder - - localAddrSrc network.LocalAddressSource -} - -func (p *localPlacement) BuildPlacement(addr *objectSDK.Address, policy *netmap.PlacementPolicy) ([]netmap.Nodes, error) { - vs, err := p.builder.BuildPlacement(addr, policy) - if err != nil { - return nil, errors.Wrapf(err, "(%T) could not build object placement", p) - } - - for i := range vs { - for j := range vs[i] { - addr, err := network.AddressFromString(vs[i][j].NetworkAddress()) - if err != nil { - // TODO: log error - continue - } - - if network.IsLocalAddress(p.localAddrSrc, addr) { - return []netmap.Nodes{{vs[i][j]}}, nil - } - } - } - - return nil, errors.Errorf("(%T) local node is outside of object placement", p) -} - type localTarget struct { storage *localstore.Storage diff --git a/pkg/services/object/put/streamer.go b/pkg/services/object/put/streamer.go index 55e5a2c90..4662a1c50 100644 --- a/pkg/services/object/put/streamer.go +++ b/pkg/services/object/put/streamer.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer" "github.com/pkg/errors" @@ -107,10 +108,7 @@ func (p *Streamer) preparePrm(prm *PutInitPrm) error { prm.traverseOpts = append(prm.traverseOpts, placement.SuccessAfter(1)) // use local-only placement builder - builder = &localPlacement{ - builder: placement.NewNetworkMapBuilder(nm), - localAddrSrc: p.localAddrSrc, - } + builder = util.NewLocalPlacement(placement.NewNetworkMapBuilder(nm), p.localAddrSrc) } // set placement builder diff --git a/pkg/services/object/util/local.go b/pkg/services/object/util/local.go new file mode 100644 index 000000000..701a343ee --- /dev/null +++ b/pkg/services/object/util/local.go @@ -0,0 +1,45 @@ +package util + +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" + "github.com/pkg/errors" +) + +type localPlacement struct { + builder placement.Builder + + localAddrSrc network.LocalAddressSource +} + +func NewLocalPlacement(b placement.Builder, s network.LocalAddressSource) placement.Builder { + return &localPlacement{ + builder: b, + localAddrSrc: s, + } +} + +func (p *localPlacement) BuildPlacement(addr *object.Address, policy *netmap.PlacementPolicy) ([]netmap.Nodes, error) { + vs, err := p.builder.BuildPlacement(addr, policy) + if err != nil { + return nil, errors.Wrapf(err, "(%T) could not build object placement", p) + } + + for i := range vs { + for j := range vs[i] { + addr, err := network.AddressFromString(vs[i][j].NetworkAddress()) + if err != nil { + // TODO: log error + continue + } + + if network.IsLocalAddress(p.localAddrSrc, addr) { + return []netmap.Nodes{{vs[i][j]}}, nil + } + } + } + + return nil, errors.Errorf("(%T) local node is outside of object placement", p) +}