2020-09-17 15:37:44 +00:00
|
|
|
package placement
|
|
|
|
|
|
|
|
import (
|
2020-10-21 09:19:58 +00:00
|
|
|
netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-17 15:37:44 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-10-21 09:19:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-17 15:37:44 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type netMapBuilder struct {
|
2020-10-21 09:19:58 +00:00
|
|
|
nmSrc netmap.Source
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 09:19:58 +00:00
|
|
|
type netMapSrc struct {
|
|
|
|
nm *netmapSDK.Netmap
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMapBuilder(nm *netmapSDK.Netmap) Builder {
|
|
|
|
return &netMapBuilder{
|
|
|
|
nmSrc: &netMapSrc{nm},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder {
|
2020-09-21 14:27:02 +00:00
|
|
|
return &netMapBuilder{
|
2020-10-21 09:19:58 +00:00
|
|
|
nmSrc: nmSrc,
|
2020-09-21 14:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:19:58 +00:00
|
|
|
func (s *netMapSrc) GetNetMap(diff uint64) (*netmapSDK.Netmap, error) {
|
|
|
|
return s.nm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *netMapBuilder) BuildPlacement(a *object.Address, p *netmapSDK.PlacementPolicy) ([]netmapSDK.Nodes, error) {
|
|
|
|
nm, err := netmap.GetLatestNetworkMap(b.nmSrc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get network map")
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:53:11 +00:00
|
|
|
cn, err := nm.GetContainerNodes(p, a.ContainerID().ToV2().GetValue())
|
2020-09-17 15:37:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get container nodes")
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:53:11 +00:00
|
|
|
return BuildObjectPlacement(nm, cn, a.ObjectID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildObjectPlacement(nm *netmapSDK.Netmap, cnrNodes netmapSDK.ContainerNodes, id *object.ID) ([]netmapSDK.Nodes, error) {
|
|
|
|
objectID := id.ToV2()
|
|
|
|
if objectID == nil {
|
|
|
|
return cnrNodes.Replicas(), nil
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 09:53:11 +00:00
|
|
|
on, err := nm.GetPlacementVectors(cnrNodes, objectID.GetValue())
|
2020-09-17 15:37:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get placement vectors for object")
|
|
|
|
}
|
|
|
|
|
|
|
|
return on, nil
|
|
|
|
}
|