fix: share equal-to-HEAD proof across staging and cut retries

This commit is contained in:
pingqiu committed 2026-11-29 16:03:39 -08:00
1 parent 54960b59ea
commit 1369da1694
3 files changed
+22 -5

No files matched your search

+17 -4
View File
@@ -486,8 +486,8 @@ func (l *chunkLayerImpl) captureCandidates() map[uint64]bool {
}
}
filled := map[uint64]bool{}
for idx, m := range l.filledClean {
if l.chunkVersion(idx) == m.version && l.head.Chunks[idx] == m.ref {
for idx := range l.filledClean {
if l.knownEqualToHeadLocked(idx) {
filled[idx] = true
}
}
@@ -513,6 +513,14 @@ type filledMark struct {
ref chunkRef
}
// knownEqualToHeadLocked holds only while both the local mutation version
// and HEAD's reference match the proof. Own batches still upload equal bytes.
// Caller holds stMu.
func (l *chunkLayerImpl) knownEqualToHeadLocked(idx uint64) bool {
m, ok := l.filledClean[idx]
return ok && !l.own[idx] && l.chunkVersion(idx) == m.version && l.head.Chunks[idx] == m.ref
}
// chunkVersion is chunk idx's mutation count.
func (l *chunkLayerImpl) chunkVersion(idx uint64) uint64 {
return l.chunkMut[idx].Load()
@@ -993,7 +1001,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
// its version, and a newer HEAD changes its ref. A chunk an own batch
// re-uploads under this layer's key stays: equal bytes are its point.
for idx := range candidates {
if m, ok := l.filledClean[idx]; ok && !l.own[idx] && l.chunkVersion(idx) == m.version && l.head.Chunks[idx] == m.ref {
if l.knownEqualToHeadLocked(idx) {
delete(candidates, idx)
}
}
@@ -1735,6 +1743,9 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats, spillCap
l.stMu.Lock()
need := map[uint64]bool{}
consider := func(idx uint64) {
if l.knownEqualToHeadLocked(idx) {
return
}
if sc, ok := staged[idx]; ok && sc.ver == l.chunkVersion(idx) && sc.headGen == l.head.Gen {
return // still current
}
@@ -1813,7 +1824,9 @@ func (l *chunkLayerImpl) requeueCut(all bool) {
l.stMu.Lock()
defer l.stMu.Unlock()
for idx := range l.inflight {
l.dirty[idx] = true
if !l.knownEqualToHeadLocked(idx) {
l.dirty[idx] = true
}
}
l.inflight = map[uint64]bool{}
if all {
@@ -26,10 +26,14 @@ func TestRefusedFullCutStagesOnlyTheUnmarkedChunk(t *testing.T) {
for idx := range l.filledClean {
marked[idx] = true
}
requeued := len(l.dirty)
l.stMu.Unlock()
if len(marked) != ctNumBlocks/ctChunkBlocks-1 {
t.Fatalf("kept %d equal marks, want %d", len(marked), ctNumBlocks/ctChunkBlocks-1)
}
if requeued != 1 {
t.Fatalf("refused cut requeued %d chunks, want only the unmarked chunk", requeued)
}
// 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 }
+1 -1
View File
@@ -602,7 +602,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w
| `INV-WAL-V3-FLUSH-PROFILE-001` | Native flusher header/full-record/decode counters describe actual reads and validation; I/O failure is distinct from validation failure and only successfully materialized blocks count validated. | M1 checkpoint instrumentation | `walstore_v3.go` `readWholeRecordProfiledV3`, `readDirtyRecordV3` | `TestFlusherV3ProfileCountsRealRecordStages` | Observability; no durability transition change | ACTIVE |
| `INV-WAL-V3-WRITE-AHEAD-001` | No extent byte reaches the disk before its WAL record is durable: the flusher writes an extent only from dirty entries at or below the synced (acknowledged) WAL frontier; Recover and the install log fsync their records before raising it. | M1 Serving (flusher extent write) | `core/storage/flusher.go` `flushOnceInternal`; `walstore_v3.go` `recoverV3` (descriptor publish fsync before `wack`); `install_log_v3.go` | `TestRedCrashKeepingAnUnsyncedExtentWriteLeavesLocalAndHEADApart` (`core/storage/chunk_u1_reorder_test.go`); `TestFlusherWriteAheadNeverFlushingGuestReachesExtentAtPressure` (`core/storage/flusher_write_ahead_test.go`) | A checkpoint may pass a synced record superseded by an unsynced newer one only because the cycle's sync covers the whole file, WAL included (one file, one descriptor: `INV-WAL-FLUSH-SYNC-COVERS-WAL-001`); separating WAL and extent, or a range sync, must first change the checkpoint bound | ACTIVE |
| `INV-WAL-V3-CAPTURE-CANDIDATES-001` | A retained WAL entry already covered by HEAD is not dirt since HEAD. New post-HEAD WAL entries, UNKNOWN map chunks and map max-LSN above HEAD remain capture candidates, except as `INV-WAL-V3-CAPTURE-COMPARED-001`. | M5 candidate selection | `chunk_layer.go` `captureCandidates` | `TestChunkLayerV3CapturedUncheckpointedWALIsCleanUntilRewritten`, `TestChunkLayerV3_CaptureUsesOwnedMapAfterCheckpoint`, `TestBaseInstallV3CompletePersistsTheMapUnknownOfBaseWrites` | Conservative reachability unchanged; narrower redundant work | ACTIVE |
| `INV-WAL-V3-CAPTURE-COMPARED-001` | A chunk a cut compared equal to HEAD (marked at its compare, kept by a cut out of time) and did not publish, or filled cleanly, is not compared again, whatever made it a candidate, while it has no mutation since and HEAD still names that ref; a chunk an own batch re-uploads stays a candidate. | M5 candidate selection | `chunk_layer.go` `capture`, `fillOnce`; `chunk_cut_payload.go` `markEqualNow` | `TestFreshFromHeadReadBackLeavesNoCutCandidates`, `TestFreshFromHeadFilledThenWrittenIsACandidate`, `TestEveryLocalWriteAfterACleanFillMakesTheChunkACandidate`, `TestABaseInstallAfterCleanFillsEndsTheirRecords`, `TestACleanFillRecordEndsWithItsHeadRef`, `TestARestoreTargetThatOnlyReadCutsNothing`, `TestARerunAfterAFailedCutComparesOnlyWhatDiffers`, `TestWithoutAFailedCutEveryUnknownChunkIsCompared`, `TestACutOffCompareKeepsItsWorkAndTheRerunComparesOnlyTheRest`, `TestFilledChunksAreFirstCutCandidatesAfterARestart`, `TestEveryDirtyMapInsertNotesTheChunkMutation`, `TestOwnBatch_BoundedBatchesReplaceEveryForeignKey` | Conservative reachability unchanged; narrower redundant work | ACTIVE |
| `INV-WAL-V3-CAPTURE-COMPARED-001` | A chunk known equal to HEAD is neither staged nor compared again while its mutation version and HEAD ref match; own-batch chunks remain candidates. A refused cut retains these proofs and requeues only unproven chunks. All candidate paths use one predicate. | M5 candidate selection | `core/storage/chunk_layer.go` `knownEqualToHeadLocked`, `captureCandidates`, `captureLocked`, `stage`, `requeueCut`; `chunk_cut_payload.go` `markEqualNow` | `TestFreshFromHeadReadBackLeavesNoCutCandidates`, `TestFreshFromHeadFilledThenWrittenIsACandidate`, `TestEveryLocalWriteAfterACleanFillMakesTheChunkACandidate`, `TestABaseInstallAfterCleanFillsEndsTheirRecords`, `TestACleanFillRecordEndsWithItsHeadRef`, `TestARestoreTargetThatOnlyReadCutsNothing`, `TestARerunAfterAFailedCutComparesOnlyWhatDiffers`, `TestWithoutAFailedCutEveryUnknownChunkIsCompared`, `TestACutOffCompareKeepsItsWorkAndTheRerunComparesOnlyTheRest`, `TestFilledChunksAreFirstCutCandidatesAfterARestart`, `TestEveryDirtyMapInsertNotesTheChunkMutation`, `TestOwnBatch_BoundedBatchesReplaceEveryForeignKey`; `TestRefusedFullCutStagesOnlyTheUnmarkedChunk` (`core/storage/chunk_staging_equal_test.go`) | Conservative reachability unchanged; narrower redundant work | ACTIVE |
| `INV-WAL-V3-DESCRIPTOR-001` | A v3 descriptor slot is used only if it is valid: a matching CRC32C (Castagnoli), every reserved byte zero, an accepted impl, a nonzero slotGen at most 2^64-2, a nonzero nonce, and geometry consistent under checked arithmetic. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `decodeSlotV3`, `descriptorV3.validate`, `checkedAddV3` | `TestSlotV3VectorsDecodeAsExpected`, `TestSlotV3CounterExhaustion` (`core/storage/superblock_v3_test.go`) | — | STUB |
| `INV-WAL-V3-DESCRIPTOR-BASE-001` | A valid v3 descriptor slot has CheckpointLSN ≤ mapCoveredCheckpoint ≤ W, and BASE fields that follow the rules of its BASE state. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `descriptorV3.validate` | `TestSlotV3VectorsDecodeAsExpected`, `TestSlotV3InstalledMayHaveCursorBelowTail` (`core/storage/superblock_v3_test.go`) | — | STUB |
| `INV-WAL-V3-DESCRIPTOR-SELECT-001` | Of the two v3 descriptor slots, open selects the valid selectable slot with the higher slotGen, and never a prepared migration slot. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `selectSlotV3` | `TestSlotV3Selection` (`core/storage/superblock_v3_test.go`); `TestWALStoreV3CloseKeepsTheNewestSlotAAndItsW` (`core/storage/walstore_close_v3_test.go`) | — | ACTIVE |