forked from TrueCloudLab/frostfs-api-go
[#174] netmap: process unspecified attribute as ID
When selector attribute is provided it should be silently replaced by transparent ID, so each node resides in a separate bucket. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
4e6350f6d4
commit
6a69a896e5
2 changed files with 61 additions and 17 deletions
|
@ -43,23 +43,27 @@ func GetNodesCount(p *netmap.PlacementPolicy, s *netmap.Selector) (int, int) {
|
||||||
// getSelection returns nodes grouped by s.attribute.
|
// getSelection returns nodes grouped by s.attribute.
|
||||||
func (c *Context) getSelection(p *netmap.PlacementPolicy, s *netmap.Selector) ([]Nodes, error) {
|
func (c *Context) getSelection(p *netmap.PlacementPolicy, s *netmap.Selector) ([]Nodes, error) {
|
||||||
bucketCount, nodesInBucket := GetNodesCount(p, s)
|
bucketCount, nodesInBucket := GetNodesCount(p, s)
|
||||||
m := c.getSelectionBase(s)
|
buckets := c.getSelectionBase(s)
|
||||||
if len(m) < bucketCount {
|
if len(buckets) < bucketCount {
|
||||||
return nil, fmt.Errorf("%w: '%s'", ErrNotEnoughNodes, s.GetName())
|
return nil, fmt.Errorf("%w: '%s'", ErrNotEnoughNodes, s.GetName())
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make(sort.StringSlice, 0, len(m))
|
|
||||||
for k := range m {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
if len(c.pivot) == 0 {
|
if len(c.pivot) == 0 {
|
||||||
// deterministic order in case of zero seed
|
// Deterministic order in case of zero seed.
|
||||||
keys.Sort()
|
if s.GetAttribute() == "" {
|
||||||
|
sort.Slice(buckets, func(i, j int) bool {
|
||||||
|
return buckets[i].nodes[0].ID < buckets[j].nodes[0].ID
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
sort.Slice(buckets, func(i, j int) bool {
|
||||||
|
return buckets[i].attr < buckets[j].attr
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes := make([]Nodes, 0, len(m))
|
nodes := make([]Nodes, 0, len(buckets))
|
||||||
for i := range keys {
|
for i := range buckets {
|
||||||
ns := m[keys[i]]
|
ns := buckets[i].nodes
|
||||||
if len(ns) >= nodesInBucket {
|
if len(ns) >= nodesInBucket {
|
||||||
nodes = append(nodes, ns[:nodesInBucket])
|
nodes = append(nodes, ns[:nodesInBucket])
|
||||||
}
|
}
|
||||||
|
@ -77,21 +81,39 @@ func (c *Context) getSelection(p *netmap.PlacementPolicy, s *netmap.Selector) ([
|
||||||
return nodes[:bucketCount], nil
|
return nodes[:bucketCount], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type nodeAttrPair struct {
|
||||||
|
attr string
|
||||||
|
nodes Nodes
|
||||||
|
}
|
||||||
|
|
||||||
// getSelectionBase returns nodes grouped by selector attribute.
|
// getSelectionBase returns nodes grouped by selector attribute.
|
||||||
func (c *Context) getSelectionBase(s *netmap.Selector) map[string]Nodes {
|
// It it guaranteed that each pair will contain at least one node.
|
||||||
|
func (c *Context) getSelectionBase(s *netmap.Selector) []nodeAttrPair {
|
||||||
f := c.Filters[s.GetFilter()]
|
f := c.Filters[s.GetFilter()]
|
||||||
isMain := s.GetFilter() == MainFilterName
|
isMain := s.GetFilter() == MainFilterName
|
||||||
result := map[string]Nodes{}
|
result := []nodeAttrPair{}
|
||||||
|
nodeMap := map[string]Nodes{}
|
||||||
|
attr := s.GetAttribute()
|
||||||
for i := range c.Netmap.Nodes {
|
for i := range c.Netmap.Nodes {
|
||||||
if isMain || c.match(f, c.Netmap.Nodes[i]) {
|
if isMain || c.match(f, c.Netmap.Nodes[i]) {
|
||||||
v := c.Netmap.Nodes[i].Attribute(s.GetAttribute())
|
if attr == "" {
|
||||||
result[v] = append(result[v], c.Netmap.Nodes[i])
|
// Default attribute is transparent identifier which is different for every node.
|
||||||
|
result = append(result, nodeAttrPair{attr: "", nodes: Nodes{c.Netmap.Nodes[i]}})
|
||||||
|
} else {
|
||||||
|
v := c.Netmap.Nodes[i].Attribute(attr)
|
||||||
|
nodeMap[v] = append(nodeMap[v], c.Netmap.Nodes[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if attr != "" {
|
||||||
|
for k, ns := range nodeMap {
|
||||||
|
result = append(result, nodeAttrPair{attr: k, nodes: ns})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.pivot) != 0 {
|
if len(c.pivot) != 0 {
|
||||||
for _, ns := range result {
|
for i := range result {
|
||||||
hrw.SortSliceByWeightValue(ns, ns.Weights(c.weightFunc), c.pivotHash)
|
hrw.SortSliceByWeightValue(result[i].nodes, result[i].nodes.Weights(c.weightFunc), c.pivotHash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -9,6 +9,28 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestPlacementPolicy_UnspecifiedClause(t *testing.T) {
|
||||||
|
p := newPlacementPolicy(1,
|
||||||
|
[]*netmap.Replica{newReplica(1, "X")},
|
||||||
|
[]*netmap.Selector{
|
||||||
|
newSelector("X", "", netmap.Distinct, 4, "*"),
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
nodes := []netmap.NodeInfo{
|
||||||
|
nodeInfoFromAttributes("ID", "1", "Country", "RU", "City", "St.Petersburg", "SSD", "0"),
|
||||||
|
nodeInfoFromAttributes("ID", "2", "Country", "RU", "City", "St.Petersburg", "SSD", "1"),
|
||||||
|
nodeInfoFromAttributes("ID", "3", "Country", "RU", "City", "Moscow", "SSD", "1"),
|
||||||
|
nodeInfoFromAttributes("ID", "4", "Country", "RU", "City", "Moscow", "SSD", "1"),
|
||||||
|
}
|
||||||
|
|
||||||
|
nm, err := NewNetmap(NodesFromV2(nodes))
|
||||||
|
require.NoError(t, err)
|
||||||
|
v, err := nm.GetContainerNodes(p, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 4, len(v.Flatten()))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPlacementPolicy_GetPlacementVectors(t *testing.T) {
|
func TestPlacementPolicy_GetPlacementVectors(t *testing.T) {
|
||||||
p := newPlacementPolicy(2,
|
p := newPlacementPolicy(2,
|
||||||
[]*netmap.Replica{
|
[]*netmap.Replica{
|
||||||
|
|
Loading…
Reference in a new issue