2020-12-18 13:30:13 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
2020-12-23 08:44:29 +00:00
|
|
|
"context"
|
2020-12-18 13:30:13 +00:00
|
|
|
"crypto/ecdsa"
|
2020-12-23 08:44:29 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-12-18 13:30:13 +00:00
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2022-06-08 23:18:26 +00:00
|
|
|
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2022-07-01 18:16:28 +00:00
|
|
|
storagegroup2 "github.com/nspcc-dev/neofs-node/pkg/core/storagegroup"
|
2021-10-27 12:12:05 +00:00
|
|
|
neofsapiclient "github.com/nspcc-dev/neofs-node/pkg/innerring/internal/client"
|
2020-12-18 13:30:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2022-05-19 18:26:27 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit/auditor"
|
2020-12-23 08:44:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2022-09-28 07:41:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2020-12-23 08:44:29 +00:00
|
|
|
"go.uber.org/zap"
|
2020-12-18 13:30:13 +00:00
|
|
|
)
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
type (
|
|
|
|
ClientCache struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
log *logger.Logger
|
2021-09-28 05:39:18 +00:00
|
|
|
cache interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
Get(clientcore.NodeInfo) (clientcore.Client, error)
|
2021-09-28 05:39:18 +00:00
|
|
|
CloseAll()
|
|
|
|
}
|
|
|
|
key *ecdsa.PrivateKey
|
2020-12-23 08:44:29 +00:00
|
|
|
|
|
|
|
sgTimeout, headTimeout, rangeTimeout time.Duration
|
|
|
|
}
|
2020-12-18 13:30:13 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
clientCacheParams struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
Log *logger.Logger
|
2020-12-23 08:44:29 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
2022-09-26 12:34:01 +00:00
|
|
|
AllowExternal bool
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
SGTimeout, HeadTimeout, RangeTimeout time.Duration
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newClientCache(p *clientCacheParams) *ClientCache {
|
2020-12-18 13:30:13 +00:00
|
|
|
return &ClientCache{
|
2020-12-23 08:44:29 +00:00
|
|
|
log: p.Log,
|
2022-11-22 16:27:39 +00:00
|
|
|
cache: cache.NewSDKClientCache(cache.ClientCacheOpts{AllowExternal: p.AllowExternal, Key: p.Key}),
|
2020-12-23 08:44:29 +00:00
|
|
|
key: p.Key,
|
|
|
|
sgTimeout: p.SGTimeout,
|
|
|
|
headTimeout: p.HeadTimeout,
|
|
|
|
rangeTimeout: p.RangeTimeout,
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (c *ClientCache) Get(info clientcore.NodeInfo) (clientcore.Client, error) {
|
2021-03-13 15:22:21 +00:00
|
|
|
// Because cache is used by `ClientCache` exclusively,
|
|
|
|
// client will always have valid key.
|
2021-09-28 04:46:10 +00:00
|
|
|
return c.cache.Get(info)
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
2020-12-22 12:14:50 +00:00
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
// GetSG polls the container to get the object by id.
|
2020-12-22 12:14:50 +00:00
|
|
|
// Returns storage groups structure from received object.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if storage group is missing.
|
2022-07-01 18:16:28 +00:00
|
|
|
func (c *ClientCache) GetSG(prm storagegroup2.GetSGPrm) (*storagegroup.StorageGroup, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var sgAddress oid.Address
|
2022-05-19 18:26:27 +00:00
|
|
|
sgAddress.SetContainer(prm.CID)
|
|
|
|
sgAddress.SetObject(prm.OID)
|
2020-12-23 08:44:29 +00:00
|
|
|
|
2022-05-19 19:39:43 +00:00
|
|
|
return c.getSG(prm.Context, sgAddress, &prm.NetMap, prm.Container)
|
2021-01-26 10:33:28 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *ClientCache) getSG(ctx context.Context, addr oid.Address, nm *netmap.NetMap, cn [][]netmap.NodeInfo) (*storagegroup.StorageGroup, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
obj := addr.Object()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
nodes, err := placement.BuildObjectPlacement(nm, cn, &obj)
|
2021-01-26 10:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't build object placement: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2022-05-19 17:33:48 +00:00
|
|
|
var getObjPrm neofsapiclient.GetObjectPrm
|
|
|
|
getObjPrm.SetAddress(addr)
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
for _, node := range placement.FlattenNodes(nodes) {
|
2022-06-08 23:18:26 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, netmapcore.Node(node))
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't setup remote connection",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, c.sgTimeout)
|
2022-05-19 17:33:48 +00:00
|
|
|
getObjPrm.SetContext(cctx)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
// NOTE: we use the function which does not verify object integrity (checksums, signature),
|
|
|
|
// but it would be useful to do as part of a data audit.
|
2022-05-19 17:33:48 +00:00
|
|
|
res, err := cli.GetObject(getObjPrm)
|
2020-12-23 09:54:34 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-19 17:33:48 +00:00
|
|
|
c.log.Warn("can't get storage group object",
|
2020-12-23 08:44:29 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-13 09:25:13 +00:00
|
|
|
var sg storagegroup.StorageGroup
|
2020-12-23 08:44:29 +00:00
|
|
|
|
2022-05-19 17:33:48 +00:00
|
|
|
err = storagegroup.ReadFromObject(&sg, *res.Object())
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2022-05-19 17:33:48 +00:00
|
|
|
return nil, fmt.Errorf("can't parse storage group from a object: %w", err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 09:25:13 +00:00
|
|
|
return &sg, nil
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader requests node from the container under audit to return object header by id.
|
2022-05-19 18:26:27 +00:00
|
|
|
func (c *ClientCache) GetHeader(prm auditor.GetHeaderPrm) (*object.Object, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var objAddress oid.Address
|
2022-05-19 18:26:27 +00:00
|
|
|
objAddress.SetContainer(prm.CID)
|
|
|
|
objAddress.SetObject(prm.OID)
|
2020-12-23 08:44:29 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, netmapcore.Node(prm.Node))
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", info.AddressGroup(), err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
cctx, cancel := context.WithTimeout(prm.Context, c.headTimeout)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
var obj *object.Object
|
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
if prm.NodeIsRelay {
|
2021-10-27 12:12:05 +00:00
|
|
|
obj, err = neofsapiclient.GetObjectHeaderFromContainer(cctx, cli, objAddress)
|
|
|
|
} else {
|
|
|
|
obj, err = neofsapiclient.GetRawObjectHeaderLocally(cctx, cli, objAddress)
|
|
|
|
}
|
2020-12-23 09:54:34 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("object head error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return obj, nil
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRangeHash requests node from the container under audit to return Tillich-Zemor hash of the
|
|
|
|
// payload range of the object with specified identifier.
|
2022-05-19 18:26:27 +00:00
|
|
|
func (c *ClientCache) GetRangeHash(prm auditor.GetRangeHashPrm) ([]byte, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var objAddress oid.Address
|
2022-05-19 18:26:27 +00:00
|
|
|
objAddress.SetContainer(prm.CID)
|
|
|
|
objAddress.SetObject(prm.OID)
|
2020-12-23 14:11:14 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, netmapcore.Node(prm.Node))
|
2020-12-23 14:11:14 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 14:11:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 14:11:14 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", info.AddressGroup(), err)
|
2020-12-23 14:11:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
cctx, cancel := context.WithTimeout(prm.Context, c.rangeTimeout)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
h, err := neofsapiclient.HashObjectRange(cctx, cli, objAddress, prm.Range)
|
2020-12-23 14:11:14 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("object rangehash error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientCache) getWrappedClient(info clientcore.NodeInfo) (neofsapiclient.Client, error) {
|
|
|
|
// can be also cached
|
|
|
|
var cInternal neofsapiclient.Client
|
|
|
|
|
|
|
|
cli, err := c.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return cInternal, fmt.Errorf("could not get API client from cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
cInternal.WrapBasicClient(cli)
|
|
|
|
cInternal.SetPrivateKey(c.key)
|
|
|
|
|
|
|
|
return cInternal, nil
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
func (c ClientCache) ListSG(dst *storagegroup2.SearchSGDst, prm storagegroup2.SearchSGPrm) error {
|
|
|
|
cli, err := c.getWrappedClient(prm.NodeInfo)
|
2021-10-27 12:12:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get API client from cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cliPrm neofsapiclient.SearchSGPrm
|
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
cliPrm.SetContext(prm.Context)
|
|
|
|
cliPrm.SetContainerID(prm.Container)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
res, err := cli.SearchSG(cliPrm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:16:28 +00:00
|
|
|
dst.Objects = res.IDList()
|
2020-12-23 14:11:14 +00:00
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return nil
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|