frostfs-node/pkg/services/object/search/v2/util.go
Dmitrii Stepanov d85703a963 [#208] searchsvc: Refactor request forwarding
Resolve funlen & gocognit linters for toPrm method.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-04-04 16:24:55 +03:00

97 lines
2.3 KiB
Go

package searchsvc
import (
"context"
"errors"
"fmt"
"sync"
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
"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"
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"
)
func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStream) (*searchsvc.Prm, error) {
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)
}
commonPrm, err := util.CommonPrmFromV2(req)
if err != nil {
return nil, err
}
p := new(searchsvc.Prm)
p.SetCommonParameters(commonPrm)
p.SetWriter(&streamWriter{
stream: stream,
})
if !commonPrm.LocalOnly() {
key, err := s.keyStorage.GetKey(nil)
if err != nil {
return nil, err
}
forwarder := &requestForwarder{
OnceResign: &sync.Once{},
Request: req,
Key: key,
}
p.SetRequestForwarder(groupAddressRequestForwarder(forwarder.forwardRequest))
}
p.WithContainerID(id)
p.WithSearchFilters(object.NewSearchFiltersFromV2(body.GetFilters()))
return p, nil
}
func groupAddressRequestForwarder(f func(context.Context, network.Address, client.MultiAddressClient, []byte) ([]oid.ID, error)) searchsvc.RequestForwarder {
return func(ctx context.Context, info client.NodeInfo, c client.MultiAddressClient) ([]oid.ID, error) {
var (
firstErr error
res []oid.ID
key = info.PublicKey()
)
info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
var err error
defer func() {
stop = err == nil
if stop || firstErr == nil {
firstErr = err
}
// would be nice to log otherwise
}()
res, err = f(ctx, addr, c, key)
return
})
return res, firstErr
}
}