From 7d3b3ec1269fbc55dd3fa95ea7cbc949fbc80064 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 16:25:52 -0800 Subject: [PATCH] test: keep partial staging comparisons across cancellation --- .../storage/chunk_copy_error_equal_test.go | 94 ++++++++++++++++++- 1 file changed, 90 insertions(+), 4 deletions(-) diff --git a/seaweed-block/core/storage/chunk_copy_error_equal_test.go b/seaweed-block/core/storage/chunk_copy_error_equal_test.go index b4ed424d1e..6e16b230e6 100644 --- a/seaweed-block/core/storage/chunk_copy_error_equal_test.go +++ b/seaweed-block/core/storage/chunk_copy_error_equal_test.go @@ -67,7 +67,9 @@ func TestCopyErrorKeepsCapturedEqualMarks(t *testing.T) { l.stMu.Lock() marked := make(map[uint64]bool) for idx := range l.filledClean { - marked[idx] = true + if l.knownEqualToHeadLocked(idx) { + marked[idx] = true + } } l.stMu.Unlock() want := 6 @@ -78,8 +80,9 @@ func TestCopyErrorKeepsCapturedEqualMarks(t *testing.T) { t.Fatalf("kept equal marks %v, want %d captured versions (no later write)", marked, want) } l.testBeforeBarrierLockHook, l.testCOWRead = nil, nil - var repeated atomic.Int64 + var compared, repeated atomic.Int64 l.testAfterCompareHook = func(idx uint64) { + compared.Add(1) if marked[idx] { repeated.Add(1) } @@ -88,8 +91,15 @@ func TestCopyErrorKeepsCapturedEqualMarks(t *testing.T) { if err != nil { t.Fatal(err) } - if result.Stats.CandidateChunks != 8-want || repeated.Load() != 0 { - t.Fatalf("retry candidates=%d repeated=%d; want %d and 0", result.Stats.CandidateChunks, repeated.Load(), 8-want) + wantCompared, wantCandidates := int64(2), 0 + if mode == "write_after_capture" { + wantCompared++ + } + if want == 5 { + wantCandidates = 1 + } + if result.Stats.CandidateChunks != wantCandidates || compared.Load() != wantCompared || repeated.Load() != 0 { + t.Fatalf("retry candidates=%d compared=%d repeated=%d; want %d,%d,0", result.Stats.CandidateChunks, compared.Load(), repeated.Load(), wantCandidates, wantCompared) } if (mode == "write_after_capture" || mode == "different_staged") && !bytes.Equal(blockOf(f.imageOf(t, result.Head), 2*ctChunkBlocks), makeBlock(ctBlockSize, 0xE9)) { t.Fatal("retry lost the write made after capture") @@ -97,3 +107,79 @@ func TestCopyErrorKeepsCapturedEqualMarks(t *testing.T) { }) } } + +// A deadline inside pre-copy must keep the completed comparisons, not only +// the results of an entire staging round that reached the capture barrier. +func TestInterruptedStagingKeepsEqualWork(t *testing.T) { + for _, mutate := range []bool{false, true} { + name := "context_end" + if mutate { + name = "write_after_read" + } + t.Run(name, func(t *testing.T) { + s, l, f := unknownFilledVolume(t) + l.cfg.UploadConcurrency = 1 + l.cfg.COWSpillFree = func() (int64, error) { return 2 * (cowSpillFreeReserve + ctChunkBytes), nil } + ctx, cancel := context.WithCancel(ctTimeout(t)) + defer cancel() + var reads atomic.Int64 + var written uint64 + l.testAfterStageReadHook = func(idx uint64) { + n := reads.Add(1) + if n == 1 && mutate { + written = idx + if _, err := s.Write(uint32(idx*ctChunkBlocks), makeBlock(ctBlockSize, 0xEC)); err != nil { + t.Error(err) + } + } + if n == 3 { + cancel() + } + } + if _, err := l.Cut(ctx); !errors.Is(err, context.Canceled) { + t.Fatalf("staging cancellation: %v", err) + } + l.stMu.Lock() + kept := make(map[uint64]bool) + for idx := range l.filledClean { + if l.knownEqualToHeadLocked(idx) { + kept[idx] = true + } + } + l.stMu.Unlock() + want := 3 + if mutate { + want-- + } + if len(kept) != want || (mutate && kept[written]) { + t.Fatalf("staging kept %v, want %d valid captured proofs", kept, want) + } + if !mutate { + written = 7 // not among the first three completed comparisons + if kept[written] { + t.Fatal("fixture wrote a retained chunk") + } + if _, err := s.Write(uint32(written*ctChunkBlocks), makeBlock(ctBlockSize, 0xEC)); err != nil { + t.Fatal(err) + } + } + var retryReads, repeated atomic.Int64 + l.testAfterStageReadHook = func(idx uint64) { + retryReads.Add(1) + if kept[idx] { + repeated.Add(1) + } + } + result, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if result.Stats.CandidateChunks != 1 || retryReads.Load() != int64(8-want) || repeated.Load() != 0 { + t.Fatalf("retry candidates=%d reads=%d repeated=%d, want 1,%d,0", result.Stats.CandidateChunks, retryReads.Load(), repeated.Load(), 8-want) + } + if !bytes.Equal(blockOf(f.imageOf(t, result.Head), uint32(written*ctChunkBlocks)), makeBlock(ctBlockSize, 0xEC)) { + t.Fatal("retry lost the write after the staging read") + } + }) + } +}