From 25523066e7e8f3edca1e32abf2606b41bf7704f0 Mon Sep 17 00:00:00 2001 From: Airat Arifullin Date: Tue, 5 Nov 2024 16:07:40 +0300 Subject: [PATCH] [#XX] object: Fix payload size limiter * Sending empty chunks by `writeChunk` should not release new objects as this doesn't change `payloadSizeLimiter` internal state. * This also fixes the bug with patcher when an offset of a patch equals to `MaxSize` - `payloadSizeLimiter` releases object again although state is the same. This led to error because EC-encoder receieved empty payload and couldn't not process it. Signed-off-by: Airat Arifullin --- object/transformer/transformer.go | 2 +- object/transformer/transformer_test.go | 35 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/object/transformer/transformer.go b/object/transformer/transformer.go index 16c4066..f7e5cd3 100644 --- a/object/transformer/transformer.go +++ b/object/transformer/transformer.go @@ -261,7 +261,7 @@ func (s *payloadSizeLimiter) initializeLinking(parHdr *object.Object) { func (s *payloadSizeLimiter) writeChunk(ctx context.Context, chunk []byte) error { for { // statement is true if the previous write of bytes reached exactly the boundary. - if s.written > 0 && s.written%s.MaxSize == 0 { + if len(chunk) > 0 && s.written > 0 && s.written%s.MaxSize == 0 { if s.written == s.MaxSize { s.prepareFirstChild() } diff --git a/object/transformer/transformer_test.go b/object/transformer/transformer_test.go index a170835..b40b58e 100644 --- a/object/transformer/transformer_test.go +++ b/object/transformer/transformer_test.go @@ -15,6 +15,41 @@ import ( "github.com/stretchr/testify/require" ) +func TestTransformerNonEffectiveWrites(t *testing.T) { + const maxSize = 16 + + tt := new(testTarget) + target, pk := newPayloadSizeLimiter(maxSize, 0, func() ObjectWriter { return tt }) + cnr := cidtest.ID() + hdr := newObject(cnr) + var owner user.ID + user.IDFromKey(&owner, pk.PrivateKey.PublicKey) + hdr.SetOwnerID(owner) + payload := make([]byte, 3*maxSize) + _, _ = rand.Read(payload) + + ctx := context.Background() + require.NoError(t, target.WriteHeader(ctx, hdr)) + + _, err := target.Write(ctx, payload) + require.NoError(t, err) + + for range 5 { + // run onto unchanged target state + _, err = target.Write(ctx, []byte{}) + require.NoError(t, err) + } + + _, err = target.Close(ctx) + require.NoError(t, err) + + require.Equal(t, 4, len(tt.objects)) + require.Equal(t, payload[:maxSize], tt.objects[0].Payload()) + require.Equal(t, payload[maxSize:2*maxSize], tt.objects[1].Payload()) + require.Equal(t, payload[2*maxSize:], tt.objects[2].Payload()) + require.Equal(t, 3, len(tt.objects[3].Children())) +} + func TestTransformer(t *testing.T) { const maxSize = 100