diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/DeployHappyPathTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/DeployHappyPathTests.cs index b04c3c9bf..68a69fd5e 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/DeployHappyPathTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/DeployHappyPathTests.cs @@ -59,7 +59,7 @@ public sealed class DeployHappyPathTests /// lmxopcua#1106 regression: a deploy started the moment the harness reports the cluster formed must reach BOTH /// nodes. With DistributedPubSub's registry gossip slowed to 10 s, node A's mediator does not learn node B's /// deployments subscription for ~10 s after B is Up; before the harness waited for the deploy topics to - /// gossip () the dispatch went to node A only and the + /// gossip () the dispatch went to node A only and the /// deployment sat AwaitingApplyAcks until the 120 s apply deadline — deterministically here, and 1 in ~10–20 runs /// under heavy load at the default 1 s interval (the #843/#1106 seal-timeout flake). /// diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs index 35c19737c..82a6a3bc4 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs @@ -194,9 +194,12 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable /// Optional probe that both nodes expose to the admin operations singleton. /// Optional HOCON prepended to both nodes' Akka config (tests that need a non-default /// Akka setting, e.g. a slow DistributedPubSub gossip interval). + /// The pub/sub topics whose registry gossip to wait for before reporting the cluster + /// formed (lmxopcua#1108); null (default) waits for every topic the host nodes subscribe to at boot + /// (). A test that publishes on a topic outside the set is exposed to the #1106 window. /// - /// lmxopcua#1106: "formed" includes the deploy topics' DistributedPubSub registries having gossiped to both - /// nodes () — not just both members being Up. A deploy published in the + /// lmxopcua#1106 / #1108: "formed" includes the host topics' DistributedPubSub registries having gossiped to both + /// nodes () — not just both members being Up. A deploy published in the /// window between the two was silently delivered to node A only (DPS publishes to the buckets it knows), so the /// deployment could never seal: the seal-timeout flake behind #843/#1106. /// @@ -204,9 +207,11 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable TimeSpan? formationTimeout = null, IDriverFactory? driverFactory = null, IDriverProbe? driverProbe = null, - string? extraHocon = null) + string? extraHocon = null, + IReadOnlyList? requiredTopics = null) { var harness = new TwoNodeClusterHarness(); + harness._requiredTopics = requiredTopics ?? BootTopics; harness._driverFactory = driverFactory; harness._driverProbe = driverProbe; harness._extraHocon = extraHocon; @@ -243,7 +248,7 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable harness.NodeASystem, harness.NodeBSystem, formationTimeout ?? TimeSpan.FromSeconds(20)); - await harness.WaitForDeployTopicsAsync(formationTimeout ?? TimeSpan.FromSeconds(20)); + await harness.WaitForPubSubConvergedAsync(formationTimeout ?? TimeSpan.FromSeconds(20), harness._requiredTopics); return harness; } @@ -251,47 +256,94 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable private string? _extraHocon; /// - /// lmxopcua#1106: waits until each node's DistributedPubSub registry carries BOTH nodes' deployments - /// subscriptions (the dispatch reaches every driver host, whichever node hosts the coordinator) and the same, - /// non-empty deployment-acks subscriber set (every node's ack reaches the coordinator). + /// A DistributedPubSub topic a host node subscribes to, and how many of the two nodes must hold a subscriber for it + /// before the harness reports the cluster formed (lmxopcua#1108). + /// + /// The topic name. + /// 1 when the subscriber is a cluster singleton (it lives on one node), 2 for a per-node subscriber. + /// True when the subscriber exists as soon as the harness has booted (the default wait set); + /// false when it is only created later (a deploy spawns it, or a test/host adds the bridge), so waiting for it in + /// StartAsync would time out - such a test calls itself once the + /// subscriber exists. + public readonly record struct PubSubTopic(string Name, int MinNodes, bool AtBoot = true); + + /// + /// lmxopcua#1108: every topic the host nodes subscribe to (the Subscribe(...) call sites under + /// src/). waits for the subset by default + /// (measured against the harness hosts: the others have no subscriber until something creates one). A new + /// subscribed topic must be added here or a test publishing on it has the #1106 lost-publish window. + /// + public static readonly IReadOnlyList AllHostTopics = + [ + new(ZB.MOM.WW.OtOpcUa.Runtime.Drivers.DriverHostActor.DeploymentsTopic, 2), + new(ZB.MOM.WW.OtOpcUa.Runtime.Drivers.DriverHostActor.DeploymentAcksTopic, 1), + new(ZB.MOM.WW.OtOpcUa.Runtime.Drivers.DriverHostActor.DriverControlTopic, 2), + new(ZB.MOM.WW.OtOpcUa.Runtime.OpcUa.OpcUaPublishActor.RedundancyStateTopic, 2), + new(ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms.ScriptedAlarmHostActor.AlarmCommandsTopic, 2, AtBoot: false), + new(ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms.ScriptedAlarmHostActor.AlertsTopic, 2), + new(ZB.MOM.WW.OtOpcUa.Runtime.Redundancy.OpcUaEndpointStepDownActor.Topic, 2, AtBoot: false), + new(ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers.DriverHealthChanged.TopicName, 2, AtBoot: false), + new(ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers.DriverResilienceStatusChanged.TopicName, 2, AtBoot: false), + new(ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags.VirtualTagActor.ScriptLogsTopic, 2, AtBoot: false), + ]; + + private IReadOnlyList _requiredTopics = BootTopics; + + /// The topics waits for by default: those subscribed as soon as the hosts boot. + public static IReadOnlyList BootTopics { get; } = AllHostTopics.Where(t => t.AtBoot).ToList(); + + /// + /// lmxopcua#1106 / #1108: waits until each node's DistributedPubSub registry carries a subscriber from at least + /// nodes for every topic in (default + /// ), and both nodes see the same owner set - so a publish from either node reaches + /// every subscriber. /// /// /// Cluster membership and the DPS registry converge independently: a member is Up as soon as cluster gossip says /// so, but the mediators exchange their subscription buckets on their own gossip tick (1 s by default) and a /// Publish reaches only the buckets its mediator already holds. Under heavy load (#1106 reproduced it at - /// load average 250–430, 1 in ~10–20 runs) the test's deploy landed in that window — 13 ms after node B came Up — + /// load average 250-430, 1 in ~10-20 runs) the test's deploy landed in that window - 13 ms after node B came Up - /// node B never received the dispatch, and the deployment sat AwaitingApplyAcks until the 120 s apply deadline. /// In production this is the documented fire-and-forget dispatch (no re-dispatch; the apply deadline is the - /// backstop) — on the DPS transport only, which split-topology nodes no longer use. + /// backstop) - on the DPS transport only, which split-topology nodes no longer use. /// /// The time allowed; the gossip interval plus slack is plenty. - public async Task WaitForDeployTopicsAsync(TimeSpan timeout) + /// The topics to wait for; null waits for . + public async Task WaitForPubSubConvergedAsync(TimeSpan timeout, IReadOnlyList? topics = null) { - var a = NodeAAkkaPort.ToString(); - var b = NodeBAkkaPort.ToString(); + topics ??= BootTopics; var deadline = DateTime.UtcNow + timeout; while (true) { var viewA = ReadDpsRegistry(NodeASystem); var viewB = ReadDpsRegistry(NodeBSystem); - if (Converged(viewA) && Converged(viewB) - && Owners(viewA, "deployment-acks").SetEquals(Owners(viewB, "deployment-acks"))) - return; + var pending = topics.Where(t => !Converged(t, viewA, viewB)).ToList(); + if (pending.Count == 0) return; if (DateTime.UtcNow >= deadline) + { + var names = pending.Select(t => $"{t.Name}(>={t.MinNodes} node(s))").ToArray(); throw new TimeoutException( - $"The deploy topics did not gossip to both nodes within {timeout}: " - + $"A=[{DescribeDpsRegistry(NodeASystem, "deployments", "deployment-acks")}] " - + $"B=[{DescribeDpsRegistry(NodeBSystem, "deployments", "deployment-acks")}] (expected deployments@{{{a},{b}}})"); + $"These pub/sub topics did not gossip to both nodes within {timeout}: {string.Join(", ", names)}. " + + $"A=[{DescribeDpsRegistry(NodeASystem, pending.Select(t => t.Name).ToArray())}] " + + $"B=[{DescribeDpsRegistry(NodeBSystem, pending.Select(t => t.Name).ToArray())}]"); + } await Task.Delay(50); } - bool Converged(IReadOnlyList<(string Owner, List Keys)> view) + static bool Converged( + PubSubTopic t, + IReadOnlyList<(string Owner, List Keys)> a, + IReadOnlyList<(string Owner, List Keys)> b) { - var deployments = Owners(view, "deployments"); - return deployments.Contains(a) && deployments.Contains(b) && Owners(view, "deployment-acks").Count > 0; + var oa = Owners(a, t.Name); + return oa.Count >= t.MinNodes && oa.SetEquals(Owners(b, t.Name)); } } + /// lmxopcua#1106 name kept for callers; equivalent to over the required topics. + /// The time allowed. + public Task WaitForDeployTopicsAsync(TimeSpan timeout) => WaitForPubSubConvergedAsync(timeout, _requiredTopics); + private static HashSet Owners(IReadOnlyList<(string Owner, List Keys)> view, string topic) => view.Where(x => x.Keys.Any(k => k.EndsWith("/" + topic, StringComparison.Ordinal))) .Select(x => x.Owner) @@ -331,7 +383,7 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable NodeASystem, NodeBSystem, formationTimeout ?? TimeSpan.FromSeconds(20)); - await WaitForDeployTopicsAsync(formationTimeout ?? TimeSpan.FromSeconds(20)); + await WaitForPubSubConvergedAsync(formationTimeout ?? TimeSpan.FromSeconds(20), _requiredTopics); } /// @@ -427,7 +479,7 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable /// /// lmxopcua#843 / #1106: where a deploy stood when a seal wait gave up — the deployment row, each node's /// per-node state, both nodes' cluster views, which members each node's DistributedPubSub registry holds a - /// subscriber on for the two deploy topics (), and both nodes' recent Akka log + /// subscriber on for each host topic (), and both nodes' recent Akka log /// events. A node whose driver host never logged "reading artifact" for the deployment never received the /// dispatch; one that applied (its own Applied row — on a fused node DriverHostActor writes that itself, so /// it does NOT prove the coordinator counted the ack) while the coordinator never sealed lost the ack. @@ -545,17 +597,25 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable + $" unreachable={Akka.Cluster.Cluster.Get(s).State.Unreachable.Count}"; return $"{dbPart}; A view=[{View(NodeASystem)}] B view=[{View(NodeBSystem)}]; " - + $"A dps=[{DescribeDpsRegistry(NodeASystem, "deployments", "deployment-acks")}] " - + $"B dps=[{DescribeDpsRegistry(NodeBSystem, "deployments", "deployment-acks")}]"; + + $"A dps=[{DescribeDpsRegistry(NodeASystem, AllHostTopics.Select(t => t.Name).ToArray())}] " + + $"B dps=[{DescribeDpsRegistry(NodeBSystem, AllHostTopics.Select(t => t.Name).ToArray())}]"; } /// /// lmxopcua#1106: which cluster members THIS node's DistributedPubSub mediator currently knows a subscriber on, per /// topic — i.e. where a Publish from this node would be delivered right now. A publish only reaches the /// members whose registry bucket has gossiped here, so a message published before the peer's bucket arrived is - /// silently lost. Akka exposes no public per-topic, cluster-wide view (CountSubscribers counts only this - /// node's LOCAL subscribers), so this reads the mediator's private _registry by reflection: test-only, a - /// best-effort snapshot taken off the mediator's thread, and it reports a failure instead of throwing. + /// silently lost. + /// + /// lmxopcua#1108 - why reflection: Akka.Cluster.Tools 1.5.62's public mediator queries cannot answer "which + /// member's bucket do I hold". GetTopics/CurrentTopics returns the union of every bucket's keys with + /// no owner (a topic subscribed on one node looks identical whichever node's bucket supplied it); + /// CountSubscribers/Count are internal and local-only. A public publish-probe can prove a + /// bucket arrived, but only for the probe's own topic, not that the real subscriptions are in it. So this reads + /// the mediator's private _registry: test-only, a best-effort snapshot taken off the mediator's thread. + /// On an Akka upgrade that renames those members throws a + /// naming the Akka version; this method reports it inline. + /// /// /// The node whose mediator to read. /// The topics to report. @@ -582,6 +642,23 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable /// The node whose mediator to read. /// (owner port, keys) per registry bucket. private static IReadOnlyList<(string Owner, List Keys)> ReadDpsRegistry(ActorSystem system) + { + try + { + return ReadDpsRegistryCore(system); + } + catch (NotSupportedException ex) + { + var version = typeof(Akka.Cluster.Tools.PublishSubscribe.DistributedPubSub).Assembly.GetName().Version; + throw new NotSupportedException( + $"TwoNodeClusterHarness reads DistributedPubSubMediator's private registry by reflection (lmxopcua#1108), " + + $"which no longer matches Akka.Cluster.Tools {version} (Directory.Packages.props pins 1.5.62): {ex.Message}. " + + "Update ReadDpsRegistryCore for the new internals, or replace it with a public API if the new Akka " + + "version offers a per-owner registry query.", ex); + } + } + + private static IReadOnlyList<(string Owner, List Keys)> ReadDpsRegistryCore(ActorSystem system) { const System.Reflection.BindingFlags Any = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;