2020-12-10 12:26:40 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-10 12:26:40 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger/test"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type idsErr struct {
|
2022-03-15 12:11:35 +00:00
|
|
|
ids []oidSDK.ID
|
2020-12-10 12:26:40 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type testStorage struct {
|
|
|
|
items map[string]idsErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type testTraverserGenerator struct {
|
|
|
|
c *container.Container
|
2021-01-12 14:55:02 +00:00
|
|
|
b map[uint64]placement.Builder
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testPlacementBuilder struct {
|
|
|
|
vectors map[string][]netmap.Nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
type testClientCache struct {
|
|
|
|
clients map[string]*testStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleIDWriter struct {
|
2022-03-15 12:11:35 +00:00
|
|
|
ids []oidSDK.ID
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type testEpochReceiver uint64
|
|
|
|
|
|
|
|
func (e testEpochReceiver) currentEpoch() (uint64, error) {
|
|
|
|
return uint64(e), nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
func (s *simpleIDWriter) WriteIDs(ids []oidSDK.ID) error {
|
2020-12-10 12:26:40 +00:00
|
|
|
s.ids = append(s.ids, ids...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestStorage() *testStorage {
|
|
|
|
return &testStorage{
|
|
|
|
items: make(map[string]idsErr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (g *testTraverserGenerator) generateTraverser(_ *cid.ID, epoch uint64) (*placement.Traverser, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
return placement.NewTraverser(
|
|
|
|
placement.ForContainer(g.c),
|
2021-01-12 14:55:02 +00:00
|
|
|
placement.UseBuilder(g.b[epoch]),
|
2020-12-10 12:26:40 +00:00
|
|
|
placement.WithoutSuccessTracking(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *testPlacementBuilder) BuildPlacement(addr *addressSDK.Address, _ *netmap.PlacementPolicy) ([]netmap.Nodes, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
vs, ok := p.vectors[addr.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("vectors for address not found")
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
res := make([]netmap.Nodes, len(vs))
|
|
|
|
copy(res, vs)
|
|
|
|
|
|
|
|
return res, nil
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *testClientCache) get(info clientcore.NodeInfo) (searchClient, error) {
|
|
|
|
v, ok := c.clients[network.StringifyGroup(info.AddressGroup())]
|
2020-12-10 12:26:40 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("could not construct client")
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
func (s *testStorage) search(exec *execCtx) ([]oidSDK.ID, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
v, ok := s.items[exec.containerID().String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.ids, v.err
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
func (c *testStorage) searchObjects(exec *execCtx, _ clientcore.NodeInfo) ([]oidSDK.ID, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
v, ok := c.items[exec.containerID().String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.ids, v.err
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func (c *testStorage) addResult(addr cid.ID, ids []oidSDK.ID, err error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
c.items[addr.String()] = idsErr{
|
|
|
|
ids: ids,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSHA256() (cs [sha256.Size]byte) {
|
|
|
|
rand.Read(cs[:])
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
func generateIDs(num int) []oidSDK.ID {
|
|
|
|
res := make([]oidSDK.ID, num)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
res[i].SetSHA256(testSHA256())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLocalOnly(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
newSvc := func(storage *testStorage) *Service {
|
|
|
|
svc := &Service{cfg: new(cfg)}
|
|
|
|
svc.log = test.NewLogger(false)
|
|
|
|
svc.localStorage = storage
|
|
|
|
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
newPrm := func(cnr cid.ID, w IDListWriter) Prm {
|
2020-12-10 12:26:40 +00:00
|
|
|
p := Prm{}
|
2022-05-12 16:37:46 +00:00
|
|
|
p.WithContainerID(&cnr)
|
2020-12-10 12:26:40 +00:00
|
|
|
p.SetWriter(w)
|
|
|
|
p.common = new(util.CommonPrm).WithLocalOnly(true)
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
storage := newTestStorage()
|
|
|
|
svc := newSvc(storage)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-10 12:26:40 +00:00
|
|
|
ids := generateIDs(10)
|
2022-05-12 16:37:46 +00:00
|
|
|
storage.addResult(cnr, ids, nil)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
w := new(simpleIDWriter)
|
2022-05-12 16:37:46 +00:00
|
|
|
p := newPrm(cnr, w)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
err := svc.Search(ctx, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ids, w.ids)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("FAIL", func(t *testing.T) {
|
|
|
|
storage := newTestStorage()
|
|
|
|
svc := newSvc(storage)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-10 12:26:40 +00:00
|
|
|
testErr := errors.New("any error")
|
2022-05-12 16:37:46 +00:00
|
|
|
storage.addResult(cnr, nil, testErr)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
w := new(simpleIDWriter)
|
2022-05-12 16:37:46 +00:00
|
|
|
p := newPrm(cnr, w)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
err := svc.Search(ctx, p)
|
2022-04-29 10:22:29 +00:00
|
|
|
require.ErrorIs(t, err, testErr)
|
2020-12-10 12:26:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) {
|
|
|
|
mNodes := make([]netmap.Nodes, len(dim))
|
|
|
|
mAddr := make([][]string, len(dim))
|
|
|
|
|
|
|
|
for i := range dim {
|
|
|
|
ns := make([]netmap.NodeInfo, dim[i])
|
|
|
|
as := make([]string, dim[i])
|
|
|
|
|
|
|
|
for j := 0; j < dim[i]; j++ {
|
|
|
|
a := fmt.Sprintf("/ip4/192.168.0.%s/tcp/%s",
|
|
|
|
strconv.Itoa(i),
|
|
|
|
strconv.Itoa(60000+j),
|
|
|
|
)
|
|
|
|
|
2021-06-22 12:19:30 +00:00
|
|
|
ni := netmap.NewNodeInfo()
|
2021-06-23 12:27:00 +00:00
|
|
|
ni.SetAddresses(a)
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-06-22 12:19:30 +00:00
|
|
|
var na network.AddressGroup
|
2020-12-10 12:26:40 +00:00
|
|
|
|
2021-06-22 12:19:30 +00:00
|
|
|
err := na.FromIterator(ni)
|
|
|
|
require.NoError(t, err)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
2021-06-22 12:19:30 +00:00
|
|
|
as[j] = network.StringifyGroup(na)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
ns[j] = *ni
|
|
|
|
}
|
|
|
|
|
|
|
|
mNodes[i] = netmap.NodesFromInfo(ns)
|
|
|
|
mAddr[i] = as
|
|
|
|
}
|
|
|
|
|
|
|
|
return mNodes, mAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRemoteSmall(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
placementDim := []int{2}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
rs := make([]netmap.Replica, len(placementDim))
|
2020-12-10 12:26:40 +00:00
|
|
|
for i := range placementDim {
|
2022-03-15 12:11:35 +00:00
|
|
|
rs[i].SetCount(uint32(placementDim[i]))
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp := netmap.NewPlacementPolicy()
|
|
|
|
pp.SetReplicas(rs...)
|
|
|
|
|
|
|
|
cnr := container.New(container.WithPolicy(pp))
|
2021-05-31 11:03:17 +00:00
|
|
|
id := container.CalculateID(cnr)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
newSvc := func(b *testPlacementBuilder, c *testClientCache) *Service {
|
|
|
|
svc := &Service{cfg: new(cfg)}
|
|
|
|
svc.log = test.NewLogger(false)
|
|
|
|
svc.localStorage = newTestStorage()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
const curEpoch = 13
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
svc.traverserGenerator = &testTraverserGenerator{
|
|
|
|
c: cnr,
|
2021-01-12 14:55:02 +00:00
|
|
|
b: map[uint64]placement.Builder{
|
|
|
|
curEpoch: b,
|
|
|
|
},
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
2021-03-23 18:40:36 +00:00
|
|
|
svc.clientConstructor = c
|
2021-01-12 14:55:02 +00:00
|
|
|
svc.currentEpochReceiver = testEpochReceiver(curEpoch)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
return svc
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
newPrm := func(id cid.ID, w IDListWriter) Prm {
|
2020-12-10 12:26:40 +00:00
|
|
|
p := Prm{}
|
2022-05-12 16:37:46 +00:00
|
|
|
p.WithContainerID(&id)
|
2020-12-10 12:26:40 +00:00
|
|
|
p.SetWriter(w)
|
|
|
|
p.common = new(util.CommonPrm).WithLocalOnly(false)
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr := addressSDK.NewAddress()
|
2021-05-31 11:03:17 +00:00
|
|
|
addr.SetContainerID(id)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
ns, as := testNodeMatrix(t, placementDim)
|
|
|
|
|
|
|
|
builder := &testPlacementBuilder{
|
|
|
|
vectors: map[string][]netmap.Nodes{
|
|
|
|
addr.String(): ns,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 := newTestStorage()
|
|
|
|
ids1 := generateIDs(10)
|
2021-05-31 11:03:17 +00:00
|
|
|
c1.addResult(id, ids1, nil)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
c2 := newTestStorage()
|
|
|
|
ids2 := generateIDs(10)
|
2021-05-31 11:03:17 +00:00
|
|
|
c2.addResult(id, ids2, nil)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
svc := newSvc(builder, &testClientCache{
|
|
|
|
clients: map[string]*testStorage{
|
|
|
|
as[0][0]: c1,
|
|
|
|
as[0][1]: c2,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
w := new(simpleIDWriter)
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
p := newPrm(id, w)
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
err := svc.Search(ctx, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, w.ids, len(ids1)+len(ids2))
|
|
|
|
|
|
|
|
for _, id := range append(ids1, ids2...) {
|
|
|
|
require.Contains(t, w.ids, id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-12 14:55:02 +00:00
|
|
|
|
|
|
|
func TestGetFromPastEpoch(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
placementDim := []int{2, 2}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
rs := make([]netmap.Replica, len(placementDim))
|
2021-01-12 14:55:02 +00:00
|
|
|
|
|
|
|
for i := range placementDim {
|
2022-03-15 12:11:35 +00:00
|
|
|
rs[i].SetCount(uint32(placementDim[i]))
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp := netmap.NewPlacementPolicy()
|
|
|
|
pp.SetReplicas(rs...)
|
|
|
|
|
|
|
|
cnr := container.New(container.WithPolicy(pp))
|
|
|
|
cid := container.CalculateID(cnr)
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addr := addressSDK.NewAddress()
|
2021-01-12 14:55:02 +00:00
|
|
|
addr.SetContainerID(cid)
|
|
|
|
|
|
|
|
ns, as := testNodeMatrix(t, placementDim)
|
|
|
|
|
|
|
|
c11 := newTestStorage()
|
|
|
|
ids11 := generateIDs(10)
|
|
|
|
c11.addResult(cid, ids11, nil)
|
|
|
|
|
|
|
|
c12 := newTestStorage()
|
|
|
|
ids12 := generateIDs(10)
|
|
|
|
c12.addResult(cid, ids12, nil)
|
|
|
|
|
|
|
|
c21 := newTestStorage()
|
|
|
|
ids21 := generateIDs(10)
|
|
|
|
c21.addResult(cid, ids21, nil)
|
|
|
|
|
|
|
|
c22 := newTestStorage()
|
|
|
|
ids22 := generateIDs(10)
|
|
|
|
c22.addResult(cid, ids22, nil)
|
|
|
|
|
|
|
|
svc := &Service{cfg: new(cfg)}
|
|
|
|
svc.log = test.NewLogger(false)
|
|
|
|
svc.localStorage = newTestStorage()
|
|
|
|
|
|
|
|
const curEpoch = 13
|
|
|
|
|
|
|
|
svc.traverserGenerator = &testTraverserGenerator{
|
|
|
|
c: cnr,
|
|
|
|
b: map[uint64]placement.Builder{
|
|
|
|
curEpoch: &testPlacementBuilder{
|
|
|
|
vectors: map[string][]netmap.Nodes{
|
|
|
|
addr.String(): ns[:1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
curEpoch - 1: &testPlacementBuilder{
|
|
|
|
vectors: map[string][]netmap.Nodes{
|
|
|
|
addr.String(): ns[1:],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
svc.clientConstructor = &testClientCache{
|
2021-01-12 14:55:02 +00:00
|
|
|
clients: map[string]*testStorage{
|
|
|
|
as[0][0]: c11,
|
|
|
|
as[0][1]: c12,
|
|
|
|
as[1][0]: c21,
|
|
|
|
as[1][1]: c22,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
svc.currentEpochReceiver = testEpochReceiver(curEpoch)
|
|
|
|
|
|
|
|
w := new(simpleIDWriter)
|
|
|
|
|
|
|
|
p := Prm{}
|
2022-05-12 16:37:46 +00:00
|
|
|
p.WithContainerID(&cid)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetWriter(w)
|
|
|
|
|
|
|
|
commonPrm := new(util.CommonPrm)
|
|
|
|
p.SetCommonParameters(commonPrm)
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
assertContains := func(idsList ...[]oidSDK.ID) {
|
2021-01-12 14:55:02 +00:00
|
|
|
var sz int
|
|
|
|
|
|
|
|
for _, ids := range idsList {
|
|
|
|
sz += len(ids)
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
require.Contains(t, w.ids, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, w.ids, sz)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := svc.Search(ctx, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertContains(ids11, ids12)
|
|
|
|
|
|
|
|
commonPrm.SetNetmapLookupDepth(1)
|
|
|
|
w = new(simpleIDWriter)
|
|
|
|
p.SetWriter(w)
|
|
|
|
|
|
|
|
err = svc.Search(ctx, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertContains(ids11, ids12, ids21, ids22)
|
|
|
|
}
|