fix: the flusher writes an extent only from a synced WAL record

Page writeback has no order across offsets of one file, so an extent
written from a WAL record that is not yet durable could survive a power
loss that loses the record: extent bytes no recovered record or map
entry accounts for, under a chunk certified equal to HEAD. The flusher
now skips dirty entries above the synced (acknowledged) WAL frontier;
they stay dirty until a Sync or the pressure sync makes them durable,
and the checkpoint is computed from the written entries only (it was
already clamped to that frontier). Recover and the install log already
fsync their records before raising the frontier.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-23 21:26:23 -08:00
1 parent de0dfd0575
commit c921de54e0
4 files changed
+26 -16

No files matched your search

@@ -126,10 +126,10 @@ func TestCheckpointDiagnostics_LogRateLimit(t *testing.T) {
}
}
// A starved checkpoint: flush cycles keep succeeding (clamped to the
// acknowledged frontier, nothing persisted moves) while the persisted
// CheckpointLSN and the last advance stay frozen. Only a real move of the
// persisted checkpoint counts as an advance.
// A starved checkpoint: writes that are never synced are never flushed
// (INV-WAL-V3-WRITE-AHEAD-001), so flush cycles move nothing: the persisted
// CheckpointLSN, the success count and the last advance stay frozen. Only a
// real move of the persisted checkpoint counts as an advance.
func TestCheckpointDiagnostics_StarvedCheckpointIsVisible(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
mustAttach(t, s, newFakeChunkRemote(), ctConfig(1))
@@ -150,8 +150,8 @@ func TestCheckpointDiagnostics_StarvedCheckpointIsVisible(t *testing.T) {
}
}
starved := s.CheckpointPersistence()
if starved.Successes <= first.Successes {
t.Fatalf("flush cycles did not keep succeeding: %+v", starved)
if starved.Successes != first.Successes {
t.Fatalf("flush cycles over unsynced writes made a checkpoint call: first %+v starved %+v", first, starved)
}
if starved.CheckpointLSN != 1 || starved.Advances != 1 || !starved.LastAdvance.Equal(first.LastAdvance) {
t.Fatalf("a clamped success counted as an advance: first %+v starved %+v", first, starved)
@@ -8,15 +8,14 @@ import (
"testing"
)
// The flusher writes a dirty block's extent before the WAL record behind it
// is synced (below the WAL pressure threshold it does not sync first), and
// page writeback has no order across offsets of one file. A crash can then
// keep the extent bytes of a write whose WAL record is lost (an unacked
// write). After recovery the chunk's map entry has not moved, so the chunk
// is not a cut candidate: local reads return the lost write's bytes while
// the published HEAD holds the chunk without them, and no cut compares the
// two. Local and published must agree for every block of a chunk that is
// not a candidate.
// Page writeback has no order across offsets of one file, so an extent
// written from a WAL record that is not yet durable could survive a power
// loss that loses the record (an unacked write). After recovery the chunk's
// map entry would not have moved, the chunk would not be a cut candidate,
// and local reads would return the lost write's bytes while the published
// HEAD holds the chunk without them. The flusher therefore writes extents
// only from synced records (INV-WAL-V3-WRITE-AHEAD-001). Local and published
// must agree for every block of a chunk that is not a candidate.
//
// The crash is injected, not raced: the real flusher runs and is stopped at
// its fsync; the store file after the crash is the file as it was before the
@@ -24,6 +23,7 @@ import (
// only that page had reached the disk. A flusher that writes extents only
// from synced records leaves the page unchanged and passes.
func TestRedCrashKeepingAnUnsyncedExtentWriteLeavesLocalAndHEADApart(t *testing.T) {
// The name keeps its red-first form so the history and the row match.
ctx := context.Background()
s, path, crash := crashableStore(t)
remote := NewMemChunkRemote()
+9
View File
@@ -183,6 +183,12 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
recoveryGen = store.v3.recoverGen.Load()
entries := store.dm.snapshot()
// Write-ahead (INV-WAL-V3-WRITE-AHEAD-001): only entries whose WAL
// record is durable reach the extent. An entry above the synced WAL
// frontier stays dirty for a later cycle: written now, a power loss
// could keep its extent page and lose its record, leaving extent bytes
// that no recovered record or map entry accounts for.
synced := store.v3.wack.Load()
store.mu.RUnlock()
if len(entries) == 0 {
@@ -218,6 +224,9 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
recordCache := &flushRecordCacheV3{recoveryGen: recoveryGen}
for _, e := range entries {
if e.LSN > synced {
continue
}
data, _, err := f.readDirtyRecordV3Cached(e, recordCache)
if err != nil {
return err
+2 -1
View File
@@ -497,7 +497,7 @@ Tests: `TestSnapshotSchedule_SlotsAreOffsetPerVolume`, `_FailureIsTypedAndBacksO
## Part B. Invariant ledger
321 rows: 250 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 306 new.
322 rows: 251 ACTIVE, 68 STUB, 3 ARCHIVED; 15 kept from the internal ledger, 307 new.
The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger.
@@ -529,6 +529,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w
| `INV-WAL-V3-ACTIVATION-PROVIDER-001` | The durable provider's capture, HEAD restore and status paths probe, open and create volumes through the public v3 selectors (`ProbeWALStoreV3`, `OpenWALStore`, `CreateWALStoreWithOptions`). | M1 open/create/readonly | `core/frontend/durable/provider.go` `openOrCreate` | `TestDurableProviderV3_HEADRestorePreservesRemoteImageAndNextLSN`, `TestDurableProviderV3_StatusTracksRecoverSyncAndCapture` (`core/frontend/durable/provider_format_v3_test.go`) | — | ACTIVE |
| `INV-WAL-V3-LEGACY-FIXTURE-ISOLATION-001` | No default-build production code calls a legacy fixture constructor, and no legacy fixture is in the transitive import closure of the production storage packages. | M1 open/create/readonly | build layout: legacy fixtures outside the default production import closure | `TestLegacyFixtureIsolation` (`core/storage/legacy_fixture_isolation_test.go`) | — | ACTIVE |
| `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`) | — | 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. | M5 candidate selection | `chunk_layer.go` `captureCandidates` | `TestChunkLayerV3CapturedUncheckpointedWALIsCleanUntilRewritten`, `TestChunkLayerV3_CaptureUsesOwnedMapAfterCheckpoint`, `TestBaseInstallV3CompletePersistsTheMapUnknownOfBaseWrites` | 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 |