2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-24 11:31:34 +00:00
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-10-27 10:00:35 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2021-10-27 10:00:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-12-24 11:31:34 +00:00
|
|
|
func BenchmarkHRWSort(b *testing.B) {
|
|
|
|
const netmapSize = 1000
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
vectors := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
weights := make([]float64, netmapSize)
|
2022-06-07 02:12:39 +00:00
|
|
|
for i := range vectors {
|
|
|
|
key := make([]byte, 33)
|
|
|
|
rand.Read(key)
|
|
|
|
|
|
|
|
var node NodeInfo
|
2022-06-07 08:25:34 +00:00
|
|
|
node.SetPrice(1)
|
|
|
|
node.SetCapacity(100)
|
2022-06-07 02:12:39 +00:00
|
|
|
node.SetPublicKey(key)
|
|
|
|
|
|
|
|
vectors[i] = nodes{node}
|
2021-12-24 11:31:34 +00:00
|
|
|
weights[i] = float64(rand.Uint32()%10) / 10.0
|
|
|
|
}
|
|
|
|
|
|
|
|
pivot := rand.Uint64()
|
|
|
|
b.Run("sort by index, no weight", func(b *testing.B) {
|
2022-06-07 02:12:39 +00:00
|
|
|
realNodes := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-06-07 02:12:39 +00:00
|
|
|
copy(realNodes, vectors)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
hrw.SortSliceByIndex(realNodes, pivot)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("sort by value, no weight", func(b *testing.B) {
|
2022-06-07 02:12:39 +00:00
|
|
|
realNodes := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-06-07 02:12:39 +00:00
|
|
|
copy(realNodes, vectors)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
2023-02-28 11:04:02 +00:00
|
|
|
hrw.SortHasherSliceByValue(realNodes, pivot)
|
2021-12-24 11:31:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("only sort by index", func(b *testing.B) {
|
2022-06-07 02:12:39 +00:00
|
|
|
realNodes := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-06-07 02:12:39 +00:00
|
|
|
copy(realNodes, vectors)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
hrw.SortSliceByWeightIndex(realNodes, weights, pivot)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("sort by value", func(b *testing.B) {
|
2022-06-07 02:12:39 +00:00
|
|
|
realNodes := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-06-07 02:12:39 +00:00
|
|
|
copy(realNodes, vectors)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
2023-02-28 11:04:02 +00:00
|
|
|
hrw.SortHasherSliceByWeightValue(realNodes, weights, pivot)
|
2021-12-24 11:31:34 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("sort by ID, then by index (deterministic)", func(b *testing.B) {
|
2022-06-07 02:12:39 +00:00
|
|
|
realNodes := make([]nodes, netmapSize)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2022-06-07 02:12:39 +00:00
|
|
|
copy(realNodes, vectors)
|
2021-12-24 11:31:34 +00:00
|
|
|
b.StartTimer()
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
sort.Slice(vectors, func(i, j int) bool {
|
2022-06-07 08:25:34 +00:00
|
|
|
return less(vectors[i][0], vectors[j][0])
|
2021-12-24 11:31:34 +00:00
|
|
|
})
|
|
|
|
hrw.SortSliceByWeightIndex(realNodes, weights, pivot)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPolicyHRWType(b *testing.B) {
|
|
|
|
const netmapSize = 100
|
|
|
|
|
|
|
|
p := newPlacementPolicy(1,
|
2022-06-07 08:25:34 +00:00
|
|
|
[]ReplicaDescriptor{
|
2021-12-24 11:31:34 +00:00
|
|
|
newReplica(1, "loc1"),
|
|
|
|
newReplica(1, "loc2")},
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Selector{
|
2022-06-07 08:25:34 +00:00
|
|
|
newSelector("loc1", "Location", 1, "loc1", (*Selector).SelectSame),
|
|
|
|
newSelector("loc2", "Location", 1, "loc2", (*Selector).SelectSame)},
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Filter{
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("loc1", "Location", "Shanghai", netmap.EQ),
|
|
|
|
newFilter("loc2", "Location", "Shanghai", netmap.NE),
|
2021-12-24 11:31:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
nodes := make([]NodeInfo, netmapSize)
|
|
|
|
for i := range nodes {
|
|
|
|
var loc string
|
|
|
|
switch i % 20 {
|
|
|
|
case 0:
|
|
|
|
loc = "Shanghai"
|
|
|
|
default:
|
|
|
|
loc = strconv.Itoa(i % 20)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Having the same price and capacity ensures equal weights for all nodes.
|
|
|
|
// This way placement is more dependent on the initial order.
|
|
|
|
nodes[i] = nodeInfoFromAttributes("Location", loc, "Price", "1", "Capacity", "10")
|
|
|
|
pub := make([]byte, 33)
|
|
|
|
pub[0] = byte(i)
|
|
|
|
nodes[i].SetPublicKey(pub)
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(nodes)
|
2021-12-24 11:31:34 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-06-07 08:25:34 +00:00
|
|
|
_, err := nm.ContainerNodes(p, []byte{1})
|
2021-12-24 11:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlacementPolicy_DeterministicOrder(t *testing.T) {
|
|
|
|
const netmapSize = 100
|
|
|
|
|
|
|
|
p := newPlacementPolicy(1,
|
2022-06-07 08:25:34 +00:00
|
|
|
[]ReplicaDescriptor{
|
2021-12-24 11:31:34 +00:00
|
|
|
newReplica(1, "loc1"),
|
|
|
|
newReplica(1, "loc2")},
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Selector{
|
2022-06-07 08:25:34 +00:00
|
|
|
newSelector("loc1", "Location", 1, "loc1", (*Selector).SelectSame),
|
|
|
|
newSelector("loc2", "Location", 1, "loc2", (*Selector).SelectSame)},
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Filter{
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("loc1", "Location", "Shanghai", netmap.EQ),
|
|
|
|
newFilter("loc2", "Location", "Shanghai", netmap.NE),
|
2021-12-24 11:31:34 +00:00
|
|
|
})
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
nodeList := make([]NodeInfo, netmapSize)
|
|
|
|
for i := range nodeList {
|
2021-12-24 11:31:34 +00:00
|
|
|
var loc string
|
|
|
|
switch i % 20 {
|
|
|
|
case 0:
|
|
|
|
loc = "Shanghai"
|
|
|
|
default:
|
|
|
|
loc = strconv.Itoa(i % 20)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Having the same price and capacity ensures equal weights for all nodes.
|
|
|
|
// This way placement is more dependent on the initial order.
|
2022-06-07 02:12:39 +00:00
|
|
|
nodeList[i] = nodeInfoFromAttributes("Location", loc, "Price", "1", "Capacity", "10")
|
2021-12-24 11:31:34 +00:00
|
|
|
pub := make([]byte, 33)
|
|
|
|
pub[0] = byte(i)
|
2022-06-07 02:12:39 +00:00
|
|
|
nodeList[i].SetPublicKey(pub)
|
2021-12-24 11:31:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(nodeList)
|
|
|
|
|
|
|
|
getIndices := func(t *testing.T) (uint64, uint64) {
|
2022-06-07 08:25:34 +00:00
|
|
|
v, err := nm.ContainerNodes(p, []byte{1})
|
2021-12-24 11:31:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-07 02:12:39 +00:00
|
|
|
|
|
|
|
nss := make([]nodes, len(v))
|
|
|
|
for i := range v {
|
|
|
|
nss[i] = v[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := flattenNodes(nss)
|
2021-12-24 11:31:34 +00:00
|
|
|
require.Equal(t, 2, len(ns))
|
2022-06-07 02:12:39 +00:00
|
|
|
return ns[0].Hash(), ns[1].Hash()
|
2021-12-24 11:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a, b := getIndices(t)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
x, y := getIndices(t)
|
|
|
|
require.Equal(t, a, x)
|
|
|
|
require.Equal(t, b, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:00:35 +00:00
|
|
|
func TestPlacementPolicy_ProcessSelectors(t *testing.T) {
|
|
|
|
p := newPlacementPolicy(2, nil,
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Selector{
|
2022-06-07 08:25:34 +00:00
|
|
|
newSelector("SameRU", "City", 2, "FromRU", (*Selector).SelectSame),
|
|
|
|
newSelector("DistinctRU", "City", 2, "FromRU", (*Selector).SelectDistinct),
|
|
|
|
newSelector("Good", "Country", 2, "Good", (*Selector).SelectDistinct),
|
|
|
|
newSelector("Main", "Country", 3, "*", (*Selector).SelectDistinct),
|
2021-10-27 10:00:35 +00:00
|
|
|
},
|
2022-03-11 09:13:08 +00:00
|
|
|
[]Filter{
|
2022-06-07 08:25:34 +00:00
|
|
|
newFilter("FromRU", "Country", "Russia", netmap.EQ),
|
|
|
|
newFilter("Good", "Rating", "4", netmap.GE),
|
2021-10-27 10:00:35 +00:00
|
|
|
})
|
|
|
|
nodes := []NodeInfo{
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "1", "City", "SPB"),
|
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "5", "City", "Berlin"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "6", "City", "Moscow"),
|
|
|
|
nodeInfoFromAttributes("Country", "France", "Rating", "4", "City", "Paris"),
|
|
|
|
nodeInfoFromAttributes("Country", "France", "Rating", "1", "City", "Lyon"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "5", "City", "SPB"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "7", "City", "Moscow"),
|
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "3", "City", "Darmstadt"),
|
2023-05-24 09:50:13 +00:00
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "7", "City", "Frankfurt"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "9", "City", "SPB"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "9", "City", "SPB"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var nm NetMap
|
|
|
|
nm.SetNodes(nodes)
|
|
|
|
c := newContext(nm)
|
|
|
|
c.setCBF(p.backupFactor)
|
|
|
|
require.NoError(t, c.processFilters(p))
|
|
|
|
require.NoError(t, c.processSelectors(p))
|
|
|
|
|
|
|
|
for _, s := range p.selectors {
|
|
|
|
sel := c.selections[s.GetName()]
|
|
|
|
s := c.processedSelectors[s.GetName()]
|
|
|
|
bucketCount, nodesInBucket := calcNodesCount(*s)
|
|
|
|
nodesInBucket *= int(c.cbf)
|
|
|
|
targ := fmt.Sprintf("selector '%s'", s.GetName())
|
|
|
|
require.Equal(t, bucketCount, len(sel), targ)
|
|
|
|
fName := s.GetFilter()
|
|
|
|
for _, res := range sel {
|
|
|
|
require.Equal(t, nodesInBucket, len(res), targ)
|
|
|
|
for j := range res {
|
|
|
|
require.True(t, fName == mainFilterName || c.match(c.processedFilters[fName], res[j]), targ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlacementPolicy_ProcessSelectorsExceptForNodes(t *testing.T) {
|
|
|
|
p := newPlacementPolicy(1, nil,
|
|
|
|
[]Selector{
|
|
|
|
newSelector("ExceptRU", "City", 2, "ExceptRU", (*Selector).SelectSame),
|
|
|
|
},
|
|
|
|
[]Filter{
|
|
|
|
newFilter("ExceptRU", "", "", netmap.NOT,
|
|
|
|
newFilter("", "", "", netmap.AND,
|
|
|
|
newFilter("", "City", "Lyon", netmap.EQ),
|
|
|
|
newFilter("", "Rating", "10", netmap.LE),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
nodes := []NodeInfo{
|
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "1", "City", "Berlin"),
|
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "5", "City", "Berlin"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "6", "City", "Moscow"),
|
|
|
|
nodeInfoFromAttributes("Country", "France", "Rating", "4", "City", "Paris"),
|
|
|
|
nodeInfoFromAttributes("Country", "France", "Rating", "1", "City", "Lyon"),
|
|
|
|
nodeInfoFromAttributes("Country", "France", "Rating", "5", "City", "Lyon"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "7", "City", "Moscow"),
|
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "3", "City", "Darmstadt"),
|
2021-10-27 10:00:35 +00:00
|
|
|
nodeInfoFromAttributes("Country", "Germany", "Rating", "7", "City", "Frankfurt"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "9", "City", "SPB"),
|
|
|
|
nodeInfoFromAttributes("Country", "Russia", "Rating", "9", "City", "SPB"),
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var nm NetMap
|
2022-06-07 02:12:39 +00:00
|
|
|
nm.SetNodes(nodes)
|
2022-06-07 08:25:34 +00:00
|
|
|
c := newContext(nm)
|
|
|
|
c.setCBF(p.backupFactor)
|
2021-10-27 10:00:35 +00:00
|
|
|
require.NoError(t, c.processFilters(p))
|
|
|
|
require.NoError(t, c.processSelectors(p))
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for _, s := range p.selectors {
|
|
|
|
sel := c.selections[s.GetName()]
|
|
|
|
s := c.processedSelectors[s.GetName()]
|
|
|
|
bucketCount, nodesInBucket := calcNodesCount(*s)
|
2021-10-27 10:00:35 +00:00
|
|
|
nodesInBucket *= int(c.cbf)
|
2022-06-07 08:25:34 +00:00
|
|
|
targ := fmt.Sprintf("selector '%s'", s.GetName())
|
2021-10-27 10:00:35 +00:00
|
|
|
require.Equal(t, bucketCount, len(sel), targ)
|
2022-06-07 08:25:34 +00:00
|
|
|
fName := s.GetFilter()
|
2021-10-27 10:00:35 +00:00
|
|
|
for _, res := range sel {
|
|
|
|
require.Equal(t, nodesInBucket, len(res), targ)
|
|
|
|
for j := range res {
|
2022-06-07 08:25:34 +00:00
|
|
|
require.True(t, fName == mainFilterName || c.match(c.processedFilters[fName], res[j]), targ)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestSelector_SetName(t *testing.T) {
|
|
|
|
const name = "some name"
|
|
|
|
var s Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Zero(t, s.m.GetName())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
s.SetName(name)
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, name, s.m.GetName())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
func TestSelector_SetNumberOfNodes(t *testing.T) {
|
|
|
|
const num = 3
|
2022-06-07 08:25:34 +00:00
|
|
|
var s Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Zero(t, s.m.GetCount())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
s.SetNumberOfNodes(num)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-14 14:27:51 +00:00
|
|
|
require.EqualValues(t, num, s.m.GetCount())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestSelectorClauses(t *testing.T) {
|
|
|
|
var s Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, netmap.UnspecifiedClause, s.m.GetClause())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
s.SelectDistinct()
|
|
|
|
require.Equal(t, netmap.Distinct, s.m.GetClause())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
s.SelectSame()
|
|
|
|
require.Equal(t, netmap.Same, s.m.GetClause())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestSelector_SelectByBucketAttribute(t *testing.T) {
|
|
|
|
const attr = "some attribute"
|
|
|
|
var s Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Zero(t, s.m.GetAttribute())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
s.SelectByBucketAttribute(attr)
|
|
|
|
require.Equal(t, attr, s.m.GetAttribute())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestSelector_SetFilterName(t *testing.T) {
|
|
|
|
const fName = "some filter"
|
|
|
|
var s Selector
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Zero(t, s.m.GetFilter())
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
s.SetFilterName(fName)
|
|
|
|
require.Equal(t, fName, s.m.GetFilter())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|