Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests/ConfigPublishCoordinatorRedispatchTests.cs
T
Joseph Doherty 323bbd2a63 fix(deploy): #1107 re-send the deploy dispatch to nodes that have not acked
ConfigPublishCoordinator dispatched a deployment exactly once: on DPS a node whose
deployments registry entry had not gossiped to central missed it (#1106), on gRPC a
failed dial was dropped, and either way the deployment timed out at 120 s.

Coordinator: re-sends the same DispatchDeployment at +5/+15/+35 s (bounded by half the
remaining deadline) with the new additive TargetNodes naming only expected nodes whose
ack has not arrived (an ack still being persisted counts). Stops on last ack,
completion, time-out, supersede or schedule end; a recovered in-flight deployment gets
the same re-send. Counter otopcua.deploy.redispatched.

Transport: CentralCommunicationActor's gRPC fan dials only the named nodes; proto gains
repeated target_nodes = 4 (additive). On DPS, non-addressed nodes ignore the re-send.

Node (DriverHostActor): a re-send is idempotent per DeploymentId — ignored if not
addressed here or mid-apply; ANY copy of a deployment whose Applied ack is held on the
address-space outcome no longer starts a second apply; a re-send of a deployment it
already acked re-delivers the remembered ack (last 16) instead of re-applying (#601
would otherwise re-apply a degraded revision). Original dispatches are unchanged.

Docs: Redundancy.md §Dispatch re-send, Configuration.md, cluster-topology rule.
2026-12-04 22:29:17 -05:00

288 lines
14 KiB
C#

using Akka.Actor;
using Akka.TestKit;
using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Mesh;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Coordinators;
using ZB.MOM.WW.OtOpcUa.ControlPlane.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Tests;
/// <summary>
/// Gitea #1107 — the deploy dispatch used to go out exactly once, so a node that missed it (a DistributedPubSub
/// registry entry not yet gossiped to central, #1106; a failed gRPC dial) timed the deployment out at the apply
/// deadline. The coordinator now re-sends it, bounded, to the expected nodes whose ack has not arrived — and ONLY to
/// them: under #601 a same-revision dispatch makes a node whose apply was degraded re-apply.
/// </summary>
/// <remarks>
/// The mesh router is a probe, so every dispatch the coordinator hands to the transport (original or re-send) is
/// observed directly. Every absence assertion is paired with a positive control proving re-sends were flowing.
/// </remarks>
public sealed class ConfigPublishCoordinatorRedispatchTests : ControlPlaneActorTestBase
{
private static readonly RevisionHash TestRevision = RevisionHash.Parse(new string('c', 64));
private static readonly TimeSpan NoTimeout = TimeSpan.FromMinutes(5);
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(5);
private static IReadOnlyList<TimeSpan> Every(TimeSpan delay, int count) => Enumerable.Repeat(delay, count).ToList();
[Fact]
public void A_resend_goes_only_to_the_nodes_that_have_not_acked()
{
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053", "site-a-2:4053");
var router = CreateTestProbe();
var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(300), 3)));
var correlation = CorrelationId.NewId();
actor.Tell(new DispatchDeployment(deploymentId, TestRevision, correlation));
var original = ExpectDispatch(router);
original.TargetNodes.ShouldBeNull();
original.IsResend.ShouldBeFalse();
// site-a-1 answers before the first re-send is due.
actor.Tell(new ApplyAck(deploymentId, NodeId.Parse("site-a-1:4053"), ApplyAckOutcome.Applied, null, correlation));
var resend = ExpectDispatch(router);
resend.DeploymentId.ShouldBe(deploymentId);
resend.RevisionHash.ShouldBe(TestRevision);
resend.CorrelationId.ShouldBe(original.CorrelationId); // the SAME dispatch, re-sent
resend.TargetNodes.ShouldBe(["site-a-2:4053"]);
}
[Fact]
public void An_ack_whose_write_is_still_in_flight_already_counts_as_answered()
{
// The ack write is off-thread; a node whose ack ARRIVED must never be re-sent to, however slow the ConfigDb.
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053", "site-a-2:4053");
var router = CreateTestProbe();
var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(250), 3)));
actor.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router);
actor.Tell(new ApplyAck(deploymentId, NodeId.Parse("SITE-A-2:4053"), ApplyAckOutcome.Applied, null, CorrelationId.NewId()));
// Three re-sends (the positive control: re-sends ARE flowing), every one naming only site-a-1.
for (var i = 0; i < 3; i++)
ExpectDispatch(router).TargetNodes.ShouldBe(["site-a-1:4053"]);
}
[Fact]
public void Resends_stop_once_every_expected_node_has_acked()
{
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053");
var router = CreateTestProbe();
var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(200), 10)));
actor.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router);
// Positive control: the silent node IS being re-sent to.
ExpectDispatch(router).TargetNodes.ShouldBe(["site-a-1:4053"]);
actor.Tell(new ApplyAck(deploymentId, NodeId.Parse("site-a-1:4053"), ApplyAckOutcome.Applied, null, CorrelationId.NewId()));
AwaitStatus(dbFactory, DeploymentStatus.Sealed);
// Seven more re-sends were left on the schedule (1.4 s): none may go out after the seal.
router.ExpectNoMsg(TimeSpan.FromMilliseconds(800), TestContext.Current.CancellationToken);
}
[Fact]
public void A_fully_acked_dispatch_is_never_resent_while_a_silent_one_is()
{
var dbFactory = NewInMemoryDbFactory();
var schedule = Every(TimeSpan.FromMilliseconds(200), 3);
SeedNodes(dbFactory, "site-a-1:4053");
// Acked before the first re-send was due: nothing after the original.
var acked = SeedDispatchingDeployment(dbFactory);
var router = CreateTestProbe();
var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(dbFactory, NoTimeout, router.Ref, redispatchSchedule: schedule));
actor.Tell(new DispatchDeployment(acked, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router);
actor.Tell(new ApplyAck(acked, NodeId.Parse("site-a-1:4053"), ApplyAckOutcome.Applied, null, CorrelationId.NewId()));
AwaitStatus(dbFactory, DeploymentStatus.Sealed, acked);
router.ExpectNoMsg(TimeSpan.FromMilliseconds(700), TestContext.Current.CancellationToken);
// Positive control on the same coordinator and schedule: a deployment nobody acks IS re-sent.
var silent = SeedDispatchingDeployment(dbFactory);
actor.Tell(new DispatchDeployment(silent, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router).DeploymentId.ShouldBe(silent);
var resend = ExpectDispatch(router);
resend.DeploymentId.ShouldBe(silent);
resend.IsResend.ShouldBeTrue();
}
[Fact]
public void Resends_are_bounded_by_the_schedule()
{
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053");
var router = CreateTestProbe();
Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(150), 3)))
.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router).IsResend.ShouldBeFalse();
for (var i = 0; i < 3; i++) ExpectDispatch(router).IsResend.ShouldBeTrue();
router.ExpectNoMsg(TimeSpan.FromMilliseconds(600), TestContext.Current.CancellationToken);
}
[Fact]
public void Resends_stay_inside_the_window_and_stop_at_the_deadline()
{
// A 1 s deadline ⇒ re-sends only in its first half; 100 ms steps ⇒ at most 4-5 re-sends, then the TimedOut
// seal, then silence — although the schedule has 30 entries.
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory);
SeedNodes(dbFactory, "site-a-1:4053");
var router = CreateTestProbe();
Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, TimeSpan.FromSeconds(1), router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(100), 30)))
.Tell(new DispatchDeployment(deploymentId, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router).IsResend.ShouldBeFalse();
AwaitStatus(dbFactory, DeploymentStatus.TimedOut);
var resends = router.ReceiveWhile<MeshCommand>(TimeSpan.FromMilliseconds(100), m => (MeshCommand)m, cancellationToken: TestContext.Current.CancellationToken);
// Positive control: re-sends did happen before the deadline...
resends.Count.ShouldBeGreaterThanOrEqualTo(1);
resends.Count.ShouldBeLessThanOrEqualTo(5);
// ...and none after it.
router.ExpectNoMsg(TimeSpan.FromMilliseconds(600), TestContext.Current.CancellationToken);
}
[Fact]
public void A_superseded_deployment_is_no_longer_resent()
{
var dbFactory = NewInMemoryDbFactory();
SeedNodes(dbFactory, "site-a-1:4053");
var first = SeedDispatchingDeployment(dbFactory);
var router = CreateTestProbe();
var actor = Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(150), 20)));
actor.Tell(new DispatchDeployment(first, TestRevision, CorrelationId.NewId()));
ExpectDispatch(router).DeploymentId.ShouldBe(first);
ExpectDispatch(router).DeploymentId.ShouldBe(first); // positive control: the first IS being re-sent
var second = SeedDispatchingDeployment(dbFactory);
actor.Tell(new DispatchDeployment(second, TestRevision, CorrelationId.NewId()));
// Drain up to the second's original (a first re-send may already have been queued ahead of it).
router.FishForMessage<MeshCommand>(m => ((DispatchDeployment)m.Message) is { DeploymentId: var id, IsResend: false } && id == second,
Wait, cancellationToken: TestContext.Current.CancellationToken);
var after = router.ReceiveWhile<MeshCommand>(TimeSpan.FromMilliseconds(400), m => (MeshCommand)m, msgs: 6,
cancellationToken: TestContext.Current.CancellationToken);
after.ShouldNotBeEmpty(); // the second IS re-sent...
after.ShouldAllBe(m => ((DispatchDeployment)m.Message).DeploymentId == second); // ...the first never again
AwaitStatus(dbFactory, DeploymentStatus.TimedOut, first);
}
[Fact]
public void A_recovered_in_flight_deployment_is_resent_to_its_unacked_nodes_only()
{
// A singleton fail-over: the new incarnation recovers the deployment and resumes re-sending to the node that
// has not acked — never to the one whose Applied row already landed.
var dbFactory = NewInMemoryDbFactory();
var deploymentId = SeedDispatchingDeployment(dbFactory, DeploymentStatus.AwaitingApplyAcks);
using (var db = dbFactory.CreateDbContext())
{
db.NodeDeploymentStates.Add(new NodeDeploymentState
{
NodeId = "site-a-1:4053", DeploymentId = deploymentId.Value, Status = NodeDeploymentStatus.Applied,
});
db.NodeDeploymentStates.Add(new NodeDeploymentState
{
NodeId = "site-a-2:4053", DeploymentId = deploymentId.Value, Status = NodeDeploymentStatus.Applying,
});
db.SaveChanges();
}
var router = CreateTestProbe();
Sys.ActorOf(ConfigPublishCoordinator.Props(
dbFactory, NoTimeout, router.Ref, redispatchSchedule: Every(TimeSpan.FromMilliseconds(200), 2)));
var first = ExpectDispatch(router);
first.DeploymentId.ShouldBe(deploymentId);
first.RevisionHash.ShouldBe(TestRevision);
first.TargetNodes.ShouldBe(["site-a-2:4053"]);
ExpectDispatch(router).TargetNodes.ShouldBe(["site-a-2:4053"]);
router.ExpectNoMsg(TimeSpan.FromMilliseconds(500), TestContext.Current.CancellationToken);
}
private static DispatchDeployment ExpectDispatch(TestProbe router)
{
var cmd = router.ExpectMsg<MeshCommand>(Wait, cancellationToken: TestContext.Current.CancellationToken);
cmd.Topic.ShouldBe(ConfigPublishCoordinator.DeploymentsTopic);
return cmd.Message.ShouldBeOfType<DispatchDeployment>();
}
private void AwaitStatus(IDbContextFactory<OtOpcUaConfigDbContext> dbFactory, DeploymentStatus status, DeploymentId? id = null) =>
AwaitAssert(() =>
{
using var db = dbFactory.CreateDbContext();
var row = id is { } d ? db.Deployments.Single(x => x.DeploymentId == d.Value) : db.Deployments.Single();
row.Status.ShouldBe(status);
}, duration: Wait, cancellationToken: TestContext.Current.CancellationToken);
private static void SeedNodes(IDbContextFactory<OtOpcUaConfigDbContext> dbFactory, params string[] nodeIds)
{
using var db = dbFactory.CreateDbContext();
db.ServerClusters.Add(new ServerCluster
{
ClusterId = "SITE-A",
Name = "Site A",
Enterprise = "zb",
Site = "site-a",
NodeCount = (byte)nodeIds.Length,
RedundancyMode = RedundancyMode.Warm,
CreatedBy = "test",
});
foreach (var nodeId in nodeIds)
{
db.ClusterNodes.Add(new ClusterNode
{
NodeId = nodeId,
ClusterId = "SITE-A",
Host = nodeId.Split(':')[0],
ApplicationUri = $"urn:OtOpcUa:{nodeId}",
Enabled = true,
CreatedBy = "test",
});
}
db.SaveChanges();
}
private static DeploymentId SeedDispatchingDeployment(
IDbContextFactory<OtOpcUaConfigDbContext> dbFactory, DeploymentStatus status = DeploymentStatus.Dispatching)
{
var id = DeploymentId.NewId();
using var db = dbFactory.CreateDbContext();
db.Deployments.Add(new Deployment
{
DeploymentId = id.Value,
RevisionHash = TestRevision.Value,
Status = status,
CreatedBy = "test",
CreatedAtUtc = DateTime.UtcNow,
});
db.SaveChanges();
return id;
}
}