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
@@ -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()
{