2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2024-07-12 07:22:35 +00:00
|
|
|
"cmp"
|
2021-10-27 10:00:35 +00:00
|
|
|
"fmt"
|
2024-07-12 07:22:35 +00:00
|
|
|
"slices"
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/netmap"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
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
|
|
|
|
2023-04-18 09:56:23 +00:00
|
|
|
result, err := c.getSelection(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.
|
2023-04-18 09:56:23 +00:00
|
|
|
func (c *context) getSelection(s netmap.Selector) ([]nodes, error) {
|
2022-06-07 08:25:34 +00:00
|
|
|
bucketCount, nodesInBucket := calcNodesCount(s)
|
2023-04-14 13:31:08 +00:00
|
|
|
buckets := c.getSelectionBase(s)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2023-09-15 11:29:49 +00:00
|
|
|
if c.strict && 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() == "" {
|
2024-07-12 07:22:35 +00:00
|
|
|
slices.SortFunc(buckets, func(b1, b2 nodeAttrPair) int {
|
|
|
|
return cmp.Compare(b1.nodes[0].Hash(), b2.nodes[0].Hash())
|
2021-10-27 10:00:35 +00:00
|
|
|
})
|
|
|
|
} else {
|
2024-07-12 07:22:35 +00:00
|
|
|
slices.SortFunc(buckets, func(b1, b2 nodeAttrPair) int {
|
|
|
|
return cmp.Compare(b1.attr, b2.attr)
|
2021-10-27 10:00:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2023-09-15 11:29:49 +00:00
|
|
|
if c.strict && 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))
|
2024-07-12 07:33:34 +00:00
|
|
|
a := new(meanIQRAgg)
|
2022-06-07 02:12:39 +00:00
|
|
|
for i := range res {
|
2024-07-12 07:33:34 +00:00
|
|
|
a.clear()
|
|
|
|
weights[i] = calcBucketWeight(res[i], a, c.weightFunc)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 11:04:02 +00:00
|
|
|
hrw.SortHasherSliceByWeightValue(res, weights, c.hrwSeedHash)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 11:29:49 +00:00
|
|
|
if len(res) < bucketCount {
|
|
|
|
if len(res) == 0 {
|
|
|
|
return nil, errNotEnoughNodes
|
|
|
|
}
|
|
|
|
bucketCount = len(res)
|
|
|
|
}
|
|
|
|
|
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.
|
2023-04-14 13:31:08 +00:00
|
|
|
func (c *context) getSelectionBase(s netmap.Selector) []nodeAttrPair {
|
2022-06-07 08:25:34 +00:00
|
|
|
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 {
|
2023-06-05 10:38:15 +00:00
|
|
|
if c.usedNodes[c.netMap.nodes[i].hash] {
|
|
|
|
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 {
|
2024-07-12 07:46:53 +00:00
|
|
|
var ws []float64
|
2021-10-27 10:00:35 +00:00
|
|
|
for i := range result {
|
2024-07-12 07:46:53 +00:00
|
|
|
ws = result[i].nodes.appendWeightsTo(c.weightFunc, ws[:0])
|
|
|
|
hrw.SortHasherSliceByWeightValue(result[i].nodes, ws, c.hrwSeedHash)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|