feat: bound parallel capture and pre-copy uploads

This commit is contained in:
pingqiu committed 2026-11-15 18:22:35 -08:00
1 parent 517062f058
commit 76c8de6b10
3 files changed
+72 -8

No files matched your search

+20 -8
View File
@@ -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
+3
View File
@@ -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.
@@ -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()
}