storage: one page lock across a write-through and a rewind

pageMu is held across a write-through (an outstanding rewind's publication,
the page write, the cursor after it) and across a rewind (the caller's
rebuild in memory, now a callback of Rewind, and its publication), so no
page write is in flight while a rewind is made or unpublished.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-30 07:42:37 -08:00
1 parent 20a3d21177
commit 694e473691
3 files changed
+39 -17

No files matched your search

@@ -111,6 +111,13 @@ const noRewind = ^uint64(0)
// next open). A rebuilt page is written by the caller only after Rewind has
// returned nil.
//
// THE PAGE LOCK. pageMu is held across a write-through (the publication of an
// outstanding rewind, the page write, the cursor that follows it) and across
// a rewind (the caller's rebuild in memory, the rewind's publication). So no
// page write is in flight while a rewind is being made or is unpublished:
// a page write never carries a rebuilt page's unknown chunks under a cursor
// past them.
//
// Every field after construction is guarded by mu.
type upgradePass struct {
dir string
@@ -123,6 +130,8 @@ type upgradePass struct {
interval time.Duration
persistEvery uint64
pageMu sync.Mutex // see THE PAGE LOCK; taken before mu
mu sync.Mutex
cur upgradePassCursor // the persisted cursor
next uint64 // the next chunk to compare, in memory
@@ -176,11 +185,18 @@ func (p *upgradePass) Hurry() func() {
}
// Rewind moves the pass back to chunk (the first chunk of a page rebuilt
// after damage). Admission closes and the rewind is recorded as outstanding
// before the cursor is written; a failed write keeps it outstanding, so every
// later write (and every page write) honours it. The caller writes the
// rebuilt page only after Rewind returns nil.
func (p *upgradePass) Rewind(chunk uint64) error {
// after damage). It waits for a page write in flight, then runs rebuild (the
// caller's rebuild of the page in memory; nil for none) under the page lock.
// Admission closes and the rewind is recorded as outstanding before the
// cursor is written; a failed write keeps it outstanding, so every later
// write (and every page write) honours it. The caller writes the rebuilt page
// only after Rewind returns nil.
func (p *upgradePass) Rewind(chunk uint64, rebuild func()) error {
p.pageMu.Lock()
defer p.pageMu.Unlock()
if rebuild != nil {
rebuild()
}
p.mu.Lock()
defer p.mu.Unlock()
if chunk >= p.next && chunk >= p.cur.Next && p.rew == noRewind {
@@ -222,6 +238,8 @@ func (p *upgradePass) publishLocked(addPending bool) error {
// advance writes through the walk position and then the cursor. An
// outstanding rewind is published first; until it is, no page is written.
func (p *upgradePass) advance() error {
p.pageMu.Lock()
defer p.pageMu.Unlock()
p.mu.Lock()
if p.rew != noRewind {
if err := p.publishLocked(false); err != nil {
@@ -208,7 +208,7 @@ func TestUpgradePassRewindAfterARebuiltPage(t *testing.T) {
delete(v.durable, c)
}
v.mu.Unlock()
if err := p.Rewind(40); err != nil {
if err := p.Rewind(40, nil); err != nil {
t.Fatal(err)
}
if c, _ := readUpgradePassCursor(dir, 7); c.Next != 40 {
@@ -281,7 +281,7 @@ func TestUpgradePassFailedRewindKeepsAdmissionClosed(t *testing.T) {
t.Fatal(err)
}
p.dir = blocked // the cursor write fails
if err := p.Rewind(40); err == nil {
if err := p.Rewind(40, nil); err == nil {
t.Fatal("the control needs a failed cursor write")
}
if err := p.Admit(); err == nil {
@@ -335,7 +335,7 @@ func TestUpgradePassOneRunAndRewindDuringACompare(t *testing.T) {
delete(v.durable, c)
}
v.mu.Unlock()
if err := p.Rewind(20); err != nil {
if err := p.Rewind(20, nil); err != nil {
t.Fatal(err)
}
release.Do(func() { close(resume) })
@@ -373,11 +373,11 @@ func TestUpgradePassAFailedRewindIsKeptByTheNext(t *testing.T) {
t.Fatal(err)
}
p.dir = blocked
if err := p.Rewind(20); err == nil {
if err := p.Rewind(20, nil); err == nil {
t.Fatal("the control needs a failed cursor write")
}
p.dir = dir
if err := p.Rewind(60); err != nil {
if err := p.Rewind(60, nil); err != nil {
t.Fatal(err)
}
c, err := readUpgradePassCursor(dir, 7)
@@ -513,13 +513,17 @@ func TestUpgradePassCursorRuleUnderInterleavings(t *testing.T) {
case 2, 3: // a page is damaged; the rebuild rewinds, then writes the page only on success
pg := uint64(rng.Intn(chunks / passPage))
for c := pg * passPage; c < (pg+1)*passPage; c++ {
v.known[c], v.durable[c], v.damaged[c] = false, false, true
v.durable[c], v.damaged[c] = false, true // the page is lost on disk
}
fail := rng.Intn(3) == 0
if fail {
p.dir = blocked
}
err := p.Rewind(pg * passPage)
err := p.Rewind(pg*passPage, func() {
for c := pg * passPage; c < (pg+1)*passPage; c++ {
v.known[c] = false // rebuilt in memory: unknown
}
})
p.dir = dir
if err == nil {
for c := pg * passPage; c < (pg+1)*passPage; c++ {
@@ -534,7 +538,7 @@ func TestUpgradePassCursorRuleUnderInterleavings(t *testing.T) {
p = open()
for pg := uint64(0); pg < chunks/passPage; pg++ {
if v.damaged[pg*passPage] {
if err := p.Rewind(pg * passPage); err == nil {
if err := p.Rewind(pg*passPage, nil); err == nil {
for c := pg * passPage; c < (pg+1)*passPage; c++ {
v.damaged[c] = false
}
@@ -582,7 +586,7 @@ func TestUpgradePassRewindWritesNoCursorPastTheWrittenPages(t *testing.T) {
delete(v.durable, c)
}
p.dir = blocked
if err := p.Rewind(20); err == nil {
if err := p.Rewind(20, nil); err == nil {
t.Fatal("the control needs a failed cursor write")
}
p.dir = dir
@@ -596,7 +600,7 @@ func TestUpgradePassRewindWritesNoCursorPastTheWrittenPages(t *testing.T) {
return v.compare(c, ch)
}
_ = p.Run(ctx)
if err := p.Rewind(60); err != nil {
if err := p.Rewind(60, nil); err != nil {
t.Fatal(err)
}
c, err := readUpgradePassCursor(dir, 7)
@@ -649,7 +653,7 @@ func TestUpgradePassRewindWaitsForAPageWriteInFlight(t *testing.T) {
t.Fatal("the page write never started")
}
rewound := make(chan error, 1)
go func() { rewound <- p.Rewind(20) }()
go func() { rewound <- p.Rewind(20, nil) }()
select {
case err := <-rewound:
t.Fatalf("Rewind returned (%v) while a page write was in flight", err)
+1 -1
View File
@@ -615,7 +615,7 @@ The "Machine / transition" column refers to Part A. Notes under a table record w
| `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-CHUNK-RECORD-PAGES-CURRENT-001` | An unfinished chunk record is used, and its header written, only when every page passes its check and is valid through exactly the store's synced position after recovery (behind: missed writes; ahead: not durable); an unmarked store discards it; the header follows the two-slot fence. | M1 Opened (chunk record load, v1.1 upgrade) | `core/storage/chunk_record.go` `loadChunkRecord`, `completeChunkRecord` | `TestUnfinishedRecordPagesBehindTheStoreAreNotTrusted`, `TestUnfinishedRecordAfterAnOldCheckpointIsDiscarded`, `TestStalePagesAreNeverCompleted`, `TestUnfinishedPagesAheadOfTheStoreAreNotTrusted`, `TestCompleteRecordAheadOfTheStoreIsNotTrusted`, `TestCompleteRecordPagePositions`, `TestRecordHeaderWaitsForTheFence`, `TestRecordHeaderIsNotWrittenAheadOfTheSyncedStore`, `TestDamagedPage` (`core/storage/chunk_record_test.go`) | SlotFence `PagesCurrent`, `FenceHolds`; pages carry chunk-kernel/2 states (the per-chunk state models are not in this repository) | STUB |
| `INV-CHUNK-UPGRADE-PASS-CURSOR-001` | The upgrade pass's persisted cursor is only ever min(the earliest unpublished rewind, the last written-through position), and no page is written while a rewind below it is unpublished; a restart resumes there; while the pass runs a request needing every hash answers in_progress. | M1 Opened (v1.1 upgrade compare pass) | `core/storage/chunk_upgrade_pass.go` `upgradePass.Run`, `advance`, `Rewind`, `Admit` | `TestUpgradePassCursorFollowsThePagesBehindIt`, `TestUpgradePassRestartContinuesAtTheCursor`, `TestUpgradePassRewindAfterARebuiltPage`, `TestUpgradePassRefusesWithProgress`, `TestUpgradePassPauseAndHurry`, `TestUpgradePassFailedRewindKeepsAdmissionClosed`, `TestUpgradePassAFailedRewindIsKeptByTheNext`, `TestUpgradePassOneRunAndRewindDuringACompare`, `TestUpgradePassKeepsItsCountAcrossRuns`, `TestUpgradePassCursorRuleUnderInterleavings`, `TestUpgradePassRewindWritesNoCursorPastTheWrittenPages` (`core/storage/chunk_upgrade_pass_test.go`) | UpgradePass `BehindCursorKnown`, `NoWorkTwice` (the per-chunk state models are not in this repository) | STUB |
| `INV-CHUNK-UPGRADE-PASS-CURSOR-001` | The upgrade pass's persisted cursor is only ever min(the earliest unpublished rewind, the last written-through position), and no page is written while a rewind below it is unpublished; a restart resumes there; while the pass runs a request needing every hash answers in_progress. | M1 Opened (v1.1 upgrade compare pass) | `core/storage/chunk_upgrade_pass.go` `upgradePass.Run`, `advance`, `Rewind`, `Admit` | `TestUpgradePassCursorFollowsThePagesBehindIt`, `TestUpgradePassRestartContinuesAtTheCursor`, `TestUpgradePassRewindAfterARebuiltPage`, `TestUpgradePassRefusesWithProgress`, `TestUpgradePassPauseAndHurry`, `TestUpgradePassFailedRewindKeepsAdmissionClosed`, `TestUpgradePassAFailedRewindIsKeptByTheNext`, `TestUpgradePassOneRunAndRewindDuringACompare`, `TestUpgradePassKeepsItsCountAcrossRuns`, `TestUpgradePassCursorRuleUnderInterleavings`, `TestUpgradePassRewindWritesNoCursorPastTheWrittenPages`, `TestUpgradePassRewindWaitsForAPageWriteInFlight` (`core/storage/chunk_upgrade_pass_test.go`) | UpgradePass `BehindCursorKnown`, `NoWorkTwice` (the per-chunk state models are not in this repository) | STUB |
| `INV-CHUNK-RECORD-PAYLOAD-001` | Persisting and loading a chunk record preserves every entry's state and hash, including entries on either side of a segment boundary. | M1 chunk record codec (caller not wired) | `core/storage/chunk_record.go` `encodeChunkRecordPage`, `decodeChunkRecordPage` | `TestRecordEntryPayloadRoundTripsAcrossSegments` (`core/storage/chunk_record_test.go`; state-zero and one-entry hash-drop controls) | Encoding fidelity; no state transition change | 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 |
| `INV-WAL-V3-DESCRIPTOR-CONFLICT-001` | Open is refused when both v3 descriptor slots are valid but carry the same slotGen with different bytes, or disagree on the UUID or the geometry. | M1 Opened (v3 descriptor selection) | `core/storage/superblock_v3.go` `selectSlotV3`, `sameGeometryV3` | `TestSlotV3Selection` (`core/storage/superblock_v3_test.go`) | — | ACTIVE |