From 2d2327e8e85dc36f4e0f4aa21b76694f0399464b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 22:05:37 -0800 Subject: [PATCH] durable: test, every store read on the busy path is bounded The no-flush busy path observes the store only through a bounded snapshot: the checkpoint at the busy write, the target, the retry log and the timeout diagnostic, which reports the last observation (or "unavailable") instead of a fresh read. The busy-path check is a function the burst test calls, so a control drives it with the store lock held across the deadline at each observation, and released early. The wait's control gains an early-release case that must return the advanced checkpoint, a lower bound for the held case, and joins every fake read. Co-Authored-By: Claude Opus 5.5 --- .../durable/provider_chunk_noflush_test.go | 269 +++++++++++++++--- 1 file changed, 227 insertions(+), 42 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider_chunk_noflush_test.go b/seaweed-block/core/frontend/durable/provider_chunk_noflush_test.go index 77271ba5ee..d7de112fdf 100644 --- a/seaweed-block/core/frontend/durable/provider_chunk_noflush_test.go +++ b/seaweed-block/core/frontend/durable/provider_chunk_noflush_test.go @@ -15,6 +15,8 @@ import ( "reflect" "runtime" "sort" + "sync" + "sync/atomic" "testing" "time" ) @@ -81,31 +83,217 @@ func passiveCheckpointWait(getCP func() uint64, flushing func() bool, cpAtBusy, return r } -// The passive wait ends at its outer deadline even when a checkpoint read -// blocks behind a held store lock, and a flusher reported in a cycle the whole -// time does not extend it past the deadline. +// storeView is what the no-Flush check observes, with getters only. +// checkpoint and next take the store lock (CheckpointLSN, NextLSN); flushing +// reads atomic flusher counters. +type storeView struct { + checkpoint, next func() uint64 + flushing func() bool +} + +// storeSnap is one bounded observation. When ok is false a read was still +// blocked at its deadline and cp and next are unavailable, never re-read. +type storeSnap struct { + cp, next uint64 + ok bool + at time.Time +} + +func (s storeSnap) String() string { + if !s.ok { + return fmt.Sprintf("cp=unavailable next=unavailable (a store read blocked, %v ago)", time.Since(s.at).Round(time.Millisecond)) + } + return fmt.Sprintf("cp=%d next=%d", s.cp, s.next) +} + +// snapshot reads the checkpoint and the next LSN, each bounded by deadline. +func (v storeView) snapshot(deadline time.Time) storeSnap { + cp, ok := boundedRead(v.checkpoint, deadline) + if !ok { + return storeSnap{at: time.Now()} + } + next, ok := boundedRead(v.next, deadline) + if !ok { + return storeSnap{at: time.Now()} + } + return storeSnap{cp: cp, next: next, ok: true, at: time.Now()} +} + +// busyCheck is the outcome of the first busy write's passive check. +type busyCheck struct { + atBusy storeSnap // the checkpoint at the busy write, and the next LSN for the target + wait passiveResult + waited time.Duration + failed string // empty when the check passed +} + +func (c busyCheck) String() string { + return fmt.Sprintf("wait=%v atBusy: %v cpObserved=%d, last in a cycle %v ago, read blocked=%v", c.waited, c.atBusy, c.wait.cp, time.Since(c.wait.lastInCycle), c.wait.blocked) +} + +// passiveBusyCheck is the first busy write's check: with getters only, the +// checkpoint must pass its value at the busy write before the window ends. +// Every store observation, including the target, is bounded by deadline. +func passiveBusyCheck(v storeView, deadline time.Time, window time.Duration) busyCheck { + start := time.Now() + c := busyCheck{atBusy: v.snapshot(deadline)} + c.wait.lastInCycle = start + if !c.atBusy.ok { + c.waited = time.Since(start) + c.failed = "a store read at the busy write blocked past the write's retry deadline (a store lock held)" + return c + } + target := c.atBusy.next - 1 + c.wait = passiveCheckpointWait(v.checkpoint, v.flushing, c.atBusy.cp, target, deadline, window) + c.waited = time.Since(start) + if c.atBusy.cp < target && c.wait.cp <= c.atBusy.cp { + c.failed = "the checkpoint did not advance without another write" + if c.wait.blocked { + c.failed = "a checkpoint read blocked past the write's retry deadline (a store lock held)" + } + } + return c +} + +// lockedStore models the store's lock-taking getters: the first free reads +// answer at once, later ones take mu, which a test holds as a stalled +// checkpoint publication would. +type lockedStore struct { + mu sync.RWMutex + free int32 + reads atomic.Int32 + finished atomic.Int32 + cp, next atomic.Uint64 +} + +func (s *lockedStore) get(v *atomic.Uint64) func() uint64 { + return func() uint64 { + defer s.finished.Add(1) + if s.reads.Add(1) > s.free { + s.mu.RLock() + defer s.mu.RUnlock() + } + return v.Load() + } +} + +func (s *lockedStore) view() storeView { + return storeView{checkpoint: s.get(&s.cp), next: s.get(&s.next), flushing: func() bool { return true }} +} + +// join waits, bounded, for every read started to return (call it after mu is +// released): a read left running would outlive the test. +func (s *lockedStore) join(t *testing.T) { + t.Helper() + deadline := time.Now().Add(2 * time.Second) + for s.finished.Load() != s.reads.Load() { + if time.Now().After(deadline) { + t.Fatalf("store reads did not return after the lock was released: started=%d finished=%d", s.reads.Load(), s.finished.Load()) + } + time.Sleep(time.Millisecond) + } +} + +// heldUntil holds s.mu until release (nil: never), or until the test ends, +// and at the end joins every read it held. +func heldUntil(t *testing.T, s *lockedStore, release <-chan struct{}) { + s.mu.Lock() + stop, done := make(chan struct{}), make(chan struct{}) + go func() { + defer close(done) + select { + case <-release: + case <-stop: + } + s.mu.Unlock() + }() + t.Cleanup(func() { close(stop); <-done; s.join(t) }) +} + +// The passive wait waits for a read held behind the store lock: one released +// before the deadline returns the advanced checkpoint, not blocked; one held +// past it ends the wait at the deadline, not before. A flusher reported in a +// cycle the whole time does not extend the wait past the deadline. func TestPassiveCheckpointWaitIsBoundedByItsDeadline(t *testing.T) { const bound = 300 * time.Millisecond - release := make(chan struct{}) - defer close(release) - reads := 0 - blockingGet := func() uint64 { // the first read answers; later ones wait for the lock - if reads++; reads > 1 { - <-release - } - return 5 - } + const early = bound - 50*time.Millisecond // tolerant lower bound + const late = bound + 200*time.Millisecond inCycle := func() bool { return true } - start := time.Now() - r := passiveCheckpointWait(blockingGet, inCycle, 5, 9, start.Add(bound), 10*time.Second) - if took := time.Since(start); !r.blocked || r.cp > 5 || took > bound+200*time.Millisecond { - t.Fatalf("a read blocked behind a held lock: blocked=%v cp=%d after %s, want blocked at the %s deadline", r.blocked, r.cp, took, bound) - } - start = time.Now() - r = passiveCheckpointWait(func() uint64 { return 5 }, inCycle, 5, 9, start.Add(bound), 10*time.Second) - if took := time.Since(start); r.blocked || r.cp > 5 || took > bound+200*time.Millisecond { - t.Fatalf("a checkpoint that never advances with a flusher always in a cycle: blocked=%v cp=%d after %s, want the %s deadline", r.blocked, r.cp, took, bound) + + t.Run("released before the deadline", func(t *testing.T) { + s := &lockedStore{} + s.cp.Store(5) + release := make(chan struct{}) + heldUntil(t, s, release) + start := time.Now() + time.AfterFunc(bound/3, func() { s.cp.Store(7); close(release) }) + r := passiveCheckpointWait(s.get(&s.cp), inCycle, 5, 9, start.Add(bound), 10*time.Second) + if took := time.Since(start); r.blocked || r.cp != 7 || took < bound/3-20*time.Millisecond || took >= bound { + t.Fatalf("a read released at %v: blocked=%v cp=%d after %v, want cp=7 not blocked before the %v deadline", bound/3, r.blocked, r.cp, took, bound) + } + }) + t.Run("held past the deadline", func(t *testing.T) { + s := &lockedStore{free: 1} // the first read answers; later ones wait for the lock + s.cp.Store(5) + heldUntil(t, s, nil) + start := time.Now() + r := passiveCheckpointWait(s.get(&s.cp), inCycle, 5, 9, start.Add(bound), 10*time.Second) + if took := time.Since(start); !r.blocked || r.cp != 5 || took < early || took > late { + t.Fatalf("a read held behind the lock: blocked=%v cp=%d after %v, want blocked at the %v deadline", r.blocked, r.cp, took, bound) + } + }) + t.Run("never advances", func(t *testing.T) { + start := time.Now() + r := passiveCheckpointWait(func() uint64 { return 5 }, inCycle, 5, 9, start.Add(bound), 10*time.Second) + if took := time.Since(start); r.blocked || r.cp != 5 || took < early || took > late { + t.Fatalf("a checkpoint that never advances with a flusher always in a cycle: blocked=%v cp=%d after %v, want the %v deadline", r.blocked, r.cp, took, bound) + } + }) +} + +// The busy path itself, with the store lock held across the deadline at each +// of its observations (the checkpoint at the busy write, the target, a read in +// the wait), fails at the deadline and formats its report without a store +// read; released early, it passes with the advanced checkpoint. +func TestPassiveBusyCheckWithTheStoreLockHeld(t *testing.T) { + const bound = 300 * time.Millisecond + for _, tc := range []struct { + name string + free int32 + }{{"at the busy checkpoint read", 0}, {"at the target read", 1}, {"in the wait", 2}} { + t.Run(tc.name, func(t *testing.T) { + s := &lockedStore{free: tc.free} + s.cp.Store(5) + s.next.Store(10) + heldUntil(t, s, nil) + start := time.Now() + c := passiveBusyCheck(s.view(), start.Add(bound), 10*time.Second) + report := c.String() + took := time.Since(start) + if c.failed == "" || took < bound-50*time.Millisecond || took > bound+200*time.Millisecond { + t.Fatalf("lock held %s: failed=%q after %v (%s), want a failure at the %v deadline", tc.name, c.failed, took, report, bound) + } + if tc.free < 2 && (c.atBusy.ok || !bytes.Contains([]byte(report), []byte("cp=unavailable"))) { + t.Fatalf("lock held %s: the report must say unavailable: %s", tc.name, report) + } + if tc.free == 2 && (!c.wait.blocked || c.atBusy.cp != 5 || c.atBusy.next != 10) { + t.Fatalf("lock held %s: %s", tc.name, report) + } + }) } + t.Run("released before the deadline", func(t *testing.T) { + s := &lockedStore{free: 2} + s.cp.Store(5) + s.next.Store(10) + release := make(chan struct{}) + heldUntil(t, s, release) + start := time.Now() + time.AfterFunc(bound/3, func() { s.cp.Store(7); close(release) }) + c := passiveBusyCheck(s.view(), start.Add(bound), 10*time.Second) + if c.failed != "" || c.wait.blocked || c.wait.cp != 7 || time.Since(start) >= bound { + t.Fatalf("lock released at %v: failed=%q %s", bound/3, c.failed, c) + } + }) } // Public provider + actual NVMe handler, file-backed v3 with the default @@ -155,6 +343,13 @@ func runNoClientFlushBurst(t *testing.T, mode storage.WMode) { if err := ws.SetWMode(mode); err != nil { t.Fatal(err) } + // Getters only: no Write, Sync, NotifyUrgent or Close may drive progress + // before the first retry. Inside the write loop the store is observed only + // through bounded reads, so a held store lock cannot outlast a deadline. + view := storeView{checkpoint: ws.CheckpointLSN, next: ws.NextLSN, flushing: func() bool { + fi := ws.FlusherInstrumentation() + return fi.CyclesStarted > fi.CyclesSucceeded+fi.CyclesFailed + }} syncsBefore := ws.WALSyncCount() var durations []time.Duration busyCount := 0 @@ -170,7 +365,8 @@ func runNoClientFlushBurst(t *testing.T, mode storage.WMode) { } } retryDeadline := time.Now().Add(30 * time.Second) - cpBefore := ws.CheckpointLSN() + snapBefore := view.snapshot(retryDeadline) + last := snapBefore // diagnostics use the last observation, never a fresh read retried := false for { writeStart := time.Now() @@ -181,11 +377,11 @@ func runNoClientFlushBurst(t *testing.T, mode storage.WMode) { dump := make([]byte, 2<<20) n := runtime.Stack(dump, true) t.Logf("TIMEOUT STACKS\n%s", dump[:n]) - t.Fatalf("NO FLUSH exceeded wait bound bytes=%d attempt=%v total=%v busy=%d result=%+v cp=%d next=%d", off, elapsed, time.Since(start), busyCount, r, ws.CheckpointLSN(), ws.NextLSN()) + t.Fatalf("NO FLUSH exceeded wait bound bytes=%d attempt=%v total=%v busy=%d result=%+v last observed %v", off, elapsed, time.Since(start), busyCount, r, last) } if r.AsError() == nil { if retried { - t.Logf("NO FLUSH resumed bytes=%d cp=%d->%d next=%d syncs=%d busy=%d", off, cpBefore, ws.CheckpointLSN(), ws.NextLSN(), ws.WALSyncCount(), busyCount) + t.Logf("NO FLUSH resumed bytes=%d before: %v now: %v syncs=%d busy=%d", off, snapBefore, view.snapshot(retryDeadline), ws.WALSyncCount(), busyCount) } break } @@ -194,32 +390,21 @@ func runNoClientFlushBurst(t *testing.T, mode storage.WMode) { } busyCount++ retried = true - t.Logf("NO FLUSH retry bytes=%d busy=%d attempt=%v cp=%d next=%d syncs=%d", off, busyCount, elapsed, ws.CheckpointLSN(), ws.NextLSN(), ws.WALSyncCount()) + last = view.snapshot(retryDeadline) + t.Logf("NO FLUSH retry bytes=%d busy=%d attempt=%v %v syncs=%d", off, busyCount, elapsed, last, ws.WALSyncCount()) if busyCount == 1 { - passiveStart := time.Now() - cpAtBusy, ok := boundedRead(ws.CheckpointLSN, retryDeadline) - if !ok { - t.Fatalf("NO FLUSH the checkpoint read blocked past the write's retry deadline (a store lock held): bytes=%d busy=%d", off, busyCount) - } - target := ws.NextLSN() - 1 - // Getters only: no Write, Sync, NotifyUrgent or Close may - // drive progress before the first retry. - flushing := func() bool { - fi := ws.FlusherInstrumentation() - return fi.CyclesStarted > fi.CyclesSucceeded+fi.CyclesFailed - } - w := passiveCheckpointWait(ws.CheckpointLSN, flushing, cpAtBusy, target, retryDeadline, 10*time.Second) + c := passiveBusyCheck(view, retryDeadline, 10*time.Second) fi := ws.FlusherInstrumentation() - t.Logf("NO FLUSH passive bytes=%d wait=%v cpBefore=%d cpAtBusy=%d cpObserved=%d target=%d flusher: cycles started=%d succeeded=%d failed=%d max=%v, last in a cycle %v ago, read blocked=%v", off, time.Since(passiveStart), cpBefore, cpAtBusy, w.cp, target, fi.CyclesStarted, fi.CyclesSucceeded, fi.CyclesFailed, time.Duration(fi.CycleMaxDurationNanos), time.Since(w.lastInCycle), w.blocked) - if cpAtBusy < target && w.cp <= cpAtBusy { - t.Fatalf("NO FLUSH passive checkpoint did not advance without another write: bytes=%d cp=%d busy=%d (flusher in a cycle %v ago, cycles started=%d succeeded=%d failed=%d, read blocked=%v)", off, w.cp, busyCount, time.Since(w.lastInCycle), fi.CyclesStarted, fi.CyclesSucceeded, fi.CyclesFailed, w.blocked) + t.Logf("NO FLUSH passive bytes=%d before: %v %v flusher: cycles started=%d succeeded=%d failed=%d max=%v", off, snapBefore, c, fi.CyclesStarted, fi.CyclesSucceeded, fi.CyclesFailed, time.Duration(fi.CycleMaxDurationNanos)) + if c.failed != "" { + t.Fatalf("NO FLUSH passive check: %s: bytes=%d busy=%d %v", c.failed, off, busyCount, c) } } // Retry the identical command; no caller Flush drives reclamation. time.Sleep(25 * time.Millisecond) } if (off+transfer)%(16*1024*1024) == 0 { - t.Logf("NO FLUSH progress bytes=%d elapsed=%v cp=%d next=%d", off+transfer, time.Since(start), ws.CheckpointLSN(), ws.NextLSN()) + t.Logf("NO FLUSH progress bytes=%d elapsed=%v %v", off+transfer, time.Since(start), view.snapshot(time.Now().Add(5*time.Second))) } } // The expected bytes come from the submitted pattern, not another reader.