mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
transport: P1 — BlockExecutor WalShipper registry + Ship delegate
Per v3-recovery-wal-shipper-mini-plan.md §3 P1, with TDD discipline
(tests first, impl drives by failing tests):
Tests written first (wal_shipper_executor_test.go, RED phase):
TestBlockExecutor_WalShipperRegistry_GetOrCreate
spec §3 INV-SINGLE — WalShipperFor idempotent get-or-create;
different replicaIDs get distinct instances; concurrent calls
for same replicaID linearize through walShipperMu.
TestBlockExecutor_Ship_AdvancesWalShipperCursor
Ship() delegates to per-replica WalShipper. Observable via
cursor advancing per Ship call. Idempotent Ship with lsn ≤
cursor (NotifyAppend's idempotent-skip path) does NOT regress
cursor.
TestBlockExecutor_WalShipperFor_StableUnderConcurrent_Ship
Strong INV-SINGLE under load — 8 writer goroutines × 25 Ships
each (200 distinct LSNs) all funnel through the same shipper
instance; cursor reaches max LSN.
3a coverage (architect 2026-04-29 ratification): "仅 Ship 只走
WalShipper (mock transport)". Mock here is net.Pipe with
io.Copy(io.Discard, replicaConn) — Ship's wire writes succeed,
test asserts on cursor advancement (the delegation observable).
3b deferred per architect to nightly: "Sender + Ship integration
冒烟" (cross-component INV-NO-DOUBLE-LIVE). Will be added when
P2 wires recovery.Sender to call walShipper.StartSession /
DrainBacklog (replacing its own streamUntilHead).
Implementation (driven by failing tests):
core/transport/executor.go:
+ walShipperMu sync.Mutex
+ walShippers map[string]*walShipperEntry
+ walShipperEntry struct { shipper *WalShipper; emit conn ctx }
+ WalShipperFor(replicaID) *WalShipper — get-or-create; on
create, the EmitFunc closes over entry.emitConn (mu-guarded
pointer that Ship updates per-call)
+ updateWalShipperConn(replicaID, conn) — Ship's per-call
hook to set the conn the EmitFunc will use
core/transport/ship_sender.go:
Modified Ship():
- Lineage validation + epoch fence + lazy dial: UNCHANGED.
- After conn resolved + payload encoded: delegate to
WalShipperFor + updateWalShipperConn + NotifyAppend.
- The walShipper's EmitFunc does the WriteMsg under shipMu
(INV-NO-DOUBLE-LIVE at the wire path).
No behavior change for callers: Ship's signature, return
semantics (silent-drop on stale epoch, error on dial/write
failure, nil on success) all preserved.
Architectural notes:
- WalShipper.Activate(0) at first WalShipperFor — Realtime mode
immediate. First Ship's lsn > 0 emits cleanly.
- emitConn is mutable per-Ship: caller's session conn may change
over time (lazy dial, session re-attach). EmitFunc reads under
entry.emitCtxMu so mutations are visible.
- Idempotent NotifyAppend (lsn <= cursor) returns nil silently;
Ship caller can't distinguish from successful emit. Acceptable
for fire-and-forget contract.
Test sweep (TDD GREEN):
go test ./core/transport/ -run "TestBlockExecutor_(WalShipper|Ship_)"
→ 3 PASS, 0 FAIL.
Wider sweep:
go test ./core/transport/ ./core/recovery/ ./core/storage/...
./core/replication/component/ ./core/replication/
→ all PASS — no regression in existing executor / recovery /
storage / replication tests.
LOC delta:
+ executor.go ~70 LOC (registry struct + WalShipperFor +
updateWalShipperConn)
~ ship_sender.go ~10 LOC change (delegate via NotifyAppend)
+ wal_shipper_executor_test.go ~250 LOC (3 tests)
Next: P2 — recovery.Sender drops streamUntilHead/PushLiveWrite/
drainAndSeal; calls walShipper.StartSession + DrainBacklog
instead. TDD again, write tests first against the new
"recovery.Sender does NOT own WAL pump" contract.
This commit is contained in:
1 parent
c3d8907725
commit
0d3e3795d2
3 files changed
+330
-4
No files matched your search
@@ -38,6 +38,26 @@ type BlockExecutor struct {
|
||||
onFenceComplete adapter.OnFenceComplete
|
||||
sessions map[uint64]*activeSession
|
||||
stepDelay time.Duration
|
||||
|
||||
// walShippers is the per-replica WalShipper registry per
|
||||
// v3-recovery-wal-shipper-spec.md §3 INV-SINGLE: at most one
|
||||
// WalShipper per (volume, replicaID). Get-or-create via
|
||||
// WalShipperFor. Each entry owns a shipper + a mutable emit-conn
|
||||
// pointer that Ship() updates before delegating NotifyAppend.
|
||||
walShipperMu sync.Mutex
|
||||
walShippers map[string]*walShipperEntry
|
||||
}
|
||||
|
||||
// walShipperEntry pairs a per-replica WalShipper with its mutable
|
||||
// wire context (the conn that the WalShipper's EmitFunc will write
|
||||
// to at emit time). Ship() updates entry.emitConn under
|
||||
// emitCtxMu before delegating to the shipper; the EmitFunc reads
|
||||
// emitConn under the same mutex.
|
||||
type walShipperEntry struct {
|
||||
shipper *WalShipper
|
||||
|
||||
emitCtxMu sync.Mutex
|
||||
emitConn net.Conn
|
||||
}
|
||||
|
||||
// DualLaneConfig holds the wiring for routing StartRebuild through
|
||||
@@ -527,6 +547,69 @@ func (e *BlockExecutor) PublishDegraded(replicaID string, reason string) {
|
||||
log.Printf("executor: publish degraded for %s: %s", replicaID, reason)
|
||||
}
|
||||
|
||||
// WalShipperFor returns the per-replica WalShipper for replicaID,
|
||||
// creating one on first call. Idempotent: subsequent calls for the
|
||||
// same replicaID return the same instance (INV-SINGLE per
|
||||
// v3-recovery-wal-shipper-spec.md §3).
|
||||
//
|
||||
// The returned WalShipper is in Realtime mode immediately (Activate
|
||||
// at cursor=0). Steady-state Ship() delegates here. Recovery
|
||||
// sessions can later transition the same shipper into Backlog mode
|
||||
// via StartSession (P2 wiring).
|
||||
//
|
||||
// Concurrent calls for the same replicaID linearize through
|
||||
// walShipperMu; all callers observe the same instance.
|
||||
func (e *BlockExecutor) WalShipperFor(replicaID string) *WalShipper {
|
||||
e.walShipperMu.Lock()
|
||||
defer e.walShipperMu.Unlock()
|
||||
|
||||
if e.walShippers == nil {
|
||||
e.walShippers = make(map[string]*walShipperEntry)
|
||||
}
|
||||
if entry, ok := e.walShippers[replicaID]; ok {
|
||||
return entry.shipper
|
||||
}
|
||||
|
||||
entry := &walShipperEntry{}
|
||||
emit := func(lba uint32, lsn uint64, data []byte) error {
|
||||
entry.emitCtxMu.Lock()
|
||||
conn := entry.emitConn
|
||||
entry.emitCtxMu.Unlock()
|
||||
if conn == nil {
|
||||
return fmt.Errorf("transport: WalShipper for replica %s has no conn", replicaID)
|
||||
}
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(shipWriteDeadline))
|
||||
err := WriteMsg(conn, MsgShipEntry, data)
|
||||
_ = conn.SetWriteDeadline(time.Time{})
|
||||
return err
|
||||
}
|
||||
s := NewWalShipper(replicaID, HeadSourceFromStorage(e.primaryStore), e.primaryStore, emit)
|
||||
// Activate immediately into Realtime — first Ship's lsn > 0 will
|
||||
// emit cleanly. Recovery sessions later StartSession to transition
|
||||
// to Backlog.
|
||||
_ = s.Activate(0)
|
||||
entry.shipper = s
|
||||
e.walShippers[replicaID] = entry
|
||||
return s
|
||||
}
|
||||
|
||||
// updateWalShipperConn sets the wire conn that the per-replica
|
||||
// WalShipper's EmitFunc will use on its next emit. Ship() calls this
|
||||
// before delegating NotifyAppend; the EmitFunc reads emitConn at
|
||||
// emit time. No-op if the WalShipper hasn't been created yet (caller
|
||||
// will create it via WalShipperFor and re-call).
|
||||
func (e *BlockExecutor) updateWalShipperConn(replicaID string, conn net.Conn) {
|
||||
e.walShipperMu.Lock()
|
||||
entry, ok := e.walShippers[replicaID]
|
||||
e.walShipperMu.Unlock()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
entry.emitCtxMu.Lock()
|
||||
entry.emitConn = conn
|
||||
entry.emitCtxMu.Unlock()
|
||||
}
|
||||
|
||||
func (e *BlockExecutor) registerSession(lineage RecoveryLineage) (*activeSession, error) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
@@ -121,10 +121,18 @@ func (e *BlockExecutor) Ship(replicaID string, lineage RecoveryLineage, lba uint
|
||||
Data: data,
|
||||
})
|
||||
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(shipWriteDeadline))
|
||||
err := WriteMsg(conn, MsgShipEntry, payload)
|
||||
_ = conn.SetWriteDeadline(time.Time{})
|
||||
if err != nil {
|
||||
// P1 delegation: the WAL emit decision (whether/when this LSN
|
||||
// actually goes on the wire) belongs to the per-replica
|
||||
// WalShipper, not directly to Ship. We update the shipper's
|
||||
// emit-conn context with the conn we just resolved (above), then
|
||||
// hand the encoded payload to NotifyAppend. The shipper's
|
||||
// EmitFunc will WriteMsg under shipMu — INV-NO-DOUBLE-LIVE +
|
||||
// INV-MONOTONIC-CURSOR enforced at one place.
|
||||
//
|
||||
// Per v3-recovery-wal-shipper-mini-plan.md §3 P1.
|
||||
s := e.WalShipperFor(replicaID)
|
||||
e.updateWalShipperConn(replicaID, conn)
|
||||
if err := s.NotifyAppend(lba, lsn, payload); err != nil {
|
||||
return fmt.Errorf("transport: ship: write failed replica=%s lsn=%d: %w",
|
||||
replicaID, lsn, err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
package transport
|
||||
|
||||
// P1 — BlockExecutor ↔ WalShipper integration tests.
|
||||
//
|
||||
// Spec drivers:
|
||||
// - v3-recovery-wal-shipper-spec.md §3 INV-SINGLE — at most one
|
||||
// WalShipper instance per (volume, replicaID).
|
||||
// - v3-recovery-wal-shipper-mini-plan.md §3 P1 — "BlockExecutor
|
||||
// registry: create/destroy WalShipper with StartRebuild/
|
||||
// EndSession/steady attach; Ship delegates Emit".
|
||||
// - mini-plan §4 row "INV-NO-DOUBLE-LIVE (CHK surrogate) — Two
|
||||
// entrypoints: call WalShipper + legacy Ship mock — after P1,
|
||||
// Ship must not bypass (single emit counter)".
|
||||
//
|
||||
// Test scope (architect 2026-04-29 ratification):
|
||||
// - 3a: Ship() delegates to per-replica WalShipper — observable
|
||||
// via WalShipper.Cursor() advancing per Ship() call.
|
||||
// - 3b: Registry idempotent — WalShipperFor returns same instance
|
||||
// for same replicaID; different instances for different replicaIDs.
|
||||
// - 3c (deferred): cross-component "Sender + Ship same replica
|
||||
// INV-NO-DOUBLE-LIVE" integration — nightly / follow-up per
|
||||
// architect.
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweed-block/core/storage/memorywal"
|
||||
)
|
||||
|
||||
// TestBlockExecutor_WalShipperRegistry_GetOrCreate — spec §3 INV-SINGLE
|
||||
// at executor level.
|
||||
//
|
||||
// Idempotent get-or-create:
|
||||
// - First WalShipperFor(r1) creates and returns instance A.
|
||||
// - Second WalShipperFor(r1) returns SAME instance A.
|
||||
// - WalShipperFor(r2) returns DIFFERENT instance B.
|
||||
// - Concurrent WalShipperFor(r1) from N goroutines returns the
|
||||
// SAME instance to all callers.
|
||||
//
|
||||
// "INV-SINGLE: at any moment, at most one WalShipper per
|
||||
// (volumeID, replicaID)" — get-or-create satisfies this trivially.
|
||||
func TestBlockExecutor_WalShipperRegistry_GetOrCreate(t *testing.T) {
|
||||
primary := memorywal.NewStore(8, 4096)
|
||||
exec := NewBlockExecutor(primary, "127.0.0.1:0")
|
||||
|
||||
// First-time creation for r1.
|
||||
s1a := exec.WalShipperFor("r1")
|
||||
if s1a == nil {
|
||||
t.Fatal("WalShipperFor(r1) returned nil")
|
||||
}
|
||||
|
||||
// Second call for same replicaID returns SAME instance.
|
||||
s1b := exec.WalShipperFor("r1")
|
||||
if s1b != s1a {
|
||||
t.Errorf("WalShipperFor(r1) idempotency violated: first=%p second=%p", s1a, s1b)
|
||||
}
|
||||
|
||||
// Different replicaID returns DIFFERENT instance.
|
||||
s2 := exec.WalShipperFor("r2")
|
||||
if s2 == s1a {
|
||||
t.Error("WalShipperFor(r2) returned same instance as r1 — registry collision")
|
||||
}
|
||||
if s2 == nil {
|
||||
t.Fatal("WalShipperFor(r2) returned nil")
|
||||
}
|
||||
|
||||
// Concurrent get-or-create for same replicaID — all goroutines
|
||||
// must observe the same instance (or at most one goroutine wins
|
||||
// the create race; others see its result).
|
||||
const goroutines = 16
|
||||
var wg sync.WaitGroup
|
||||
results := make([]*WalShipper, goroutines)
|
||||
wg.Add(goroutines)
|
||||
for i := 0; i < goroutines; i++ {
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
results[idx] = exec.WalShipperFor("r3")
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// All concurrent calls must return the same instance.
|
||||
first := results[0]
|
||||
if first == nil {
|
||||
t.Fatal("concurrent WalShipperFor(r3): goroutine 0 got nil")
|
||||
}
|
||||
for i, got := range results {
|
||||
if got != first {
|
||||
t.Errorf("concurrent WalShipperFor(r3): goroutine %d got %p, want %p (registry race)", i, got, first)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBlockExecutor_Ship_AdvancesWalShipperCursor — mini-plan §3 P1
|
||||
// "Ship delegates Emit" + spec §3 INV-MONOTONIC-CURSOR.
|
||||
//
|
||||
// After Ship(replicaID, lineage, lba, lsn, data) returns nil, the
|
||||
// per-replica WalShipper's cursor MUST be lsn (or higher if a later
|
||||
// Ship has come in). Idempotent retries (same lsn) must NOT regress
|
||||
// cursor.
|
||||
//
|
||||
// This pins "Ship delegates to WalShipper" — observable via cursor —
|
||||
// without asserting on the wire format or transport. Wire-level
|
||||
// "exactly one emit per LSN" is mini-plan §4 INV-NO-DOUBLE-LIVE
|
||||
// (3c, deferred to integration).
|
||||
func TestBlockExecutor_Ship_AdvancesWalShipperCursor(t *testing.T) {
|
||||
primary := memorywal.NewStore(8, 4096)
|
||||
exec := NewBlockExecutor(primary, "")
|
||||
|
||||
// Set up a session with attached conn so Ship's lineage check
|
||||
// passes and the lazy-dial path is bypassed.
|
||||
lineage := RecoveryLineage{
|
||||
SessionID: 42,
|
||||
Epoch: 1,
|
||||
EndpointVersion: 1,
|
||||
TargetLSN: 1000,
|
||||
}
|
||||
session, err := exec.registerSession(lineage)
|
||||
if err != nil {
|
||||
t.Fatalf("registerSession: %v", err)
|
||||
}
|
||||
|
||||
// Real net.Pipe with drainer on the other side so Ship's wire
|
||||
// writes succeed. Drainer reads-and-discards; we don't assert on
|
||||
// the wire format here (that's existing ship_sender_test).
|
||||
primaryConn, replicaConn := net.Pipe()
|
||||
defer primaryConn.Close()
|
||||
defer replicaConn.Close()
|
||||
go io.Copy(io.Discard, replicaConn)
|
||||
|
||||
if err := exec.attachConn(session, primaryConn); err != nil {
|
||||
t.Fatalf("attachConn: %v", err)
|
||||
}
|
||||
|
||||
const replicaID = "r1"
|
||||
|
||||
// Ship LSN=10 → walShipper cursor must reach 10.
|
||||
if err := exec.Ship(replicaID, lineage, 0, 10, []byte{0xAA, 0xBB}); err != nil {
|
||||
t.Fatalf("Ship lsn=10: %v", err)
|
||||
}
|
||||
s := exec.WalShipperFor(replicaID)
|
||||
if got := s.Cursor(); got != 10 {
|
||||
t.Errorf("after Ship lsn=10: walShipper.Cursor()=%d want 10 (Ship must delegate to WalShipper)", got)
|
||||
}
|
||||
|
||||
// Ship LSN=20 → walShipper cursor advances.
|
||||
if err := exec.Ship(replicaID, lineage, 1, 20, []byte{0xCC}); err != nil {
|
||||
t.Fatalf("Ship lsn=20: %v", err)
|
||||
}
|
||||
if got := s.Cursor(); got != 20 {
|
||||
t.Errorf("after Ship lsn=20: walShipper.Cursor()=%d want 20", got)
|
||||
}
|
||||
|
||||
// Idempotent Ship with lsn=15 (less than current cursor=20) —
|
||||
// walShipper's NotifyAppend in Realtime drops it; cursor unchanged.
|
||||
// Ship should still return nil (caller can't tell idempotent skip
|
||||
// from successful emit at this layer).
|
||||
if err := exec.Ship(replicaID, lineage, 2, 15, []byte{0xDD}); err != nil {
|
||||
t.Fatalf("Ship lsn=15 (idempotent): %v", err)
|
||||
}
|
||||
if got := s.Cursor(); got != 20 {
|
||||
t.Errorf("after idempotent Ship lsn=15: walShipper.Cursor()=%d want 20 (no regress)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBlockExecutor_WalShipperFor_StableUnderConcurrent_Ship —
|
||||
// strong INV-SINGLE under concurrent Ship() to same replica.
|
||||
//
|
||||
// Concurrent Ship() calls for replicaID=r1 must all funnel through
|
||||
// the SAME WalShipper instance. If the registry races and creates a
|
||||
// second WalShipper, INV-SINGLE is violated.
|
||||
func TestBlockExecutor_WalShipperFor_StableUnderConcurrent_Ship(t *testing.T) {
|
||||
primary := memorywal.NewStore(64, 4096)
|
||||
exec := NewBlockExecutor(primary, "")
|
||||
|
||||
lineage := RecoveryLineage{
|
||||
SessionID: 7,
|
||||
Epoch: 1,
|
||||
EndpointVersion: 1,
|
||||
TargetLSN: 10000,
|
||||
}
|
||||
session, err := exec.registerSession(lineage)
|
||||
if err != nil {
|
||||
t.Fatalf("registerSession: %v", err)
|
||||
}
|
||||
primaryConn, replicaConn := net.Pipe()
|
||||
defer primaryConn.Close()
|
||||
defer replicaConn.Close()
|
||||
go io.Copy(io.Discard, replicaConn)
|
||||
if err := exec.attachConn(session, primaryConn); err != nil {
|
||||
t.Fatalf("attachConn: %v", err)
|
||||
}
|
||||
|
||||
const replicaID = "r1"
|
||||
const writers = 8
|
||||
const perWriter = 25
|
||||
|
||||
// First, capture the walShipper instance from main goroutine.
|
||||
expected := exec.WalShipperFor(replicaID)
|
||||
|
||||
// Drive concurrent Ship() — each writer ships distinct LSNs.
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < writers; w++ {
|
||||
wg.Add(1)
|
||||
go func(workerID int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < perWriter; i++ {
|
||||
// LSNs: 1..200, distinct per (worker, i).
|
||||
lsn := uint64(workerID*perWriter + i + 1)
|
||||
if err := exec.Ship(replicaID, lineage, uint32(i), lsn, []byte{byte(workerID)}); err != nil {
|
||||
t.Errorf("Ship: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// After concurrent Ships, the WalShipper for r1 must STILL be
|
||||
// the same instance — registry must have linearized.
|
||||
got := exec.WalShipperFor(replicaID)
|
||||
if got != expected {
|
||||
t.Errorf("concurrent Ship: WalShipperFor(r1) returned different instance after load (registry race)")
|
||||
}
|
||||
|
||||
// Cursor should be at the highest LSN any Ship saw.
|
||||
const maxLSN = uint64(writers * perWriter)
|
||||
if got := expected.Cursor(); got != maxLSN {
|
||||
t.Errorf("after %d concurrent Ships: cursor=%d want %d (some emit was lost or out of order)",
|
||||
writers*perWriter, got, maxLSN)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user