storage: P2 review fold: Lock-request handshake, bounded test helper, design wording

- A test-only hook right before the capture barrier's Lock request; the
  pause tests hold the barrier shared for 300 ms after that handshake (not
  after admission plus a sleep), so the cut provably waited at its Lock and
  pre-barrier time is excluded.
- The helper releases the shared hold on every path (also on a failed
  assertion) and joins the cut within a bound.
- Design docs: barrierPauseNs runs from the Lock request and includes
  lockWaitNs.

Mutants (declared, 4/4): pause timed after the Lock, lock wait unset, body
drops the field, the hook fired after the Lock (no handshake).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-18 20:22:30 -08:00
1 parent 9302f31e48
commit bc5e3e6446
5 files changed
+43 -17

No files matched your search

@@ -1,13 +1,16 @@
package storage
import (
"sync"
"testing"
"time"
)
// cutWithBarrierHeld runs one cut while a writer holds the capture barrier
// shared (as a writer parked in WAL admission does) for hold after the cut was
// admitted, and returns the cut's stats.
// shared (as a writer parked in WAL admission does). The hold ends hold after
// the cut's barrier Lock REQUEST (a handshake, not a guess at the cut's
// progress), so the cut waited at least hold at its Lock. The shared hold is
// released on every path, and the cut is joined within a bound.
func cutWithBarrierHeld(t *testing.T, hold time.Duration) chunkCutStats {
t.Helper()
s, _ := newChunkTestStore(t)
@@ -16,22 +19,39 @@ func cutWithBarrierHeld(t *testing.T, hold time.Duration) chunkCutStats {
l := layer.(*chunkLayerImpl)
w := newWriteLog()
w.write(t, s, 2, 0x22)
admitted := make(chan struct{})
l.testAfterAdmitHook = func() { close(admitted) }
requested := make(chan struct{})
var reqOnce sync.Once
l.testBeforeBarrierLockHook = func() { reqOnce.Do(func() { close(requested) }) }
s.captureMu.RLock()
var unlockOnce sync.Once
release := func() { unlockOnce.Do(s.captureMu.RUnlock) }
type result struct {
r chunkCutResult
err error
}
done := make(chan result, 1)
t.Cleanup(func() {
release()
select {
case <-done:
case <-time.After(ctLocalBound):
t.Errorf("cut not joined within %v", ctLocalBound)
}
})
go func() {
r, err := layer.Cut(ctTimeout(t))
done <- result{r, err}
}()
cowWaitSignal(t, admitted, "cut admitted")
time.Sleep(hold) // the cut is now waiting for the shared holder at captureMu.Lock
s.captureMu.RUnlock()
got := <-done
cowWaitSignal(t, requested, "the cut's barrier Lock request")
time.Sleep(hold) // the cut is waiting at captureMu.Lock for the shared holder
release()
var got result
select {
case got = <-done:
case <-time.After(ctLocalBound):
t.Fatalf("cut did not finish within %v after the shared holder released the barrier", ctLocalBound)
}
done <- got // for the cleanup's join
if got.err != nil {
t.Fatal(got.err)
}
@@ -44,8 +64,8 @@ func cutWithBarrierHeld(t *testing.T, hold time.Duration) chunkCutStats {
func TestCutStats_BarrierPauseIncludesTheLockWait(t *testing.T) {
const hold = 300 * time.Millisecond
st := cutWithBarrierHeld(t, hold)
if st.BarrierPauseNs < (hold - 50*time.Millisecond).Nanoseconds() {
t.Fatalf("BarrierPauseNs %v does not include the %v wait for a writer holding the barrier", time.Duration(st.BarrierPauseNs), hold)
if st.BarrierPauseNs < hold.Nanoseconds() {
t.Fatalf("BarrierPauseNs %v does not include the >= %v wait for a writer holding the barrier", time.Duration(st.BarrierPauseNs), hold)
}
}
@@ -53,7 +73,7 @@ func TestCutStats_BarrierPauseIncludesTheLockWait(t *testing.T) {
func TestCutStats_LockWaitNsReportsTheWait(t *testing.T) {
const hold = 300 * time.Millisecond
st := cutWithBarrierHeld(t, hold)
if st.LockWaitNs < (hold - 50*time.Millisecond).Nanoseconds() || st.BarrierPauseNs < st.LockWaitNs+st.SyncNs {
t.Fatalf("LockWaitNs %v (pause %v, sync %v): want >= ~%v and inside the pause before the sync", time.Duration(st.LockWaitNs), time.Duration(st.BarrierPauseNs), time.Duration(st.SyncNs), hold)
if st.LockWaitNs < hold.Nanoseconds() || st.BarrierPauseNs < st.LockWaitNs+st.SyncNs {
t.Fatalf("LockWaitNs %v (pause %v, sync %v): want >= %v and inside the pause before the sync", time.Duration(st.LockWaitNs), time.Duration(st.BarrierPauseNs), time.Duration(st.SyncNs), hold)
}
}
@@ -132,6 +132,7 @@ type chunkLayerImpl struct {
testAfterStageHook func(round int) // after each pre-copy staging round
testAfterStageReadHook func(idx uint64) // after a staging read, before its result is kept
testAfterBarrierSyncHook func(h uint64) // under the capture barrier, right after Sync fixed H
testBeforeBarrierLockHook func() // right before the capture barrier's Lock request
syncDirFn func(dir string) error // directory fsync after a rename; syncDir in production
// Absence: chunks whose local bytes are not authoritative (fresh host,
@@ -840,6 +841,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
// waits for writers already holding it (e.g. parked in WAL admission) is
// paused too. LockWaitNs reports that wait on its own.
pauseStart := time.Now()
if hook := l.testBeforeBarrierLockHook; hook != nil {
hook()
}
l.s.captureMu.Lock()
barrier := time.Now()
st.LockWaitNs = barrier.Sub(pauseStart).Nanoseconds()
+3 -3
View File
@@ -369,9 +369,9 @@ type chunkLayerConfig struct {
// chunkCutStats describes one cut. The periodic-cut work fills it; a zero
// value means "not measured".
type chunkCutStats struct {
COWSavedChunks int
COWSaveNs int64
COWWaitNs int64
COWSavedChunks int
COWSaveNs int64
COWWaitNs int64
// BarrierPauseNs is the guest pause: from the capture barrier's Lock
// request until it is released. LockWaitNs is its first part, the wait for
// writers already holding the capture barrier shared.
@@ -177,7 +177,7 @@ stateDiagram-v2
Cleanup --> Retry: ABORTED, merge all candidates back
```
**Statistics.** `barrierPauseNs` covers Sync and window installation; native `barrierCopyChunks` is zero. `copyNs` covers outside-barrier materialization/drain. `cowSavedChunks` counts successful saves; `cowSaveNs` and `cowWaitNs` sum per-owner/per-wait elapsed time and are not wall-clock pause. These fields mirror into the actual cut response. Upload sizes and remote GC roots are unchanged.
**Statistics.** `barrierPauseNs` runs from the capture barrier's Lock request to its release: `lockWaitNs` (the wait for writers already holding it shared), Sync and window installation; native `barrierCopyChunks` is zero. `copyNs` covers outside-barrier materialization/drain. `cowSavedChunks` counts successful saves; `cowSaveNs` and `cowWaitNs` sum per-owner/per-wait elapsed time and are not wall-clock pause. These fields mirror into the actual cut response. Upload sizes and remote GC roots are unchanged.
**Model and tests.** The existing S4c r2 `CutCOW2` verdict and limits are bound in the [spec correspondence](../spec/README.md#cut-window-implementation-correspondence). One-copier whole-chunk modelling does not certify actual writer ownership, batches or Close/attach. `TestCutCOWV3_*` exercises real native mutation, abort, spill, recovery and close callers; earlier ACTIVE staging/image/intent/CAS controls remain. See [the implementation lock graph and mount rehearsal](cut-cow-validation.md).
@@ -45,7 +45,9 @@ this volume's reserved `.cut-cow-` prefix; other prefixes, directories and
symlinks are left alone. Cleanup failure fails the cut diagnostically, without
changing any guest write outcome.
`barrierPauseNs` includes Sync/window setup, not chunk reads. `copyNs` is the
`barrierPauseNs` runs from the capture barrier's Lock request (a pending Lock
already blocks new writers) to its release: it includes `lockWaitNs`, the wait for
writers already holding the barrier shared, and Sync/window setup, not chunk reads. `copyNs` is the
outside-barrier copy and drain. Native `barrierCopyChunks`/`barrierOverBound`
are zero; the configured threshold still bounds staging, not a hard COW-copy
limit. `cowSavedChunks`, `cowSaveNs`, `cowWaitNs` are mirrored into the cut