Merge #1108: TwoNodeClusterHarness waits for every boot-time pub/sub topic to converge (requiredTopics override; lazy topics opt-in); reflection registry read kept (no public Akka 1.5.62 API) and fails loudly on internals drift

This commit is contained in:
Joseph Doherty
2026-12-04 22:15:56 -05:00
2 changed files with 106 additions and 29 deletions
@@ -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
/// <c>deployments</c> subscription for ~10 s after B is Up; before the harness waited for the deploy topics to
/// gossip (<see cref="TwoNodeClusterHarness.WaitForDeployTopicsAsync"/>) the dispatch went to node A only and the
/// gossip (<see cref="TwoNodeClusterHarness.WaitForPubSubConvergedAsync"/>) 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).
/// </summary>
@@ -194,9 +194,12 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
/// <param name="driverProbe">Optional probe that both nodes expose to the admin operations singleton.</param>
/// <param name="extraHocon">Optional HOCON prepended to both nodes' Akka config (tests that need a non-default
/// Akka setting, e.g. a slow DistributedPubSub gossip interval).</param>
/// <param name="requiredTopics">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
/// (<see cref="BootTopics"/>). A test that publishes on a topic outside the set is exposed to the #1106 window.</param>
/// <remarks>
/// lmxopcua#1106: "formed" includes the deploy topics' DistributedPubSub registries having gossiped to both
/// nodes (<see cref="WaitForDeployTopicsAsync"/>) — 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 (<see cref="WaitForPubSubConvergedAsync"/>) — 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.
/// </remarks>
@@ -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<PubSubTopic>? 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;
/// <summary>
/// lmxopcua#1106: waits until each node's DistributedPubSub registry carries BOTH nodes' <c>deployments</c>
/// subscriptions (the dispatch reaches every driver host, whichever node hosts the coordinator) and the same,
/// non-empty <c>deployment-acks</c> 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).
/// </summary>
/// <param name="Name">The topic name.</param>
/// <param name="MinNodes">1 when the subscriber is a cluster singleton (it lives on one node), 2 for a per-node subscriber.</param>
/// <param name="AtBoot">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
/// <c>StartAsync</c> would time out - such a test calls <see cref="WaitForPubSubConvergedAsync"/> itself once the
/// subscriber exists.</param>
public readonly record struct PubSubTopic(string Name, int MinNodes, bool AtBoot = true);
/// <summary>
/// lmxopcua#1108: every topic the host nodes subscribe to (the <c>Subscribe(...)</c> call sites under
/// <c>src/</c>). <see cref="StartAsync"/> waits for the <see cref="PubSubTopic.AtBoot"/> 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.
/// </summary>
public static readonly IReadOnlyList<PubSubTopic> 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<PubSubTopic> _requiredTopics = BootTopics;
/// <summary>The topics <see cref="StartAsync"/> waits for by default: those subscribed as soon as the hosts boot.</summary>
public static IReadOnlyList<PubSubTopic> BootTopics { get; } = AllHostTopics.Where(t => t.AtBoot).ToList();
/// <summary>
/// lmxopcua#1106 / #1108: waits until each node's DistributedPubSub registry carries a subscriber from at least
/// <see cref="PubSubTopic.MinNodes"/> nodes for every topic in <paramref name="topics"/> (default
/// <see cref="BootTopics"/>), and both nodes see the same owner set - so a publish from either node reaches
/// every subscriber.
/// </summary>
/// <remarks>
/// 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
/// <c>Publish</c> 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.
/// </remarks>
/// <param name="timeout">The time allowed; the gossip interval plus slack is plenty.</param>
public async Task WaitForDeployTopicsAsync(TimeSpan timeout)
/// <param name="topics">The topics to wait for; null waits for <see cref="BootTopics"/>.</param>
public async Task WaitForPubSubConvergedAsync(TimeSpan timeout, IReadOnlyList<PubSubTopic>? 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<string> Keys)> view)
static bool Converged(
PubSubTopic t,
IReadOnlyList<(string Owner, List<string> Keys)> a,
IReadOnlyList<(string Owner, List<string> 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));
}
}
/// <summary>lmxopcua#1106 name kept for callers; equivalent to <see cref="WaitForPubSubConvergedAsync"/> over the required topics.</summary>
/// <param name="timeout">The time allowed.</param>
public Task WaitForDeployTopicsAsync(TimeSpan timeout) => WaitForPubSubConvergedAsync(timeout, _requiredTopics);
private static HashSet<string> Owners(IReadOnlyList<(string Owner, List<string> 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);
}
/// <summary>
@@ -427,7 +479,7 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
/// <summary>
/// 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 (<see cref="DescribeDpsRegistry"/>), and both nodes' recent Akka log
/// subscriber on for each host topic (<see cref="DescribeDpsRegistry"/>), 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 <c>DriverHostActor</c> 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())}]";
}
/// <summary>
/// lmxopcua#1106: which cluster members THIS node's DistributedPubSub mediator currently knows a subscriber on, per
/// topic — i.e. where a <c>Publish</c> 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 (<c>CountSubscribers</c> counts only this
/// node's LOCAL subscribers), so this reads the mediator's private <c>_registry</c> by reflection: test-only, a
/// best-effort snapshot taken off the mediator's thread, and it reports a failure instead of throwing.
/// silently lost.
/// <para>
/// lmxopcua#1108 - why reflection: Akka.Cluster.Tools 1.5.62's public mediator queries cannot answer "which
/// member's bucket do I hold". <c>GetTopics</c>/<c>CurrentTopics</c> 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);
/// <c>CountSubscribers</c>/<c>Count</c> are <c>internal</c> 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 <c>_registry</c>: test-only, a best-effort snapshot taken off the mediator's thread.
/// On an Akka upgrade that renames those members <see cref="ReadDpsRegistry"/> throws a
/// <see cref="NotSupportedException"/> naming the Akka version; this method reports it inline.
/// </para>
/// </summary>
/// <param name="system">The node whose mediator to read.</param>
/// <param name="topics">The topics to report.</param>
@@ -582,6 +642,23 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable
/// <param name="system">The node whose mediator to read.</param>
/// <returns>(owner port, keys) per registry bucket.</returns>
private static IReadOnlyList<(string Owner, List<string> 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<string> Keys)> ReadDpsRegistryCore(ActorSystem system)
{
const System.Reflection.BindingFlags Any = System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;