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) + } + }) + } +}