From 306831c07f65665643ae089f34d7f3d2e23f6cf7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 18:52:10 -0800 Subject: [PATCH] storage, volume: candidate set review: absent-to-present touch, oracle, telemetry - A fill's install (absent to present) touches its chunk: a fetch that outlived a same-open recovery installs HEAD's bytes with no clean mark, and the full scan made that chunk a candidate; now the touched set does too (TestAFillThatOutlivesARecoveryMakesItsChunkACandidate, which fails without the touch). - Under this package's tests every barrier runs the full scan as an oracle (candidateOracle, nil in production) against the barrier's candidates with l.dirty and allDirty; a chunk it finds that the set missed is a finding. The whole package runs with 0 oracle findings. - chunk_layer.go carries the table of each predicate input that moves and what touches the chunk, including the clears that need none. - The cut body (/chunk/cut) and the status schema carry CandidateVisits and PreSyncNs. - The unknown-outcome guard compares the remote's full images (any store format) and asserts the injected cut's outcome is unknown. Co-Authored-By: Claude Opus 5.5 --- .../core/host/volume/chunk_control.go | 4 + .../storage/chunk_cut_candidate_set_test.go | 70 +++++++++++++-- seaweed-block/core/storage/chunk_layer.go | 89 +++++++++++++++++++ .../storage/chunk_record_shadow_main_test.go | 8 +- seaweed-block/docs/status-schema.md | 2 + 5 files changed, 166 insertions(+), 7 deletions(-) diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index 428a39fcb4..270c8b98f9 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -62,6 +62,8 @@ type ChunkCutResponse struct { // step); every duration is in nanoseconds. type ChunkCutStatsBody struct { SyncPhases storage.SyncPhaseStats `json:"syncPhases"` + CandidateVisits int64 `json:"candidateVisits"` // chunk indices the barrier's candidate step touched + PreSyncNs int64 `json:"preSyncNs"` // the sync right before the barrier, outside the pause CandidateNs int64 `json:"candidateNs"` WindowSetupNs int64 `json:"windowSetupNs"` PublishPrepareNs int64 `json:"publishPrepareNs"` @@ -112,6 +114,8 @@ type ChunkCutStatsBody struct { func cutStatsBody(s storage.ChunkCutStats) ChunkCutStatsBody { return ChunkCutStatsBody{ SyncPhases: s.SyncPhases, + CandidateVisits: s.CandidateVisits, + PreSyncNs: s.PreSyncNs, CandidateNs: s.CandidateNs, WindowSetupNs: s.WindowSetupNs, PublishPrepareNs: s.PublishPrepareNs, diff --git a/seaweed-block/core/storage/chunk_cut_candidate_set_test.go b/seaweed-block/core/storage/chunk_cut_candidate_set_test.go index bd090e53f0..637c2955a9 100644 --- a/seaweed-block/core/storage/chunk_cut_candidate_set_test.go +++ b/seaweed-block/core/storage/chunk_cut_candidate_set_test.go @@ -1,13 +1,66 @@ package storage import ( + "bytes" "crypto/sha256" "encoding/binary" + "errors" "fmt" "path/filepath" "testing" + "time" ) +// A fill whose fetch outlives a same-open recovery installs HEAD's bytes but +// no clean mark (the recovery moved the chunk's version), so the chunk is a +// candidate again, as the full scan says; the absent-to-present install +// touches it. (The package's candidate oracle checks the cut's barrier too.) +func TestAFillThatOutlivesARecoveryMakesItsChunkACandidate(t *testing.T) { + s, l, remote, _ := hydrationFixture(t, 2, 64) + // The engine's predicate is what is checked here. The chunk record names + // such a chunk HashPublished (the fill verified HEAD's hash), so the + // engine's conservative candidate is an extra to the record's shadow, as + // it was with the full scan: the shadow is off for this schedule. + l.shadow = nil + entered, release := holdGets(t, remote) + defer release() + read := make(chan error, 1) + go func() { + _, err := s.Read(0) + read <- err + }() + select { + case <-entered: + case <-time.After(ctLocalBound): + t.Fatal("the read miss never reached the remote") + } + if l.captureCandidates()[0] { + t.Fatal("setup: the absent chunk is already a candidate") + } + l.noteAllMutated() // a same-open recovery, while the fetch is held + release() + select { + case err := <-read: + if err != nil { + t.Fatal(err) + } + case <-time.After(ctLocalBound): + t.Fatal("the read did not complete") + } + l.stMu.Lock() + absent, marked := l.absent[0], l.filledClean[0] != (filledMark{}) + l.stMu.Unlock() + if absent || marked { + t.Fatalf("setup: after the fill, absent=%v clean-marked=%v; want present without a mark", absent, marked) + } + if !l.captureCandidates()[0] { + t.Fatal("a chunk filled across a recovery, present without a clean mark, is not a candidate") + } + if _, err := l.Cut(ctTimeout(t)); err != nil { + t.Fatal(err) + } +} + // freshShape attaches a lazy fresh-from-HEAD layer over n one-block chunks // (512-byte blocks), every chunk named by HEAD and absent locally, as the // SPACE test builds the 1 TiB shape (n = 262,144). @@ -126,17 +179,20 @@ func TestUnknownCommitOutcomeLosesNoChunk(t *testing.T) { for lba := uint32(0); lba < ctNumBlocks; lba++ { w.write(t, s, lba, byte(lba+1)) } - base, err := l.Cut(ctTimeout(t)) - if err != nil { + if _, err := l.Cut(ctTimeout(t)); err != nil { t.Fatal(err) } + base := f.imageOf(t, f.stats().head) // the remote's full image, any format w.write(t, s, 0*ctChunkBlocks, 0xa0) // chunk 0 w.write(t, s, 2*ctChunkBlocks, 0xa2) // chunk 2 f.mu.Lock() f.casModes = []fakeCASMode{casUnknownNotApplied} f.mu.Unlock() - _, _ = l.Cut(ctTimeout(t)) // the outcome is unknown; it did not land + if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) { + t.Fatalf("the injected cut answered %v, want its outcome unknown", err) + } w.write(t, s, 5*ctChunkBlocks, 0xa5) // chunk 5, after the cut + var err error var last chunkCutResult for i := 0; i < 3; i++ { // the next cuts settle the unknown outcome and publish if last, err = l.Cut(ctTimeout(t)); err == nil && !last.Noop { @@ -146,9 +202,11 @@ func TestUnknownCommitOutcomeLosesNoChunk(t *testing.T) { if err != nil { t.Fatal(err) } - head, _ := f.GetHead(ctTimeout(t)) - for idx := uint64(0); idx < ctNumBlocks/ctChunkBlocks; idx++ { - changed := head.Chunks[idx].SHA256 != base.Head.Chunks[idx].SHA256 + img := f.imageOf(t, f.stats().head) + chunkBytes := ctChunkBlocks * ctBlockSize + for idx := 0; idx < ctNumBlocks/ctChunkBlocks; idx++ { + at := idx * chunkBytes + changed := !bytes.Equal(img[at:at+chunkBytes], base[at:at+chunkBytes]) want := idx == 0 || idx == 2 || idx == 5 if changed != want { t.Fatalf("chunk %d changed=%v after the unknown outcome and the next cut, want %v", idx, changed, want) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 1e1bc334cb..f96eeb4c87 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -521,6 +521,58 @@ func (l *chunkLayerImpl) resolveTouchFilled() { l.touch(filled...) } +// candidateOracle, when set (by this package's tests), receives every chunk +// the full predicate scan finds that a barrier's touched-set candidate step +// missed. Production leaves it nil: the scan is O(chunks). +var candidateOracle func(string) + +// checkCandidatesAgainstScan is the oracle: the full scan of the old +// candidate step at this barrier, against what this barrier's candidates +// will be (the touched set's, unioned with l.dirty and allDirty as the +// barrier does next, as the scan's were). A chunk in the live set (touched +// after the take) or covered by a pending recovery request is the next +// cut's, not a miss. +func (l *chunkLayerImpl) checkCandidatesAgainstScan(candidates map[uint64]bool) { + report := candidateOracle + if report == nil { + return + } + l.stMu.Lock() + base := l.remoteLSN + all := l.allDirty + dirty := make(map[uint64]bool, len(l.dirty)) + for i := range l.dirty { + dirty[i] = true + } + l.stMu.Unlock() + if all { + return // every chunk is a candidate of this barrier + } + l.s.mu.RLock() + defer l.s.mu.RUnlock() + l.stMu.Lock() + defer l.stMu.Unlock() + pendingFilled := l.touchFilled.Load() + l.unkMu.Lock() + live := l.touched + defer l.unkMu.Unlock() + for idx := 0; idx < l.s.v3.mapEntries.len(); idx++ { + i := uint64(idx) + if candidates[i] || dirty[i] || (l.absent[i] && !l.chunkHasLocalLocked(i)) { + continue + } + e := l.s.v3.mapEntries.at(idx) + if !((e.Unknown && !l.knownEqualToHeadLocked(i)) || e.LSN > base) { + continue + } + if (live != nil && live.has(i)) || (pendingFilled && l.filledClean[i] != (filledMark{})) { + continue + } + report(fmt.Sprintf("candidate oracle: chunk %d is a candidate by the full scan (unknown=%v lsn=%d remoteLSN=%d) but not by the touched set", i, e.Unknown, e.LSN, base)) + return + } +} + // touch adds chunks to the touched set (leaf lock). func (l *chunkLayerImpl) touch(chunks ...uint64) { if len(chunks) == 0 { @@ -861,6 +913,38 @@ func (l *chunkLayerImpl) captureCandidates() map[uint64]bool { return out } +// The candidate predicate, per chunk i (INV-WAL-V3-CAPTURE-CANDIDATES-001): +// +// (entry.Unknown && !knownEqualToHead(i)) || entry.LSN > remoteLSN, +// unless absent(i) && !chunkHasLocal(i) +// +// The touched set must hold every chunk whose answer can turn true between +// two barriers. Each input that moves, and what touches the chunk: +// +// input mover touch +// entry.LSN up recordMapWriteV3 (write, replay) OnMapEntryChange +// entry.Unknown on markMapUnknownV3, recordMapWriteV3 OnMapEntryChange +// (reused LSN); map creation, open seedTouched +// entry.Unknown off capture's clear, known-zero clear none: true to false only +// (persistCapturedMapV3) +// remoteLSN up finishCut none: true to false only +// version (proof) noteMutation touch(i) +// noteAllMutated (Recover, BASE resume touchFilled, resolved at +// under WALStore.mu; BASE transition the next take or peek +// under capMu) +// HEAD ref (proof) setHeadLocked, applyCommitLocked touchRefChangedLocked, +// touch(changed) +// clean mark added a fill none: true to false only +// clean marks gone BASE install (allDirty: every chunk) none: allDirty +// own (proof) OwnBatch addOwnCandidatesLocked +// absent off a fill's install touch(i) +// a local write; BASE install noteMutation; allDirty +// absent on an evict none: true to false only +// +// A recovery that lands after a barrier's take is the next cut's, as a write +// after the barrier is. Under tests, every barrier also runs the full scan as +// an oracle (candidateOracle) and reports any chunk the set missed. +// // candidatesFrom is the candidate step over a touched set (peeked by a // reader, taken by the barrier). Its work is O(changed): the WAL's dirty // entries, the own batch, and the set's members, each judged on its own; no @@ -1036,6 +1120,10 @@ func (l *chunkLayerImpl) installFill(idx, gen, version uint64, ref chunkRef, dat sh.fill(idx, ref.SHA256) } delete(l.absent, idx) + // Absent to present: the predicate's exclusion ends. Without a fresh + // clean mark (a version moved during the fetch, e.g. by a recovery) the + // chunk may now be a candidate, so the next candidate step judges it. + l.touch(idx) for i := uint64(0); i < l.chunkBlocks; i++ { delete(l.localBlocks, first+i) } @@ -1500,6 +1588,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch candidates, visits := l.candidatesFrom(taken.each) visits += int64(len(taken.summary)) // the summary words the walk read l.returnTouched(taken) + l.checkCandidatesAgainstScan(candidates) // tests only (nil in production) l.stMu.Lock() all := l.allDirty for idx := range l.dirty { diff --git a/seaweed-block/core/storage/chunk_record_shadow_main_test.go b/seaweed-block/core/storage/chunk_record_shadow_main_test.go index cf4d679673..78a45f5b36 100644 --- a/seaweed-block/core/storage/chunk_record_shadow_main_test.go +++ b/seaweed-block/core/storage/chunk_record_shadow_main_test.go @@ -18,6 +18,12 @@ func TestMain(m *testing.M) { found = append(found, msg) mu.Unlock() } + // Every barrier also checks its touched-set candidates against the full + // scan; a chunk the set missed is a finding too. + candidateOracle = func(msg string) { + fmt.Fprintln(os.Stderr, msg) // in place, next to the test that ran it + shadowFindings(msg) + } // CHUNK_RECORD_V11=1 runs the whole package on the draft v1.1 store // format (the lab build's mode); it selects a format, it turns no check // off. @@ -28,7 +34,7 @@ func TestMain(m *testing.M) { mu.Lock() defer mu.Unlock() if len(found) > 0 { - fmt.Fprintf(os.Stderr, "FAIL: %d chunk record shadow findings; first: %s\n", len(found), found[0]) + fmt.Fprintf(os.Stderr, "FAIL: %d chunk record shadow or candidate oracle findings; first: %s\n", len(found), found[0]) if code == 0 { code = 1 } diff --git a/seaweed-block/docs/status-schema.md b/seaweed-block/docs/status-schema.md index 41533450f0..3cbd6f3fed 100644 --- a/seaweed-block/docs/status-schema.md +++ b/seaweed-block/docs/status-schema.md @@ -273,6 +273,8 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`. | `Volumes[].Chunk.Stats.LastCut.SyncPhases.storeWaitNs` | int64 | waiting for the store lock | | `Volumes[].Chunk.Stats.LastCut.SyncPhases.groupCommitNs` | int64 | in the group commit that made the data durable | | `Volumes[].Chunk.Stats.LastCut.SyncPhases.completionWaitNs` | int64 | waiting for the sync leader role to validate the completion | +| `Volumes[].Chunk.Stats.LastCut.CandidateVisits` | int64 | CandidateVisits counts the chunk indices the barrier's candidate step visited (scanned entries and set members), the work a pause pays for. | +| `Volumes[].Chunk.Stats.LastCut.PreSyncNs` | int64 | PreSyncNs is the sync right before the barrier, outside the guest pause (never part of BarrierPauseNs): the pause's own sync then has only what came after it. | | `Volumes[].Chunk.Stats.LastCut.CandidateNs` | int64 | Disjoint non-payload intervals within barrier-to-settlement fixed cost. | | `Volumes[].Chunk.Stats.LastCut.WindowSetupNs` | int64 | (group of `CandidateNs`) Disjoint non-payload intervals within barrier-to-settlement fixed cost. | | `Volumes[].Chunk.Stats.LastCut.PublishPrepareNs` | int64 | (group of `CandidateNs`) Disjoint non-payload intervals within barrier-to-settlement fixed cost. |