storage: cap the cut COW spill file; a save past the cap aborts the cut (#216 follow-up P3)

Past the 64 MiB in-memory budget a copy window spills saved chunks to a
sparse side file next to the volume, with no byte limit: under a long copy
and steady writes it could fill the filesystem under the WAL/extent.

New chunkLayerConfig.COWSpillLimit (bytes; 0 defaults to 1 GiB, negative
refused at attach). A save that would take the side file past it aborts the
window (errCutCOWAborted, the caller retries the cut) instead of writing;
the guest write still completes.

Tests: a save past the cap aborts the cut, the write completes and no side
file is written; a spill within the cap is accounted (one chunk) and the cut
completes; the default applies and a negative cap is refused. Four declared
mutants (no cap, no accounting, no default, negative accepted) each turn
exactly their declared tests red.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-18 20:39:50 -08:00
1 parent 7e39a9ce32
commit ea0b8955dc
4 files changed
+112 -1

No files matched your search

+13 -1
View File
@@ -18,6 +18,9 @@ import (
)
const cutCOWMemoryLimit = 64 << 20
// defaultCOWSpillLimit caps a copy window's side-file bytes by default.
const defaultCOWSpillLimit = 1 << 30
const (
cowPending uint8 = iota
cowCopying
@@ -53,6 +56,8 @@ type cutCOWWindow struct {
entries map[uint64]*cutCOWEntry
head map[uint64]chunkRef // the cut's base HEAD: the source of missing blocks
memory int64
spillBytes int64 // bytes written to the side file; at most spillLimit
spillLimit int64
chunkBytes int64
side *os.File
sideErr error
@@ -129,7 +134,7 @@ func (l *chunkLayerImpl) installCOWWindow(candidates, staged map[uint64]bool, he
if size == 0 || size > math.MaxInt64 || uint64(len(candidates)) > math.MaxInt64/size {
return nil, fmt.Errorf("%w: side-store geometry", errCutCOWAborted)
}
w := &cutCOWWindow{layer: l, ended: make(chan struct{}), entries: make(map[uint64]*cutCOWEntry), head: head, chunkBytes: int64(size)}
w := &cutCOWWindow{layer: l, ended: make(chan struct{}), entries: make(map[uint64]*cutCOWEntry), head: head, spillLimit: l.cfg.COWSpillLimit, chunkBytes: int64(size)}
var slot int64
for _, idx := range sortedKeys(candidates) {
state := cowPending
@@ -293,6 +298,13 @@ func (w *cutCOWWindow) save(e *cutCOWEntry, data []byte, missing []uint64) error
inMemory := w.memory+w.chunkBytes <= cutCOWMemoryLimit
if inMemory {
w.memory += w.chunkBytes
} else if w.spillBytes+w.chunkBytes > w.spillLimit {
// The side file is sparse but its saved chunks are real bytes on the
// volume's filesystem: abort the cut (retried) instead of filling it.
w.mu.Unlock()
return fmt.Errorf("%w: COW spill would exceed its cap of %d bytes", errCutCOWAborted, w.spillLimit)
} else {
w.spillBytes += w.chunkBytes
}
w.mu.Unlock()
// e.missing (and e.data / the spilled bytes) are published to readers by
@@ -0,0 +1,87 @@
package storage
import (
"context"
"errors"
"strings"
"testing"
"time"
)
// P3: once the in-memory budget is used, a save that would take the side file
// past its byte cap aborts the cut (retried) instead of growing the file; the
// guest write still completes and no side file is left.
func TestCutCOWV3_SpillCapAbortsTheCut(t *testing.T) {
s, l, _, gate := cowFixture(t)
cutDone, cutErr := cowStartCut(t, l, gate)
w := l.cowActive.Load()
w.mu.Lock()
w.memory = cutCOWMemoryLimit // the in-memory budget is used
w.spillLimit = w.chunkBytes - 1 // the cap leaves no room for one chunk
w.mu.Unlock()
writeDone := make(chan error, 1)
go func() { _, err := s.Write(5, makeBlock(ctBlockSize, 0xd5)); writeDone <- err }()
select {
case err := <-writeDone:
if err != nil {
t.Fatal("guest write refused by the spill cap:", err)
}
case <-time.After(ctLocalBound):
t.Fatal("guest write did not complete when the spill cap aborted the cut")
}
w.mu.Lock()
side, spilled := w.side, w.spillBytes
w.mu.Unlock()
if side != nil || spilled != 0 {
t.Fatalf("side file %v / %d spilled bytes past the cap", side != nil, spilled)
}
gate.unblock()
cowWaitSignal(t, cutDone, "capped cut")
if err := <-cutErr; !errors.Is(err, errCutCOWAborted) || !strings.Contains(err.Error(), "spill would exceed its cap") {
t.Fatalf("cut past the spill cap: %v, want the cap abort", err)
}
if _, err := l.Cut(context.Background()); err != nil {
t.Fatal("retried cut:", err)
}
}
// The default cap applies at attach (0), and a negative cap is refused.
func TestChunkLayer_COWSpillLimitDefaultAndRefusal(t *testing.T) {
s, _ := newChunkTestStore(t)
cfg := ctConfig(1)
cfg.COWSpillLimit = -1
if _, err := attachChunkLayer(s, newFakeChunkRemote(), cfg); err == nil || !strings.Contains(err.Error(), "spill limit") {
t.Fatalf("negative COW spill limit: %v, want a refusal", err)
}
cfg.COWSpillLimit = 0
l := mustAttach(t, s, newFakeChunkRemote(), cfg).(*chunkLayerImpl)
if l.cfg.COWSpillLimit != defaultCOWSpillLimit {
t.Fatalf("COW spill limit %d, want the default %d", l.cfg.COWSpillLimit, defaultCOWSpillLimit)
}
}
// A spill within the cap is accounted: with room for exactly one chunk the
// save spills, the window counts that chunk, and the cut completes.
func TestCutCOWV3_SpillWithinCapIsAccounted(t *testing.T) {
s, l, _, gate := cowFixture(t)
cutDone, cutErr := cowStartCut(t, l, gate)
w := l.cowActive.Load()
w.mu.Lock()
w.memory = cutCOWMemoryLimit
w.spillLimit = w.chunkBytes
w.mu.Unlock()
if _, err := s.Write(5, makeBlock(ctBlockSize, 0xd5)); err != nil {
t.Fatal(err)
}
w.mu.Lock()
side, spilled := w.side, w.spillBytes
w.mu.Unlock()
if side == nil || spilled != w.chunkBytes {
t.Fatalf("spill within the cap: side file %v, %d bytes accounted, want one chunk (%d)", side != nil, spilled, w.chunkBytes)
}
gate.unblock()
cowWaitSignal(t, cutDone, "cut within the spill cap")
if err := <-cutErr; err != nil {
t.Fatal(err)
}
}
@@ -180,6 +180,12 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig)
if cfg.ReadMissTimeout <= 0 {
cfg.ReadMissTimeout = defaultReadMissTimeout
}
if cfg.COWSpillLimit < 0 {
return nil, errors.New("storage: COW spill limit must not be negative (0 defaults to 1 GiB)")
}
if cfg.COWSpillLimit == 0 {
cfg.COWSpillLimit = defaultCOWSpillLimit
}
nb := uint64(s.NumBlocks())
l := &chunkLayerImpl{
s: s,
+6
View File
@@ -355,6 +355,12 @@ type chunkLayerConfig struct {
// UploadConcurrency bounds capture/pre-copy Put calls (0 defaults to 8).
UploadConcurrency int
// COWSpillLimit caps the bytes a cut's copy window may spill to its side
// file once the in-memory budget is used: a save beyond it aborts the cut
// (the caller retries) instead of growing the file under the WAL/extent.
// 0 defaults to defaultCOWSpillLimit; negative is refused.
COWSpillLimit int64
// testReconcileHold, set only by this package's tests, holds the
// reconciler before each slice until a token is received or the
// channel is closed.