2020-12-10 12:26:40 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2021-01-12 14:55:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2021-11-01 08:35:33 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/pkg/services/object/internal/client"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type uniqueIDWriter struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
|
|
|
written map[string]struct{}
|
|
|
|
|
|
|
|
writer IDListWriter
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
type clientConstructorWrapper struct {
|
|
|
|
constructor ClientConstructor
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client client.MultiAddressClient
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageEngineWrapper engine.StorageEngine
|
|
|
|
|
|
|
|
type traverseGeneratorWrapper util.TraverserGenerator
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type nmSrcWrapper struct {
|
|
|
|
nmSrc netmap.Source
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func newUniqueAddressWriter(w IDListWriter) IDListWriter {
|
|
|
|
return &uniqueIDWriter{
|
|
|
|
written: make(map[string]struct{}),
|
|
|
|
writer: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (w *uniqueIDWriter) WriteIDs(list []*oidSDK.ID) error {
|
2020-12-10 12:26:40 +00:00
|
|
|
w.mtx.Lock()
|
|
|
|
|
|
|
|
for i := 0; i < len(list); i++ { // don't use range, slice mutates in body
|
|
|
|
s := list[i].String()
|
|
|
|
// standard stringer is quite costly, it is better
|
|
|
|
// to facilitate the calculation of the key
|
|
|
|
|
|
|
|
if _, ok := w.written[s]; !ok {
|
|
|
|
// mark address as processed
|
|
|
|
w.written[s] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// exclude processed address
|
|
|
|
list = append(list[:i], list[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
w.mtx.Unlock()
|
|
|
|
|
|
|
|
return w.writer.WriteIDs(list)
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *clientConstructorWrapper) get(info client.NodeInfo) (searchClient, error) {
|
|
|
|
clt, err := c.constructor.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
return &clientWrapper{
|
|
|
|
client: clt,
|
2021-09-28 04:46:10 +00:00
|
|
|
}, nil
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (c *clientWrapper) searchObjects(exec *execCtx, info client.NodeInfo) ([]*oidSDK.ID, error) {
|
2021-05-20 08:05:18 +00:00
|
|
|
if exec.prm.forwarder != nil {
|
2021-09-28 04:46:10 +00:00
|
|
|
return exec.prm.forwarder(info, c.client)
|
2021-05-20 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:02:46 +00:00
|
|
|
key, err := exec.svc.keyStore.GetKey(exec.prm.common.SessionToken())
|
2021-10-26 12:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.SearchObjectsPrm
|
|
|
|
|
|
|
|
prm.SetContext(exec.context())
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetContainerID(exec.containerID())
|
|
|
|
prm.SetFilters(exec.searchFilters())
|
|
|
|
|
|
|
|
res, err := internalclient.SearchObjects(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.IDList(), nil
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (e *storageEngineWrapper) search(exec *execCtx) ([]*oidSDK.ID, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
r, err := (*engine.StorageEngine)(e).Select(new(engine.SelectPrm).
|
2020-12-10 14:38:45 +00:00
|
|
|
WithFilters(exec.searchFilters()).
|
|
|
|
WithContainerID(exec.containerID()),
|
2020-12-10 12:26:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idsFromAddresses(r.AddressList()), nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func idsFromAddresses(addrs []*addressSDK.Address) []*oidSDK.ID {
|
|
|
|
ids := make([]*oidSDK.ID, len(addrs))
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
for i := range addrs {
|
|
|
|
ids[i] = addrs[i].ObjectID()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (e *traverseGeneratorWrapper) generateTraverser(cid *cid.ID, epoch uint64) (*placement.Traverser, error) {
|
2022-01-26 12:11:13 +00:00
|
|
|
a := addressSDK.NewAddress()
|
2020-12-10 12:26:40 +00:00
|
|
|
a.SetContainerID(cid)
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
return (*util.TraverserGenerator)(e).GenerateTraverser(a, epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nmSrcWrapper) currentEpoch() (uint64, error) {
|
|
|
|
return n.nmSrc.Epoch()
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|