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
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-12-22 12:14:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
|
2020-12-23 08:44:29 +00:00
|
|
|
coreObject "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-12-18 13:30:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2020-12-22 12:14:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2020-12-23 08:44:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"go.uber.org/zap"
|
2020-12-18 13:30:13 +00:00
|
|
|
)
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
type (
|
|
|
|
ClientCache struct {
|
|
|
|
log *zap.Logger
|
|
|
|
cache *cache.ClientCache
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
sgTimeout, headTimeout, rangeTimeout time.Duration
|
|
|
|
}
|
2020-12-18 13:30:13 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
clientCacheParams struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
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,
|
|
|
|
cache: cache.NewSDKClientCache(),
|
|
|
|
key: p.Key,
|
|
|
|
sgTimeout: p.SGTimeout,
|
|
|
|
headTimeout: p.HeadTimeout,
|
|
|
|
rangeTimeout: p.RangeTimeout,
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientCache) Get(address string, opts ...client.Option) (*client.Client, error) {
|
2021-03-13 15:22:21 +00:00
|
|
|
// Because cache is used by `ClientCache` exclusively,
|
|
|
|
// client will always have valid key.
|
|
|
|
return c.cache.Get(address, opts...)
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
2020-12-22 12:14:50 +00:00
|
|
|
|
|
|
|
// GetSG polls the container from audit task to get the object by id.
|
|
|
|
// Returns storage groups structure from received object.
|
|
|
|
func (c *ClientCache) GetSG(task *audit.Task, id *object.ID) (*storagegroup.StorageGroup, error) {
|
2020-12-23 08:44:29 +00:00
|
|
|
sgAddress := new(object.Address)
|
|
|
|
sgAddress.SetContainerID(task.ContainerID())
|
|
|
|
sgAddress.SetObjectID(id)
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
return c.getSG(task.AuditContext(), sgAddress, task.NetworkMap(), task.ContainerNodes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientCache) getSG(ctx context.Context, addr *object.Address, nm *netmap.Netmap, cn netmap.ContainerNodes) (*storagegroup.StorageGroup, error) {
|
|
|
|
nodes, err := placement.BuildObjectPlacement(nm, cn, addr.ObjectID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't build object placement: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
getParams := new(client.GetObjectParams)
|
2021-01-26 10:33:28 +00:00
|
|
|
getParams.WithAddress(addr)
|
2020-12-23 08:44:29 +00:00
|
|
|
|
|
|
|
for _, node := range placement.FlattenNodes(nodes) {
|
|
|
|
addr, err := network.IPAddrFromMultiaddr(node.Address())
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't parse remote address",
|
|
|
|
zap.String("address", node.Address()),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := c.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't setup remote connection",
|
|
|
|
zap.String("address", addr),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, c.sgTimeout)
|
2021-03-13 15:22:21 +00:00
|
|
|
obj, err := cli.GetObject(cctx, getParams, client.WithKey(c.key))
|
2020-12-23 09:54:34 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't get storage group object",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sg := storagegroup.New()
|
|
|
|
|
|
|
|
err = sg.Unmarshal(obj.Payload())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse storage group payload: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, coreObject.ErrNotFound
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader requests node from the container under audit to return object header by id.
|
2020-12-23 14:47:00 +00:00
|
|
|
func (c *ClientCache) GetHeader(task *audit.Task, node *netmap.Node, id *object.ID, relay bool) (*object.Object, error) {
|
|
|
|
raw := true
|
|
|
|
ttl := uint32(1)
|
|
|
|
|
|
|
|
if relay {
|
|
|
|
ttl = 10 // todo: instead of hardcode value we can set TTL based on container length
|
|
|
|
raw = false
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
objAddress := new(object.Address)
|
|
|
|
objAddress.SetContainerID(task.ContainerID())
|
|
|
|
objAddress.SetObjectID(id)
|
|
|
|
|
|
|
|
headParams := new(client.ObjectHeaderParams)
|
2020-12-23 14:47:00 +00:00
|
|
|
headParams.WithRawFlag(raw)
|
2020-12-23 08:44:29 +00:00
|
|
|
headParams.WithMainFields()
|
|
|
|
headParams.WithAddress(objAddress)
|
|
|
|
|
|
|
|
addr, err := network.IPAddrFromMultiaddr(node.Address())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse remote address %s: %w", node.Address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := c.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(task.AuditContext(), c.headTimeout)
|
2021-03-13 15:22:21 +00:00
|
|
|
head, err := cli.GetObjectHeader(cctx, headParams,
|
|
|
|
client.WithTTL(ttl),
|
|
|
|
client.WithKey(c.key))
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return head, 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.
|
|
|
|
func (c *ClientCache) GetRangeHash(task *audit.Task, node *netmap.Node, id *object.ID, rng *object.Range) ([]byte, error) {
|
2020-12-23 14:11:14 +00:00
|
|
|
objAddress := new(object.Address)
|
|
|
|
objAddress.SetContainerID(task.ContainerID())
|
|
|
|
objAddress.SetObjectID(id)
|
|
|
|
|
|
|
|
rangeParams := new(client.RangeChecksumParams)
|
|
|
|
rangeParams.WithAddress(objAddress)
|
|
|
|
rangeParams.WithRangeList(rng)
|
|
|
|
rangeParams.WithSalt(nil) // it MUST be nil for correct hash concatenation in PDP game
|
|
|
|
|
|
|
|
addr, err := network.IPAddrFromMultiaddr(node.Address())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse remote address %s: %w", node.Address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := c.Get(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", addr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(task.AuditContext(), c.rangeTimeout)
|
2021-03-13 15:22:21 +00:00
|
|
|
result, err := cli.ObjectPayloadRangeTZ(cctx, rangeParams,
|
|
|
|
client.WithTTL(1),
|
|
|
|
client.WithKey(c.key))
|
2020-12-23 14:11:14 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("object rangehash error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// client guarantees that request and response have equal amount of ranges
|
|
|
|
|
|
|
|
return result[0][:], nil
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|