test: expose restaging equal chunks after a refused full cut

This commit is contained in:
pingqiu committed 2026-11-29 16:03:38 -08:00
1 parent f829e8751e
commit 54960b59ea
1 file changed
+55
@@ -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")
}
}