2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-14 10:31:30 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-21 14:31:31 +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"
|
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
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-04-27 15:46:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
|
2023-07-12 14:47:42 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type remoteTarget struct {
|
2023-03-14 10:31:30 +00:00
|
|
|
privateKey *ecdsa.PrivateKey
|
2020-09-29 16:44:59 +00:00
|
|
|
|
2020-12-29 08:24:07 +00:00
|
|
|
commonPrm *util.CommonPrm
|
2020-10-20 13:44:45 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
nodeInfo clientcore.NodeInfo
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
obj *objectSDK.Object
|
2020-11-18 13:03:31 +00:00
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientConstructor ClientConstructor
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 11:46:40 +00:00
|
|
|
// RemoteSender represents utility for
|
|
|
|
// sending an object to a remote host.
|
|
|
|
type RemoteSender struct {
|
2020-11-23 12:59:06 +00:00
|
|
|
keyStorage *util.KeyStorage
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientConstructor ClientConstructor
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemotePutPrm groups remote put operation parameters.
|
|
|
|
type RemotePutPrm struct {
|
2022-06-08 23:18:26 +00:00
|
|
|
node netmap.NodeInfo
|
2020-10-21 11:46:40 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
obj *objectSDK.Object
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func (t *remoteTarget) WriteObject(obj *objectSDK.Object, _ objectcore.ContentMeta) error {
|
2022-03-03 14:19:05 +00:00
|
|
|
t.obj = obj
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (t *remoteTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
|
2021-09-28 05:32:30 +00:00
|
|
|
c, err := t.clientConstructor.Get(t.nodeInfo)
|
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", t, t.nodeInfo, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.PutObjectPrm
|
|
|
|
|
|
|
|
prm.SetClient(c)
|
2023-03-14 10:31:30 +00:00
|
|
|
prm.SetPrivateKey(t.privateKey)
|
2021-11-01 08:35:33 +00:00
|
|
|
prm.SetSessionToken(t.commonPrm.SessionToken())
|
|
|
|
prm.SetBearerToken(t.commonPrm.BearerToken())
|
|
|
|
prm.SetXHeaders(t.commonPrm.XHeaders())
|
2022-03-03 14:19:05 +00:00
|
|
|
prm.SetObject(t.obj)
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2023-07-12 14:47:42 +00:00
|
|
|
res, err := t.putSingle(ctx, prm)
|
|
|
|
if status.Code(err) != codes.Unimplemented {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.putStream(ctx, prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *remoteTarget) putStream(ctx context.Context, prm internalclient.PutObjectPrm) (*transformer.AccessIdentifiers, error) {
|
2023-04-06 12:36:37 +00:00
|
|
|
res, err := internalclient.PutObject(ctx, prm)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not put object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 15:46:42 +00:00
|
|
|
return &transformer.AccessIdentifiers{SelfID: res.ID()}, nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
2020-10-21 11:46:40 +00:00
|
|
|
|
2023-07-12 14:47:42 +00:00
|
|
|
func (t *remoteTarget) putSingle(ctx context.Context, prm internalclient.PutObjectPrm) (*transformer.AccessIdentifiers, error) {
|
|
|
|
res, err := internalclient.PutObjectSingle(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("(%T) could not put single object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &transformer.AccessIdentifiers{SelfID: res.ID()}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:46:40 +00:00
|
|
|
// NewRemoteSender creates, initializes and returns new RemoteSender instance.
|
2021-03-23 18:40:36 +00:00
|
|
|
func NewRemoteSender(keyStorage *util.KeyStorage, cons ClientConstructor) *RemoteSender {
|
2020-10-21 11:46:40 +00:00
|
|
|
return &RemoteSender{
|
2021-03-23 18:40:36 +00:00
|
|
|
keyStorage: keyStorage,
|
|
|
|
clientConstructor: cons,
|
2020-10-21 11:46:40 +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 *RemotePutPrm) WithNodeInfo(v netmap.NodeInfo) *RemotePutPrm {
|
2020-10-21 11:46:40 +00:00
|
|
|
if p != nil {
|
|
|
|
p.node = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithObject sets transferred object.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (p *RemotePutPrm) WithObject(v *objectSDK.Object) *RemotePutPrm {
|
2020-10-21 11:46:40 +00:00
|
|
|
if p != nil {
|
|
|
|
p.obj = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject sends object to remote node.
|
|
|
|
func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
|
2023-03-14 10:31:30 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:46:40 +00:00
|
|
|
t := &remoteTarget{
|
2023-03-14 10:31:30 +00:00
|
|
|
privateKey: key,
|
2021-03-23 18:40:36 +00:00
|
|
|
clientConstructor: s.clientConstructor,
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 10:31:30 +00:00
|
|
|
err = clientcore.NodeInfoFromRawNetmapElement(&t.nodeInfo, netmapCore.Node(p.node))
|
2021-09-28 05:32:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse client node info: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-01 17:32:43 +00:00
|
|
|
if err := t.WriteObject(p.obj, objectcore.ContentMeta{}); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not send object header: %w", s, err)
|
2023-04-03 11:23:53 +00:00
|
|
|
} else if _, err := t.Close(ctx); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not send object: %w", s, err)
|
2020-10-21 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|