From 41045e542fe27f05dac69ef1f5a734d564a1840d Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 27 Apr 2026 01:18:15 -0700 Subject: [PATCH] G5-5 fix: latch StorageBackend.Identity post-Healthy via dp.Open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QA round 10 disambiguated: the iSCSI write reached StorageBackend.Write but gate-rejected with 'frontend: stale primary lineage'. Root cause: G5-5 introduced EnsureStorage which constructs the StorageBackend BEFORE the assignment fact lands. Original V3 design: dp.Open blocks on waitHealthy; the backend is constructed AFTER projection.Healthy=true; wrap() captures proj.Epoch/EV at that moment as the immutable Identity. G5-5 changed: replicas need open storage even when projection won't reach Healthy (their authority is the bound primary). EnsureStorage opens storage role-agnostically. But it ALSO constructs the backend at construction time — and at that moment view.Projection() returns zero values (no assignment landed yet). The captured Identity is zero. Once the assignment lands and projection flips Healthy with Epoch=1/EV=1, every subsequent gate compares proj{Epoch:1, EV:1, ...} against b.id{Epoch:0, ...} and returns ErrStalePrimary. V2 reference confirms: V2's iscsi.BlockDevice → vol.WriteLBA path uses the engine's CURRENT epoch (not a captured one), so V2 doesn't have this issue. V3's snapshot model is sound; what's missing is a re-stamp on first promotion to Healthy. Fix: add SetIdentity(id) to StorageBackend with latch-from-zero semantic. - Latch-from-zero: zero-id backend transitions to the supplied identity. One-shot — subsequent calls with a different id are REJECTED (returns false). Preserves the failover safety net (lineage drift past the latched value still fails closed via lineageCheck → ErrStalePrimary). - dp.Open's success path (after waitHealthy) now calls h.backend.SetIdentity() with the current projection's lineage. Idempotent for already-latched same-value backends. Test: TestG5_5_SetIdentity_LatchFromZero — covers (a) zero → first identity (allowed), (b) same identity again (no-op), (c) drift attempt (rejected, identity unchanged so lineageCheck fail-closed still works). Runs against both walstore + smartwal. Tests still green: durable (0.6s), blockvolume (16.5s, G5-4 integration test still PASS), master, authority. Refs: QA round 10 — gate-rejected disambiguation; V2 reference read confirms architectural diff (V2 vol.WriteLBA uses live epoch; V3 captures snapshot). --- .../core/frontend/durable/provider.go | 13 ++++++ .../core/frontend/durable/storage_adapter.go | 40 ++++++++++++++++-- .../frontend/durable/storage_adapter_test.go | 42 +++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index b3a987c779..2dcf3528e5 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -154,6 +154,19 @@ func (p *DurableProvider) Open(ctx context.Context, volumeID string) (frontend.B } if h, ok := p.volumes[volumeID]; ok { p.mu.Unlock() + // G5-5: latch the backend's Identity to the now-Healthy + // projection. If the backend was constructed via EnsureStorage + // pre-assignment, its captured Identity is zero-value and + // lineageCheck would reject every write. Latch-from-zero here + // installs the authoritative lineage; subsequent drift still + // fails closed (SetIdentity is single-shot from zero). + proj := p.view.Projection() + h.backend.SetIdentity(frontend.Identity{ + VolumeID: proj.VolumeID, + ReplicaID: proj.ReplicaID, + Epoch: proj.Epoch, + EndpointVersion: proj.EndpointVersion, + }) return h.backend, nil } p.mu.Unlock() diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index 97fc2ca277..aa36f879be 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -137,9 +137,43 @@ func NewStorageBackend(s storage.LogicalStorage, view frontend.ProjectionView, i return b } -// Identity returns the lineage captured at Open. Never re-reads -// from the projection — stable for the backend's lifetime. -func (b *StorageBackend) Identity() frontend.Identity { return b.id } +// Identity returns the captured lineage. Stable once the backend is +// promoted out of zero-state via SetIdentity (or initialized via +// NewStorageBackend with a non-zero id). +func (b *StorageBackend) Identity() frontend.Identity { + b.mu.Lock() + defer b.mu.Unlock() + return b.id +} + +// SetIdentity latches the backend's lineage from the engine-side +// authoritative projection. This is the bridge for the G5-5 wiring +// where DurableProvider.EnsureStorage constructs the backend BEFORE +// the assignment fact lands — at construction time view.Projection() +// returns zero values, so the backend's captured identity is also +// zero, and lineageCheck rejects every subsequent write with +// ErrStalePrimary because proj.Epoch>0 != id.Epoch=0. +// +// Semantic: latch-from-zero is permitted (transitions a freshly- +// constructed backend to its first authoritative lineage). Subsequent +// SetIdentity calls with a different lineage are REJECTED — the +// backend's identity is "captured at first promotion" and lineage +// drift past that point is what lineageCheck → ErrStalePrimary +// detects (failover safety net preserved). +// +// Returns true if the latch took effect (id was zero, now set). +// Returns false on no-op (already set to same value) and on rejected +// drift (already set to different value — caller should expect +// ErrStalePrimary on subsequent gate calls anyway). +func (b *StorageBackend) SetIdentity(id frontend.Identity) bool { + b.mu.Lock() + defer b.mu.Unlock() + if b.id.Epoch == 0 && b.id.EndpointVersion == 0 && b.id.ReplicaID == "" && b.id.VolumeID == "" { + b.id = id + return true + } + return false +} // Close marks the backend closed. All subsequent I/O + Sync // return ErrBackendClosed. Does NOT close the underlying diff --git a/seaweed-block/core/frontend/durable/storage_adapter_test.go b/seaweed-block/core/frontend/durable/storage_adapter_test.go index c4607c4d26..a77145d81a 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter_test.go +++ b/seaweed-block/core/frontend/durable/storage_adapter_test.go @@ -435,3 +435,45 @@ func TestT3a_MatrixCoverage(t *testing.T) { t.Fatalf("matrix drifted: got %s want %s", got, want) } } + +// G5-5 test: SetIdentity latches a zero-id backend to the assignment's +// lineage; subsequent calls with a different id are rejected (drift +// must still fail closed via lineageCheck → ErrStalePrimary). +func TestG5_5_SetIdentity_LatchFromZero(t *testing.T) { + for _, f := range logicalStorageFactories() { + f := f + t.Run(f.name, func(t *testing.T) { + s := f.make(t, 16, 4096) + zero := frontend.Identity{} + view := newStubView(frontend.Projection{ + VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1, Healthy: true, + }) + b := durable.NewStorageBackend(s, view, zero) + // Pre-latch: Identity is zero. + if got := b.Identity(); got.Epoch != 0 || got.ReplicaID != "" { + t.Fatalf("pre-latch: expected zero identity, got %+v", got) + } + // Latch from zero: must return true and apply. + id1 := frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 1, EndpointVersion: 1} + if !b.SetIdentity(id1) { + t.Fatal("SetIdentity from zero must return true") + } + if got := b.Identity(); got != id1 { + t.Fatalf("post-latch identity: got %+v want %+v", got, id1) + } + // Same value: no-op (returns false). + if b.SetIdentity(id1) { + t.Error("SetIdentity with same value must return false (idempotent no-op)") + } + // Drift: must NOT update; subsequent gate calls would + // fail-closed via lineageCheck against the latched id1. + id2 := frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 2, EndpointVersion: 1} + if b.SetIdentity(id2) { + t.Error("SetIdentity with drifted lineage must return false (only latch-from-zero allowed)") + } + if got := b.Identity(); got != id1 { + t.Errorf("drift attempt mutated identity: got %+v want %+v (id1)", got, id1) + } + }) + } +}