From 49772850c02bbe60dd3035d26b133a5fa8aedcfe Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 11:12:06 -0800 Subject: [PATCH] test(storage): a write between a cut's barrier and its recording leaves no mark A test hook runs after the cut's compare and before it records its marks; a write there, on a chunk the cut compared equal, must reach HEAD in the next cut. Recording the current version instead of the captured one (without the version check) hides the write: the next cut compares nothing. Co-Authored-By: Claude Opus 5.5 --- .../chunk_fresh_readback_candidates_test.go | 34 +++++++++++++++++++ seaweed-block/core/storage/chunk_layer.go | 4 +++ 2 files changed, 38 insertions(+) diff --git a/seaweed-block/core/storage/chunk_fresh_readback_candidates_test.go b/seaweed-block/core/storage/chunk_fresh_readback_candidates_test.go index 118f337697..145d086b06 100644 --- a/seaweed-block/core/storage/chunk_fresh_readback_candidates_test.go +++ b/seaweed-block/core/storage/chunk_fresh_readback_candidates_test.go @@ -328,3 +328,37 @@ func TestWithoutAFailedCutEveryUnknownChunkIsCompared(t *testing.T) { t.Errorf("compared %d chunks, want %d", r.Stats.CandidateChunks, want) } } + +// A write that lands after a cut's barrier, on a chunk the cut compared +// equal, leaves no trusted mark: the mark is recorded with the version the +// barrier captured and only while the chunk still has it, so the next cut +// compares the chunk and publishes the write. Recording the current version +// instead would hide the write from every later cut. +func TestAWriteBetweenTheBarrierAndTheRecordingLeavesNoMark(t *testing.T) { + s, l, f := unknownFilledVolume(t) + local := makeBlock(ctBlockSize, 0xD7) + var wrote bool + l.testBeforeRecordMarksHook = func() { + if wrote { + return + } + wrote = true + if _, err := s.Write(ctChunkBlocks+1, local); err != nil { // chunk 1, compared equal by this cut + t.Error(err) + } + } + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } + l.testBeforeRecordMarksHook = nil + if !wrote { + t.Fatal("setup: the cut recorded no marks (the hook never ran)") + } + r, err := l.Cut(ctTimeout(t)) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(blockOf(f.imageOf(t, r.Head), ctChunkBlocks+1), local) { + t.Fatalf("the write made between the barrier and the recording is not in HEAD (the next cut compared %d chunks): a mark hid it", r.Stats.CandidateChunks) + } +} diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 4e35ecb9ab..87f42feb59 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -147,6 +147,7 @@ type chunkLayerImpl struct { testAfterStageReadHook func(idx uint64) // after a staging read, before its result is kept testAfterBarrierSyncHook func(h uint64) // under the capture barrier, right after Sync fixed H testBeforeBarrierLockHook func() // right before the capture barrier's Lock request + testBeforeRecordMarksHook func() // after a cut's compare, before it records its marks syncDirFn func(dir string) error // directory fsync after a rename; syncDir in production // Absence: chunks whose local bytes are not authoritative (fresh host, @@ -1045,6 +1046,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } uploads = append(uploads, upload{c: c, key: l.keyFor(c.idx, c.sum, l.newKey(c.idx))}) } + if hook := l.testBeforeRecordMarksHook; hook != nil { + hook() + } if len(equal) > 0 { l.stMu.Lock() for _, idx := range equal {