G5-5 fix: latch StorageBackend.Identity post-Healthy via dp.Open

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).
This commit is contained in:
pingqiu committed 2026-04-27 01:18:15 -07:00
1 parent 4ec7bd0c2e
commit 41045e542f
3 files changed
+92 -3

No files matched your search

@@ -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()
@@ -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
@@ -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)
}
})
}
}