forked from TrueCloudLab/frostfs-node
[#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 <leonard@nspcc.ru>
This commit is contained in:
parent
ded0350c5e
commit
a5ebdd1891
3 changed files with 47 additions and 37 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
45
pkg/services/object/util/local.go
Normal file
45
pkg/services/object/util/local.go
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue