2024-08-30 09:09:14 +00:00
|
|
|
package writer
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
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-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
|
|
|
)
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
type remoteWriter 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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *remoteWriter) WriteObject(ctx context.Context, obj *objectSDK.Object, _ objectcore.ContentMeta) 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 {
|
2023-01-10 12:10:54 +00:00
|
|
|
return 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())
|
2023-01-10 12:10:54 +00:00
|
|
|
prm.SetObject(obj)
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2023-01-10 12:10:54 +00:00
|
|
|
err = t.putSingle(ctx, prm)
|
2023-07-12 14:47:42 +00:00
|
|
|
if status.Code(err) != codes.Unimplemented {
|
2023-01-10 12:10:54 +00:00
|
|
|
return err
|
2023-07-12 14:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t.putStream(ctx, prm)
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *remoteWriter) putStream(ctx context.Context, prm internalclient.PutObjectPrm) error {
|
2023-01-10 12:10:54 +00:00
|
|
|
_, err := internalclient.PutObject(ctx, prm)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2023-01-10 12:10:54 +00:00
|
|
|
return fmt.Errorf("(%T) could not put object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
2023-01-10 12:10:54 +00:00
|
|
|
return nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
2020-10-21 11:46:40 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
func (t *remoteWriter) putSingle(ctx context.Context, prm internalclient.PutObjectPrm) error {
|
2023-01-10 12:10:54 +00:00
|
|
|
_, err := internalclient.PutObjectSingle(ctx, prm)
|
2023-07-12 14:47:42 +00:00
|
|
|
if err != nil {
|
2023-01-10 12:10:54 +00:00
|
|
|
return fmt.Errorf("(%T) could not put single object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
|
2023-07-12 14:47:42 +00:00
|
|
|
}
|
2023-01-10 12:10:54 +00:00
|
|
|
return nil
|
2023-07-12 14:47:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
t := &remoteWriter{
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-10 12:10:54 +00:00
|
|
|
if err := t.WriteObject(ctx, p.obj, objectcore.ContentMeta{}); 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
|
|
|
|
}
|