diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 2dcf3528e5..6057e222b2 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -29,6 +29,7 @@ import ( "errors" "fmt" "io" + "log" "os" "path/filepath" "sync" @@ -156,11 +157,13 @@ func (p *DurableProvider) Open(ctx context.Context, volumeID string) (frontend.B 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). + // pre-assignment, its captured Identity has Epoch=0 even though + // VolumeID/ReplicaID came from CLI config at construction. + // SetIdentity transitions Epoch=0 → live projection's Epoch/EV; + // subsequent drift still fails closed. proj := p.view.Projection() + log.Printf("durable: dp.Open latching identity from projection volume=%s replica=%s epoch=%d ev=%d (post-waitHealthy)", + proj.VolumeID, proj.ReplicaID, proj.Epoch, proj.EndpointVersion) h.backend.SetIdentity(frontend.Identity{ VolumeID: proj.VolumeID, ReplicaID: proj.ReplicaID, diff --git a/seaweed-block/core/frontend/durable/storage_adapter.go b/seaweed-block/core/frontend/durable/storage_adapter.go index aa36f879be..df0d2e28c6 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter.go +++ b/seaweed-block/core/frontend/durable/storage_adapter.go @@ -149,30 +149,47 @@ func (b *StorageBackend) Identity() frontend.Identity { // 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. +// the assignment fact lands. // -// 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). +// Pre-assignment state shape (round-11 finding): wrap() captures +// `view.Projection()` at construction. AdapterProjectionView reports +// VolumeID/ReplicaID from CLI config (non-empty even pre-assignment) +// but Epoch=0/EV=0 (engine state is zero-value until OnAssignment +// runs). So a freshly-constructed backend has b.id = {v1, r1, 0, 0} +// — partially-zero, not all-zero. The latch condition keys on +// Epoch=0 (the meaningful "no authority line yet" indicator); +// VolumeID/ReplicaID match is asserted but unchanged. // -// 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). +// Semantic: latch when Epoch is currently zero (transitions from +// pre-assignment to first authoritative lineage). Subsequent calls +// with a different lineage are REJECTED — drift past the latched +// value still fails closed via lineageCheck → ErrStalePrimary +// (failover safety net preserved). +// +// Returns true if the latch took effect (Epoch was 0, now set). +// Returns false on no-op (already at this Epoch+EV) and on rejected +// drift (already latched to a different non-zero Epoch+EV). 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 + // Reject drift past a non-zero latched Identity (preserves the + // failover safety net — lineage drift = ErrStalePrimary). + if b.id.Epoch != 0 || b.id.EndpointVersion != 0 { + return false } - return false + // VolumeID/ReplicaID came from CLI config at backend construction + // time and don't change; refuse silently if caller tries to install + // a different volume/replica (paranoia — should never happen in + // production). + if (b.id.VolumeID != "" && b.id.VolumeID != id.VolumeID) || + (b.id.ReplicaID != "" && b.id.ReplicaID != id.ReplicaID) { + return false + } + // Latch. + b.id = id + log.Printf("durable: StorageBackend.SetIdentity latched volume=%s replica=%s epoch=%d ev=%d", + id.VolumeID, id.ReplicaID, id.Epoch, id.EndpointVersion) + return true } // Close marks the backend closed. All subsequent I/O + Sync diff --git a/seaweed-block/core/frontend/durable/storage_adapter_test.go b/seaweed-block/core/frontend/durable/storage_adapter_test.go index a77145d81a..68520e842e 100644 --- a/seaweed-block/core/frontend/durable/storage_adapter_test.go +++ b/seaweed-block/core/frontend/durable/storage_adapter_test.go @@ -436,43 +436,40 @@ func TestT3a_MatrixCoverage(t *testing.T) { } } -// 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) { +// G5-5 test: SetIdentity latches a zero-Epoch backend (the +// pre-assignment shape that EnsureStorage produces — VolumeID + +// ReplicaID set from CLI config, Epoch=0+EV=0). Drift past the +// latched value is rejected so lineageCheck fail-closed still +// catches failover. +func TestG5_5_SetIdentity_LatchFromZeroEpoch(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{} + preAssignment := frontend.Identity{VolumeID: "v1", ReplicaID: "r1", Epoch: 0, EndpointVersion: 0} 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) + b := durable.NewStorageBackend(s, view, preAssignment) + if got := b.Identity(); got.Epoch != 0 || got.VolumeID != "v1" { + t.Fatalf("pre-latch: expected {v1, r1, 0, 0}, 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") + t.Fatal("SetIdentity from Epoch=0 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)") + t.Error("SetIdentity after latch must return false") } - // 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)") + t.Error("SetIdentity drift must return false (lineageCheck still fail-closed)") } if got := b.Identity(); got != id1 { - t.Errorf("drift attempt mutated identity: got %+v want %+v (id1)", got, id1) + t.Errorf("drift attempt mutated identity: got %+v want %+v", got, id1) } }) }