diff --git a/seaweed-block/core/host/volume/assignment_holder.go b/seaweed-block/core/host/volume/assignment_holder.go index dedbc7ec0c..c2f6928268 100644 --- a/seaweed-block/core/host/volume/assignment_holder.go +++ b/seaweed-block/core/host/volume/assignment_holder.go @@ -10,17 +10,3 @@ func (h *Host) assignmentNamesSelf(f *control.AssignmentFact) bool { (f.GetDataAddr() == "" || f.GetDataAddr() == h.cfg.DataAddr) && (f.GetCtrlAddr() == "" || f.GetCtrlAddr() == h.cfg.CtrlAddr) } - -// clearOwnEndpointRefusal implements M3's successful self-primary admission -// under INV-AUTH-HOLDER-IDENTITY-001. A rejected endpoint is not a different -// holder: a subsequent valid fact may supersede it, but never a foreign-server -// fence or a newer lineage. Caller has validated assignmentNamesSelf. -func (h *Host) clearOwnEndpointRefusal(f *control.AssignmentFact) { - h.otherMu.Lock() - defer h.otherMu.Unlock() - old := h.lastOther - if old != nil && old.GetServerId() == h.cfg.ServerID && old.GetReplicaId() == h.cfg.ReplicaID && - (f.Epoch > old.Epoch || f.Epoch == old.Epoch && f.EndpointVersion >= old.EndpointVersion) { - h.lastOther = nil - } -} diff --git a/seaweed-block/core/host/volume/assignment_holder_test.go b/seaweed-block/core/host/volume/assignment_holder_test.go index 12d76bbf41..c91b7bde44 100644 --- a/seaweed-block/core/host/volume/assignment_holder_test.go +++ b/seaweed-block/core/host/volume/assignment_holder_test.go @@ -1,8 +1,8 @@ package volume import ( - "fmt" "strings" + "sync/atomic" "testing" "time" @@ -130,60 +130,71 @@ func TestAssignmentHolderIdentityRefusesCarriedEndpointMismatch(t *testing.T) { } } -// INV-AUTH-HOLDER-IDENTITY-001 / M3 Unassigned -> Primary: recovery replay -// of a valid new holder must not inherit a foreign-holder fence from an -// endpoint-mismatched delivery naming that same server and replica. -func TestAssignmentHolderIdentityPlannedHandoffBeforeReadiness(t *testing.T) { - h, err := New(Config{MasterAddr: "127.0.0.1:1", ServerID: "new-host", VolumeID: "v1", ReplicaID: "r1", DataAddr: "127.0.0.1:19303", CtrlAddr: "127.0.0.1:19304", EnableT1Readiness: true}) - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { _ = h.Close() }) - h.BlockLocalReadiness("awaiting durable recovery") - own := holderAssignment(t, "new-host") - h.applyFact(own) - mismatch := holderAssignment(t, "new-host") - mismatch.DataAddr = "127.0.0.1:9991" - h.applyFact(mismatch) - if h.realAdpt.Projection().Epoch != 0 { - t.Fatal("assignment entered adapter before readiness") - } - h.ClearLocalReadinessBlock() - h.SetReplicationVolume(nil) - h.replayLocalReadinessFactIfReady() - deadline := time.Now().Add(time.Second) - for !h.view.Projection().Healthy && time.Now().Before(deadline) { - time.Sleep(time.Millisecond) - } - if p := h.view.Projection(); !p.Healthy || p.Epoch != 2 { - t.Fatalf("own clean-move assignment stayed fenced after readiness replay: %+v; other=%+v", p, h.LastOtherLine()) - } -} +type holderFenceCounter struct{ calls atomic.Uint32 } -// A same-holder endpoint refusal can be superseded only by a valid current -// fact; neither a true foreign holder nor a newer refusal is erased. -func TestAssignmentHolderIdentityReplayCannotClearForeignOrNewerFence(t *testing.T) { - for _, foreign := range []bool{false, true} { - t.Run(fmt.Sprint("foreign=", foreign), func(t *testing.T) { +func (f *holderFenceCounter) FenceAsPrimary(uint64) error { f.calls.Add(1); return nil } +func (f *holderFenceCounter) AdmitAsReplica(uint64) error { f.calls.Add(1); return nil } + +// INV-AUTH-HOLDER-IDENTITY-001 / M3: a recorded foreign fact supersedes +// only an adopted own lineage and only when it is strictly newer. +func TestAssignmentHolderIdentityForeignStreamOrdering(t *testing.T) { + for _, mode := range []string{"target-old-first", "served-newer-foreign", "served-old-replay", "never-assigned"} { + t.Run(mode, func(t *testing.T) { h, err := New(Config{MasterAddr: "127.0.0.1:1", ServerID: "new-host", VolumeID: "v1", ReplicaID: "r1", DataAddr: "127.0.0.1:19303", CtrlAddr: "127.0.0.1:19304", EnableT1Readiness: true}) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = h.Close() }) - h.BlockLocalReadiness("awaiting durable recovery") - h.applyFact(holderAssignment(t, "new-host")) - refused := holderAssignment(t, "new-host") - if foreign { - refused.ServerId = "another-host" - } else { - refused.DataAddr = "127.0.0.1:9991" - refused.EndpointVersion = 2 + fencer := &holderFenceCounter{} + if err := h.SetSelfFence(fencer); err != nil { + t.Fatal(err) } - h.applyFact(refused) - h.ClearLocalReadinessBlock() - h.replayLocalReadinessFactIfReady() - if h.LastOtherLine() == nil || !h.IsSuperseded("r1", 2, 1) || h.view.Projection().Healthy { - t.Fatal("replay erased a foreign or newer fence") + own := holderAssignment(t, "new-host") + foreign := holderAssignment(t, "old-host") + foreign.Epoch = 1 + waitHealthy := func() { + t.Helper() + deadline := time.Now().Add(time.Second) + for !h.view.Projection().Healthy && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + if p := h.view.Projection(); !p.Healthy || p.Epoch != 2 { + t.Fatalf("rightful target did not serve: %+v", p) + } + } + switch mode { + case "target-old-first": + h.BlockLocalReadiness("awaiting durable recovery") + h.applyFact(foreign) + if fencer.calls.Load() != 0 || h.IsSuperseded("r1", 0, 0) { + t.Fatal("unassigned target was fenced by the current old line") + } + h.applyFact(own) + h.ClearLocalReadinessBlock() + h.replayLocalReadinessFactIfReady() + waitHealthy() + case "never-assigned": + h.applyFact(foreign) + foreign.Epoch = 2 + h.applyFact(foreign) + if fencer.calls.Load() != 0 || h.realAdpt.Projection().Epoch != 0 || h.view.Projection().Healthy || h.IsSuperseded("r1", 0, 0) || h.LastOtherLine() == nil { + t.Fatal("never-assigned process must record, wait, neither adopt nor fence") + } + default: + h.applyFact(own) + waitHealthy() + before := fencer.calls.Load() + if mode == "served-newer-foreign" { + foreign.Epoch = 3 + } + h.applyFact(foreign) + if mode == "served-newer-foreign" { + if fencer.calls.Load() <= before || h.view.Projection().Healthy { + t.Fatal("served holder was not fenced by newer foreign line") + } + } else if fencer.calls.Load() != before || !h.view.Projection().Healthy { + t.Fatal("old foreign replay fenced a serving holder") + } } }) } diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index a642dae41c..042c731eba 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -862,8 +862,6 @@ func (h *Host) applyFact(fact *control.AssignmentFact) { return } - h.clearOwnEndpointRefusal(fact) - // The recovery watchdog runs from the primary assignment, before any // admission step can fail (#128 rule 6). h.wd.primaryAssigned(fact.Epoch) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 808d417cb2..cd3380a110 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -132,7 +132,7 @@ RF2 qualification limitation until ordered-live certification is wired (#297). S |---|---|---|---|---| | any | (no change) | local readiness blocked (recovery pending or failed): the fact is stored and replayed when readiness clears | `core/host/volume/host.go` `applyFact`, `replayLocalReadinessFactIfReady` | `INV-ROLE-READINESS-BLOCK-001` | | any | (no change) | a role call below the current desired epoch is refused and does not clear a newer admission failure | `core/host/volume/host.go` `fenceSelfWithPeers`, `latchRoleAdmission` | `INV-ROLE-STALE-REPLAY-001` | -| Unassigned, Supporting | Primary at e | the primary fact names this process server and replica via assignmentNamesSelf, with matching data/control endpoints when carried; missing/foreign server is refused; a valid equal/newer self fact clears only a prior same-holder endpoint refusal (never a foreign holder or newer line); the replica set is installed; the listener is fenced as primary and its floor e is persisted before the adapter is told | `core/host/volume/host.go` `applyFact`, `fenceSelfWithPeers`; `core/transport/replica.go` `FenceAsPrimary`, `raiseFloor` | `INV-FENCE-SELF-ON-PROMOTE-001`, `INV-FENCE-FLOOR-DURABLE-001`, `INV-AUTH-HOLDER-IDENTITY-001` | +| Unassigned, Supporting | Primary at e | the primary fact names this process server and replica via assignmentNamesSelf, with matching data/control endpoints when carried; missing/foreign server is refused; the replica set is installed; the listener is fenced as primary and its floor e is persisted before the adapter is told | `core/host/volume/host.go` `applyFact`, `fenceSelfWithPeers`; `core/transport/replica.go` `FenceAsPrimary`, `raiseFloor` | `INV-FENCE-SELF-ON-PROMOTE-001`, `INV-FENCE-FLOOR-DURABLE-001`, `INV-AUTH-HOLDER-IDENTITY-001` | | Unassigned, Primary | Supporting at e | the fact names another primary and lists this replica as a peer; the floor e is persisted first; the frontend stays gated | `core/host/volume/host.go` `applyFact`; `core/transport/replica.go` `AdmitAsReplica` | `INV-FENCE-FLOOR-DURABLE-001` | | Primary, Supporting | Excluded | the fact names another primary and does not list this replica, or names this replica on a missing/foreign server or mismatched carried endpoints; a same-replica foreign line at or above local epoch/endpoint-version gates the projection; ingress is closed, the other line is recorded and the frontend is unhealthy | `core/host/volume/host.go` `applyFact`, `recordOtherLine`; `core/host/volume/projection_bridge.go` `projectionWithSupersede` | `INV-ROLE-SUPERSEDED-001`, `INV-FENCE-STALE-PRIMARY-001`, `INV-AUTH-HOLDER-IDENTITY-001` | | Excluded | Excluded (no change) | an older recorded foreign fact cannot replace the newer epoch/endpoint-version fence; a stale replay cannot re-enable the frontend | `core/host/volume/host.go` `recordOtherLine`, `IsSuperseded` | `INV-AUTH-HOLDER-IDENTITY-001` | @@ -575,7 +575,7 @@ Notes: | ID | Statement | Machine / transition | Enforced in | Test | TLA+ property | Status | |---|---|---|---|---|---|---| -| `INV-AUTH-HOLDER-IDENTITY-001` | A primary holder is the pair (server id, replica id): every site that decides holdership (adopting a fact, lease first grant, renew, re-grant, HEAD claim, superseded exit) compares both; a missing server id is refused, never guessed. | AUTHORITY: PRIMARY holdership (supporting replicas are not holders) | `core/host/volume/host.go` `applyFact`, `IsSuperseded`, `recordOtherLine`; `assignment_holder.go` `assignmentNamesSelf`, `clearOwnEndpointRefusal`; `core/host/master/services.go` `SubscribeAssignments`; `workspace_service.go` `holderMatches`, `RenewWorkspaceLease`, `AckWorkspaceRevoked`; `cmd/blockvolume/chunk_remote.go` `chunkHolder`; `core/storage/chunk_restore.go` `ClaimChunkHead`; superseded exit covered by #409 | `TestAssignmentHolderIdentityRestartedOldProcess`, `TestAssignmentHolderIdentityMatchingServerStillAdopts`, `TestAssignmentHolderIdentityFencesPreviouslyHealthySameReplica`, `TestAssignmentHolderIdentityRefusesCarriedEndpointMismatch`, `TestAssignmentHolderIdentityPlannedHandoffBeforeReadiness`, `TestAssignmentHolderIdentityReplayCannotClearForeignOrNewerFence` (`core/host/volume/assignment_holder_test.go`); `TestWorkspaceLeaseGuardCarriesHolderServerIdentity` (`core/host/volume/workspace_holder_identity_test.go`); `TestWorkspaceLeaseRequiresHolderServerForGrantRenewAndRegrant`, `TestWorkspaceRevocationACKRequiresHolderServer` (`core/host/master/workspace_holder_identity_test.go`); `TestWorkspaceRPCCleanMovePreservesReplicaIdentity` (`core/host/master/workspace_service_test.go`); HEAD claim: `TestChunkHolderIsServerAndReplica` (`cmd/blockvolume/chunk_remote_test.go`), `TestWALStoreChunk_SameReplicaOnAnotherServerIsFenced` (`core/frontend/durable/provider_chunk_holder_test.go`); superseded exit: covered by #409 (server-id/replica-id pair, never replica alone); supporting replicas are not holders; descriptor identity not in this row | - (unit and RPC identity boundary) | ACTIVE, `INV-AUTH-HOLDER-IDENTITY-001` | +| `INV-AUTH-HOLDER-IDENTITY-001` | A primary holder is the pair (server id, replica id): every site that decides holdership (adopting a fact, lease first grant, renew, re-grant, HEAD claim, superseded exit) compares both; a missing server id is refused, never guessed. | AUTHORITY: PRIMARY holdership (supporting replicas are not holders) | `core/host/volume/host.go` `applyFact`, `IsSuperseded`, `recordOtherLine`; `assignment_holder.go` `assignmentNamesSelf`; `core/host/master/services.go` `SubscribeAssignments`; `workspace_service.go` `holderMatches`, `RenewWorkspaceLease`, `AckWorkspaceRevoked`; `cmd/blockvolume/chunk_remote.go` `chunkHolder`; `core/storage/chunk_restore.go` `ClaimChunkHead`; superseded exit covered by #409 | `TestAssignmentHolderIdentityRestartedOldProcess`, `TestAssignmentHolderIdentityMatchingServerStillAdopts`, `TestAssignmentHolderIdentityFencesPreviouslyHealthySameReplica`, `TestAssignmentHolderIdentityRefusesCarriedEndpointMismatch` (`core/host/volume/assignment_holder_test.go`); `TestWorkspaceLeaseGuardCarriesHolderServerIdentity` (`core/host/volume/workspace_holder_identity_test.go`); `TestWorkspaceLeaseRequiresHolderServerForGrantRenewAndRegrant`, `TestWorkspaceRevocationACKRequiresHolderServer` (`core/host/master/workspace_holder_identity_test.go`); `TestWorkspaceRPCCleanMovePreservesReplicaIdentity` (`core/host/master/workspace_service_test.go`); HEAD claim: `TestChunkHolderIsServerAndReplica` (`cmd/blockvolume/chunk_remote_test.go`), `TestWALStoreChunk_SameReplicaOnAnotherServerIsFenced` (`core/frontend/durable/provider_chunk_holder_test.go`); superseded exit: covered by #409 (server-id/replica-id pair, never replica alone); supporting replicas are not holders; descriptor identity not in this row | - (unit and RPC identity boundary) | ACTIVE, `INV-AUTH-HOLDER-IDENTITY-001` | | `INV-AUTH-EPOCH-MINT-001` | Only the publisher mints epochs; a reassignment takes the per-volume maximum epoch plus one, also after a restart. | M2 Bound → Reassigned | `core/authority/authority.go` `applyWithWorkspaceMove`, `maxPublishedEpochForVolume` | `TestG8_0_Publisher_ReassignAdvancesPerVolumeEpochAcrossReplicaSlots` (`core/authority/g8_0_authority_failover_test.go`); `TestDurableAuthority_PublisherAdvancesFromReloaded` (`core/authority/authority_restart_test.go`); `TestG9F2_AssignmentRequestShapeHasNoEpochOrEndpointVersion` (`core/host/master/authority_request_bridge_test.go`) | `ControlPlane` `UniqueEpoch` | ACTIVE | | `INV-AUTH-PERSIST-BEFORE-PUBLISH-001` | With an authority store configured, an assignment is written to the store before memory or any subscriber sees it, and a failed write does not publish it: see M2 any → (store write first). | M2 any → (store write first) | `core/authority/authority.go` `applyWithWorkspaceMove` | `TestDurableAuthority_PutFailureRollsBackInMemoryState` (`core/authority/authority_restart_test.go`) | `ControlPlane` `UniqueEpoch` | ACTIVE | | `INV-AUTH-MOVE-JOURNAL-001` | A workspace move's epoch is issued only after its journal record is durable, and one move request id gets exactly one epoch, also across restarts. | M2 MoveReserved → MoveIssued | `core/authority/workspace.go` `prepareWorkspaceMoveLocked`, `MoveWorkspace`, `sameWorkspaceRequest` | `TestWorkspaceMoveCannotIssueEpochWithoutJournal`, `TestWorkspaceCrashAfterJournalBeforeEpochResumesSameGrant` (`core/authority/workspace_test.go`); `TestWorkspaceReplayedIssueStartsGranteeLease` (`core/authority/workspace_unclaimed_grant_test.go`) | `ControlPlane` `Idempotent`, `JournalComplete` | ACTIVE |