storage: the cut's pause metric ends at the barrier's Unlock (#972)

Interim merge note (spec trace workflow startup_failure since 22:45Z): metric-only change. go test (linux) and process-level tests (real binaries) passed on b197f5f7 (the first go test run died in an unrelated pre-existing test race in TestCutCOWV3_HeldFillIsCancelledByAbortAndDrained, fixed separately in #975; only the failed job was rerun); spec-trace-check.sh and public-text-check.sh run locally with the base branch's scripts against this head and body: both pass. Code reviews ACCEPT on b197f5f7 (two reviewers).
This commit is contained in:
pingqiu-cpu authored and GitHub committed 2026-12-02 20:53:59 -08:00
1 parent a6304c2101
commit 8c9358bca0
5 files changed
+185 -25

No files matched your search

@@ -87,6 +87,7 @@ type ChunkCutStatsBody struct {
COWSaveNs int64 `json:"cowSaveNs"`
COWWaitNs int64 `json:"cowWaitNs"`
BarrierPauseNs int64 `json:"barrierPauseNs"`
BarrierReleaseNs int64 `json:"barrierReleaseNs"`
LockWaitNs int64 `json:"lockWaitNs"`
DrainNs int64 `json:"drainNs"`
CASWriteNs int64 `json:"casWriteNs"`
@@ -141,6 +142,7 @@ func cutStatsBody(s storage.ChunkCutStats) ChunkCutStatsBody {
COWWALReadAttempts: s.COWWALReadAttempts,
COWLocalReadNs: s.COWLocalReadNs,
BarrierPauseNs: s.BarrierPauseNs,
BarrierReleaseNs: s.BarrierReleaseNs,
LockWaitNs: s.LockWaitNs,
DrainNs: s.DrainNs,
CASWriteNs: s.CASWriteNs,
@@ -0,0 +1,127 @@
package storage
import (
"errors"
"runtime"
"strings"
"testing"
"time"
)
// parkedOnBarrier counts the guest writes waiting for the capture barrier's
// read side: blocked in RLock, or (a write with an admission deadline)
// retrying TryRLock in acquireWriteLock. These are the writes the barrier's
// release lets in.
func parkedOnBarrier() int {
buf := make([]byte, 1<<22)
buf = buf[:runtime.Stack(buf, true)]
n := 0
for _, g := range strings.Split(string(buf), "\n\n") {
waiting := strings.Contains(g, "sync.(*RWMutex).RLock") || strings.Contains(g, "(*WALStore).acquireWriteLock")
if waiting && strings.Contains(g, "(*WALStore).WriteContext") {
n++
}
}
return n
}
// The guest pause ends where the capture barrier's Unlock starts. The Unlock
// itself (it wakes each reader blocked on the barrier before it returns), the
// flusher hold's release and the flusher's wake are reported apart in
// BarrierReleaseNs. With N writers seen parked on the barrier and the release
// step at the Unlock made slow (a stand-in for its wake work), the pause stays
// below that step for every N and the release time carries it. (A pause read
// after the Unlock carried the step: about 100 ms more.)
func TestTheBarrierPauseEndsAtUnlockAndItsReleaseIsReportedApart(t *testing.T) {
const slowRelease = 100 * time.Millisecond
for _, n := range []int{1, 64} {
s, _ := newChunkTestStore(t)
l := mustAttach(t, s, newFakeChunkRemote(), ctConfig(1)).(*chunkLayerImpl)
if _, err := l.Cut(ctTimeout(t)); err != nil { // the seed and an empty set
t.Fatal(err)
}
if _, err := s.Write(0, makeBlock(ctBlockSize, 0x41)); err != nil {
t.Fatal(err)
}
done := make(chan error, n)
parked := 0
l.testAfterBarrierSyncHook = func(uint64) { // under the barrier: these writers park on it
l.testAfterBarrierSyncHook = nil
for i := 0; i < n; i++ {
go func(i int) {
_, err := s.Write(uint32(1+i%(ctNumBlocks-1)), makeBlock(ctBlockSize, byte(i)))
done <- err
}(i)
}
for deadline := time.Now().Add(ctLocalBound); parked < n && time.Now().Before(deadline); {
if parked = parkedOnBarrier(); parked < n {
time.Sleep(time.Millisecond)
}
}
}
l.testBarrierUnlockHook = func() { time.Sleep(slowRelease) }
r, err := l.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
for i := 0; i < n; i++ { // every parked writer finishes, bounded
select {
case werr := <-done:
if werr != nil {
t.Fatalf("a parked writer failed: %v", werr)
}
case <-time.After(ctLocalBound):
t.Fatalf("N=%d: a parked writer did not finish", n)
}
}
if parked < n {
t.Fatalf("setup: N=%d: only %d writers were seen parked on the barrier", n, parked)
}
st := r.Stats
pause, release := time.Duration(st.BarrierPauseNs), time.Duration(st.BarrierReleaseNs)
t.Logf("N=%d (%d parked): pause %v (window setup %v), release %v", n, parked, pause, time.Duration(st.WindowSetupNs), release)
if release < slowRelease {
t.Errorf("N=%d: release %v, want at least the slow release step %v", n, release, slowRelease)
}
if pause >= slowRelease {
t.Errorf("N=%d: pause %v includes the release step (%v): the pause must end where the Unlock starts", n, pause, slowRelease)
}
if time.Duration(st.WindowSetupNs) >= slowRelease {
t.Errorf("N=%d: window setup %v includes the release step", n, time.Duration(st.WindowSetupNs))
}
}
}
// A cut whose in-pause sync fails splits its pause and release the same way:
// the pause it reports ends where the barrier's Unlock starts, so a slow
// release step is not in it. (The failed exit read the pause after the whole
// release: about 100 ms more.)
func TestAFailedBarrierSyncSplitsItsPauseFromItsRelease(t *testing.T) {
const slowRelease = 100 * time.Millisecond
s, _ := newChunkTestStore(t)
l := mustAttach(t, s, newFakeChunkRemote(), ctConfig(1)).(*chunkLayerImpl)
if _, err := l.Cut(ctTimeout(t)); err != nil { // the seed and an empty set
t.Fatal(err)
}
if _, err := s.Write(0, makeBlock(ctBlockSize, 0x42)); err != nil {
t.Fatal(err)
}
injected := errors.New("injected in-pause sync failure")
s.testSyncCacheFault = func() error {
if l.paused {
return injected
}
return nil
}
t.Cleanup(func() { s.testSyncCacheFault = nil })
pause, calls := time.Duration(-1), 0
l.precopyPause = func(d time.Duration) { pause = d; calls++ } // the failed attempt's reported pause
l.testBarrierUnlockHook = func() { time.Sleep(slowRelease) }
if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, injected) {
t.Fatalf("the cut's answer: %v, want the in-pause sync's failure", err)
}
t.Logf("failed cut's pause %v (%d observations)", pause, calls)
if calls != 1 || pause < 0 || pause >= slowRelease {
t.Fatalf("failed cut's pause %v: want it to end where the Unlock starts (below the %v release step)", pause, slowRelease)
}
}
+29 -6
View File
@@ -247,6 +247,7 @@ type chunkLayerImpl struct {
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
testBarrierUnlockHook func() // in the release span, right before the barrier's Unlock (a stand-in for its wake work)
testBeforeRecordMarksHook func() // after a cut's compare, before it records its marks
testAfterCompareHook func(idx uint64) // after a captured chunk is compared (and marked if equal)
syncDirFn func(dir string) error // directory fsync after a rename; syncDir in production
@@ -1773,14 +1774,31 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
return chunkCutResult{}, cause
}
// Resume rejected tentative admission before any ordinary COW upload.
// unlockBarrier reads the pause's end clock right before the barrier's
// Unlock: Unlock admits the parked readers and then wakes each of them
// before it returns, and that wake work belongs to the release
// (BarrierReleaseNs), not to the guest pause.
unlockBarrier := func() time.Time {
at := time.Now()
if hook := l.testBarrierUnlockHook; hook != nil {
hook()
}
l.s.captureMu.Unlock()
return at
}
unlockCapture := func() {
l.paused = false
l.s.captureMu.Unlock()
drainedHere := !detach && releasePrecopy != nil
at := unlockBarrier()
releaseExtentHold()
if !detach && releasePrecopy != nil {
if drainedHere {
releasePrecopy()
releasePrecopy = nil
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds() // a drained guest is back only now
} else {
st.BarrierPauseNs = at.Sub(pauseStart).Nanoseconds()
}
st.BarrierReleaseNs = time.Since(at).Nanoseconds()
}
if hook := l.testBeforeBarrierLockHook; hook != nil {
hook()
@@ -1811,7 +1829,6 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
}
if err != nil {
unlockCapture()
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds()
return chunkCutResult{}, fmt.Errorf("storage: capture sync: %w", err)
}
l.cutPhase("candidates")
@@ -1850,13 +1867,19 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
// guest pause runs to that release, as before. An ordinary cut's guest
// is back now, and its pause did O(1) work.
drained := releasePrecopy != nil
l.s.captureMu.Unlock()
// An ordinary cut's guest is back from the Unlock on: the pause ends there.
// The Unlock's own wakes, the hold's release and the flusher's wake are
// reported apart (BarrierReleaseNs).
unlocked := unlockBarrier()
st.WindowSetupNs += unlocked.Sub(windowStart).Nanoseconds()
if !drained {
st.BarrierPauseNs = unlocked.Sub(pauseStart).Nanoseconds()
}
releaseExtentHold()
st.WindowSetupNs += time.Since(windowStart).Nanoseconds()
if !drained {
l.paused = false
st.BarrierPauseNs = time.Since(pauseStart).Nanoseconds()
}
st.BarrierReleaseNs = time.Since(unlocked).Nanoseconds()
releaseDrain := func() {
if drained {
if releasePrecopy != nil && !detach {
+9 -2
View File
@@ -463,8 +463,9 @@ type chunkCutStats struct {
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.
// request to the start of its Unlock (BarrierReleaseNs below is the rest).
// LockWaitNs is its first part, the wait for writers already holding the
// capture barrier shared.
BarrierPauseNs int64
LockWaitNs int64
DrainNs int64
@@ -482,6 +483,12 @@ type chunkCutStats struct {
MapPersistNs int64
CASResult string
TotalNs int64
// BarrierReleaseNs is the barrier's release, outside the guest pause: the
// Unlock (which wakes each guest writer parked on the barrier), releasing
// the flusher hold and waking the flusher. A failed sync's exit splits the
// same way.
BarrierReleaseNs int64
// Pre-copy (staging by the copy-on-write side budget, M5 Stage):
// StageRounds staging rounds staged StagedChunks chunks outside the
// barrier; StagedChunksUsed of the uploads in HEAD came from staging.
+18 -17
View File
@@ -347,23 +347,24 @@ Durable store state of the served volume. Go type `volume.durableStatusBody`.
| `Volumes[].Chunk.Stats.LastCut.COWSavedChunks` | int | (group of `COWWALReadAttempts`) Local validated WAL attempts and local copy duration, including savers. |
| `Volumes[].Chunk.Stats.LastCut.COWSaveNs` | int64 | (group of `COWWALReadAttempts`) Local validated WAL attempts and local copy duration, including savers. |
| `Volumes[].Chunk.Stats.LastCut.COWWaitNs` | int64 | (group of `COWWALReadAttempts`) Local validated WAL attempts and local copy duration, including savers. |
| `Volumes[].Chunk.Stats.LastCut.BarrierPauseNs` | 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. |
| `Volumes[].Chunk.Stats.LastCut.LockWaitNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.DrainNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.CASWriteNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.HeadReadbackNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.SyncNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.CopyNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.CandidateChunks` | int | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.UploadedChunks` | int | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.UploadedBytes` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.ZeroedChunks` | int | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.UnchangedChunks` | int | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.UploadNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.CASNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.MapPersistNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.CASResult` | string | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.TotalNs` | int64 | (group of `BarrierPauseNs`) 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. |
| `Volumes[].Chunk.Stats.LastCut.BarrierPauseNs` | int64 | BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.LockWaitNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.DrainNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.CASWriteNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.HeadReadbackNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.SyncNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.CopyNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.CandidateChunks` | int | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.UploadedChunks` | int | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.UploadedBytes` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.ZeroedChunks` | int | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.UnchangedChunks` | int | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.UploadNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.CASNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.MapPersistNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.CASResult` | string | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.TotalNs` | int64 | (group of `BarrierPauseNs`) BarrierPauseNs is the guest pause: from the capture barrier's Lock request to the start of its Unlock (BarrierReleaseNs below is the rest). LockWaitNs is its first part, the wait for writers already holding the capture barrier shared. |
| `Volumes[].Chunk.Stats.LastCut.BarrierReleaseNs` | int64 | BarrierReleaseNs is the barrier's release, outside the guest pause: the Unlock (which wakes each guest writer parked on the barrier), releasing the flusher hold and waking the flusher. A failed sync's exit splits the same way. |
| `Volumes[].Chunk.Stats.LastCut.StageRounds` | int | Pre-copy (staging by the copy-on-write side budget, M5 Stage): StageRounds staging rounds staged StagedChunks chunks outside the barrier; StagedChunksUsed of the uploads in HEAD came from staging. BarrierCopyChunks were copied under the barrier. BarrierOverBound: more than the legacy fixture BarrierBound; always false in production, where that bound is zero. |
| `Volumes[].Chunk.Stats.LastCut.StagedChunks` | int | (group of `StageRounds`) Pre-copy (staging by the copy-on-write side budget, M5 Stage): StageRounds staging rounds staged StagedChunks chunks outside the barrier; StagedChunksUsed of the uploads in HEAD came from staging. BarrierCopyChunks were copied under the barrier. BarrierOverBound: more than the legacy fixture BarrierBound; always false in production, where that bound is zero. |
| `Volumes[].Chunk.Stats.LastCut.StagedChunksUsed` | int | (group of `StageRounds`) Pre-copy (staging by the copy-on-write side budget, M5 Stage): StageRounds staging rounds staged StagedChunks chunks outside the barrier; StagedChunksUsed of the uploads in HEAD came from staging. BarrierCopyChunks were copied under the barrier. BarrierOverBound: more than the legacy fixture BarrierBound; always false in production, where that bound is zero. |