[#6] services/object: Simplify local/remote targets

We do not use the return result from Close() and we always execute both
methods in succession. It makes sense to unite them.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-01-10 15:10:54 +03:00
parent 448b48287c
commit 5ff82ff04f
4 changed files with 27 additions and 58 deletions

View file

@ -18,8 +18,7 @@ import (
) )
type preparedObjectTarget interface { type preparedObjectTarget interface {
WriteObject(*objectSDK.Object, object.ContentMeta) error WriteObject(context.Context, *objectSDK.Object, object.ContentMeta) error
Close(ctx context.Context) (*transformer.AccessIdentifiers, error)
} }
type distributedTarget struct { type distributedTarget struct {
@ -170,10 +169,9 @@ func (t *distributedTarget) sendObject(ctx context.Context, node nodeDesc) error
target := t.nodeTargetInitializer(node) target := t.nodeTargetInitializer(node)
if err := target.WriteObject(t.obj, t.objMeta); err != nil { err := target.WriteObject(ctx, t.obj, t.objMeta)
if err != nil {
return fmt.Errorf("could not write header: %w", err) return fmt.Errorf("could not write header: %w", err)
} else if _, err := target.Close(ctx); err != nil {
return fmt.Errorf("could not close object stream: %w", err)
} }
return nil return nil
} }

View file

@ -7,7 +7,6 @@ import (
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object" objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
) )
// ObjectStorage is an object storage interface. // ObjectStorage is an object storage interface.
@ -27,41 +26,26 @@ type ObjectStorage interface {
type localTarget struct { type localTarget struct {
storage ObjectStorage storage ObjectStorage
obj *objectSDK.Object
meta objectCore.ContentMeta
} }
func (t *localTarget) WriteObject(obj *objectSDK.Object, meta objectCore.ContentMeta) error { func (t localTarget) WriteObject(ctx context.Context, obj *objectSDK.Object, meta objectCore.ContentMeta) error {
t.obj = obj switch meta.Type() {
t.meta = meta
return nil
}
func (t *localTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
switch t.meta.Type() {
case objectSDK.TypeTombstone: case objectSDK.TypeTombstone:
err := t.storage.Delete(ctx, objectCore.AddressOf(t.obj), t.meta.Objects()) err := t.storage.Delete(ctx, objectCore.AddressOf(obj), meta.Objects())
if err != nil { if err != nil {
return nil, fmt.Errorf("could not delete objects from tombstone locally: %w", err) return fmt.Errorf("could not delete objects from tombstone locally: %w", err)
} }
case objectSDK.TypeLock: case objectSDK.TypeLock:
err := t.storage.Lock(ctx, objectCore.AddressOf(t.obj), t.meta.Objects()) err := t.storage.Lock(ctx, objectCore.AddressOf(obj), meta.Objects())
if err != nil { if err != nil {
return nil, fmt.Errorf("could not lock object from lock objects locally: %w", err) return fmt.Errorf("could not lock object from lock objects locally: %w", err)
} }
default: default:
// objects that do not change meta storage // objects that do not change meta storage
} }
if err := t.storage.Put(ctx, t.obj); err != nil { //TODO if err := t.storage.Put(ctx, obj); err != nil {
return nil, fmt.Errorf("(%T) could not put object to local storage: %w", t, err) return fmt.Errorf("(%T) could not put object to local storage: %w", t, err)
} }
return nil
id, _ := t.obj.ID()
return &transformer.AccessIdentifiers{
SelfID: id,
}, nil
} }

View file

