From 9ccda0ead6c490a9523a84dc68c04818b993fde2 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 30 Nov 2026 07:19:10 -0800 Subject: [PATCH] storage: one walker, rewind closes admission first, counts kept on the pass Run refuses a second walker. A rewind moves a generation: a compare begun before it does not move the walk position past the rewound chunk, and a persist begun before it keeps the lower cursor. Rewind closes admission and moves the walk position before writing the cursor, so a failed write leaves the pass refusing and the next Run writes the lower cursor. Work counted since the last persisted cursor lives on the pass, so a cancelled or failed Run's count is kept by the next one. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_upgrade_pass.go | 122 ++++++++++++------ seaweed-block/docs/design/invariants.md | 2 +- 2 files changed, 80 insertions(+), 44 deletions(-) diff --git a/seaweed-block/core/storage/chunk_upgrade_pass.go b/seaweed-block/core/storage/chunk_upgrade_pass.go index efe30840a5..6548a62523 100644 --- a/seaweed-block/core/storage/chunk_upgrade_pass.go +++ b/seaweed-block/core/storage/chunk_upgrade_pass.go @@ -93,7 +93,13 @@ func readUpgradePassCursor(dir string, epoch uint64) (upgradePassCursor, error) return c, nil } -// upgradePass runs the pass. Its fields after construction are guarded by mu. +// ErrUpgradePassRunning refuses a second walker: one Run at a time. +var ErrUpgradePassRunning = errors.New("storage: upgrade pass: already running") + +// upgradePass runs the pass. One Run at a time walks the chunks; Rewind, +// Admit and Hurry may be called from anywhere. After a Rewind while no Run is +// active, the pass's owner starts Run again: Admit refuses until it ends. +// Every field after construction is guarded by mu. type upgradePass struct { dir string chunks uint64 @@ -108,9 +114,12 @@ type upgradePass struct { mu sync.Mutex cur upgradePassCursor // the persisted cursor next uint64 // the next chunk to compare, in memory + pending uint64 // chunks compared since the persisted cursor (work, not position) + since uint64 // chunks walked since the persisted cursor + gen uint64 // moves on every rewind: a compare begun before it does not move next done bool + running bool hurry int // requests waiting: the pass runs at full rate - rewound bool } func newUpgradePass(dir string, epoch, chunks uint64, pages upgradePassPages, compare upgradePassCompare) (*upgradePass, error) { @@ -142,31 +151,40 @@ func (p *upgradePass) Hurry() func() { p.mu.Lock() p.hurry++ p.mu.Unlock() + var once sync.Once return func() { - p.mu.Lock() - p.hurry-- - p.mu.Unlock() + once.Do(func() { + p.mu.Lock() + p.hurry-- + p.mu.Unlock() + }) } } // Rewind moves the cursor back to chunk (the first chunk of a page rebuilt -// after damage) and persists it before returning: a rebuilt chunk is unknown -// again, and a cursor past it would never compare it. +// after damage): a rebuilt chunk is unknown again, and a cursor past it would +// never compare it. Admission closes and the walk position moves back BEFORE +// the cursor is written, so a failed write leaves the pass refusing and the +// next Run compares from chunk; that Run's next persist writes the lower +// cursor. func (p *upgradePass) Rewind(chunk uint64) error { p.mu.Lock() defer p.mu.Unlock() if chunk >= p.next && chunk >= p.cur.Next { return nil } + p.done = false + p.gen++ + if chunk < p.next { + p.next = chunk + } + p.since = 0 + if chunk >= p.cur.Next { + return nil + } c := p.cur - if chunk < c.Next { - c.Next = chunk - } - if err := p.persistLocked(c); err != nil { - return err - } - p.next, p.done, p.rewound = c.Next, false, true - return nil + c.Next = chunk + return p.persistLocked(c) } func (p *upgradePass) persistLocked(c upgradePassCursor) error { @@ -177,50 +195,68 @@ func (p *upgradePass) persistLocked(c upgradePassCursor) error { return nil } -// advance persists the cursor at end: the pages of every chunk below end -// first, then the cursor. -func (p *upgradePass) advance(end, compared uint64) error { +// advance persists the cursor at the walk position: the pages of every chunk +// below it first, then the cursor with the work counted since the last one. +// A rewind between the two keeps the lower cursor it wrote. +func (p *upgradePass) advance() error { + p.mu.Lock() + end, gen := p.next, p.gen + p.mu.Unlock() if err := p.pages.WriteThrough(end); err != nil { return err } p.mu.Lock() defer p.mu.Unlock() - if p.rewound { // a rewind persisted a lower cursor meanwhile + if p.gen != gen { return nil } c := p.cur - c.Next, c.Compared = end, c.Compared+compared - return p.persistLocked(c) + c.Next, c.Compared = end, c.Compared+p.pending + if err := p.persistLocked(c); err != nil { + return err + } + p.pending, p.since = 0, 0 + return nil } -// Run walks the chunks from the cursor to the end. It returns when the pass -// has ended, ctx ends, or a compare or a write fails; a later Run continues. +// Run walks the chunks from the walk position to the end. It returns when the +// pass has ended, ctx ends, or a compare or a write fails; a later Run on the +// same pass continues with the work it counted. A second Run while one walks +// is refused. func (p *upgradePass) Run(ctx context.Context) error { - var compared, since uint64 + p.mu.Lock() + if p.running { + p.mu.Unlock() + return ErrUpgradePassRunning + } + p.running = true + p.mu.Unlock() + defer func() { + p.mu.Lock() + p.running = false + p.mu.Unlock() + }() for { p.mu.Lock() - if p.rewound { - p.rewound, compared, since = false, 0, 0 - } - i, pause := p.next, p.interval + i, gen, pause := p.next, p.gen, p.interval if p.hurry > 0 { pause = 0 } p.mu.Unlock() if i >= p.chunks { - if err := p.advance(p.chunks, compared); err != nil { + if err := p.advance(); err != nil { return err } p.mu.Lock() - if !p.rewound { + end := p.gen == gen && p.next >= p.chunks + if end { p.done = true } - again := p.rewound p.mu.Unlock() - if again { - continue + if end { + return nil } - return nil + continue } if err := ctx.Err(); err != nil { return err @@ -229,20 +265,20 @@ func (p *upgradePass) Run(ctx context.Context) error { if err != nil { return err } - if did { - compared++ - } - since++ p.mu.Lock() - if !p.rewound { - p.next = i + 1 + if did { + p.pending++ } + if p.gen == gen { + p.next = i + 1 + p.since++ + } + persist := p.since >= p.persistEvery p.mu.Unlock() - if since >= p.persistEvery { - if err := p.advance(i+1, compared); err != nil { + if persist { + if err := p.advance(); err != nil { return err } - compared, since = 0, 0 } if pause > 0 { t := time.NewTimer(pause) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 9a6f1eb06e..c0b9c64291 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -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's compare pass persists its cursor only after the pages of every chunk behind it, resumes at the persisted cursor after a restart, moves it back to a rebuilt page's first chunk, and refuses a request needing every hash known with in_progress and its progress. | M1 Opened (v1.1 upgrade compare pass) | `core/storage/chunk_upgrade_pass.go` `upgradePass.Run`, `advance`, `Rewind`, `Admit` | `TestUpgradePassCursorFollowsThePagesBehindIt`, `TestUpgradePassRestartContinuesAtTheCursor`, `TestUpgradePassRewindAfterARebuiltPage`, `TestUpgradePassRefusesWithProgress`, `TestUpgradePassHurry` (`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's compare pass persists its cursor only after the pages of every chunk behind it, resumes at the persisted cursor after a restart, moves it back to a rebuilt page's first chunk, and refuses a request needing every hash known with in_progress and its 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`, `TestUpgradePassOneRunAndRewindDuringACompare`, `TestUpgradePassKeepsItsCountAcrossRuns` (`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 |