Optimize complex object assembly #539

Merged
fyrchik merged 2 commits from dstepanov-yadro/frostfs-node:fix/assembling_optimizations into master 2024-09-04 19:51:02 +00:00
3 changed files with 26 additions and 14 deletions

View file

@ -113,8 +113,10 @@ func (r *request) HeadObject(ctx context.Context, id oid.ID) (*objectSDK.Object,
return w.Object(), nil return w.Object(), nil
} }
func (r *request) GetObject(ctx context.Context, id oid.ID, rng *objectSDK.Range) (*objectSDK.Object, error) { func (r *request) GetObjectAndWritePayload(ctx context.Context, id oid.ID, rng *objectSDK.Range, writer ChunkWriter) (*objectSDK.Object, error) {
w := NewSimpleObjectWriter() w := &payloadWriter{
origin: writer,
}
p := r.prm p := r.prm
p.common = p.common.WithLocalOnly(false) p.common = p.common.WithLocalOnly(false)
@ -127,7 +129,7 @@ func (r *request) GetObject(ctx context.Context, id oid.ID, rng *objectSDK.Range
if err := r.getObjectWithIndependentRequest(ctx, p); err != nil { if err := r.getObjectWithIndependentRequest(ctx, p); err != nil {
return nil, err return nil, err
} }
return w.Object(), nil return w.obj, nil
} }
func (r *request) getObjectWithIndependentRequest(ctx context.Context, prm RequestParameters) error { func (r *request) getObjectWithIndependentRequest(ctx context.Context, prm RequestParameters) error {

View file

@ -10,7 +10,7 @@ import (
) )
type objectGetter interface { type objectGetter interface {
GetObject(ctx context.Context, id oid.ID, rng *objectSDK.Range) (*objectSDK.Object, error) GetObjectAndWritePayload(ctx context.Context, id oid.ID, rng *objectSDK.Range, writer ChunkWriter) (*objectSDK.Object, error)
HeadObject(ctx context.Context, id oid.ID) (*objectSDK.Object, error) HeadObject(ctx context.Context, id oid.ID) (*objectSDK.Object, error)
} }
@ -77,10 +77,12 @@ func (a *assembler) getLastPartOrLinkObjectID() (oid.ID, bool) {
} }
func (a *assembler) initializeFromSourceObjectID(ctx context.Context, id oid.ID) (*oid.ID, []oid.ID, error) { func (a *assembler) initializeFromSourceObjectID(ctx context.Context, id oid.ID) (*oid.ID, []oid.ID, error) {
sourceObject, err := a.getChildObject(ctx, id, nil, true) w := NewSimpleObjectWriter()
sourceObject, err := a.getChildObject(ctx, id, nil, true, w)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
sourceObject.SetPayload(w.pld)
parentObject := sourceObject.Parent() parentObject := sourceObject.Parent()
if parentObject == nil { if parentObject == nil {
@ -131,8 +133,8 @@ func (a *assembler) initializeFromSourceObjectID(ctx context.Context, id oid.ID)
return nil, sourceObject.Children(), nil return nil, sourceObject.Children(), nil
} }
func (a *assembler) getChildObject(ctx context.Context, id oid.ID, rng *objectSDK.Range, verifyIsChild bool) (*objectSDK.Object, error) { func (a *assembler) getChildObject(ctx context.Context, id oid.ID, rng *objectSDK.Range, verifyIsChild bool, writer ChunkWriter) (*objectSDK.Object, error) {
obj, err := a.objGetter.GetObject(ctx, id, rng) obj, err := a.objGetter.GetObjectAndWritePayload(ctx, id, rng, writer)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,14 +184,10 @@ func (a *assembler) assemblePayloadByObjectIDs(ctx context.Context, writer Objec
r = &partRanges[i] r = &partRanges[i]
} }
child, err := a.getChildObject(ctx, partIDs[i], r, verifyIsChild) _, err := a.getChildObject(ctx, partIDs[i], r, verifyIsChild, writer)
if err != nil { if err != nil {
return err return err
} }
if err := writer.WriteChunk(ctx, child.Payload()); err != nil {
return err
}
} }
return nil return nil
} }

View file

@ -51,9 +51,7 @@ func NewSimpleObjectWriter() *SimpleObjectWriter {
func (s *SimpleObjectWriter) WriteHeader(_ context.Context, obj *objectSDK.Object) error { func (s *SimpleObjectWriter) WriteHeader(_ context.Context, obj *objectSDK.Object) error {
s.obj = obj s.obj = obj
s.pld = make([]byte, 0, obj.PayloadSize()) s.pld = make([]byte, 0, obj.PayloadSize())
return nil return nil
} }
@ -82,3 +80,17 @@ func (h *hasherWrapper) WriteChunk(_ context.Context, p []byte) error {
_, err := h.hash.Write(p) _, err := h.hash.Write(p)
return err return err
} }
type payloadWriter struct {
origin ChunkWriter
obj *objectSDK.Object
}
func (w *payloadWriter) WriteChunk(ctx context.Context, p []byte) error {
return w.origin.WriteChunk(ctx, p)
}
func (w *payloadWriter) WriteHeader(_ context.Context, o *objectSDK.Object) error {
w.obj = o
return nil
}