diff --git a/.claude/rules/cluster-topology.md b/.claude/rules/cluster-topology.md index f91d99a30..830a70e4b 100644 --- a/.claude/rules/cluster-topology.md +++ b/.claude/rules/cluster-topology.md @@ -152,7 +152,9 @@ Things that carried over unchanged from the `ClusterClient` era, because they we — central's gRPC fan dials only those; on DPS the others ignore it. ⚠️ Never re-send to an acked node (#601: a same-revision dispatch re-applies a degraded revision); the node treats a re-send idempotently per `DeploymentId` (re-acks if answered, ignores while applying / while its ack is held) — except a remembered - **Failed** ack, which it retries (#1111; only while that deployment is its latest answer). Re-sends carry + **Failed** ack, which it retries (#1111; only while that deployment is its latest answer and no other ack is held). + Central never lets a late `Failed` overwrite that node's arrived `Applied` for the same deployment (a stale + duplicate); a late `Failed` that closes the deployment first is a documented residual. Re-sends carry `DeadlineRemaining` (proto field 5, additive) so a late-reached node bounds its address-space wait to fit. Remembered acks are in-memory only (a restarted node treats a re-send as fresh). The apply deadline and boot-time replay remain the backstops. `docs/Redundancy.md` §"Dispatch re-send". diff --git a/docs/Redundancy.md b/docs/Redundancy.md index 6d1f01ba4..308e1d1ee 100644 --- a/docs/Redundancy.md +++ b/docs/Redundancy.md @@ -844,6 +844,25 @@ dial was dropped — either way the deployment sat until the 120 s deadline and that deployment is still the node's **latest** answer with no other apply's ack held — a stale re-send of an older failed deployment after a newer one was applied replays `Failed` rather than rolling the node back. `Applied` and `Applied`+degraded acks keep the replay, so #601's "no repeated re-apply of a degraded revision on re-sends" holds. +- **Central: Applied wins over a late Failed (#1111 review).** Because the node re-applies only after its own `Failed`, + two acks from one node for one deployment can now differ — and the ORIGINAL `Failed` can still arrive after the + retry's `Applied` (`GrpcApplyAckSender` walks the central endpoints, and a call that timed out may still have + landed). Within one deployment `ConfigPublishCoordinator` therefore never lets a `Failed` overwrite that node's + `Applied` (clean or degraded) once the Applied has **arrived** — counted, being persisted, parked behind a write, or + held for a persist retry. The late `Failed` is discarded before its row write (logged at Info), so neither the count + nor the node's `NodeDeploymentState` row flips back. The reverse is unchanged: an `Applied` arriving after a counted + `Failed` still replaces it, as long as the deployment is open. +- **Residual: a late `Failed` that CLOSES the deployment first.** If the stale `Failed` reaches central before the + retry's `Applied` and it completes the expected set (a one-node cluster, or the peer already acked), the deployment is + marked `PartiallyFailed` at once and the retry's `Applied` is then discarded as a stale ack ("Discarding stale ApplyAck"). + The node runs the new revision, but central's `Deployment` row reads `PartiallyFailed` and that node's + `NodeDeploymentState` row reads `Failed` (on a fused `Direct` node the node's own row write races central's, so it may + read either). Deliberately NOT corrected after the fact: re-opening a terminal deployment would break + "a terminal outcome is written once" (review L8), sealing it late would give an older deployment a newer + `SealedAtUtc` than one dispatched since — and "latest sealed" drives drift detection and the publish fallback — and + the coordinator keeps no per-deployment state past closure to judge it with. It needs a re-send to reach the node + after its `Failed` was delivered-but-unacknowledged, so it is narrow; it heals on the next deploy (a dispatch of the + same revision short-circuits to `Applied` on a node whose address space is confirmed, and re-applies otherwise). - **A re-send carries what is left of the deadline (#1111).** `DispatchDeployment.DeadlineRemaining` (proto `optional int64 deadline_remaining_ms = 5`, additive) is set on every re-send — a RELATIVE duration, so central's and the node's clocks need not agree — and left unset on the original. A node whose apply was started by such a diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.ControlPlane/Coordinators/ConfigPublishCoordinator.cs b/src/Server/ZB.MOM.WW.OtOpcUa.ControlPlane/Coordinators/ConfigPublishCoordinator.cs index ce8bcbcc6..41f32fff2 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.ControlPlane/Coordinators/ConfigPublishCoordinator.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.ControlPlane/Coordinators/ConfigPublishCoordinator.cs @@ -994,11 +994,41 @@ public sealed class ConfigPublishCoordinator : ReceiveActor, IWithTimers, IWithU return; } + // #1111 review: within one deployment an Applied (clean or degraded) ack from a node is never overwritten by a + // later Failed from that node. A node re-applies a deployment only after its OWN Failed (the re-send retry), so a + // Failed arriving after that node's Applied can only be a stale or duplicate delivery of the earlier Failed (the + // gRPC ack sender walks endpoints, and a timed-out call may still have landed). Discarded BEFORE the persist, so + // the row is not flipped back to Failed either. + if (msg.Outcome == ApplyAckOutcome.Failed && HasAppliedAckFrom(msg.NodeId)) + { + _log.Info( + "Discarding a late Failed ApplyAck from {Node} for {Id}: that node's Applied ack for this deployment " + + "already arrived (a Failed after Applied is a stale/duplicate delivery of the ack the re-send retry replaced)", + msg.NodeId, msg.DeploymentId); + // The stale Failed may itself be the node's unpersisted entry (its write failed and is on a retry tick that + // re-enters here): drop it, or the deadline would wait on a retry this guard now always discards. + if (_unpersisted.TryGetValue(msg.NodeId, out var parked) && parked.Ack.Outcome == ApplyAckOutcome.Failed) + DropUnpersisted(msg.NodeId); + return; + } + // Persist FIRST: the row is the system of record (driver-only nodes never write their own, and nodes do not // resend). A throw here used to restart the actor and drop the ack; it is re-queued instead. StartAckPersist(msg); } + /// + /// #1111 review: whether an Applied (clean or degraded) ack of the CURRENT deployment from + /// has arrived — counted, held for a persist retry, being persisted, or parked behind a write. + /// + private bool HasAppliedAckFrom(NodeId node) => + (_acks.TryGetValue(node, out var counted) && counted == ApplyAckOutcome.Applied) + || (_unpersisted.TryGetValue(node, out var held) && IsAppliedForCurrent(held.Ack)) + || (_persisting.TryGetValue(node, out var inflight) && IsAppliedForCurrent(inflight)) + || (_pendingBehind.TryGetValue(node, out var behind) && IsAppliedForCurrent(behind)); + + private bool IsAppliedForCurrent(ApplyAck ack) => ack.Outcome == ApplyAckOutcome.Applied && IsForCurrent(ack); + /// /// lmxopcua#996 (review H-6): the ack write runs OFF the actor thread and its outcome comes back as /// . The write used to be a blocking SaveChanges in the handler, so with the diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/ConfigPublishCoordinatorDegradedAckTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/ConfigPublishCoordinatorDegradedAckTests.cs index f67424b10..641b19f74 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/ConfigPublishCoordinatorDegradedAckTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/ConfigPublishCoordinatorDegradedAckTests.cs @@ -108,6 +108,87 @@ public sealed class ConfigPublishCoordinatorDegradedAckTests : ControlPlaneActor db.NodeDeploymentStates.Single(s => s.NodeId == NodeA).Status.ShouldBe(NodeDeploymentStatus.Failed); } + [Theory] + [InlineData(false)] + [InlineData(true)] + public void A_late_Failed_after_a_nodes_Applied_ack_does_not_overwrite_it(bool degraded) + { + // #1111 review: the node retried after its first (Failed) ack was not seen, and acked Applied; the ORIGINAL + // Failed then arrives late (a timed-out gRPC call that still landed). The node re-applies only after its own + // Failed, so a Failed after Applied is stale: it must neither flip the row nor fail the deployment. + var dbFactory = NewInMemoryDbFactory(); + var deploymentId = SeedDispatchingDeployment(dbFactory); + SeedNodes(dbFactory, NodeA, NodeB); + var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(dbFactory, NoTimeout)); + actor.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId())); + AwaitStatus(dbFactory, DeploymentStatus.AwaitingApplyAcks); + + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Applied, null, + CorrelationId.NewId(), Degraded: degraded, DegradedReason: degraded ? "x" : null)); + AwaitAssert(() => + { + using var db = dbFactory.CreateDbContext(); + db.NodeDeploymentStates.Single(s => s.NodeId == NodeA).Status.ShouldBe(NodeDeploymentStatus.Applied); + }, duration: PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Failed, "fetch failed", CorrelationId.NewId())); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeB), ApplyAckOutcome.Applied, null, CorrelationId.NewId())); + + AwaitStatus(dbFactory, DeploymentStatus.Sealed); + using var verify = dbFactory.CreateDbContext(); + var rowA = verify.NodeDeploymentStates.Single(s => s.NodeId == NodeA); + rowA.Status.ShouldBe(NodeDeploymentStatus.Applied); + ConfigPublishCoordinator.IsDegraded(rowA.Status, rowA.FailureReason).ShouldBe(degraded); + } + + [Fact] + public void A_late_Failed_racing_an_in_flight_Applied_write_does_not_overwrite_it() + { + // Same policy, both acks back-to-back: the Failed lands while the Applied's write is still in flight. + var dbFactory = NewInMemoryDbFactory(); + var deploymentId = SeedDispatchingDeployment(dbFactory); + SeedNodes(dbFactory, NodeA, NodeB); + var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(dbFactory, NoTimeout)); + actor.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId())); + AwaitStatus(dbFactory, DeploymentStatus.AwaitingApplyAcks); + + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Applied, null, CorrelationId.NewId())); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Failed, "fetch failed", CorrelationId.NewId())); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeB), ApplyAckOutcome.Applied, null, CorrelationId.NewId())); + + AwaitStatus(dbFactory, DeploymentStatus.Sealed); + using var verify = dbFactory.CreateDbContext(); + verify.NodeDeploymentStates.Single(s => s.NodeId == NodeA).Status.ShouldBe(NodeDeploymentStatus.Applied); + } + + [Fact] + public void An_Applied_after_a_nodes_Failed_ack_still_replaces_it() + { + // Positive control for the guard above: the retry's Applied arriving AFTER the Failed was counted wins, so the + // guard is one-directional (Applied is never overwritten by Failed; Failed is still overwritten by Applied). + var dbFactory = NewInMemoryDbFactory(); + var deploymentId = SeedDispatchingDeployment(dbFactory); + SeedNodes(dbFactory, NodeA, NodeB); + var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(dbFactory, NoTimeout)); + actor.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId())); + AwaitStatus(dbFactory, DeploymentStatus.AwaitingApplyAcks); + + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Failed, "fetch failed", CorrelationId.NewId())); + AwaitAssert(() => + { + using var db = dbFactory.CreateDbContext(); + db.NodeDeploymentStates.Single(s => s.NodeId == NodeA).Status.ShouldBe(NodeDeploymentStatus.Failed); + }, duration: PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeA), ApplyAckOutcome.Applied, null, CorrelationId.NewId())); + AwaitAssert(() => + { + using var db = dbFactory.CreateDbContext(); + db.NodeDeploymentStates.Single(s => s.NodeId == NodeA).Status.ShouldBe(NodeDeploymentStatus.Applied); + }, duration: PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); + actor.Tell(new ApplyAck(deploymentId, NodeId.Parse(NodeB), ApplyAckOutcome.Applied, null, CorrelationId.NewId())); + + AwaitStatus(dbFactory, DeploymentStatus.Sealed); + } + [Fact] public void A_recovered_coordinator_remembers_a_degraded_ack_that_landed_before_failover() {