2020-09-17 15:37:44 +00:00
|
|
|
package placement
|
|
|
|
|
|
|
|
import (
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-21 09:19:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2021-11-10 07:08:33 +00:00
|
|
|
netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2021-01-12 14:42:00 +00:00
|
|
|
netmap.Source
|
|
|
|
|
2020-10-21 09:19:58 +00:00
|
|
|
nm *netmapSDK.Netmap
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMapBuilder(nm *netmapSDK.Netmap) Builder {
|
|
|
|
return &netMapBuilder{
|
2021-01-12 14:42:00 +00:00
|
|
|
nmSrc: &netMapSrc{nm: nm},
|
2020-10-21 09:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (b *netMapBuilder) BuildPlacement(a *addressSDK.Address, p *netmapSDK.PlacementPolicy) ([]netmapSDK.Nodes, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, ok := a.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing container in object address")
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:19:58 +00:00
|
|
|
nm, err := netmap.GetLatestNetworkMap(b.nmSrc)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get network map: %w", err)
|
2020-10-21 09:19:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(binCnr)
|
|
|
|
|
|
|
|
cn, err := nm.GetContainerNodes(p, binCnr)
|
2020-09-17 15:37:44 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get container nodes: %w", err)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var idPtr *oidSDK.ID
|
|
|
|
|
|
|
|
if id, ok := a.ObjectID(); ok {
|
|
|
|
idPtr = &id
|
|
|
|
}
|
|
|
|
|
|
|
|
return BuildObjectPlacement(nm, cn, idPtr)
|
2020-12-23 09:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func BuildObjectPlacement(nm *netmapSDK.Netmap, cnrNodes netmapSDK.ContainerNodes, id *oidSDK.ID) ([]netmapSDK.Nodes, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
if id == nil {
|
2020-12-23 09:53:11 +00:00
|
|
|
return cnrNodes.Replicas(), nil
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
binObj := make([]byte, sha256.Size)
|
|
|
|
id.Encode(binObj)
|
|
|
|
|
|
|
|
on, err := nm.GetPlacementVectors(cnrNodes, binObj)
|
2020-09-17 15:37:44 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get placement vectors for object: %w", err)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return on, nil
|
|
|
|
}
|
2020-12-23 09:54:12 +00:00
|
|
|
|
|
|
|
// FlattenNodes appends each row to the flat list.
|
|
|
|
func FlattenNodes(ns []netmapSDK.Nodes) netmapSDK.Nodes {
|
|
|
|
result := make(netmapSDK.Nodes, 0, len(ns))
|
|
|
|
for i := range ns {
|
|
|
|
result = append(result, ns[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|