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.
This commit is contained in:
+34
@@ -234,6 +234,40 @@ public class CentralCommunicationActorTests : ControlPlaneActorTestBase
|
||||
rig.Dialer.Calls.Count.ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_deployment_resend_dials_only_the_nodes_it_names()
|
||||
{
|
||||
// #1107: a coordinator re-send names the nodes that have not acked. Dialling an acked node would make it
|
||||
// re-apply a degraded revision (#601), so the fan must narrow to the named nodes — case-insensitively, as the
|
||||
// coordinator's expected-ack set compares node ids.
|
||||
var factory = NewInMemoryDbFactory();
|
||||
SeedGrpcCluster(
|
||||
factory,
|
||||
"C1",
|
||||
("host-a:4053", "host-a", 4056, true, false),
|
||||
("host-b:4053", "host-b", 4056, true, false));
|
||||
var rig = SpawnGrpcRig(factory);
|
||||
AwaitContactsLoaded(rig);
|
||||
|
||||
// Positive control: the original dispatch (no targets) reaches both nodes.
|
||||
var original = NewDispatch();
|
||||
rig.Actor.Tell(new MeshCommand(DeploymentsTopic, original));
|
||||
AwaitCondition(() => rig.Dialer.Calls.Count == 2, TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
|
||||
rig.Actor.Tell(new MeshCommand(DeploymentsTopic, original with { TargetNodes = ["HOST-B:4053"] }));
|
||||
AwaitCondition(() => rig.Dialer.Calls.Count == 3, TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
var resend = rig.Dialer.Calls[2];
|
||||
resend.Endpoint.ShouldBe("http://host-b:4056");
|
||||
resend.Envelope.DispatchDeployment.TargetNodes.ShouldBe(["HOST-B:4053"]);
|
||||
|
||||
// ...and host-a is not dialled late. A follow-up command to everyone is the positive control that the
|
||||
// actor is still dialling (so the count staying put is not just a stalled fan).
|
||||
rig.Actor.Tell(new MeshCommand(DeploymentsTopic, NewDispatch()));
|
||||
AwaitCondition(() => rig.Dialer.Calls.Count == 5, TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
rig.Dialer.Calls.Count(c => c.Envelope.DispatchDeployment.DeploymentId == original.DeploymentId.ToString())
|
||||
.ShouldBe(3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_node_with_no_GrpcPort_is_skipped_with_a_warning_and_the_others_are_still_dialled()
|
||||
{
|
||||
|
||||
+287
@@ -0,0 +1,287 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
using System.Text.Json;
|
||||
using Akka.Actor;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.DeploymentCache;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
|
||||
|
||||
/// <summary>
|
||||
/// Gitea #1107 — the node side of the coordinator's dispatch re-send. A re-send names the nodes that have not
|
||||
/// acked; it must never make a node apply the same deployment twice (under #601 a second apply of a degraded
|
||||
/// revision is a full re-apply), and a late or duplicate copy must be harmless.
|
||||
/// </summary>
|
||||
[Collection("opcua-publish-meters")]
|
||||
public sealed class DriverHostActorRedispatchTests : RuntimeActorTestBase
|
||||
{
|
||||
private static readonly NodeId TestNode = NodeId.Parse("driver-test");
|
||||
private static readonly RevisionHash RevA = RevisionHash.Parse(new string('a', 64));
|
||||
private static readonly TimeSpan Quiet = TimeSpan.FromMilliseconds(400);
|
||||
|
||||
[Fact]
|
||||
public void A_resend_of_a_deployment_already_acked_degraded_re_sends_the_ack_instead_of_re_applying()
|
||||
{
|
||||
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));
|
||||
|
||||
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
|
||||
actor.Tell(dispatch);
|
||||
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
publish.Reply(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, true, "failedNodes=1"));
|
||||
var first = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
first.Degraded.ShouldBeTrue();
|
||||
|
||||
// The coordinator never saw that ack and re-sends to this node (node id case differs, as SQL/DNS may).
|
||||
actor.Tell(dispatch with { TargetNodes = ["DRIVER-TEST"] });
|
||||
|
||||
var again = coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
again.ShouldBe(first); // the SAME answer, not a new apply's
|
||||
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
|
||||
|
||||
// Positive control: an ORIGINAL dispatch of a degraded revision still re-applies (#601) — so the silence above
|
||||
// is the re-send being recognised, not a host that has stopped applying.
|
||||
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId()));
|
||||
publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_resend_addressed_to_another_node_is_ignored()
|
||||
{
|
||||
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));
|
||||
|
||||
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId(), TargetNodes: ["driver-other"]));
|
||||
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
|
||||
coordinator.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
|
||||
// Positive control: the same re-send naming THIS node (never acked here) is a first apply.
|
||||
actor.Tell(new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId(), TargetNodes: ["driver-other", "driver-test"]));
|
||||
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
publish.Reply(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null));
|
||||
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_duplicate_while_the_ack_waits_on_the_address_space_outcome_does_not_apply_twice()
|
||||
{
|
||||
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));
|
||||
|
||||
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
|
||||
actor.Tell(dispatch);
|
||||
var rebuild = publish.FishForMessage<OpcUaPublishActor.RebuildAddressSpace>(_ => true, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
|
||||
// The ack is held on the outcome. Neither a re-send nor a plain duplicate may start a second apply.
|
||||
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
|
||||
actor.Tell(dispatch);
|
||||
publish.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
|
||||
coordinator.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
|
||||
// Exactly one ack, when the one apply's outcome lands.
|
||||
actor.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId, false, null), publish.Ref);
|
||||
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
|
||||
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
|
||||
publish.ExpectNoMsg(TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_resend_arriving_mid_apply_does_not_start_a_second_apply()
|
||||
{
|
||||
var db = NewInMemoryDbFactory();
|
||||
var fetcher = new GatedFetcher(JsonSerializer.SerializeToUtf8Bytes(new { DriverInstances = Array.Empty<object>() }));
|
||||
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));
|
||||
|
||||
try
|
||||
{
|
||||
var dispatch = new DispatchDeployment(deploymentId, RevA, CorrelationId.NewId());
|
||||
actor.Tell(dispatch);
|
||||
AwaitAssert(() => fetcher.Calls.ShouldBe(1), duration: PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
||||
|
||||
// Re-sends while the artifact fetch is still parked.
|
||||
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
|
||||
actor.Tell(dispatch with { TargetNodes = [TestNode.Value] });
|
||||
|
||||
fetcher.Release();
|
||||
// Presence: the one apply acks...
|
||||
coordinator.ExpectMsg<ApplyAck>(PresenceBudget, cancellationToken: TestContext.Current.CancellationToken)
|
||||
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
|
||||
// ...absence: no second fetch, no second ack.
|
||||
coordinator.ExpectNoMsg(Quiet, TestContext.Current.CancellationToken);
|
||||
fetcher.Calls.ShouldBe(1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
fetcher.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private (IDbContextFactory<OtOpcUaConfigDbContext> Db, DeploymentId Id) SeedOneTagDeployment()
|
||||
{
|
||||
var db = NewInMemoryDbFactory();
|
||||
var id = V3ArtifactSeeder.SeedV3Deployment(db, RevA,
|
||||
rawTags: new[] { (Driver: "drv-1", DriverName: "Modbus", Device: "dev1", Tag: "speed", DataType: "Double", Writable: false) },
|
||||
unsRefs: new[] { (Area: "filling", Line: "line1", Equip: "station1", BackingTag: "speed", Effective: (string?)null) });
|
||||
return (db, id);
|
||||
}
|
||||
|
||||
/// <summary>A fetcher whose first fetch parks until <see cref="Release"/>.</summary>
|
||||
private sealed class GatedFetcher(byte[] bytes) : IDeploymentArtifactFetcher
|
||||
{
|
||||
private readonly TaskCompletionSource _gate = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private int _calls;
|
||||
|
||||
public int Calls => Volatile.Read(ref _calls);
|
||||
|
||||
public void Release() => _gate.TrySetResult();
|
||||
|
||||
public async Task<byte[]?> FetchAsync(string deploymentId, string expectedRevisionHash, CancellationToken ct)
|
||||
{
|
||||
if (Interlocked.Increment(ref _calls) == 1) await _gate.Task.ConfigureAwait(false);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NullArtifactCache : IDeploymentArtifactCache
|
||||
{
|
||||
public Task StoreAsync(string clusterId, string deploymentId, string revisionHash,
|
||||
byte[] artifact, CancellationToken ct = default)
|
||||
=> Task.CompletedTask;
|
||||
|
||||
public Task<CachedDeploymentArtifact?> GetCurrentAsync(string clusterId, CancellationToken ct = default)
|
||||
=> Task.FromResult<CachedDeploymentArtifact?>(null);
|
||||
|
||||
public Task<CachedDeploymentArtifact?> GetCurrentUnkeyedAsync(CancellationToken ct = default)
|
||||
=> Task.FromResult<CachedDeploymentArtifact?>(null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user