2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/hrw"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2021-11-26 12:31:36 +00:00
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// processSelectors processes selectors and returns error is any of them is invalid.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) processSelectors(p PlacementPolicy) error {
|
|
|
|
for i := range p.selectors {
|
|
|
|
fName := p.selectors[i].GetFilter()
|
|
|
|
if fName != mainFilterName {
|
|
|
|
_, ok := c.processedFilters[p.selectors[i].GetFilter()]
|
2021-10-27 10:00:35 +00:00
|
|
|
if !ok {
|
2022-06-07 08:25:34 +00:00
|
|
|
return fmt.Errorf("%w: SELECT FROM '%s'", errFilterNotFound, fName)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
sName := p.selectors[i].GetName()
|
|
|
|
|
|
|
|
c.processedSelectors[sName] = &p.selectors[i]
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
result, err := c.getSelection(p, p.selectors[i])
|
2021-10-27 10:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c.selections[sName] = result
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
// calcNodesCount returns number of buckets and minimum number of nodes in every bucket
|
2021-10-27 10:00:35 +00:00
|
|
|
// for the given selector.
|
2022-06-07 08:25:34 +00:00
|
|
|
func calcNodesCount(s netmap.Selector) (int, int) {
|
|
|
|
switch s.GetClause() {
|
|
|
|
case netmap.Same:
|
|
|
|
return 1, int(s.GetCount())
|
2021-10-27 10:00:35 +00:00
|
|
|
default:
|
2022-06-07 08:25:34 +00:00
|
|
|
return int(s.GetCount()), 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcBucketWeight computes weight for a node bucket.
|
|
|
|
func calcBucketWeight(ns nodes, a aggregator, wf weightFunc) float64 {
|
|
|
|
for i := range ns {
|
|
|
|
a.Add(wf(ns[i]))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
|
|
|
|
return a.Compute()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getSelection returns nodes grouped by s.attribute.
|
2021-11-08 12:02:17 +00:00
|
|
|
// Last argument specifies if more buckets can be used to fulfill CBF.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) getSelection(p PlacementPolicy, s netmap.Selector) ([]nodes, error) {
|
|
|
|
bucketCount, nodesInBucket := calcNodesCount(s)
|
|
|
|
buckets := c.getSelectionBase(p.subnet, s)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
if len(buckets) < bucketCount {
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil, fmt.Errorf("%w: '%s'", errNotEnoughNodes, s.GetName())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 11:31:34 +00:00
|
|
|
// We need deterministic output in case there is no pivot.
|
|
|
|
// If pivot is set, buckets are sorted by HRW.
|
|
|
|
// However, because initial order influences HRW order for buckets with equal weights,
|
|
|
|
// we also need to have deterministic input to HRW sorting routine.
|
2022-06-07 08:25:34 +00:00
|
|
|
if len(c.hrwSeed) == 0 {
|
|
|
|
if s.GetAttribute() == "" {
|
2021-10-27 10:00:35 +00:00
|
|
|
sort.Slice(buckets, func(i, j int) bool {
|
2022-06-07 08:25:34 +00:00
|
|
|
return less(buckets[i].nodes[0], buckets[j].nodes[0])
|
2021-10-27 10:00:35 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
sort.Slice(buckets, func(i, j int) bool {
|
|
|
|
return buckets[i].attr < buckets[j].attr
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maxNodesInBucket := nodesInBucket * int(c.cbf)
|
2022-06-07 02:12:39 +00:00
|
|
|
res := make([]nodes, 0, len(buckets))
|
|
|
|
fallback := make([]nodes, 0, len(buckets))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
for i := range buckets {
|
|
|
|
ns := buckets[i].nodes
|
|
|
|
if len(ns) >= maxNodesInBucket {
|
2022-06-07 02:12:39 +00:00
|
|
|
res = append(res, ns[:maxNodesInBucket])
|
2021-10-27 10:00:35 +00:00
|
|
|
} else if len(ns) >= nodesInBucket {
|
|
|
|
fallback = append(fallback, ns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
if len(res) < bucketCount {
|
2021-10-27 10:00:35 +00:00
|
|
|
// Fallback to using minimum allowed backup factor (1).
|
2022-06-07 02:12:39 +00:00
|
|
|
res = append(res, fallback...)
|
|
|
|
if len(res) < bucketCount {
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil, fmt.Errorf("%w: '%s'", errNotEnoughNodes, s.GetName())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if len(c.hrwSeed) != 0 {
|
2022-06-07 02:12:39 +00:00
|
|
|
weights := make([]float64, len(res))
|
|
|
|
for i := range res {
|
2022-06-07 08:25:34 +00:00
|
|
|
weights[i] = calcBucketWeight(res[i], newMeanIQRAgg(), c.weightFunc)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
hrw.SortSliceByWeightValue(res, weights, c.hrwSeedHash)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if s.GetAttribute() == "" {
|
2022-06-07 02:12:39 +00:00
|
|
|
res, fallback = res[:bucketCount], res[bucketCount:]
|
2021-10-27 10:00:35 +00:00
|
|
|
for i := range fallback {
|
|
|
|
index := i % bucketCount
|
2022-06-07 02:12:39 +00:00
|
|
|
if len(res[index]) >= maxNodesInBucket {
|
2021-10-27 10:00:35 +00:00
|
|
|
break
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
res[index] = append(res[index], fallback[i]...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
return res[:bucketCount], nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeAttrPair struct {
|
|
|
|
attr string
|
2022-06-07 02:12:39 +00:00
|
|
|
nodes nodes
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getSelectionBase returns nodes grouped by selector attribute.
|
|
|
|
// It it guaranteed that each pair will contain at least one node.
|
2022-06-07 08:25:34 +00:00
|
|
|
func (c *context) getSelectionBase(subnetID subnetid.ID, s netmap.Selector) []nodeAttrPair {
|
|
|
|
fName := s.GetFilter()
|
|
|
|
f := c.processedFilters[fName]
|
|
|
|
isMain := fName == mainFilterName
|
2021-10-27 10:00:35 +00:00
|
|
|
result := []nodeAttrPair{}
|
2022-06-07 02:12:39 +00:00
|
|
|
nodeMap := map[string][]NodeInfo{}
|
2022-06-07 08:25:34 +00:00
|
|
|
attr := s.GetAttribute()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := range c.netMap.nodes {
|
2021-11-26 12:31:36 +00:00
|
|
|
// TODO(fyrchik): make `BelongsToSubnet` to accept pointer
|
2022-06-07 08:25:34 +00:00
|
|
|
if !BelongsToSubnet(c.netMap.nodes[i], subnetID) {
|
2021-11-26 12:31:36 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
if isMain || c.match(f, c.netMap.nodes[i]) {
|
2021-10-27 10:00:35 +00:00
|
|
|
if attr == "" {
|
|
|
|
// Default attribute is transparent identifier which is different for every node.
|
2022-06-07 08:25:34 +00:00
|
|
|
result = append(result, nodeAttrPair{attr: "", nodes: nodes{c.netMap.nodes[i]}})
|
2021-10-27 10:00:35 +00:00
|
|
|
} else {
|
2022-06-07 08:25:34 +00:00
|
|
|
v := c.netMap.nodes[i].Attribute(attr)
|
|
|
|
nodeMap[v] = append(nodeMap[v], c.netMap.nodes[i])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr != "" {
|
|
|
|
for k, ns := range nodeMap {
|
|
|
|
result = append(result, nodeAttrPair{attr: k, nodes: ns})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if len(c.hrwSeed) != 0 {
|
2021-10-27 10:00:35 +00:00
|
|
|
for i := range result {
|
2022-06-07 08:25:34 +00:00
|
|
|
hrw.SortSliceByWeightValue(result[i].nodes, result[i].nodes.weights(c.weightFunc), c.hrwSeedHash)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|