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
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
clientcore "github.com/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
netmapCore "github.com/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
internalclient "github.com/TrueCloudLab/frostfs-node/pkg/services/object/internal/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/services/object/util"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
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
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
node netmap.NodeInfo
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 11:58:15 +00:00
|
|
|
const remoteOpTTL = 1
|
|
|
|
|
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.
|
2022-06-08 23:18:26 +00:00
|
|
|
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.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *RemoteHeadPrm) WithObjectAddress(v oid.Address) *RemoteHeadPrm {
|
2020-10-21 09:18:36 +00:00
|
|
|
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
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
err = clientcore.NodeInfoFromRawNetmapElement(&info, netmapCore.Node(prm.node))
|
2021-09-28 05:32:30 +00:00
|
|
|
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)
|
2022-01-19 11:58:15 +00:00
|
|
|
headPrm.SetTTL(remoteOpTTL)
|
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
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return res.Header(), nil
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|