storage: cut barrier pause counts the lock wait; report LockWaitNs (#216 follow-up P2)

BarrierPauseNs started after captureMu.Lock returned, so the wait for
writers already holding the capture barrier shared (e.g. parked in WAL
admission) was not counted, although a pending Lock already blocks new
writers: the reported guest pause understated it.

The pause is now timed from the Lock request, and LockWaitNs reports the
wait for the shared holders on its own (also in the /chunk/cut body).

Tests: a cut whose barrier is held shared for 300 ms reports a pause that
includes the wait (red on main: about 24 ms) and a LockWaitNs of about the
hold, inside the pause before the sync. Three declared mutants (pause timed
after the Lock, lock wait unset, body drops the field) 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 19:47:49 -08:00
1 parent b6ca5237fd
commit 9302f31e48
4 files changed
+74 -4

No files matched your search

@@ -59,6 +59,7 @@ type ChunkCutStatsBody struct {
COWSaveNs int64 `json:"cowSaveNs"`
COWWaitNs int64 `json:"cowWaitNs"`
BarrierPauseNs int64 `json:"barrierPauseNs"`
LockWaitNs int64 `json:"lockWaitNs"`
SyncNs int64 `json:"syncNs"`
CopyNs int64 `json:"copyNs"`
CandidateChunks int `json:"candidateChunks"`
@@ -80,6 +81,7 @@ type ChunkCutStatsBody struct {
func cutStatsBody(s storage.ChunkCutStats) ChunkCutStatsBody {
return ChunkCutStatsBody{
BarrierPauseNs: s.BarrierPauseNs,
LockWaitNs: s.LockWaitNs,
SyncNs: s.SyncNs,
CopyNs: s.CopyNs,
COWSavedChunks: s.COWSavedChunks,
@@ -0,0 +1,59 @@
package storage
import (
"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.
func cutWithBarrierHeld(t *testing.T, hold time.Duration) chunkCutStats {
t.Helper()
s, _ := newChunkTestStore(t)
f := newFakeChunkRemote()
layer := mustAttach(t, s, f, ctConfig(1))
l := layer.(*chunkLayerImpl)
w := newWriteLog()
w.write(t, s, 2, 0x22)
admitted := make(chan struct{})
l.testAfterAdmitHook = func() { close(admitted) }
s.captureMu.RLock()
type result struct {
r chunkCutResult
err error
}
done := make(chan result, 1)
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
if got.err != nil {
t.Fatal(got.err)
}
return got.r.Stats
}
// P2: the guest pause starts at the barrier's Lock request (a pending writer
// blocks new readers), so the wait for writers already holding the barrier is
// part of BarrierPauseNs.
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)
}
}
// P2: LockWaitNs reports that wait on its own, as the first part of the pause.
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)
}
}
+9 -4
View File
@@ -835,9 +835,14 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
return chunkCutResult{}, err
}
// Quiesced cut.
// Quiesced cut. The guest pause starts at the Lock REQUEST: a pending
// writer blocks new readers of captureMu, so a write arriving while the cut
// 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()
l.s.captureMu.Lock()
barrier := time.Now()
st.LockWaitNs = barrier.Sub(pauseStart).Nanoseconds()
h, err := l.s.Sync()
st.SyncNs = time.Since(barrier).Nanoseconds()
if hook := l.testAfterBarrierSyncHook; hook != nil && err == nil {
@@ -845,7 +850,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
}
if err != nil {
l.s.captureMu.Unlock()
st.BarrierPauseNs = time.Since(barrier).Nanoseconds()
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds()
l.dropIntents(ctx, intents) // a clean abort: staged keys are released
return chunkCutResult{}, fmt.Errorf("storage: capture sync: %w", err)
}
@@ -900,7 +905,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
var w *cutCOWWindow
w, copyErr = l.installCOWWindow(candidates, useStaged)
l.s.captureMu.Unlock()
st.BarrierPauseNs = time.Since(barrier).Nanoseconds()
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds()
copyStart = time.Now()
if copyErr == nil {
var copied []capturedChunk
@@ -923,7 +928,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
captured = append(captured, capturedChunk{idx: idx, data: buf, sum: sha256.Sum256(buf), zero: isAllZero(buf)})
}
l.s.captureMu.Unlock()
st.BarrierPauseNs = time.Since(barrier).Nanoseconds()
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds()
}
st.CopyNs = time.Since(copyStart).Nanoseconds()
if copyErr != nil {
+4
View File
@@ -372,7 +372,11 @@ type chunkCutStats struct {
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.
BarrierPauseNs int64
LockWaitNs int64
SyncNs int64
CopyNs int64
CandidateChunks int