diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 99753e159e..461ea7a337 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -134,6 +134,12 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig) if remote == nil || cfg.Bitmap == nil || cfg.ChunkBlocks == 0 { return nil, errors.New("storage: chunk layer needs a remote, a bitmap store and ChunkBlocks > 0") } + if cfg.UploadConcurrency < 0 || cfg.UploadConcurrency > 64 { + return nil, errors.New("storage: upload concurrency must be between 1 and 64 (0 defaults to 8)") + } + if cfg.UploadConcurrency == 0 { + cfg.UploadConcurrency = 8 + } if cfg.ReadMissTimeout <= 0 { cfg.ReadMissTimeout = defaultReadMissTimeout } @@ -846,12 +852,15 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } 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.c.data) + }); err != nil { + l.abortCut(all) + l.dropIntents(ctx, intents) + return chunkCutResult{}, fmt.Errorf("storage: capture upload: %w: %w", errChunkRemoteUnavailable, err) + } for _, u := range uploads { - if err := l.remote.Put(ctx, u.key, u.c.data); err != nil { - l.abortCut(all) - l.dropIntents(ctx, intents) - return chunkCutResult{}, fmt.Errorf("storage: capture upload chunk %d: %w: %w", u.c.idx, errChunkRemoteUnavailable, err) - } next.Chunks[u.c.idx] = chunkRef{Key: u.key, SHA256: u.c.sum} newKeys[u.c.idx] = u.key changed = append(changed, u.c.idx) @@ -1454,10 +1463,13 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats) (map[uint return nil, intents, fmt.Errorf("storage: capture stage intent: %w: %w", errChunkRemoteUnavailable, err) } 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) + }); err != nil { + return nil, intents, fmt.Errorf("storage: capture stage upload: %w: %w", errChunkRemoteUnavailable, err) + } for _, u := range uploads { - if err := l.remote.Put(ctx, u.key, u.data); err != nil { - return nil, intents, fmt.Errorf("storage: capture stage chunk %d: %w: %w", u.idx, errChunkRemoteUnavailable, err) - } st.UploadedChunks++ st.UploadedBytes += int64(len(u.data)) u.data = nil diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index 2a11147c43..f72e31e5ed 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -333,6 +333,9 @@ type chunkLayerConfig struct { // Zero: unbounded, no staging. BarrierBound int + // UploadConcurrency bounds capture/pre-copy Put calls (0 defaults to 8). + UploadConcurrency int + // testReconcileHold, set only by this package's tests, holds the // reconciler before each slice until a token is received or the // channel is closed. diff --git a/seaweed-block/core/storage/chunk_upload.go b/seaweed-block/core/storage/chunk_upload.go new file mode 100644 index 0000000000..b1e79b7b2f --- /dev/null +++ b/seaweed-block/core/storage/chunk_upload.go @@ -0,0 +1,49 @@ +package storage + +import ( + "context" + "sync" +) + +// 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. +func (l *chunkLayerImpl) uploadBatch(ctx context.Context, n int, put func(context.Context, int) error) error { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + var mu sync.Mutex + next := 0 + var first error + var wg sync.WaitGroup + workers := min(l.cfg.UploadConcurrency, n) + for j := 0; j < workers; j++ { + wg.Add(1) + go func() { + defer wg.Done() + for { + mu.Lock() + if first != nil || next == n || ctx.Err() != nil { + mu.Unlock() + return + } + i := next + next++ + mu.Unlock() + if err := put(ctx, i); err != nil { + mu.Lock() + if first == nil { + first = err + cancel() + } + mu.Unlock() + return + } + } + }() + } + wg.Wait() + if first != nil { + return first + } + return ctx.Err() +}