From 9302f31e4816ae7845052ed24b2d5561997b69bb Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 18 Nov 2026 19:47:49 -0800 Subject: [PATCH] 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 --- .../core/host/volume/chunk_control.go | 2 + .../core/storage/chunk_cut_pause_test.go | 59 +++++++++++++++++++ seaweed-block/core/storage/chunk_layer.go | 13 ++-- seaweed-block/core/storage/chunk_seam.go | 4 ++ 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 seaweed-block/core/storage/chunk_cut_pause_test.go diff --git a/seaweed-block/core/host/volume/chunk_control.go b/seaweed-block/core/host/volume/chunk_control.go index 50d0bd234d..9e0746d99c 100644 --- a/seaweed-block/core/host/volume/chunk_control.go +++ b/seaweed-block/core/host/volume/chunk_control.go @@ -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, diff --git a/seaweed-block/core/storage/chunk_cut_pause_test.go b/seaweed-block/core/storage/chunk_cut_pause_test.go new file mode 100644 index 0000000000..c672bae9e1 --- /dev/null +++ b/seaweed-block/core/storage/chunk_cut_pause_test.go @@ -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) + } +} diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index 07c6daf783..f3cef0c02b 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -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 { diff --git a/seaweed-block/core/storage/chunk_seam.go b/seaweed-block/core/storage/chunk_seam.go index d3c01c9920..72c937a33f 100644 --- a/seaweed-block/core/storage/chunk_seam.go +++ b/seaweed-block/core/storage/chunk_seam.go @@ -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