diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Messages/Mesh/MeshTransportDtoMapper.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Messages/Mesh/MeshTransportDtoMapper.cs index e3ffd7c1e..42175c590 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Messages/Mesh/MeshTransportDtoMapper.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/Messages/Mesh/MeshTransportDtoMapper.cs @@ -80,10 +80,17 @@ public static class MeshTransportDtoMapper CorrelationId.Parse(proto.CorrelationId), // #1107: an empty repeated field is the original dispatch (null), never "addressed to nobody". proto.TargetNodes.Count > 0 ? proto.TargetNodes.ToList() : null, - // #1111: absent (an original dispatch, or a pre-#1111 central) = null = the node's full default wait. - proto.HasDeadlineRemainingMs ? TimeSpan.FromMilliseconds(proto.DeadlineRemainingMs) : null); + // #1111: absent (an original dispatch, or a pre-#1111 central) = null = the node's full default wait. Clamped: + // the bound only ever SHORTENS the node's wait, and an out-of-range wire value must not throw here (a + // TimeSpan overflow) or overflow the node's tick arithmetic — negative is "spent", a day is "unbounded". + proto.HasDeadlineRemainingMs + ? TimeSpan.FromMilliseconds(Math.Clamp(proto.DeadlineRemainingMs, 0L, MaxDeadlineRemainingMs)) + : null); } + /// #1111: the largest deadline_remaining_ms taken off the wire (one day — far past any apply deadline). + private const long MaxDeadlineRemainingMs = 24L * 60 * 60 * 1000; + // ---------------------------------------------------------------- RestartDriver /// Projects onto its wire form. diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs index 5b44f974e..807daa0dd 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs @@ -372,8 +372,8 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn private readonly Queue _recentAckOrder = new(); /// - /// #1111: the deployment this node sent its most recent ack for (any outcome, including a re-delivery's - /// re-ack of the same deployment). A re-send of a deployment whose remembered ack is Failed is + /// #1111: the deployment this node most recently PRODUCED an ack for (any outcome; set by + /// — a replay of a remembered ack does not move it). A re-send of a deployment whose remembered ack is Failed is /// re-attempted only while that deployment is still this node's latest answer — retrying an OLDER failed /// deployment after a newer one was applied would roll the node back. /// diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/MeshTransportDtoMapperTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/MeshTransportDtoMapperTests.cs index 01e436174..13b98eb86 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/MeshTransportDtoMapperTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Commons.Tests/MeshTransportDtoMapperTests.cs @@ -91,6 +91,14 @@ public class MeshTransportDtoMapperTests spent.HasDeadlineRemainingMs.ShouldBeTrue(); MeshTransportDtoMapper.FromProto(spent).DeadlineRemaining.ShouldBe(TimeSpan.Zero); + // Out-of-range wire values (only a buggy or hostile sender) are clamped, never thrown on. + var negative = Wire(MeshTransportDtoMapper.ToProto(resend)); + negative.DeadlineRemainingMs = long.MinValue; + MeshTransportDtoMapper.FromProto(negative).DeadlineRemaining.ShouldBe(TimeSpan.Zero); + var huge = Wire(MeshTransportDtoMapper.ToProto(resend)); + huge.DeadlineRemainingMs = long.MaxValue; + MeshTransportDtoMapper.FromProto(huge).DeadlineRemaining.ShouldBe(TimeSpan.FromDays(1)); + var original = Wire(MeshTransportDtoMapper.ToProto(resend with { TargetNodes = null, DeadlineRemaining = null })); original.HasDeadlineRemainingMs.ShouldBeFalse(); MeshTransportDtoMapper.FromProto(original).DeadlineRemaining.ShouldBeNull(); diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostActorRedispatchTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostActorRedispatchTests.cs index 8ad1feea9..a2615814c 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostActorRedispatchTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostActorRedispatchTests.cs @@ -217,6 +217,57 @@ public sealed class DriverHostActorRedispatchTests : RuntimeActorTestBase 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 { "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(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(_ => 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(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(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(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() {