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) {
|
|
|
|
return c.cache.Get(c.key, address, opts...)
|
|
|
|
}
|
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
|
|
|
nodes, err := placement.BuildObjectPlacement( // shuffle nodes
|
|
|
|
task.NetworkMap(),
|
|
|
|
task.ContainerNodes(),
|
|
|
|
id,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't build object placement: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sgAddress := new(object.Address)
|
|
|
|
sgAddress.SetContainerID(task.ContainerID())
|
|
|
|
sgAddress.SetObjectID(id)
|
|
|
|
|
|
|
|
getParams := new(client.GetObjectParams)
|
|
|
|
getParams.WithAddress(sgAddress)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(task.AuditContext(), c.sgTimeout)
|
|
|
|
obj, err := cli.GetObject(cctx, getParams)
|
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.
|
|
|
|
func (c *ClientCache) GetHeader(task *audit.Task, node *netmap.Node, id *object.ID) (*object.Object, error) {
|
2020-12-23 08:44:29 +00:00
|
|
|
objAddress := new(object.Address)
|
|
|
|
objAddress.SetContainerID(task.ContainerID())
|
|
|
|
objAddress.SetObjectID(id)
|
|
|
|
|
|
|
|
headParams := new(client.ObjectHeaderParams)
|
|
|
|
headParams.WithRawFlag(true)
|
|
|
|
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)
|
|
|
|
head, err := cli.GetObjectHeader(cctx, headParams, client.WithTTL(1))
|
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) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|