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"
|
2022-06-09 12:51:38 +00:00
|
|
|
"sync"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
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"
|
2022-12-30 09:54:48 +00:00
|
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type netMapBuilder struct {
|
2020-10-21 09:19:58 +00:00
|
|
|
nmSrc netmap.Source
|
2024-07-12 14:16:06 +00:00
|
|
|
// mtx protects lastEpoch and containerCache fields.
|
|
|
|
mtx sync.Mutex
|
|
|
|
// lastEpoch contains contains network map epoch for all values in the container cache.
|
|
|
|
lastEpoch uint64
|
2022-06-09 12:51:38 +00:00
|
|
|
// containerCache caches container nodes by ID. It is used to skip `GetContainerNodes` invocation if
|
|
|
|
// neither netmap nor container has changed.
|
2024-07-12 14:04:07 +00:00
|
|
|
containerCache simplelru.LRUCache[cid.ID, [][]netmapSDK.NodeInfo]
|
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-09 12:51:38 +00:00
|
|
|
// defaultContainerCacheSize is the default size for the container cache.
|
|
|
|
const defaultContainerCacheSize = 10
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func NewNetworkMapBuilder(nm *netmapSDK.NetMap) Builder {
|
2024-07-12 14:04:07 +00:00
|
|
|
cache, _ := simplelru.NewLRU[cid.ID, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error
|
2020-10-21 09:19:58 +00:00
|
|
|
return &netMapBuilder{
|
2022-06-09 12:51:38 +00:00
|
|
|
nmSrc: &netMapSrc{nm: nm},
|
|
|
|
containerCache: cache,
|
2020-10-21 09:19:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNetworkMapSourceBuilder(nmSrc netmap.Source) Builder {
|
2024-07-12 14:04:07 +00:00
|
|
|
cache, _ := simplelru.NewLRU[cid.ID, [][]netmapSDK.NodeInfo](defaultContainerCacheSize, nil) // no error
|
2020-09-21 14:27:02 +00:00
|
|
|
return &netMapBuilder{
|
2022-06-09 12:51:38 +00:00
|
|
|
nmSrc: nmSrc,
|
|
|
|
containerCache: cache,
|
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
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
cnr.Encode(binCnr)
|
|
|
|
|
2022-06-09 12:51:38 +00:00
|
|
|
b.mtx.Lock()
|
2024-07-12 14:16:06 +00:00
|
|
|
if nm.Epoch() == b.lastEpoch {
|
2024-07-12 14:04:07 +00:00
|
|
|
raw, ok := b.containerCache.Get(cnr)
|
2022-06-09 12:51:38 +00:00
|
|
|
b.mtx.Unlock()
|
|
|
|
if ok {
|
2022-12-30 09:54:48 +00:00
|
|
|
return BuildObjectPlacement(nm, raw, obj)
|
2022-06-09 12:51:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-12 14:16:06 +00:00
|
|
|
b.lastEpoch = nm.Epoch()
|
2022-06-09 12:51:38 +00:00
|
|
|
b.containerCache.Purge()
|
|
|
|
b.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
cn, err := nm.ContainerNodes(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-06-09 12:51:38 +00:00
|
|
|
b.mtx.Lock()
|
2024-07-12 14:16:06 +00:00
|
|
|
if b.lastEpoch == nm.Epoch() {
|
|
|
|
b.containerCache.Add(cnr, cn)
|
|
|
|
}
|
2022-06-09 12:51:38 +00:00
|
|
|
b.mtx.Unlock()
|
|
|
|
|
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
|
|
|
|
}
|