2020-12-02 23:45:25 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
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/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
|
|
|
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-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type localPlacement struct {
|
|
|
|
builder placement.Builder
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys netmap.AnnouncedKeys
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type remotePlacement struct {
|
|
|
|
builder placement.Builder
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys netmap.AnnouncedKeys
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TraverserGenerator represents tool that generates
|
|
|
|
// container traverser for the particular need.
|
|
|
|
type TraverserGenerator struct {
|
|
|
|
netMapSrc netmap.Source
|
|
|
|
|
|
|
|
cnrSrc container.Source
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys netmap.AnnouncedKeys
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
customOpts []placement.Option
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
func NewLocalPlacement(b placement.Builder, s netmap.AnnouncedKeys) placement.Builder {
|
2020-12-02 23:45:25 +00:00
|
|
|
return &localPlacement{
|
2021-09-06 12:17:14 +00:00
|
|
|
builder: b,
|
|
|
|
netmapKeys: s,
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (p *localPlacement) BuildPlacement(cnr cid.ID, obj *oid.ID, policy netmapSDK.PlacementPolicy) ([][]netmapSDK.NodeInfo, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
vs, err := p.builder.BuildPlacement(cnr, obj, policy)
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not build object placement: %w", p, err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range vs {
|
|
|
|
for j := range vs[i] {
|
2021-06-22 15:27:25 +00:00
|
|
|
var addr network.AddressGroup
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err := addr.FromIterator(network.NodeEndpointsIterator(vs[i][j]))
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
if p.netmapKeys.IsLocalKey(vs[i][j].PublicKey()) {
|
2022-06-08 23:18:26 +00:00
|
|
|
return [][]netmapSDK.NodeInfo{{vs[i][j]}}, nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) local node is outside of object placement", p)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemotePlacementBuilder creates, initializes and returns placement builder that
|
|
|
|
// excludes local node from any placement vector.
|
2021-09-06 12:17:14 +00:00
|
|
|
func NewRemotePlacementBuilder(b placement.Builder, s netmap.AnnouncedKeys) placement.Builder {
|
2020-12-02 23:45:25 +00:00
|
|
|
return &remotePlacement{
|
2021-09-06 12:17:14 +00:00
|
|
|
builder: b,
|
|
|
|
netmapKeys: s,
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (p *remotePlacement) BuildPlacement(cnr cid.ID, obj *oid.ID, policy netmapSDK.PlacementPolicy) ([][]netmapSDK.NodeInfo, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
vs, err := p.builder.BuildPlacement(cnr, obj, policy)
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not build object placement: %w", p, err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range vs {
|
|
|
|
for j := 0; j < len(vs[i]); j++ {
|
2021-06-22 15:27:25 +00:00
|
|
|
var addr network.AddressGroup
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err := addr.FromIterator(network.NodeEndpointsIterator(vs[i][j]))
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
if p.netmapKeys.IsLocalKey(vs[i][j].PublicKey()) {
|
2020-12-02 23:45:25 +00:00
|
|
|
vs[i] = append(vs[i][:j], vs[i][j+1:]...)
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTraverserGenerator creates, initializes and returns new TraverserGenerator instance.
|
2021-09-06 12:17:14 +00:00
|
|
|
func NewTraverserGenerator(nmSrc netmap.Source, cnrSrc container.Source, netmapKeys netmap.AnnouncedKeys) *TraverserGenerator {
|
2020-12-02 23:45:25 +00:00
|
|
|
return &TraverserGenerator{
|
2021-09-06 12:17:14 +00:00
|
|
|
netMapSrc: nmSrc,
|
|
|
|
cnrSrc: cnrSrc,
|
|
|
|
netmapKeys: netmapKeys,
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTraverseOptions returns TraverseGenerator that additionally applies provided options.
|
|
|
|
func (g *TraverserGenerator) WithTraverseOptions(opts ...placement.Option) *TraverserGenerator {
|
|
|
|
return &TraverserGenerator{
|
2021-09-06 12:17:14 +00:00
|
|
|
netMapSrc: g.netMapSrc,
|
|
|
|
cnrSrc: g.cnrSrc,
|
|
|
|
netmapKeys: g.netmapKeys,
|
|
|
|
customOpts: opts,
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
// GenerateTraverser generates placement Traverser for provided object address
|
|
|
|
// using epoch-th network map.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (g *TraverserGenerator) GenerateTraverser(idCnr cid.ID, idObj *oid.ID, epoch uint64) (*placement.Traverser, error) {
|
2021-01-12 14:55:02 +00:00
|
|
|
// get network map by epoch
|
|
|
|
nm, err := g.netMapSrc.GetNetMapByEpoch(epoch)
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get network map #%d: %w", epoch, err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get container related container
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr, err := g.cnrSrc.Get(idCnr)
|
2020-12-02 23:45:25 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get container: %w", err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate placement traverser options
|
|
|
|
traverseOpts := make([]placement.Option, 0, 3+len(g.customOpts))
|
|
|
|
traverseOpts = append(traverseOpts, g.customOpts...)
|
|
|
|
|
|
|
|
// create builder of the remote nodes from network map
|
|
|
|
builder := NewRemotePlacementBuilder(
|
|
|
|
placement.NewNetworkMapBuilder(nm),
|
2021-09-06 12:17:14 +00:00
|
|
|
g.netmapKeys,
|
2020-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
traverseOpts = append(traverseOpts,
|
|
|
|
// set processing container
|
2022-06-22 10:55:31 +00:00
|
|
|
placement.ForContainer(cnr.Value),
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
// set placement builder
|
|
|
|
placement.UseBuilder(builder),
|
|
|
|
)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
if idObj != nil {
|
2022-05-12 16:37:46 +00:00
|
|
|
traverseOpts = append(traverseOpts,
|
|
|
|
// set identifier of the processing object
|
2022-05-31 17:00:41 +00:00
|
|
|
placement.ForObject(*idObj),
|
2022-05-12 16:37:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
return placement.NewTraverser(traverseOpts...)
|
|
|
|
}
|