storage: defer claimed chunks during flush and cap reclaim below skipped entries

This commit is contained in:
pingqiu committed 2026-11-19 20:16:35 -08:00
1 parent 69a1689ee4
commit 8cb3295c94
4 files changed
+88 -11

No files matched your search

+31 -11
View File
@@ -193,46 +193,54 @@ func (w *cutCOWWindow) status() error { w.mu.Lock(); defer w.mu.Unlock(); return
// A nil result without a notification means done/terminal. Waiters own a
// lifetime reference through wakeup/accounting, never an entry or I/O lock.
func (w *cutCOWWindow) claim(idx uint64, saving bool) (*cutCOWEntry, uint8, <-chan struct{}) {
func (w *cutCOWWindow) claim(idx uint64, saving bool) (*cutCOWEntry, uint8, <-chan struct{}, func()) {
w.mu.Lock()
defer w.mu.Unlock()
if w.terminal {
return nil, cowDone, nil
return nil, cowDone, nil, nil
}
e := w.entries[idx]
if e == nil {
return nil, cowDone, nil
return nil, cowDone, nil, nil
}
e.mu.Lock()
defer e.mu.Unlock()
state := e.state
switch state {
case cowPending:
gate := w.layer.s.chunkFlushGate(idx)
if !gate.TryRLock() {
w.terminal = true
w.err = fmt.Errorf("%w: local chunk flush in progress", errCutCOWAborted)
close(w.ended)
w.cancel()
return nil, cowDone, nil, nil
}
if saving {
e.state = cowSaving
} else {
e.state = cowCopying
}
w.refs.Add(1)
return e, state, nil
return e, state, nil, gate.RUnlock
case cowSaved:
if saving {
return nil, cowDone, nil
return nil, cowDone, nil, nil
} // live bytes are already saved
if e.readingSaved {
w.refs.Add(1)
return nil, state, e.ready
return nil, state, e.ready, nil
}
e.readingSaved = true
w.refs.Add(1)
return e, state, nil
return e, state, nil, nil
case cowSaving, cowCopying:
// Waiters acquire while OPEN too: drain includes their accounting
// before the coordinator snapshots aggregate wait time.
w.refs.Add(1)
return nil, state, e.ready
return nil, state, e.ready, nil
default:
return nil, cowDone, nil
return nil, cowDone, nil, nil
}
}
func (w *cutCOWWindow) finish(e *cutCOWEntry, state uint8) bool {
@@ -270,7 +278,7 @@ func (l *chunkLayerImpl) beforeCOWMutation(first, count uint64) {
last := (first + count - 1) / l.chunkBlocks
for idx := first / l.chunkBlocks; idx <= last; idx++ {
for {
e, _, wait := w.claim(idx, true)
e, _, wait, releaseFlush := w.claim(idx, true)
if wait != nil {
w.wait(wait)
continue
@@ -287,6 +295,9 @@ func (l *chunkLayerImpl) beforeCOWMutation(first, count uint64) {
acquired = true
default:
w.abort(fmt.Errorf("%w: saver memory capacity", errCutCOWAborted))
if releaseFlush != nil {
releaseFlush()
}
w.refs.Done()
}
if !acquired {
@@ -294,6 +305,9 @@ func (l *chunkLayerImpl) beforeCOWMutation(first, count uint64) {
}
if w.status() != nil {
<-w.saverSlots
if releaseFlush != nil {
releaseFlush()
}
w.refs.Done()
break
}
@@ -310,6 +324,9 @@ func (l *chunkLayerImpl) beforeCOWMutation(first, count uint64) {
}
w.saveNs.Add(time.Since(start).Nanoseconds())
// In particular an aborting saver releases itself BEFORE anyone joins.
if releaseFlush != nil {
releaseFlush()
}
w.refs.Done()
<-w.saverSlots
break
@@ -452,7 +469,7 @@ func (l *chunkLayerImpl) copyCOWEntry(w *cutCOWWindow, idx uint64) (capturedChun
w.abort(err)
return capturedChunk{}, false, err
}
e, state, wait := w.claim(idx, false)
e, state, wait, releaseFlush := w.claim(idx, false)
if wait != nil {
w.wait(wait)
continue
@@ -487,6 +504,9 @@ func (l *chunkLayerImpl) copyCOWEntry(w *cutCOWWindow, idx uint64) (capturedChun
if err == nil {
kept = w.finish(e, cowDone)
}
if releaseFlush != nil {
releaseFlush()
}
w.refs.Done()
if !kept {
return capturedChunk{}, false, w.status()
@@ -0,0 +1,49 @@
package storage
import "sync"
// A capture borrows one chunk's local image; a flusher borrows its durable
// transition. Both are nonblocking acquisitions: a busy local flush aborts
// the point instead of parking a guest write. Neither borrow spans remote I/O.
// Gates belong to the store so a concurrent first attach cannot bypass them.
func (s *WALStore) chunkFlushGate(idx uint64) *sync.RWMutex {
value, _ := s.chunkFlushGates.LoadOrStore(idx, new(sync.RWMutex))
return value.(*sync.RWMutex)
}
// Keep every skipped entry live. Other chunks still reach durable extents,
// but checkpoint/reclaim cannot cross the earliest skipped LSN. Locks are
// released only after extent fsync, descriptor publication and DM retirement.
func (s *WALStore) borrowFlushChunks(entries []snapshotEntry, blocks uint64) ([]snapshotEntry, uint64, func()) {
if blocks == 0 {
return entries, ^uint64(0), func() {}
}
borrowed := map[uint64]*sync.RWMutex{}
skipped := map[uint64]bool{}
for _, e := range entries {
idx := e.LBA / blocks
if borrowed[idx] != nil || skipped[idx] {
continue
}
gate := s.chunkFlushGate(idx)
if gate.TryLock() {
borrowed[idx] = gate
} else {
skipped[idx] = true
}
}
limit := ^uint64(0)
kept := make([]snapshotEntry, 0, len(entries))
for _, e := range entries {
if skipped[e.LBA/blocks] {
limit = min(limit, e.LSN-1)
continue
}
kept = append(kept, e)
}
return kept, limit, func() {
for _, gate := range borrowed {
gate.Unlock()
}
}
}
+7
View File
@@ -182,8 +182,12 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
// Bind materialization to the same Recover lifetime as this snapshot.
// Recover holds store.mu at both generation boundaries.
var recoveryGen uint64
var captureChunkBlocks uint64
if store.v3 != nil {
recoveryGen = store.v3.recoverGen.Load()
if store.v3.ownedMap != nil {
captureChunkBlocks = store.v3.ownedMap.chunkBlocks
}
}
entries := store.dm.snapshot()
store.mu.RUnlock()
@@ -193,6 +197,8 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
}
return nil
}
entries, checkpointLimit, releaseChunks := store.borrowFlushChunks(entries, captureChunkBlocks)
defer releaseChunks()
if f.afterSnapshotForTest != nil {
f.afterSnapshotForTest()
}
@@ -274,6 +280,7 @@ func (f *flusher) flushOnceInternal(allowClosed bool) (err error) {
// processed the entire consistent snapshot, every entry
// with LSN <= maxLSN is in extent or superseded by a retained newer
// WAL record. Concurrent writes remain dirty for the next cycle.
maxLSN = min(maxLSN, checkpointLimit)
if maxLSN > 0 {
// The released tail is persisted with the checkpoint, before the
// writer may reuse those bytes.
+1
View File
@@ -47,6 +47,7 @@ import (
// - Online resize
// - Compaction (the WAL is bounded but periodic checkpoints free space)
type WALStore struct {
chunkFlushGates sync.Map // chunk index -> *sync.RWMutex; local capture/flush only
// recoveryWarnings and holesSuspected: the last Recover's typed
// warnings, and how many recoveries found a WAL hole (issue #51).
recoveryWarnings []string