2024-05-16 09:26:49 +00:00
|
|
|
package object
|
2020-09-22 15:04:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
clientcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
netmapCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/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 {
|
2023-04-13 17:06:34 +00:00
|
|
|
Get(clientcore.NodeInfo) (clientcore.MultiAddressClient, error)
|
2021-03-23 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 09:26:49 +00:00
|
|
|
// RemoteReader represents utility for getting
|
|
|
|
// the object from a remote host.
|
|
|
|
type RemoteReader 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
|
|
|
}
|
|
|
|
|
2024-05-16 09:26:49 +00:00
|
|
|
// RemoteRequestPrm groups remote operation parameters.
|
|
|
|
type RemoteRequestPrm struct {
|
2024-05-14 11:43:21 +00:00
|
|
|
addr oid.Address
|
|
|
|
raw bool
|
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
|
|
|
|
|
2024-05-16 09:26:49 +00:00
|
|
|
// NewRemoteReader creates, initializes and returns new RemoteHeader instance.
|
|
|
|
func NewRemoteReader(keyStorage *util.KeyStorage, cache ClientConstructor) *RemoteReader {
|
|
|
|
return &RemoteReader{
|
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.
|
2024-05-16 09:26:49 +00:00
|
|
|
func (p *RemoteRequestPrm) WithNodeInfo(v netmap.NodeInfo) *RemoteRequestPrm {
|
2020-10-21 09:18:36 +00:00
|
|
|
if p != nil {
|
|
|
|
p.node = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithObjectAddress sets object address.
|
2024-05-16 09:26:49 +00:00
|
|
|
func (p *RemoteRequestPrm) WithObjectAddress(v oid.Address) *RemoteRequestPrm {
|
2020-10-21 09:18:36 +00:00
|
|
|
if p != nil {
|
2024-05-14 11:43:21 +00:00
|
|
|
p.addr = v
|
2020-10-21 09:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:26:49 +00:00
|
|
|
func (p *RemoteRequestPrm) WithRaw(v bool) *RemoteRequestPrm {
|
2024-05-14 11:43:21 +00:00
|
|
|
if p != nil {
|
|
|
|
p.raw = v
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
// Head requests object header from the remote node.
|
2024-05-16 09:26:49 +00:00
|
|
|
func (h *RemoteReader) Head(ctx context.Context, prm *RemoteRequestPrm) (*objectSDK.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.SetClient(c)
|
|
|
|
headPrm.SetPrivateKey(key)
|
2024-05-14 11:43:21 +00:00
|
|
|
headPrm.SetAddress(prm.addr)
|
2022-01-19 11:58:15 +00:00
|
|
|
headPrm.SetTTL(remoteOpTTL)
|
2024-05-14 11:43:21 +00:00
|
|
|
if prm.raw {
|
|
|
|
headPrm.SetRawFlag()
|
|
|
|
}
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2023-04-06 12:36:37 +00:00
|
|
|
res, err := internalclient.HeadObject(ctx, 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
|
|
|
}
|
2024-05-16 09:26:49 +00:00
|
|
|
|
|
|
|
func (h *RemoteReader) Get(ctx context.Context, prm *RemoteRequestPrm) (*objectSDK.Object, error) {
|
|
|
|
key, err := h.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not receive private key: %w", h, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var info clientcore.NodeInfo
|
|
|
|
|
|
|
|
err = clientcore.NodeInfoFromRawNetmapElement(&info, netmapCore.Node(prm.node))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := h.clientCache.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not create SDK client %s: %w", h, info.AddressGroup(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var getPrm internalclient.GetObjectPrm
|
|
|
|
|
|
|
|
getPrm.SetClient(c)
|
|
|
|
getPrm.SetPrivateKey(key)
|
|
|
|
getPrm.SetAddress(prm.addr)
|
|
|
|
getPrm.SetTTL(remoteOpTTL)
|
|
|
|
if prm.raw {
|
|
|
|
getPrm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.GetObject(ctx, getPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not head object in %s: %w", h, info.AddressGroup(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Object(), nil
|
|
|
|
}
|