From 806cc13d9f49a48a22c09f170127bff326019a8a Mon Sep 17 00:00:00 2001 From: Airat Arifullin Date: Tue, 29 Aug 2023 13:25:54 +0300 Subject: [PATCH] [#658] client: Refactor PrmObjectGet/Head/Range usage Signed-off-by: Airat Arifullin --- cmd/frostfs-cli/internal/client/client.go | 96 +++++++------------ go.mod | 2 +- go.sum | 4 +- pkg/services/object/internal/client/client.go | 86 +++++++---------- 4 files changed, 73 insertions(+), 115 deletions(-) diff --git a/cmd/frostfs-cli/internal/client/client.go b/cmd/frostfs-cli/internal/client/client.go index 0c6d2a38d..22ca0aa7d 100644 --- a/cmd/frostfs-cli/internal/client/client.go +++ b/cmd/frostfs-cli/internal/client/client.go @@ -563,28 +563,19 @@ func (x GetObjectRes) Header() *objectSDK.Object { // Returns any error which prevented the operation from completing correctly in error return. // For raw reading, returns *object.SplitInfoError error if object is virtual. func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) { - var getPrm client.PrmObjectGet - getPrm.FromContainer(prm.objAddr.Container()) - getPrm.ByID(prm.objAddr.Object()) + cnr := prm.objAddr.Container() + obj := prm.objAddr.Object() - if prm.sessionToken != nil { - getPrm.WithinSession(*prm.sessionToken) + getPrm := client.PrmObjectGet{ + XHeaders: prm.xHeaders, + BearerToken: prm.bearerToken, + Session: prm.sessionToken, + Raw: prm.raw, + Local: prm.local, + ContainerID: &cnr, + ObjectID: &obj, } - if prm.bearerToken != nil { - getPrm.WithBearerToken(*prm.bearerToken) - } - - if prm.raw { - getPrm.MarkRaw() - } - - if prm.local { - getPrm.MarkLocal() - } - - getPrm.WithXHeaders(prm.xHeaders...) - rdr, err := prm.cli.ObjectGetInit(ctx, getPrm) if err != nil { return nil, fmt.Errorf("init object reading on client: %w", err) @@ -639,29 +630,20 @@ func (x HeadObjectRes) Header() *objectSDK.Object { // Returns any error which prevented the operation from completing correctly in error return. // For raw reading, returns *object.SplitInfoError error if object is virtual. func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) { - var cliPrm client.PrmObjectHead - cliPrm.FromContainer(prm.objAddr.Container()) - cliPrm.ByID(prm.objAddr.Object()) + cnr := prm.objAddr.Container() + obj := prm.objAddr.Object() - if prm.sessionToken != nil { - cliPrm.WithinSession(*prm.sessionToken) + headPrm := client.PrmObjectHead{ + XHeaders: prm.xHeaders, + BearerToken: prm.bearerToken, + Session: prm.sessionToken, + Raw: prm.raw, + Local: prm.local, + ContainerID: &cnr, + ObjectID: &obj, } - if prm.bearerToken != nil { - cliPrm.WithBearerToken(*prm.bearerToken) - } - - if prm.raw { - cliPrm.MarkRaw() - } - - if prm.local { - cliPrm.MarkLocal() - } - - cliPrm.WithXHeaders(prm.xHeaders...) - - res, err := prm.cli.ObjectHead(ctx, cliPrm) + res, err := prm.cli.ObjectHead(ctx, headPrm) if err != nil { return nil, fmt.Errorf("read object header via client: %w", err) } @@ -862,32 +844,22 @@ type PayloadRangeRes struct{} // Returns any error which prevented the operation from completing correctly in error return. // For raw reading, returns *object.SplitInfoError error if object is virtual. func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) { - var cliPrm client.PrmObjectRange - cliPrm.FromContainer(prm.objAddr.Container()) - cliPrm.ByID(prm.objAddr.Object()) + cnr := prm.objAddr.Container() + obj := prm.objAddr.Object() - if prm.sessionToken != nil { - cliPrm.WithinSession(*prm.sessionToken) + rangePrm := client.PrmObjectRange{ + XHeaders: prm.xHeaders, + BearerToken: prm.bearerToken, + Session: prm.sessionToken, + Raw: prm.raw, + Local: prm.local, + ContainerID: &cnr, + ObjectID: &obj, + Offset: prm.rng.GetOffset(), + Length: prm.rng.GetLength(), } - if prm.bearerToken != nil { - cliPrm.WithBearerToken(*prm.bearerToken) - } - - if prm.raw { - cliPrm.MarkRaw() - } - - if prm.local { - cliPrm.MarkLocal() - } - - cliPrm.SetOffset(prm.rng.GetOffset()) - cliPrm.SetLength(prm.rng.GetLength()) - - cliPrm.WithXHeaders(prm.xHeaders...) - - rdr, err := prm.cli.ObjectRangeInit(ctx, cliPrm) + rdr, err := prm.cli.ObjectRangeInit(ctx, rangePrm) if err != nil { return nil, fmt.Errorf("init payload reading: %w", err) } diff --git a/go.mod b/go.mod index 46639d49c..cbe20e39f 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 v2.15.1-0.20230802075510-964c3edb3f44 git.frostfs.info/TrueCloudLab/frostfs-contract v0.0.0-20230627134746-36f3d39c406a git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6 - git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230821090303-202412230a05 + git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230828082657-84e7e69f98ac git.frostfs.info/TrueCloudLab/hrw v1.2.1 git.frostfs.info/TrueCloudLab/tzhash v1.8.0 github.com/cheggaaa/pb v1.0.29 diff --git a/go.sum b/go.sum index 58f1fb217..4b5e40174 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,8 @@ git.frostfs.info/TrueCloudLab/frostfs-crypto v0.6.0 h1:FxqFDhQYYgpe41qsIHVOcdzSV git.frostfs.info/TrueCloudLab/frostfs-crypto v0.6.0/go.mod h1:RUIKZATQLJ+TaYQa60X2fTDwfuhMfm8Ar60bQ5fr+vU= git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6 h1:aGQ6QaAnTerQ5Dq5b2/f9DUQtSqPkZZ/bkMx/HKuLCo= git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6/go.mod h1:W8Nn08/l6aQ7UlIbpF7FsQou7TVpcRD1ZT1KG4TrFhE= -git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230821090303-202412230a05 h1:OuViMF54N87FXmaBEpYw3jhzaLrJ/EWOlPL1wUkimE0= -git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230821090303-202412230a05/go.mod h1:t1akKcUH7iBrFHX8rSXScYMP17k2kYQXMbZooiL5Juw= +git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230828082657-84e7e69f98ac h1:BRmJd04eUgzmdLe/2I0Ok/LNOkTyEAPymYUrCCF0a1U= +git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230828082657-84e7e69f98ac/go.mod h1:t1akKcUH7iBrFHX8rSXScYMP17k2kYQXMbZooiL5Juw= git.frostfs.info/TrueCloudLab/hrw v1.2.1 h1:ccBRK21rFvY5R1WotI6LNoPlizk7qSvdfD8lNIRudVc= git.frostfs.info/TrueCloudLab/hrw v1.2.1/go.mod h1:C1Ygde2n843yTZEQ0FP69jYiuaYV0kriLvP4zm8JuvM= git.frostfs.info/TrueCloudLab/rfc6979 v0.4.0 h1:M2KR3iBj7WpY3hP10IevfIB9MURr4O9mwVfJ+SjT3HA= diff --git a/pkg/services/object/internal/client/client.go b/pkg/services/object/internal/client/client.go index 73f4ff7c4..0301a593a 100644 --- a/pkg/services/object/internal/client/client.go +++ b/pkg/services/object/internal/client/client.go @@ -88,7 +88,7 @@ func (x *readPrmCommon) SetNetmapEpoch(_ uint64) { type GetObjectPrm struct { readPrmCommon - cliPrm client.PrmObjectGet + ClientParams client.PrmObjectGet obj oid.ID } @@ -97,7 +97,7 @@ type GetObjectPrm struct { // // By default request will not be raw. func (x *GetObjectPrm) SetRawFlag() { - x.cliPrm.MarkRaw() + x.ClientParams.Raw = true } // SetAddress sets object address. @@ -105,8 +105,10 @@ func (x *GetObjectPrm) SetRawFlag() { // Required parameter. func (x *GetObjectPrm) SetAddress(addr oid.Address) { x.obj = addr.Object() - x.cliPrm.FromContainer(addr.Container()) - x.cliPrm.ByID(x.obj) + cnr := addr.Container() + + x.ClientParams.ContainerID = &cnr + x.ClientParams.ObjectID = &x.obj } // GetObjectRes groups the resulting values of GetObject operation. @@ -134,23 +136,15 @@ func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) { // request will almost definitely fail. The case can occur, for example, // when session is bound to the parent object and child object is requested. if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { - prm.cliPrm.WithinSession(*prm.tokenSession) + prm.ClientParams.Session = prm.tokenSession } - if prm.tokenBearer != nil { - prm.cliPrm.WithBearerToken(*prm.tokenBearer) - } + prm.ClientParams.XHeaders = prm.xHeaders + prm.ClientParams.BearerToken = prm.tokenBearer + prm.ClientParams.Local = prm.local + prm.ClientParams.Key = prm.key - if prm.local { - prm.cliPrm.MarkLocal() - } - - prm.cliPrm.WithXHeaders(prm.xHeaders...) - if prm.key != nil { - prm.cliPrm.UseKey(*prm.key) - } - - rdr, err := prm.cli.ObjectGetInit(ctx, prm.cliPrm) + rdr, err := prm.cli.ObjectGetInit(ctx, prm.ClientParams) if err != nil { return nil, fmt.Errorf("init object reading: %w", err) } @@ -187,7 +181,7 @@ func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) { type HeadObjectPrm struct { readPrmCommon - cliPrm client.PrmObjectHead + ClientParams client.PrmObjectHead obj oid.ID } @@ -196,7 +190,7 @@ type HeadObjectPrm struct { // // By default request will not be raw. func (x *HeadObjectPrm) SetRawFlag() { - x.cliPrm.MarkRaw() + x.ClientParams.Raw = true } // SetAddress sets object address. @@ -204,8 +198,10 @@ func (x *HeadObjectPrm) SetRawFlag() { // Required parameter. func (x *HeadObjectPrm) SetAddress(addr oid.Address) { x.obj = addr.Object() - x.cliPrm.FromContainer(addr.Container()) - x.cliPrm.ByID(x.obj) + cnr := addr.Container() + + x.ClientParams.ContainerID = &cnr + x.ClientParams.ObjectID = &x.obj } // HeadObjectRes groups the resulting values of GetObject operation. @@ -230,22 +226,16 @@ func (x HeadObjectRes) Header() *objectSDK.Object { // // HeadObject ignores the provided session if it is not related to the requested objectSDK. func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) { - if prm.local { - prm.cliPrm.MarkLocal() - } - // see details in same statement of GetObject if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { - prm.cliPrm.WithinSession(*prm.tokenSession) + prm.ClientParams.Session = prm.tokenSession } - if prm.tokenBearer != nil { - prm.cliPrm.WithBearerToken(*prm.tokenBearer) - } + prm.ClientParams.BearerToken = prm.tokenBearer + prm.ClientParams.Local = prm.local + prm.ClientParams.XHeaders = prm.xHeaders - prm.cliPrm.WithXHeaders(prm.xHeaders...) - - cliRes, err := prm.cli.ObjectHead(ctx, prm.cliPrm) + cliRes, err := prm.cli.ObjectHead(ctx, prm.ClientParams) if err == nil { // pull out an error from status err = apistatus.ErrFromStatus(cliRes.Status()) @@ -272,7 +262,7 @@ type PayloadRangePrm struct { ln uint64 - cliPrm client.PrmObjectRange + ClientParams client.PrmObjectRange obj oid.ID } @@ -281,7 +271,7 @@ type PayloadRangePrm struct { // // By default request will not be raw. func (x *PayloadRangePrm) SetRawFlag() { - x.cliPrm.MarkRaw() + x.ClientParams.Raw = true } // SetAddress sets object address. @@ -289,15 +279,17 @@ func (x *PayloadRangePrm) SetRawFlag() { // Required parameter. func (x *PayloadRangePrm) SetAddress(addr oid.Address) { x.obj = addr.Object() - x.cliPrm.FromContainer(addr.Container()) - x.cliPrm.ByID(x.obj) + cnr := addr.Container() + + x.ClientParams.ContainerID = &cnr + x.ClientParams.ObjectID = &x.obj } // SetRange range of the object payload to be read. // // Required parameter. func (x *PayloadRangePrm) SetRange(rng *objectSDK.Range) { - x.cliPrm.SetOffset(rng.GetOffset()) + x.ClientParams.Offset = rng.GetOffset() x.ln = rng.GetLength() } @@ -329,23 +321,17 @@ const maxInitialBufferSize = 1024 * 1024 // 1 MiB // // PayloadRange ignores the provided session if it is not related to the requested objectSDK. func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) { - if prm.local { - prm.cliPrm.MarkLocal() - } - // see details in same statement of GetObject if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { - prm.cliPrm.WithinSession(*prm.tokenSession) + prm.ClientParams.Session = prm.tokenSession } - if prm.tokenBearer != nil { - prm.cliPrm.WithBearerToken(*prm.tokenBearer) - } + prm.ClientParams.XHeaders = prm.xHeaders + prm.ClientParams.BearerToken = prm.tokenBearer + prm.ClientParams.Local = prm.local + prm.ClientParams.Length = prm.ln - prm.cliPrm.SetLength(prm.ln) - prm.cliPrm.WithXHeaders(prm.xHeaders...) - - rdr, err := prm.cli.ObjectRangeInit(ctx, prm.cliPrm) + rdr, err := prm.cli.ObjectRangeInit(ctx, prm.ClientParams) if err != nil { return nil, fmt.Errorf("init payload reading: %w", err) }