fix(deploy): #1111 review — Applied wins over a late Failed

Since #1111 a node re-applies a deployment only after its own Failed (the re-send retry), so
two acks from one node for one deployment can differ, and the ORIGINAL Failed can still land
after the retry's Applied (GrpcApplyAckSender's endpoint walk: a timed-out call may have been
delivered). ConfigPublishCoordinator.HandleAck now discards a Failed from a node whose Applied
(clean or degraded) for the current deployment has arrived — counted, persisting, parked behind
a write, or held for a persist retry — before the row write, so neither the count nor the
NodeDeploymentState row flips back. A stale unpersisted Failed is dropped with it so the deadline
never waits on a retry the guard always discards. Applied after a counted Failed still wins.

Residual, documented in docs/Redundancy.md (not corrected after the fact): a late Failed that
CLOSES the deployment before the retry's Applied arrives — re-opening or late-sealing a terminal
deployment would break write-once terminal outcomes (L8) and latest-sealed ordering.

Tests: late Failed after a persisted Applied (clean + degraded), back-to-back Failed racing the
in-flight Applied write (all three fail with the guard disabled), and an Applied-after-Failed
positive control.
This commit is contained in:
Joseph Doherty
2026-12-05 00:52:44 -05:00
parent 9be85f5a94
commit 2fcaffd2ed
4 changed files with 133 additions and 1 deletions
+3 -1
View File
@@ -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".
+19
View File
@@ -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
@@ -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);
}
/// <summary>
/// #1111 review: whether an Applied (clean or degraded) ack of the CURRENT deployment from <paramref name="node"/>
/// has arrived — counted, held for a persist retry, being persisted, or parked behind a write.
/// </summary>
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);
/// <summary>
/// lmxopcua#996 (review H-6): the ack write runs OFF the actor thread and its outcome comes back as
/// <see cref="AckPersisted"/>. The write used to be a blocking <c>SaveChanges</c> in the handler, so with the
@@ -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()
{