2020-09-22 06:51:47 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-29 08:54:40 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
|
|
|
|
rpcclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
objectSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal"
|
|
|
|
searchsvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/search"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-09-22 06:51:47 +00:00
|
|
|
)
|
|
|
|
|
2023-03-20 07:18:24 +00:00
|
|
|
// nolint: funlen, gocognit
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStream) (*searchsvc.Prm, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
body := req.GetBody()
|
|
|
|
|
|
|
|
cnrV2 := body.GetContainerID()
|
|
|
|
if cnrV2 == nil {
|
|
|
|
return nil, errors.New("missing container ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id cid.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*cnrV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid container ID: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
meta := req.GetMetaHeader()
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p := new(searchsvc.Prm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p.SetWriter(&streamWriter{
|
|
|
|
stream: stream,
|
|
|
|
})
|
|
|
|
|
2021-04-29 08:54:40 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.MultiAddressClient, pubkey []byte) ([]oid.ID, error) {
|
2021-04-29 08:54:40 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1165 think how to set the other fields
|
2021-04-29 08:54:40 +00:00
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
var searchStream *rpc.SearchResponseReader
|
|
|
|
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
|
|
|
|
searchStream, err = rpc.SearchObjects(cli, req, rpcclient.WithContext(stream.Context()))
|
|
|
|
return err
|
|
|
|
})
|
2021-04-29 08:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.SearchObjects implementation,
|
2022-12-23 17:35:35 +00:00
|
|
|
// perhaps it is worth highlighting the utility function in frostfs-api-go
|
2021-04-29 08:54:40 +00:00
|
|
|
var (
|
2022-05-31 17:00:41 +00:00
|
|
|
searchResult []oid.ID
|
2021-04-29 08:54:40 +00:00
|
|
|
resp = new(objectV2.SearchResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2022-03-11 15:24:11 +00:00
|
|
|
err := searchStream.Read(resp)
|
2021-04-29 08:54:40 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 08:54:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:54:40 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunk := resp.GetBody().GetIDList()
|
2022-05-31 17:00:41 +00:00
|
|
|
var id oid.ID
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2021-04-29 08:54:40 +00:00
|
|
|
for i := range chunk {
|
2022-05-12 16:37:46 +00:00
|
|
|
err = id.ReadFromV2(chunk[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid object ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResult = append(searchResult, id)
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult, nil
|
2021-06-22 12:19:30 +00:00
|
|
|
}))
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
p.WithContainerID(id)
|
2022-03-03 14:19:05 +00:00
|
|
|
p.WithSearchFilters(object.NewSearchFiltersFromV2(body.GetFilters()))
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
return p, nil
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
2021-06-22 12:19:30 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func groupAddressRequestForwarder(f func(network.Address, client.MultiAddressClient, []byte) ([]oid.ID, error)) searchsvc.RequestForwarder {
|
|
|
|
return func(info client.NodeInfo, c client.MultiAddressClient) ([]oid.ID, error) {
|
2021-06-22 12:19:30 +00:00
|
|
|
var (
|
|
|
|
firstErr error
|
2022-05-31 17:00:41 +00:00
|
|
|
res []oid.ID
|
2021-09-28 06:02:02 +00:00
|
|
|
|
|
|
|
key = info.PublicKey()
|
2021-06-22 12:19:30 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
|
2021-06-22 12:19:30 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
stop = err == nil
|
|
|
|
|
|
|
|
if stop || firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// would be nice to log otherwise
|
|
|
|
}()
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
res, err = f(addr, c, key)
|
2021-06-22 12:19:30 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, firstErr
|
|
|
|
}
|
|
|
|
}
|