f5485f57d8
1. Node (DriverHostActor): a coordinator re-send of a deployment whose remembered ack is Failed now RE-ATTEMPTS the apply instead of replaying Failed — a failed apply left the node on last-good config, and a fetch failure and a lost ack usually share a cause. Once per re-send (a copy mid-retry is dropped by Applying), and only while that deployment is still the node's latest answer with no other ack held, so a stale re-send of an OLDER failed deployment replays Failed rather than rolling back a newer apply. Applied / Applied+degraded keep the replay (#601 unchanged). 2. DispatchDeployment gains additive DeadlineRemaining (proto optional int64 deadline_remaining_ms = 5), set by ConfigPublishCoordinator on every re-send (relative, no clock agreement needed), unset on the original. A node whose apply was started by a re-send bounds its held Applied ack's address-space wait to remaining - 15 s from receipt (floor 1 s), so a node first reached at +35 s acks Applied (degraded) before central's deadline instead of sealing TimedOut. Old central -> field absent -> full 105 s (today); old node ignores the field (limit documented). 3. Docs: remembered acks are in memory only (restart/supervision restart forgets them); Redundancy.md §Dispatch re-send + cluster-topology rule updated. Tests: Failed-ack retry + later replay, stale older-failed guard with positive control, re-send deadline bound with original-dispatch positive control (both behaviour tests verified to fail with the change disabled), mapper presence round-trip, coordinator DeadlineRemaining countdown.
313 lines
15 KiB
C#
313 lines
15 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 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()
|
|
{
|
|
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;
|
|
}
|
|
}
|