From a9d24e425665eed1f542ef284db8e69843ebc655 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 18 Nov 2026 22:14:03 -0800 Subject: [PATCH] storage: resolve lost upload replies before bounded single-object retries --- seaweed-block/core/storage/chunk_layer.go | 9 +++-- seaweed-block/core/storage/chunk_upload.go | 34 +++++++++++++++++++ .../core/storage/chunk_upload_retry_test.go | 8 +++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index d8d56fc664..ffee0274f9 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -800,6 +800,11 @@ type stagedChunk struct { const chunkStageRounds = 3 func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res chunkCutResult, err error) { + // Retries share one cut budget, shortened by the caller's own deadline. + // As with every remote operation, an implementation ignoring cancellation + // is still joined before buffers and intents are settled. + ctx, deadlineCancel := context.WithTimeout(ctx, 5*time.Minute) + defer deadlineCancel() done, err := l.register() if err != nil { return chunkCutResult{}, err @@ -1012,7 +1017,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } if err := l.uploadBatch(ctx, len(uploads), func(ctx context.Context, i int) error { u := uploads[i] - return l.remote.Put(ctx, u.key, u.c.data) + return l.putCaptured(ctx, u.key, u.c.data) }); err != nil { l.abortCut(all) l.dropIntents(ctx, intents) @@ -1662,7 +1667,7 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats) (map[uint intents = append(intents, id) if err := l.uploadBatch(ctx, len(uploads), func(ctx context.Context, i int) error { u := uploads[i] - return l.remote.Put(ctx, u.key, u.data) + return l.putCaptured(ctx, u.key, u.data) }); err != nil { return nil, intents, fmt.Errorf("storage: capture stage upload: %w: %w", errChunkRemoteUnavailable, err) } diff --git a/seaweed-block/core/storage/chunk_upload.go b/seaweed-block/core/storage/chunk_upload.go index b1e79b7b2f..f03fc831f9 100644 --- a/seaweed-block/core/storage/chunk_upload.go +++ b/seaweed-block/core/storage/chunk_upload.go @@ -1,10 +1,44 @@ package storage import ( + "bytes" "context" "sync" + "time" ) +// putCaptured retries one immutable object, never a whole batch or HEAD CAS. +// A lost binding reply can turn the next attempt into KeyExists: only exact +// bytes read from that binding prove completion; a collision is terminal. +func (l *chunkLayerImpl) putCaptured(ctx context.Context, key string, data []byte) error { + backoff := 100 * time.Millisecond + for { + if err := ctx.Err(); err != nil { + return err + } + err := l.remote.Put(ctx, key, data) + if err == nil { + return nil + } + // Resolve a possibly committed binding before replaying the Put. + got, readErr := l.remote.Get(ctx, key) + if readErr == nil { + if bytes.Equal(got, data) { + return nil + } + return errChunkKeyExists + } + timer := time.NewTimer(backoff) + select { + case <-ctx.Done(): + timer.Stop() + return ctx.Err() + case <-timer.C: + } + backoff = min(2*time.Second, 2*backoff) + } +} + // uploadBatch owns and joins all Put calls before its caller settles intents. // No storage/publication lock is acquired by workers. Cancellation stops new // admission; a remote ignoring cancellation remains joined, never declared done. diff --git a/seaweed-block/core/storage/chunk_upload_retry_test.go b/seaweed-block/core/storage/chunk_upload_retry_test.go index d55ce37a22..cbee7db935 100644 --- a/seaweed-block/core/storage/chunk_upload_retry_test.go +++ b/seaweed-block/core/storage/chunk_upload_retry_test.go @@ -65,8 +65,12 @@ func TestChunkCutRetriesOnlyTheFailedUpload(t *testing.T) { t.Fatalf("new key generated on retry: %v", r.calls) } for key, n := range r.calls { - if n != 2 { - t.Fatalf("key %s attempted %d times, want 2", key, n) + want := 2 + if mode == "lost" { + want = 1 + } + if n != want { + t.Fatalf("key %s attempted %d times, want %d", key, n, want) } } })