2020-09-17 15:37:44 +00:00
|
|
|
package placement
|
|
|
|
|
|
|
|
import (
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type netMapBuilder struct {
|
2024-07-12 14:27:59 +00:00
|
|
|
nmSrc netmap.Source
|
|
|
|
containerCache *ContainerNodesCache
|
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
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
nm *netmapSDK.NetMap
|
2020-10-21 09:19:58 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func NewNetworkMapBuilder(nm *netmapSDK.NetMap) Builder {
|
2020-10-21 09:19:58 +00:00
|
|
|
return &netMapBuilder{
|
2022-06-09 12:51:38 +00:00
|
|
|
nmSrc: &netMapSrc{nm: nm},
|
2024-07-12 14:27:59 +00:00
|
|
|
containerCache: NewContainerNodesCache(0),
|
2020-10-21 09:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder {
|
2020-09-21 14:27:02 +00:00
|
|
|
return &netMapBuilder{
|
2022-06-09 12:51:38 +00:00
|
|
|
nmSrc: nmSrc,
|
2024-07-12 14:27:59 +00:00
|
|
|
containerCache: NewContainerNodesCache(0),
|
2020-09-21 14:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
func (s *netMapSrc) GetNetMap(_ uint64) (*netmapSDK.NetMap, error) {
|
2020-10-21 09:19:58 +00:00
|
|
|
return s.nm, nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (b *netMapBuilder) BuildPlacement(cnr cid.ID, obj *oid.ID, p netmapSDK.PlacementPolicy) ([][]netmapSDK.NodeInfo, error) {
|
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
|
|
|
}
|
|
|
|
|
2024-07-12 14:27:59 +00:00
|
|
|
cn, err := b.containerCache.ContainerNodes(nm, cnr, p)
|
2020-09-17 15:37:44 +00:00
|
|
|
if err != nil {
|
2024-07-12 14:27:59 +00:00
|
|
|
return nil, err
|
2024-07-12 14:16:06 +00:00
|
|
|
}
|
2022-06-09 12:51:38 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
return BuildObjectPlacement(nm, cn, obj)
|
2020-12-23 09:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func BuildObjectPlacement(nm *netmapSDK.NetMap, cnrNodes [][]netmapSDK.NodeInfo, id *oid.ID) ([][]netmapSDK.NodeInfo, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
if id == nil {
|
2022-06-08 23:18:26 +00:00
|
|
|
return cnrNodes, 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)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
on, err := nm.PlacementVectors(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.
|
2022-06-08 23:18:26 +00:00
|
|
|
func FlattenNodes(ns [][]netmapSDK.NodeInfo) []netmapSDK.NodeInfo {
|
|
|
|
var sz int
|
|
|
|
|
|
|
|
for i := range ns {
|
|
|
|
sz += len(ns[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]netmapSDK.NodeInfo, 0, sz)
|
|
|
|
|
2020-12-23 09:54:12 +00:00
|
|
|
for i := range ns {
|
|
|
|
result = append(result, ns[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|