From 1add3afcd04160ebe45f9142f78f8a376dda7ee7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 20:13:14 -0800 Subject: [PATCH] storage: the barrier only swaps the touched sets; a late recovery is a flag - prepareTouched, before the barrier and outside the pause, runs the once-per-layer seed, a pending recovery's clean-fill touches, and makes the cleared spare set; takeTouched at the barrier is a pointer swap. - A recovery that lands between prepareTouched and the barrier is taken at the barrier as a flag (O(1)): the window covers every chunk until it narrows, and the clean fills are judged after the pause, with the touched set's members. A refusal before the judgement keeps the request. Co-Authored-By: Claude Opus 5.5 --- seaweed-block/core/storage/chunk_layer.go | 47 +++++++++++++++++++---- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 231632af8d..0bc9d14782 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -793,18 +793,30 @@ func (l *chunkLayerImpl) peekTouched() []uint64 { // cleared spare in its place, a pointer swap: a chunk marked after the take // is the next cut's. The caller gives the set back (returnTouched). func (l *chunkLayerImpl) takeTouched() *candidateSet { - l.touchedSeed.Do(l.seedTouched) - l.resolveTouchFilled() l.unkMu.Lock() defer l.unkMu.Unlock() taken := l.touchedLocked() if l.touchedSpare == nil { - l.touchedSpare = l.newTouchedSet() + l.touchedSpare = l.newTouchedSet() // only if prepareTouched was skipped } l.touched, l.touchedSpare = l.touchedSpare, nil return taken } +// prepareTouched readies the touched sets before a barrier, outside the +// pause: the once-per-layer seed, a pending recovery's clean-fill touches, +// and the cleared spare the barrier swaps in. The barrier then only swaps. +func (l *chunkLayerImpl) prepareTouched() { + l.touchedSeed.Do(l.seedTouched) + l.resolveTouchFilled() + l.unkMu.Lock() + l.touchedLocked() + if l.touchedSpare == nil { + l.touchedSpare = l.newTouchedSet() + } + l.unkMu.Unlock() +} + // returnTouched clears a taken set (O(members)) and keeps it as the spare. func (l *chunkLayerImpl) returnTouched(set *candidateSet) { set.clear() @@ -1574,8 +1586,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch // Before the pause: the once-per-layer UNKNOWN seed, and one sync so the // pause's own sync flushes only what came after it (the first cut after a // fill pays its write-back here). The in-pause sync still fixes H. - l.touchedSeed.Do(l.seedTouched) - l.resolveTouchFilled() + l.prepareTouched() preSync := time.Now() if _, err := l.s.syncWithPhases(nil); err != nil { st.PreSyncNs = time.Since(preSync).Nanoseconds() @@ -1642,6 +1653,10 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch // pause, under the window: the judgement, the versions (atomic with the // write path), staged reuse, the base refs and the copy-order hint. taken := l.takeTouched() + // A recovery that landed after prepareTouched moved every clean fill's + // version: its request is taken here as a flag (O(1)); the window covers + // every chunk until it narrows, and the fills are judged after the pause. + filledTaken := l.touchFilled.Swap(false) oracleWant := l.candidateOracleScan() // tests only (nil in production) l.stMu.Lock() dirtyTaken, all := l.dirty, l.allDirty @@ -1649,7 +1664,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch l.cutLSN = h l.stMu.Unlock() own := l.own // stable for the cut (set and cleared under capMu) - covers := func(idx uint64) bool { return all || taken.has(idx) || dirtyTaken[idx] || own[idx] } + covers := func(idx uint64) bool { return all || filledTaken || taken.has(idx) || dirtyTaken[idx] || own[idx] } windowStart := time.Now() w, installErr := l.installCOWWindow(covers, spill) if installErr == nil { @@ -1679,6 +1694,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } if installErr != nil { releaseDrain() + if filledTaken { + l.touchFilled.Store(true) + } l.requeueTaken(taken, dirtyTaken, all) return chunkCutResult{}, fmt.Errorf("storage: capture copy: %w", installErr) } @@ -1686,7 +1704,22 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch // After the pause. The judgement: the touched set's members with the // map scan's predicate, the WAL entries up to H, the own batch, the // taken dirty set, and every chunk after a BASE write. - candidates, visits := l.candidatesFrom(taken.each, h) + members := taken.each + if filledTaken { + l.stMu.Lock() + filled := make([]uint64, 0, len(l.filledClean)) + for idx := range l.filledClean { + filled = append(filled, idx) + } + l.stMu.Unlock() + members = func(f func(uint64)) { + taken.each(f) + for _, idx := range filled { + f(idx) + } + } + } + candidates, visits := l.candidatesFrom(members, h) visits += int64(len(taken.summary)) // the summary words the walk read l.pauseOp(int(visits)) // each step counts itself if it ever runs in the pause for idx := range dirtyTaken {