fix: bound individual upload retries without timing out whole cuts

This commit is contained in:
pingqiu committed 2026-11-19 00:42:23 -08:00
1 parent aea5f5d88b
commit d9e5f7c9fa
5 files changed
+112 -11

No files matched your search

@@ -800,11 +800,6 @@ 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, 2*time.Second)
defer deadlineCancel()
done, err := l.register()
if err != nil {
return chunkCutResult{}, err
+6 -1
View File
@@ -11,10 +11,12 @@ import (
// 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.
// Bound attempts per object, without imposing a deadline on legitimate work
// across the whole cut. All attempts still obey the caller's context.
func (l *chunkLayerImpl) putCaptured(ctx context.Context, key string, data []byte) error {
backoff := 100 * time.Millisecond
var lastErr error
for {
for attempt := 1; ; attempt++ {
if err := ctx.Err(); err != nil {
return errors.Join(lastErr, err)
}
@@ -31,6 +33,9 @@ func (l *chunkLayerImpl) putCaptured(ctx context.Context, key string, data []byt
}
return errChunkKeyExists
}
if attempt == 3 {
return lastErr
}
timer := time.NewTimer(backoff)
select {
case <-ctx.Done():
@@ -41,7 +41,7 @@ func (r *cutBudgetRemote) Put(ctx context.Context, key string, data []byte) erro
return r.fakeChunkRemote.Put(ctx, key, data)
}
func TestChunkCutDefaultBudgetSharedAcrossStagesAndWritesProgress(t *testing.T) {
func TestChunkCutCallerBudgetSharedAcrossStagesAndWritesProgress(t *testing.T) {
s, _ := newChunkTestStore(t)
f := newFakeChunkRemote()
r := &cutBudgetRemote{fakeChunkRemote: f, held: make(chan struct{})}
@@ -55,7 +55,7 @@ func TestChunkCutDefaultBudgetSharedAcrossStagesAndWritesProgress(t *testing.T)
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
done := make(chan error, 1)
joined := make(chan struct{})
t.Cleanup(func() {
@@ -79,8 +79,9 @@ func TestChunkCutDefaultBudgetSharedAcrossStagesAndWritesProgress(t *testing.T)
if len(deadlines) != 3 {
t.Fatalf("want two staged and one final upload: %v", deadlines)
}
if deadlines[0].IsZero() || deadlines[0].Sub(start) > 4*time.Second {
t.Fatalf("default cut budget exceeds sub-5s target: %v", deadlines[0].Sub(start))
wantDeadline, _ := ctx.Deadline()
if !deadlines[0].Equal(wantDeadline) {
t.Fatalf("cut replaced caller's deadline: %v, want %v", deadlines[0], wantDeadline)
}
for _, d := range deadlines {
if !d.Equal(deadlines[0]) {
@@ -118,7 +119,7 @@ func TestChunkCutDefaultBudgetSharedAcrossStagesAndWritesProgress(t *testing.T)
select {
case err := <-done:
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("lost default deadline: %v", err)
t.Fatalf("lost caller deadline: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("cooperative cut did not settle")
@@ -164,6 +164,38 @@ func TestChunkCutUploadRetryHonorsDeadlineAndRetainsDirt(t *testing.T) {
assertEveryHeadIsPrefixImage(t, f, w)
}
func TestChunkCutUploadAttemptBoundRetainsDirt(t *testing.T) {
s, _ := newChunkTestStore(t)
f := newFakeChunkRemote()
r := &retryUploadRemote{fakeChunkRemote: f, mode: "down", calls: map[string]int{}}
layer := mustAttach(t, s, r, ctConfig(1))
w := newWriteLog()
w.write(t, s, 0, 0x51)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := layer.Cut(ctx); err == nil {
t.Fatal("persistent upload outage published")
}
r.mu.Lock()
if len(r.calls) != 1 {
t.Fatalf("retry changed object keys: %v", r.calls)
}
for _, n := range r.calls {
if n != 3 {
t.Errorf("single object attempted %d times, want bounded 3", n)
}
}
r.mode = "ok"
r.mu.Unlock()
if len(f.installedHeads()) != 0 {
t.Fatal("failed object published HEAD")
}
if _, err := layer.Cut(ctTimeout(t)); err != nil {
t.Fatal(err)
}
assertEveryHeadIsPrefixImage(t, f, w)
}
func TestChunkCutRetrySustainedWritesPastWALWithFlush(t *testing.T) {
const walBytes = 64 * 1024
s, err := createMappedWALStoreV3ForTest(filepath.Join(t.TempDir(), "volume"), ctNumBlocks, ctBlockSize, walBytes, WModeStrict, ctChunkBlocks)
@@ -0,0 +1,68 @@
package storage
import (
"bytes"
"context"
"testing"
"time"
)
type slowSuccessfulUploadRemote struct{ *fakeChunkRemote }
func (r *slowSuccessfulUploadRemote) Put(ctx context.Context, key string, data []byte) error {
timer := time.NewTimer(600 * time.Millisecond)
defer timer.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return r.fakeChunkRemote.Put(ctx, key, data)
}
}
func TestChunkCutLegitimateUploadsExceedTwoSeconds(t *testing.T) {
for _, detach := range []bool{false, true} {
name := "cut"
if detach {
name = "detach"
}
t.Run(name, func(t *testing.T) {
s, _ := newChunkTestStore(t)
f := newFakeChunkRemote()
r := &slowSuccessfulUploadRemote{f}
cfg := ctConfig(1)
cfg.UploadConcurrency = 1
l := mustAttach(t, s, r, cfg).(*chunkLayerImpl)
want := make([]byte, ctNumBlocks*ctBlockSize)
for i := 0; i < 5; i++ {
data := makeBlock(ctBlockSize, byte(0x41+i))
if _, err := s.Write(uint32(i*ctChunkBlocks), data); err != nil {
t.Fatal(err)
}
copy(want[i*ctChunkBlocks*ctBlockSize:], data)
}
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
start := time.Now()
var err error
if detach {
_, err = l.Detach(ctx)
} else {
_, err = l.Cut(ctx)
}
if err != nil {
t.Fatalf("legitimate multi-object work canceled: %v", err)
}
if time.Since(start) <= 2*time.Second {
t.Fatal("fixture did not exceed the former whole-cut limit")
}
heads := f.installedHeads()
if len(heads) != 1 || !bytes.Equal(f.imageOf(t, heads[0]), want) {
t.Fatal("slow successful cut did not publish the exact image once")
}
if detach && heads[0].State != ChunkHeadDetached {
t.Fatal("slow detach did not install detached proof")
}
})
}
}