From 14a0d32d40aed426fb3b05cdcad7ee48f86affba Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 30 Apr 2026 08:56:49 -0700 Subject: [PATCH] =?UTF-8?q?docs:=20=C2=A76.8=20review-derived=20invariants?= =?UTF-8?q?=20=E2=80=94=20DisableTimerDrain=20+=20lock=20order?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Architect review of C1+C2+C3 (offline against §6.8 / §IV / §V) flagged two doc gaps that should be inline in the code, not just PR descriptions: 1. DisableTimerDrain (wal_shipper.go WalShipperConfig): - PRODUCTION MUST leave false. §6.8 #4 is MUST: "primary-idle starvation forbidden". Setting true in production violates architect contract. - Test-only invariant: shipper MUST NOT linger in cursor < head without alternative drain source. Today's only users are R2 saturation tests (scope-bounded). 2. Lock hierarchy (executor.go walShipperEntry.writeMu): - shipMu → writeMu (always; reversal risks deadlock). - WalShipper holds shipMu; calls EmitFunc under it; EmitFunc acquires writeMu. - recovery.Sender.writeFrame acquires only writeMu (does not hold shipMu). Order is consistent: shipMu outermost when both in play. No code logic change; documentation tightening so future readers can verify §6.8 mechanical SINGLE-SERIALIZER without re-deriving from grep. go test all four packages — PASS. --- seaweed-block/core/transport/executor.go | 15 ++++++++++++++ seaweed-block/core/transport/wal_shipper.go | 23 +++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/seaweed-block/core/transport/executor.go b/seaweed-block/core/transport/executor.go index 588acc758c..96900bd686 100644 --- a/seaweed-block/core/transport/executor.go +++ b/seaweed-block/core/transport/executor.go @@ -121,6 +121,21 @@ type walShipperEntry struct { // mutex is uncontended. In dual-lane sessions, recovery.Sender // duck-types `WriteMu() *sync.Mutex` on the sink and uses this // mutex for its own writeFrame calls. + // + // Lock hierarchy (architect §6.8 mechanical SINGLE-SERIALIZER): + // + // walShipper.shipMu ───> walShipperEntry.writeMu + // + // - WalShipper holds shipMu around mode/cursor mutations and + // calls EmitFunc under it (NotifyAppend Realtime, DrainBacklog + // scan callback, drainOpportunity scan callback). EmitFunc + // then acquires writeMu for the wire write — shipMu is + // OUTERMOST. + // - recovery.Sender.writeFrame acquires only writeMu (Sender + // does not hold shipMu). + // + // Therefore: any code path that holds shipMu may acquire writeMu, + // but NEVER the reverse. Reversing this order risks deadlock. writeMu sync.Mutex } diff --git a/seaweed-block/core/transport/wal_shipper.go b/seaweed-block/core/transport/wal_shipper.go index 88ee3958b3..28f375a2aa 100644 --- a/seaweed-block/core/transport/wal_shipper.go +++ b/seaweed-block/core/transport/wal_shipper.go @@ -148,10 +148,25 @@ type WalShipperConfig struct { OnSaturation func(replicaID string, lag uint64) // DisableTimerDrain, if true, suppresses the C2 §6.8 #4 self- - // driving Backlog drain goroutine. Tests that need manual control - // over DrainBacklog timing (e.g., R2 saturation tests that drive - // lag without letting drain catch up) set this. Production - // callers leave it false (default). + // driving Backlog drain goroutine. + // + // PRODUCTION CALLERS MUST LEAVE THIS false. §6.8 #4 is a MUST: + // "primary-idle starvation forbidden" — backlog cannot depend + // solely on new appends. Setting DisableTimerDrain=true in + // production violates the architect contract. + // + // Test-only invariant when set true: + // "the WalShipper MUST NOT linger in cursor < head without + // an alternative drain source" — i.e., the test itself drives + // DrainBacklog explicitly, OR the test asserts on a + // transient-debt state and tears the shipper down before + // the assertion would observe starvation. + // + // Today's only callers are core/transport's R2 saturation tests + // (TestWalShipper_R2_LagSignalFires / NoSpuriousSignal): they + // build lag by writing without a manual DrainBacklog call, + // observe OnSaturation fires, then tear down. Within the test + // scope, debt is intentional and bounded. DisableTimerDrain bool }