Merge #1111 deploy re-send residuals: a re-send retries a Failed apply (newest-answered, no held ack); re-sends carry DeadlineRemaining so a late-reached node caps its address-space wait; coordinator keeps a node's Applied over a late stale Failed; in-memory acks + late-Failed-seal residual documented

This commit is contained in:
Joseph Doherty
2026-12-05 00:53:03 -05:00
11 changed files with 533 additions and 15 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()
{
@@ -127,6 +127,31 @@ public sealed class ConfigPublishCoordinatorRedispatchTests : ControlPlaneActorT
resend.IsResend.ShouldBeTrue();
}
[Fact]
public void Each_resend_carries_the_remaining_deadline_and_the_original_carries_none()
{
// #1111: a node first reached by a late re-send must bound its address-space wait to what is LEFT of central's
// deadline, so every re-send says how much that is; the original dispatch leaves it unset (full default).
var deadline = TimeSpan.FromSeconds(60);
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053");
var router = CreateTestProbe();
Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, deadline, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(400), 2)))
.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router).DeadlineRemaining.ShouldBeNull();
var first = ExpectDispatch(router).DeadlineRemaining.ShouldNotBeNull();
var second = ExpectDispatch(router).DeadlineRemaining.ShouldNotBeNull();
first.ShouldBeLessThan(deadline);
first.ShouldBeGreaterThan(deadline - TimeSpan.FromSeconds(10));
// Counts down between re-sends (by ~one schedule step), never resets to the full deadline.
second.ShouldBeLessThan(first);
(first - second).ShouldBeGreaterThan(TimeSpan.FromMilliseconds(200));
}
[Fact]
public void Resends_are_bounded_by_the_schedule()
{
@@ -144,6 +144,193 @@ public sealed class DriverHostActorRedispatchTests : RuntimeActorTestBase
}
}
[Fact]
public void A_resend_of_a_deployment_whose_apply_FAILED_retries_the_apply_and_a_later_resend_replays_the_success()
{
// #1111: the first fetch fails (a central blip), the second succeeds.
var db = NewInMemoryDbFactory();
var fetcher = new ScriptedFetcher(null, EmptyArtifact);
var deploymentId = DeploymentId.NewId();
var coordinator = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref,
localRoles: new HashSet<string> { "driver" },
deploymentArtifactCache: new NullArtifactCache(),
fetchAndCacheMode: true,
artifactFetcher: fetcher));
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
actor.Tell(dispatch);
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Failed);
// The coordinator never saw the Failed ack and re-sends: the node RETRIES rather than replaying Failed.
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
fetcher.Calls.ShouldBe(2);
// A further re-send (that Applied ack lost too) replays it — the retry did not turn re-sends into re-applies.
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(2);
}
[Fact]
public void A_resend_of_an_OLDER_failed_deployment_replays_Failed_rather_than_rolling_back_a_newer_apply()
{
var db = NewInMemoryDbFactory();
var fetcher = new ScriptedFetcher(null, EmptyArtifact);
var coordinator = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref,
localRoles: new HashSet<string> { "driver" },
deploymentArtifactCache: new NullArtifactCache(),
fetchAndCacheMode: true,
artifactFetcher: fetcher));
var older = new DispatchDeployment(DeploymentId.NewId(), RevA, CorrelationId.NewId());
actor.Tell(older);
var failed = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
failed.Outcome.ShouldBe(ApplyAckOutcome.Failed);
// A newer deployment is applied after it.
actor.Tell(new DispatchDeployment(DeploymentId.NewId(), RevisionHash.Parse(new string('b', 64)), CorrelationId.NewId()));
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
fetcher.Calls.ShouldBe(2);
// A stale re-send of the older, failed deployment: the remembered Failed is replayed, nothing is fetched.
actor.Tell(older with { TargetNodes = [TestNode.Value] });
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.ShouldBe(failed);
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(2);
// Positive control: an ORIGINAL dispatch of that same deployment does fetch and apply, so the silence above is
// the stale-retry guard, not a host that has stopped fetching.
actor.Tell(older);
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
fetcher.Calls.ShouldBe(3);
}
[Fact]
public void A_resend_of_a_failed_deployment_is_not_retried_while_a_NEWER_applys_ack_is_still_held()
{
// #1111 review: the newer deployment's Applied ack is held on its address-space outcome, so the failed one is
// still this node's latest ANSWER — only the held-ack guard stops the stale re-send from re-applying the older
// revision over the newer one (and the newer one's held Applied ack then going out for a rolled-back node).
var db = NewInMemoryDbFactory();
var fetcher = new ScriptedFetcher(null, EmptyArtifact);
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref,
localRoles: new HashSet<string> { "driver" },
deploymentArtifactCache: new NullArtifactCache(),
fetchAndCacheMode: true,
artifactFetcher: fetcher,
opcUaPublishActor: publish.Ref,
applyOutcomeTimeout: TimeSpan.FromSeconds(60)));
var older = new DispatchDeployment(DeploymentId.NewId(), RevA, CorrelationId.NewId());
actor.Tell(older);
var failed = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
failed.Outcome.ShouldBe(ApplyAckOutcome.Failed);
// The newer deployment applies; its Applied ack is HELD (the address-space outcome has not landed).
var newer = new DispatchDeployment(DeploymentId.NewId(), RevisionHash.Parse(new string('b', 64)), CorrelationId.NewId());
actor.Tell(newer);
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
rebuild.DeploymentId.ShouldBe(newer.DeploymentId);
fetcher.Calls.ShouldBe(2);
// A stale re-send of the older, failed deployment: replays Failed, fetches nothing, starts no rebuild.
actor.Tell(older with { TargetNodes = [TestNode.Value] });
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
.ShouldBe(failed);
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(2);
// The held ack is still the newer deployment's, and goes out clean when its outcome lands.
actor.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null), publish.Ref);
var ack = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
ack.DeploymentId.ShouldBe(newer.DeploymentId);
ack.Outcome.ShouldBe(ApplyAckOutcome.Applied);
ack.Degraded.ShouldBeFalse();
// Positive control: an ORIGINAL dispatch of the older deployment does fetch — the silence above was the guard.
actor.Tell(older);
publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(r => r.DeploymentId == older.DeploymentId, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
fetcher.Calls.ShouldBe(3);
}
[Fact]
public void A_late_resend_bounds_the_address_space_wait_to_the_deadline_it_carries()
{
var (db, deploymentId) = SeedOneTagDeployment();
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
// A long configured wait, so a degraded ack within seconds can only be the re-send's bound.
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref, localRoles: new HashSet<string> { "driver" },
opcUaPublishActor: publish.Ref, applyOutcomeTimeout: TimeSpan.FromSeconds(60)));
// First reached by a re-send with 17 s of central's deadline left => 17 - 15 = 2 s of address-space wait.
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId(),
TargetNodes: [TestNode.Value], DeadlineRemaining: TimeSpan.FromSeconds(17)));
publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
// The outcome never arrives.
var ack = coordinator.ExpectMsg<ApplyAck>(TimeSpan.FromSeconds(10), cancellationToken: TestContext.Current.CancellationToken);
ack.Outcome.ShouldBe(ApplyAckOutcome.Applied);
ack.Degraded.ShouldBeTrue();
ack.DegradedReason.ShouldNotBeNull();
ack.DegradedReason.ShouldStartWith(DriverHostActor.OutcomeNotReportedReasonPrefix);
ack.DegradedReason.ShouldContain("(2s)");
}
[Fact]
public void An_original_dispatch_keeps_the_full_address_space_wait()
{
// Positive control for the bound above: same set-up, no DeadlineRemaining — the ack stays held past the
// re-send's 2 s, and is released (clean) only by the outcome.
var (db, deploymentId) = SeedOneTagDeployment();
var coordinator = CreateTestProbe();
var publish = CreateTestProbe();
var actor = Sys.ActorOf(DriverHostActor.Props(
db, TestNode, coordinator.Ref, localRoles: new HashSet<string> { "driver" },
opcUaPublishActor: publish.Ref, applyOutcomeTimeout: TimeSpan.FromSeconds(60)));
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId()));
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
coordinator.ExpectNoMsg(TimeSpan.FromSeconds(4), TestContext.Current.CancellationToken);
actor.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null), publish.Ref);
var ack = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
ack.Outcome.ShouldBe(ApplyAckOutcome.Applied);
ack.Degraded.ShouldBeFalse();
}
private static readonly byte[] EmptyArtifact =
JsonSerializer.SerializeToUtf8Bytes(new { DriverInstances = Array.Empty<object>() });
/// <summary>A fetcher answering each call with the next scripted result (null = "no answer"); the last repeats.</summary>
private sealed class ScriptedFetcher(params byte[]?[] results) : IDeploymentArtifactFetcher
{
private int _calls;
public int Calls => Volatile.Read(ref _calls);
public Task<byte[]?> FetchAsync(string deploymentId, string expectedRevisionHash, CancellationToken ct)
{
var n = Interlocked.Increment(ref _calls);
return Task.FromResult(results[Math.Min(n, results.Length) - 1]);
}
}
private (IDbContextFactory<OtOpcUaConfigDbContext> Db, DeploymentId Id) SeedOneTagDeployment()
{
var db = NewInMemoryDbFactory();