@ -12,7 +12,6 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -24,8 +23,6 @@ type remoteTarget struct {
nodeInfo clientcore.NodeInfo nodeInfo clientcore.NodeInfo
obj *objectSDK.Object
clientConstructor ClientConstructor clientConstructor ClientConstructor
} }
@ -44,16 +41,10 @@ type RemotePutPrm struct {
obj *objectSDK.Object obj *objectSDK.Object
} }
func (t *remoteTarget) WriteObject(obj *objectSDK.Object, _ objectcore.ContentMeta) error { func (t *remoteTarget) WriteObject(ctx context.Context, obj *objectSDK.Object, _ objectcore.ContentMeta) error {
t.obj = obj
return nil
}
func (t *remoteTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
c, err := t.clientConstructor.Get(t.nodeInfo) c, err := t.clientConstructor.Get(t.nodeInfo)
if err != nil { if err != nil {
return nil, fmt.Errorf("(%T) could not create SDK client %s: %w", t, t.nodeInfo, err) return fmt.Errorf("(%T) could not create SDK client %s: %w", t, t.nodeInfo, err)
} }
var prm internalclient.PutObjectPrm var prm internalclient.PutObjectPrm
@ -63,32 +54,30 @@ func (t *remoteTarget) Close(ctx context.Context) (*transformer.AccessIdentifier
prm.SetSessionToken(t.commonPrm.SessionToken()) prm.SetSessionToken(t.commonPrm.SessionToken())
prm.SetBearerToken(t.commonPrm.BearerToken()) prm.SetBearerToken(t.commonPrm.BearerToken())
prm.SetXHeaders(t.commonPrm.XHeaders()) prm.SetXHeaders(t.commonPrm.XHeaders())
prm.SetObject(t.obj) prm.SetObject(obj)
res, err := t.putSingle(ctx, prm) err = t.putSingle(ctx, prm)
if status.Code(err) != codes.Unimplemented { if status.Code(err) != codes.Unimplemented {
return res, err return err
} }
return t.putStream(ctx, prm) return t.putStream(ctx, prm)
} }
func (t *remoteTarget) putStream(ctx context.Context, prm internalclient.PutObjectPrm) (*transformer.AccessIdentifiers, error) { func (t *remoteTarget) putStream(ctx context.Context, prm internalclient.PutObjectPrm) error {
res, err := internalclient.PutObject(ctx, prm) _, err := internalclient.PutObject(ctx, prm)
if err != nil { if err != nil {
return nil, fmt.Errorf("(%T) could not put object to %s: %w", t, t.nodeInfo.AddressGroup(), err) return fmt.Errorf("(%T) could not put object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
} }
return nil
return &transformer.AccessIdentifiers{SelfID: res.ID()}, nil
} }
func (t *remoteTarget) putSingle(ctx context.Context, prm internalclient.PutObjectPrm) (*transformer.AccessIdentifiers, error) { func (t *remoteTarget) putSingle(ctx context.Context, prm internalclient.PutObjectPrm) error {
res, err := internalclient.PutObjectSingle(ctx, prm) _, err := internalclient.PutObjectSingle(ctx, prm)
if err != nil { if err != nil {
return nil, fmt.Errorf("(%T) could not put single object to %s: %w", t, t.nodeInfo.AddressGroup(), err) return fmt.Errorf("(%T) could not put single object to %s: %w", t, t.nodeInfo.AddressGroup(), err)
} }
return nil
return &transformer.AccessIdentifiers{SelfID: res.ID()}, nil
} }
// NewRemoteSender creates, initializes and returns new RemoteSender instance. // NewRemoteSender creates, initializes and returns new RemoteSender instance.
@ -134,9 +123,7 @@ func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
return fmt.Errorf("parse client node info: %w", err) return fmt.Errorf("parse client node info: %w", err)
} }
if err := t.WriteObject(p.obj, objectcore.ContentMeta{}); err != nil { if err := t.WriteObject(ctx, p.obj, objectcore.ContentMeta{}); err != nil {
return fmt.Errorf("(%T) could not send object header: %w", s, err)
} else if _, err := t.Close(ctx); err != nil {
return fmt.Errorf("(%T) could not send object: %w", s, err) return fmt.Errorf("(%T) could not send object: %w", s, err)
} }

View file

@ -224,7 +224,7 @@ func (p *Streamer) newCommonTarget(prm *PutInitPrm) *distributedTarget {
getWorkerPool: p.getWorkerPool, getWorkerPool: p.getWorkerPool,
nodeTargetInitializer: func(node nodeDesc) preparedObjectTarget { nodeTargetInitializer: func(node nodeDesc) preparedObjectTarget {
if node.local { if node.local {
return &localTarget{ return localTarget{
storage: p.localStore, storage: p.localStore,
} }
} }