storage: the cut's copy window is installed in O(1) at the barrier

The window no longer sorts the candidates and makes one entry (with its
channel) per candidate inside the guest pause. It keeps the candidate set
and the staged marks (immutable once installed) and makes a chunk's entry
on its first touch: a guest write's save or the copier. Side-file slots
are handed out in touch order. After the pause, the copier lists the
covered chunks that a pre-copy does not hold and no touch has finished.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 19:26:58 -08:00
1 parent 306831c07f
commit 3dd2c6af89
1 file changed
+48 -17
+48 -17
View File
@@ -137,6 +137,15 @@ type cutCOWWindow struct {
saveNs atomic.Int64
waitNs atomic.Int64
// Scheduling/fault controls are nil in production and set before installation.
// entries (above) are made on a chunk's first touch (a guest write's
// save, or the copier), never at the barrier: the window covers cand, the
// cut's candidate set (immutable once installed), and staged marks the
// ones a pre-copy already holds. nextSlot hands out side-file slots in
// touch order. The barrier's work is O(1), not one entry per candidate.
cand map[uint64]bool
staged map[uint64]bool
nextSlot int64
}
func (s *WALStore) beforeChunkMutation(first, count uint64) {
@@ -214,15 +223,7 @@ func (l *chunkLayerImpl) installCOWWindow(candidates, staged map[uint64]bool, he
reserved += int64(2*savers) * int64(size)
w.memory, w.memoryLimit = reserved, limit
w.saverSlots = make(chan struct{}, savers)
var slot int64
for _, idx := range sortedKeys(candidates) {
state := cowPending
if staged[idx] {
state = cowDone
}
w.entries[idx] = &cutCOWEntry{state: state, ready: make(chan struct{}), slot: slot}
slot++
}
w.cand, w.staged = candidates, staged
l.cowMu.Lock()
defer l.cowMu.Unlock()
if l.cowClosed || l.cowTransitions != 0 {
@@ -266,7 +267,7 @@ func (w *cutCOWWindow) claim(idx uint64, saving bool) (*cutCOWEntry, uint8, <-ch
if w.terminal {
return nil, cowDone, nil
}
e := w.entries[idx]
e := w.entryLocked(idx)
if e == nil {
return nil, cowDone, nil
}
@@ -302,6 +303,26 @@ func (w *cutCOWWindow) claim(idx uint64, saving bool) (*cutCOWEntry, uint8, <-ch
return nil, cowDone, nil
}
}
// entryLocked is idx's entry, made on its first touch: nil when the window
// does not cover idx, done when a pre-copy holds it. Caller holds w.mu.
func (w *cutCOWWindow) entryLocked(idx uint64) *cutCOWEntry {
if e := w.entries[idx]; e != nil {
return e
}
if !w.cand[idx] {
return nil
}
state := cowPending
if w.staged[idx] {
state = cowDone
}
e := &cutCOWEntry{state: state, ready: make(chan struct{}), slot: w.nextSlot}
w.nextSlot++
w.entries[idx] = e
return e
}
func (w *cutCOWWindow) finish(e *cutCOWEntry, state uint8) bool {
w.mu.Lock()
defer w.mu.Unlock()
@@ -631,15 +652,25 @@ func (l *chunkLayerImpl) copyCOWWindow(ctx context.Context, w *cutCOWWindow, rec
}
l.cowMu.Unlock()
}()
indices := make(map[uint64]bool, len(w.entries))
for idx, e := range w.entries {
e.mu.Lock()
done := e.state == cowDone
e.mu.Unlock()
if !done {
indices[idx] = true
// The copier's work: every covered chunk a pre-copy does not hold and no
// touch has finished. Outside the pause (writers are back), O(candidates).
indices := make(map[uint64]bool, len(w.cand))
w.mu.Lock()
for idx := range w.cand {
if w.staged[idx] {
continue
}
if e := w.entries[idx]; e != nil {
e.mu.Lock()
done := e.state == cowDone
e.mu.Unlock()
if done {
continue
}
}
indices[idx] = true
}
w.mu.Unlock()
order := hotChunkKeys(indices, recent)
out, err := l.streamCutPayloads(ctx, order, head, false, func(ctx context.Context, idx uint64) (capturedChunk, error) {
c, ok, err := l.copyCOWEntry(w, idx)