2020-09-22 15:04:08 +00:00
|
|
|
package headsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2020-09-22 15:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-11-01 08:35:33 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/pkg/services/object/internal/client"
|
2020-09-29 16:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-09-22 15:04:08 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
type ClientConstructor interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
Get(clientcore.NodeInfo) (clientcore.Client, error)
|
2021-03-23 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
// RemoteHeader represents utility for getting
|
|
|
|
// the object header from a remote host.
|
|
|
|
type RemoteHeader struct {
|
2020-09-29 16:44:59 +00:00
|
|
|
keyStorage *util.KeyStorage
|
2020-11-18 13:03:00 +00:00
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientCache ClientConstructor
|
2020-10-21 09:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteHeadPrm groups remote header operation parameters.
|
|
|
|
type RemoteHeadPrm struct {
|
|
|
|
commonHeadPrm *Prm
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
node *netmap.NodeInfo
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 14:39:33 +00:00
|
|
|
var ErrNotFound = errors.New("object header not found")
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
// NewRemoteHeader creates, initializes and returns new RemoteHeader instance.
|
2021-03-23 18:40:36 +00:00
|
|
|
func NewRemoteHeader(keyStorage *util.KeyStorage, cache ClientConstructor) *RemoteHeader {
|
2020-10-21 09:18:36 +00:00
|
|
|
return &RemoteHeader{
|
2020-11-18 13:03:00 +00:00
|
|
|
keyStorage: keyStorage,
|
|
|
|
clientCache: cache,
|
2020-10-21 09:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
// WithNodeInfo sets information about the remote node.
|
|
|
|
func (p *RemoteHeadPrm) WithNodeInfo(v *netmap.NodeInfo) *RemoteHeadPrm {
|
2020-10-21 09:18:36 +00:00
|
|
|
if p != nil {
|
|
|
|
p.node = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithObjectAddress sets object address.
|
|
|
|
func (p *RemoteHeadPrm) WithObjectAddress(v *objectSDK.Address) *RemoteHeadPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.commonHeadPrm = new(Prm).WithAddress(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head requests object header from the remote node.
|
|
|
|
func (h *RemoteHeader) Head(ctx context.Context, prm *RemoteHeadPrm) (*object.Object, error) {
|
2021-11-01 08:35:33 +00:00
|
|
|
key, err := h.keyStorage.GetKey(nil)
|
2020-09-29 16:44:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not receive private key: %w", h, err)
|
2020-09-29 16:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
var info clientcore.NodeInfo
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
err = clientcore.NodeInfoFromRawNetmapElement(&info, prm.node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
|
|
|
}
|
2021-09-28 04:46:10 +00:00
|
|
|
|
|
|
|
c, err := h.clientCache.Get(info)
|
2020-09-24 07:37:42 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not create SDK client %s: %w", h, info.AddressGroup(), err)
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
var headPrm internalclient.HeadObjectPrm
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
headPrm.SetContext(ctx)
|
|
|
|
headPrm.SetClient(c)
|
|
|
|
headPrm.SetPrivateKey(key)
|
|
|
|
headPrm.SetAddress(prm.commonHeadPrm.addr)
|
|
|
|
headPrm.SetTTL(1) // FIXME: use constant
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
res, err := internalclient.HeadObject(headPrm)
|
2020-09-22 15:04:08 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not head object in %s: %w", h, info.AddressGroup(), err)
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return object.NewFromSDK(res.Header()), nil
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|