From 54960b59ea62420e23b5ea55e052f3df233c0fa1 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 29 Nov 2026 12:42:08 -0800 Subject: [PATCH] test: expose restaging equal chunks after a refused full cut --- .../core/storage/chunk_staging_equal_test.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_staging_equal_test.go diff --git a/seaweed-block/core/storage/chunk_staging_equal_test.go b/seaweed-block/core/storage/chunk_staging_equal_test.go new file mode 100644 index 0000000000..443197b111 --- /dev/null +++ b/seaweed-block/core/storage/chunk_staging_equal_test.go @@ -0,0 +1,55 @@ +package storage + +import ( + "bytes" + "errors" + "testing" +) + +// A refused full cut has already proved the unchanged chunks equal. A +// smaller spill budget on the retry must not stage them all before the +// barrier's candidate filter can apply that knowledge. +func TestRefusedFullCutStagesOnlyTheUnmarkedChunk(t *testing.T) { + s, l, f := unknownFilledVolume(t) + want := makeBlock(ctBlockSize, 0xE7) + if _, err := s.Write(ctChunkBlocks+1, want); err != nil { + t.Fatal(err) + } + f.mu.Lock() + f.beforeCAS = func(f *fakeChunkRemote) { f.head.Gen++ } // same image, conditional update refused + f.mu.Unlock() + if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, errChunkHeadPrecondition) { + t.Fatalf("setup refusal: %v", err) + } + marked := make(map[uint64]bool) + l.stMu.Lock() + for idx := range l.filledClean { + marked[idx] = true + } + l.stMu.Unlock() + if len(marked) != ctNumBlocks/ctChunkBlocks-1 { + t.Fatalf("kept %d equal marks, want %d", len(marked), ctNumBlocks/ctChunkBlocks-1) + } + // The next pass can expose less than one chunk at its barrier. Staging + // therefore has to process every actual candidate, making its count exact. + l.cfg.COWSpillFree = func() (int64, error) { return 2 * (cowSpillFreeReserve + ctChunkBytes), nil } + comparedAgain := 0 + l.testAfterCompareHook = func(idx uint64) { + if marked[idx] { + comparedAgain++ + } + } + result, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if result.Stats.StagedChunks != 1 { + t.Fatalf("retry staged %d chunks, want only the one unmarked chunk", result.Stats.StagedChunks) + } + if comparedAgain != 0 || result.Stats.CandidateChunks != 1 { + t.Fatalf("retry compared %d candidates (%d already equal), want 1 and 0", result.Stats.CandidateChunks, comparedAgain) + } + if !bytes.Equal(blockOf(f.imageOf(t, result.Head), ctChunkBlocks+1), want) { + t.Fatal("retry lost changed bytes") + } +}