2020-09-17 15:37:44 +00:00
|
|
|
package placement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-01 14:10:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2022-05-31 17:00:41 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-09-17 15:37:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testBuilder struct {
|
|
|
|
vectors []netmap.Nodes
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (b testBuilder) BuildPlacement(cid.ID, *oid.ID, *netmap.PlacementPolicy) ([]netmap.Nodes, error) {
|
2020-09-17 15:37:44 +00:00
|
|
|
return b.vectors, nil
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
func testNode(v uint32) (n netmap.NodeInfo) {
|
2021-06-23 12:27:00 +00:00
|
|
|
n.SetAddresses("/ip4/0.0.0.0/tcp/" + strconv.Itoa(int(v)))
|
2020-09-17 15:37:44 +00:00
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
func copyVectors(v []netmap.Nodes) []netmap.Nodes {
|
|
|
|
vc := make([]netmap.Nodes, 0, len(v))
|
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
ns := make(netmap.Nodes, len(v[i]))
|
|
|
|
copy(ns, v[i])
|
|
|
|
|
|
|
|
vc = append(vc, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vc
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
func testPlacement(t *testing.T, ss, rs []int) ([]netmap.Nodes, *container.Container) {
|
2020-09-18 15:14:26 +00:00
|
|
|
nodes := make([]netmap.Nodes, 0, len(rs))
|
2022-03-15 12:11:35 +00:00
|
|
|
replicas := make([]netmap.Replica, 0, len(rs))
|
2020-09-17 15:37:44 +00:00
|
|
|
num := uint32(0)
|
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
for i := range ss {
|
2020-11-05 15:51:43 +00:00
|
|
|
ns := make([]netmap.NodeInfo, 0, ss[i])
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
for j := 0; j < ss[i]; j++ {
|
2020-09-17 15:37:44 +00:00
|
|
|
ns = append(ns, testNode(num))
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
nodes = append(nodes, netmap.NodesFromInfo(ns))
|
2020-09-18 15:14:26 +00:00
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
var s netmap.Replica
|
2020-09-18 17:34:46 +00:00
|
|
|
s.SetCount(uint32(rs[i]))
|
2020-09-18 15:14:26 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
replicas = append(replicas, s)
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
policy := new(netmap.PlacementPolicy)
|
2020-11-05 15:51:43 +00:00
|
|
|
policy.SetReplicas(replicas...)
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
return nodes, container.New(container.WithPolicy(policy))
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 15:22:40 +00:00
|
|
|
func assertSameAddress(t *testing.T, ni *netmap.NodeInfo, addr network.AddressGroup) {
|
|
|
|
var netAddr network.AddressGroup
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-06-22 15:22:40 +00:00
|
|
|
err := netAddr.FromIterator(ni)
|
2021-06-16 12:03:11 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-22 15:22:40 +00:00
|
|
|
require.True(t, netAddr.Intersects(addr))
|
2021-06-16 12:03:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 15:37:44 +00:00
|
|
|
func TestTraverserObjectScenarios(t *testing.T) {
|
|
|
|
t.Run("search scenario", func(t *testing.T) {
|
2020-09-18 17:34:46 +00:00
|
|
|
selectors := []int{2, 3}
|
|
|
|
replicas := []int{1, 2}
|
2020-09-18 15:14:26 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
nodes, cnr := testPlacement(t, selectors, replicas)
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
nodesCopy := copyVectors(nodes)
|
2020-09-17 15:37:44 +00:00
|
|
|
|
|
|
|
tr, err := NewTraverser(
|
2020-09-18 15:14:26 +00:00
|
|
|
ForContainer(cnr),
|
|
|
|
UseBuilder(&testBuilder{vectors: nodesCopy}),
|
2020-09-17 15:37:44 +00:00
|
|
|
WithoutSuccessTracking(),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
for i := range selectors {
|
2020-09-18 15:14:26 +00:00
|
|
|
addrs := tr.Next()
|
|
|
|
|
|
|
|
require.Len(t, addrs, len(nodes[i]))
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
for j, n := range nodes[i] {
|
2021-09-06 11:35:06 +00:00
|
|
|
assertSameAddress(t, n.NodeInfo, addrs[j].Addresses())
|
2020-09-18 15:14:26 +00:00
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
require.Empty(t, tr.Next())
|
2020-09-17 15:37:44 +00:00
|
|
|
require.True(t, tr.Success())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("read scenario", func(t *testing.T) {
|
2020-09-18 17:34:46 +00:00
|
|
|
selectors := []int{5, 3}
|
|
|
|
replicas := []int{2, 2}
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
nodes, cnr := testPlacement(t, selectors, replicas)
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
nodesCopy := copyVectors(nodes)
|
2020-09-17 15:37:44 +00:00
|
|
|
|
|
|
|
tr, err := NewTraverser(
|
2020-09-18 15:14:26 +00:00
|
|
|
ForContainer(cnr),
|
2020-12-01 14:10:12 +00:00
|
|
|
UseBuilder(&testBuilder{
|
|
|
|
vectors: nodesCopy,
|
|
|
|
}),
|
2020-09-18 15:14:26 +00:00
|
|
|
SuccessAfter(1),
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-01 14:10:12 +00:00
|
|
|
for i := 0; i < len(nodes[0]); i++ {
|
|
|
|
require.NotNil(t, tr.Next())
|
2020-09-18 15:14:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 15:22:40 +00:00
|
|
|
var n network.AddressGroup
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-06-22 15:22:40 +00:00
|
|
|
err = n.FromIterator(nodes[1][0])
|
2020-12-01 14:10:12 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-17 15:37:44 +00:00
|
|
|
|
2021-09-06 11:35:06 +00:00
|
|
|
require.Equal(t, []Node{{addresses: n}}, tr.Next())
|
2020-09-17 15:37:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put scenario", func(t *testing.T) {
|
2020-09-18 17:34:46 +00:00
|
|
|
selectors := []int{5, 3}
|
|
|
|
replicas := []int{2, 2}
|
2020-09-18 15:14:26 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
nodes, cnr := testPlacement(t, selectors, replicas)
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
nodesCopy := copyVectors(nodes)
|
2020-09-17 15:37:44 +00:00
|
|
|
|
|
|
|
tr, err := NewTraverser(
|
2020-09-18 15:14:26 +00:00
|
|
|
ForContainer(cnr),
|
|
|
|
UseBuilder(&testBuilder{vectors: nodesCopy}),
|
2020-09-17 15:37:44 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-09-18 15:14:26 +00:00
|
|
|
fn := func(curVector int) {
|
2020-09-18 17:34:46 +00:00
|
|
|
for i := 0; i+replicas[curVector] < selectors[curVector]; i += replicas[curVector] {
|
2020-09-18 15:14:26 +00:00
|
|
|
addrs := tr.Next()
|
2020-09-18 17:34:46 +00:00
|
|
|
require.Len(t, addrs, replicas[curVector])
|
2020-09-18 15:14:26 +00:00
|
|
|
|
|
|
|
for j := range addrs {
|
2021-09-06 11:35:06 +00:00
|
|
|
assertSameAddress(t, nodes[curVector][i+j].NodeInfo, addrs[j].Addresses())
|
2020-09-18 15:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Empty(t, tr.Next())
|
2020-09-17 15:37:44 +00:00
|
|
|
require.False(t, tr.Success())
|
2020-09-18 15:14:26 +00:00
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
for i := 0; i < replicas[curVector]; i++ {
|
2020-09-18 15:14:26 +00:00
|
|
|
tr.SubmitSuccess()
|
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
for i := range selectors {
|
2020-09-18 15:14:26 +00:00
|
|
|
fn(i)
|
|
|
|
|
2020-09-18 17:34:46 +00:00
|
|
|
if i < len(selectors)-1 {
|
2020-09-18 15:14:26 +00:00
|
|
|
require.False(t, tr.Success())
|
|
|
|
} else {
|
|
|
|
require.True(t, tr.Success())
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 15:37:44 +00:00
|
|
|
})
|
2020-12-01 14:10:12 +00:00
|
|
|
|
|
|
|
t.Run("local operation scenario", func(t *testing.T) {
|
|
|
|
selectors := []int{2, 3}
|
|
|
|
replicas := []int{1, 2}
|
|
|
|
|
|
|
|
nodes, cnr := testPlacement(t, selectors, replicas)
|
|
|
|
|
|
|
|
tr, err := NewTraverser(
|
|
|
|
ForContainer(cnr),
|
|
|
|
UseBuilder(&testBuilder{
|
|
|
|
vectors: []netmap.Nodes{{nodes[1][1]}}, // single node (local)
|
|
|
|
}),
|
|
|
|
SuccessAfter(1),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEmpty(t, tr.Next())
|
|
|
|
require.False(t, tr.Success())
|
|
|
|
|
|
|
|
// add 1 OK
|
|
|
|
tr.SubmitSuccess()
|
|
|
|
|
|
|
|
// nothing more to do
|
|
|
|
require.Empty(t, tr.Next())
|
|
|
|
|
|
|
|
// common success
|
|
|
|
require.True(t, tr.Success())
|
|
|
|
})
|
2020-09-17 15:37:44 +00:00
|
|
|
}
|