From 399deea32fd24e3d870e19d7f63ed30b04d82b12 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 5 Dec 2026 03:05:41 -0500 Subject: [PATCH] fix(runtime): #1113 a stubbed driver's value nodes read BadConfigurationError, not BadWaitingForInitialData forever Item 4. A stub child (no factory for the type, a factory that rejected the config, or a type not allowed on the node's roles) never publishes, so its nodes sat at BadWaitingForInitialData forever. StampStubbedDriverValueNodes stamps BadConfigurationError (new OpcUaStatus constant, 0x80890000) on the raw + UNS value nodes of every stubbed child after each apply's RebuildAddressSpace Tell (deploy, boot-from-cache, bootstrap restore) - same host->publish-actor pair, so the publish actor defers it behind that plan and replays it onto the nodes the apply (re)creates - and after an operator Restart into a stub. A config-change respawn into a stub gets the respawn's BadWaitingForInitialData first, then this. Status-only, not latched; a stub drops any inherited latch. Tests: Restart + config-change into a stub, and a fresh deploy of a stub (all fail with the stamp disabled). --- docs/DriverLifecycle.md | 9 ++- .../OpcUa/OpcUaStatus.cs | 6 ++ .../Drivers/DriverHostActor.cs | 53 +++++++++++++++++ .../DriverHostRespawnValueNodeGapTests.cs | 57 ++++++++++++++++++- 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/docs/DriverLifecycle.md b/docs/DriverLifecycle.md index c75b2d9aa..49a580cd2 100644 --- a/docs/DriverLifecycle.md +++ b/docs/DriverLifecycle.md @@ -603,7 +603,7 @@ composition still routes for that driver (`TrimCommsLatchesToIndex`) — a ref t can never be re-published, so it must not be reported — and drops a latch left empty; this runs inside the same apply message as the respawn, so before the replacement's `Connected` can arm the audit. The Warning names the status the nodes actually carry: `still BadNoCommunication`, or `still BadWaitingForInitialData` once a respawn / Restart re-stamped them -(#1113). `StopChild` also +(#1113). A stub replacement drops the latch (it never connects, so the audit could never arm). `StopChild` also holds the id in the `ChildHealthLedger` (`Forget` then `Readmit` on the next spawn), so the stopped child's late health publish cannot resurrect its diagnostics entry. @@ -649,6 +649,13 @@ supervision restart (same driver object — unchanged, still covered by that chi disable (`RemovedInputStatus` + node removal). ScadaBridge sees a brief Bad per respawn/Restart — accepted. Pinned by `DriverHostRespawnValueNodeGapTests` (both seams, both interleavings, no-latch with a positive control). +- **A stub replacement reads `BadConfigurationError` (#1113).** When the replacement (or a freshly deployed driver) is a + STUB — no factory for its type on this node, the factory rejected its config, or the type is not allowed on the node's + roles — it never publishes, so `BadWaitingForInitialData` would be a promise that cannot be kept. + `StampStubbedDriverValueNodes` stamps `BadConfigurationError` on the raw + UNS value nodes of every stubbed child after + each apply's `RebuildAddressSpace` Tell (deploy, boot-from-cache, bootstrap restore — on the same host→publish-actor pair, + so the publish actor defers it behind that plan and replays it onto the nodes the apply creates or recreates; re-sent every + apply because a rebuild resets them) and after an operator Restart that respawned a stub. Status-only, not latched. - **The gRPC data plane's origin side is covered by the producer fence, not by this stamp (#1113).** Projected consumers (DataConnectionService read / subscribe) do not read the OPC UA node state: they read the `PartitionedSampleHub`, fed by the driver callback through `SourceSamplePublisher`. `StopChild` / `HandleRestartDriver` call diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/OpcUaStatus.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/OpcUaStatus.cs index d8531a5cb..7f6185b4a 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/OpcUaStatus.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/OpcUaStatus.cs @@ -58,6 +58,12 @@ public static class OpcUaStatus /// (lmxopcua#787) — distinct from , which means the tag's own script failed. public const uint BadOutOfService = 0x808D0000; + /// The source is misconfigured and will produce nothing until the configuration changes. The status a + /// driver's value nodes carry while that driver runs as a STUB — its type has no factory on this node, the factory + /// rejected its config, or the type is not allowed on the node's roles (lmxopcua#1113) — instead of + /// , which would promise a first value that can never come. + public const uint BadConfigurationError = 0x80890000; + /// Part 4 severity bucket of : the top two bits. /// The status code to classify. /// 0 for Good, 1 for Uncertain, 2 or 3 for Bad. 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 bdef19b95..2f980b355 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs @@ -1853,6 +1853,7 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn _opcUaPublishActor?.Tell( new ZB.MOM.WW.OtOpcUa.Runtime.OpcUa.OpcUaPublishActor.RebuildAddressSpace(correlation, deploymentId, blob, CatalogAttempt: _catalogAttempt, ArtifactRevision: ArtifactRevisionOf(blob))); + StampStubbedDriverValueNodes(); if (_opcUaPublishActor is null && _catalogAttempt is not null) _catalogCoordinator?.Complete(_catalogAttempt, topologySucceeded: true); return true; @@ -3628,6 +3629,8 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn _opcUaPublishActor?.Tell(new ZB.MOM.WW.OtOpcUa.Runtime.OpcUa.OpcUaPublishActor.RebuildAddressSpace( msg.Correlation, msg.DeploymentId, appliedBlob, ReportOutcome: true, CatalogAttempt: _catalogAttempt, ArtifactRevision: ArtifactRevisionOf(appliedBlob))); + // #1113 item 4: after the rebuild Tell, so it is replayed onto the nodes this apply (re)creates. + StampStubbedDriverValueNodes(); // Reaching here means the artifact came from central, so the node is no longer serving a // cache-sourced configuration. Note this only clears on a real apply: a dispatch of the // revision already booted from cache short-circuits in HandleDispatchFromSteady without @@ -4333,6 +4336,7 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn CacheAppliedArtifact(deploymentId, rev, appliedBlob); _opcUaPublishActor?.Tell(new ZB.MOM.WW.OtOpcUa.Runtime.OpcUa.OpcUaPublishActor.RebuildAddressSpace(correlation, deploymentId, appliedBlob, CatalogAttempt: _catalogAttempt, ArtifactRevision: ArtifactRevisionOf(appliedBlob))); + if (appliedBlob is not null) StampStubbedDriverValueNodes(); _log.Info("DriverHost {Node}: restored served state for applied deployment {Id} on bootstrap", _localNode, deploymentId); } catch (Exception ex) @@ -5418,6 +5422,53 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn _localNode, driverInstanceId, writes.Count); } + /// + /// Gitea #1113 item 4 — stamps on every raw NodeId and every + /// referencing UNS NodeId of each STUBBED child (or only ): a stub (no + /// factory for the type, a factory that rejected the config, or a type not allowed on this node's roles) never + /// publishes, so its nodes would otherwise sit at forever — + /// a promise of a first value that cannot come. + /// + /// + /// + /// Called after each apply's Tell (deploy, boot from + /// cache, bootstrap restore) on the same host→publish-actor pair, so the publish actor has already started + /// that plan: the write lands on the nodes that exist now AND is deferred and replayed onto the nodes the apply + /// creates or recreates (a fresh deploy, a rebuild) — re-sent every apply because a rebuild resets them. And + /// after an operator Restart that respawned a stub. A config-change respawn into a stub gets the respawn's + /// BadWaitingForInitialData first (at the stop), then this. + /// + /// + /// Status-only and not a comms loss: nothing is latched. A latch the previous incarnation left (a respawn + /// of a comms-lost driver, #1113 item 2) is dropped — a stub never connects, so its audit could never arm. + /// + /// + /// Restrict to this driver; for every stubbed child. + private void StampStubbedDriverValueNodes(string? onlyDriverInstanceId = null) + { + if (_opcUaPublishActor is null) return; + List? writes = null; + DateTime ts = default; + foreach (var (driverId, child) in _children) + { + if (!child.Stubbed) continue; + if (onlyDriverInstanceId is not null && !string.Equals(driverId, onlyDriverInstanceId, StringComparison.Ordinal)) continue; + ForgetCommsLatch(driverId); + if (!_valueRefsByDriver.TryGetValue(driverId, out var entries) || entries.Count == 0) continue; + if (writes is null) { writes = new List(); ts = DateTime.UtcNow; } + var before = writes.Count; + foreach (var (_, nodes) in entries) + foreach (var n in nodes) + writes.Add(new StatusWrite(n.NodeId, OpcUaStatus.BadConfigurationError, ts, n.Realm)); + _log.Info( + "DriverHost {Node}: driver {Driver} ({Type}) is a stub on this node — {Count} value node(s) marked BadConfigurationError", + _localNode, driverId, child.DriverType, writes.Count - before); + } + + if (writes is { Count: > 0 }) + _opcUaPublishActor.Tell(new OpcUa.OpcUaPublishActor.AttributeStatusBatchUpdate(writes)); + } + /// Marks a child this host is about to stop as retired and watches it until it terminates. /// The child being stopped. private void RetireChild(IActorRef child) @@ -5534,6 +5585,8 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn // Respawn from the same spec the last reconcile used — preserves RowId, Name, ClusterId. SpawnChild(entry.Spec); + // #1113 item 4: a stub replacement will never publish — not "waiting for initial data". + StampStubbedDriverValueNodes(msg.DriverInstanceId); } /// diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostRespawnValueNodeGapTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostRespawnValueNodeGapTests.cs index 3618f5293..c5247c905 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostRespawnValueNodeGapTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/DriverHostRespawnValueNodeGapTests.cs @@ -224,6 +224,56 @@ public sealed class DriverHostRespawnValueNodeGapTests : RuntimeActorTestBase TestContext.Current.CancellationToken); } + // ---- #1113 item 4: a stubbed replacement / a stub deploy ---- + + /// + /// #1113 item 4: when the replacement is a STUB (here: no factory for the type), its nodes would otherwise sit at + /// BadWaitingForInitialData forever. They go BadConfigurationError, after the respawn stamp — for a + /// config change after the apply's RebuildAddressSpace (so the publish actor replays it onto the nodes the + /// apply recreates), including the ref the new deployment adds. + /// + [Theory] + [InlineData(false)] + [InlineData(true)] + public void A_stubbed_replacement_marks_the_value_nodes_BadConfigurationError(bool configChange) + { + var oldDriver = new GatedDriver(DriverId); + var (host, publish, coordinator, db) = Arrange(oldDriver, newDriver: null); + SeedGoodFromOldDriver(oldDriver, publish); + + Replace(configChange, host, coordinator, db); + + var stamp = ExpectStampBeforeAnyValue(publish); + stamp.Items.ShouldAllBe(i => i.StatusCode == OpcUaStatus.BadWaitingForInitialData); + var rebuildSeen = false; + var config = (OpcUaPublishActor.AttributeStatusBatchUpdate)publish.FishForMessage(m => + { + if (m is OpcUaPublishActor.RebuildAddressSpace) rebuildSeen = true; + return m is OpcUaPublishActor.AttributeStatusBatchUpdate s + && s.Items.Any(i => i.StatusCode == OpcUaStatus.BadConfigurationError); + }, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken); + var expected = configChange ? AllOldNodes.Append(TempRef).ToArray() : AllOldNodes; + config.Items.Select(i => i.NodeId).ShouldBe(expected, ignoreOrder: true); + config.Items.ShouldAllBe(i => i.StatusCode == OpcUaStatus.BadConfigurationError); + rebuildSeen.ShouldBe(configChange, "a config change stamps after its apply's rebuild; a Restart has none"); + } + + /// #1113 item 4 on a fresh deploy: a driver that is a stub from the start never gets a first value either — + /// its nodes are marked BadConfigurationError right after the apply's RebuildAddressSpace. + [Fact] + public void A_fresh_deploy_of_a_stub_marks_its_value_nodes_BadConfigurationError() + { + var db = NewInMemoryDbFactory(); + var deploymentId = SeedTwoTagDeployment(db); + var (_, publish, _, _) = SpawnHostAndApplyWithCoordinator(this, db, TestNode, deploymentId, RevA, + driverFactory: new SequenceDriverFactory(new IDriver?[] { null })); + // SpawnHostAndApplyWithCoordinator consumed the RebuildAddressSpace: the stamp is what follows it. + var config = publish.FishForMessage(_ => true, PresenceBudget, + cancellationToken: TestContext.Current.CancellationToken); + config.Items.Select(i => i.NodeId).ShouldBe(AllOldNodes, ignoreOrder: true); + config.Items.ShouldAllBe(i => i.StatusCode == OpcUaStatus.BadConfigurationError); + } + /// Takes the old driver comms-lost (Force Reconnect with its re-initialise held forever) and waits for the /// host's latched BadNoCommunication stamp. private static void GoCommsLost(GatedDriver oldDriver, IActorRef host, TestProbe publish) @@ -270,7 +320,7 @@ public sealed class DriverHostRespawnValueNodeGapTests : RuntimeActorTestBase enabled: true); private (IActorRef Host, TestProbe Publish, TestProbe Coordinator, Microsoft.EntityFrameworkCore.IDbContextFactory Db) - Arrange(GatedDriver oldDriver, GatedDriver newDriver, TimeSpan? auditDelay = null) + Arrange(GatedDriver oldDriver, GatedDriver? newDriver, TimeSpan? auditDelay = null) { var db = NewInMemoryDbFactory(); var deploymentId = SeedTwoTagDeployment(db); @@ -352,8 +402,9 @@ public sealed class DriverHostRespawnValueNodeGapTests : RuntimeActorTestBase return (statuses, (OpcUaPublishActor.AttributeValueBatchUpdate)msg); } - /// Hands out the given drivers in order, one per spawn (the last one repeats). - private sealed class SequenceDriverFactory(params IDriver[] drivers) : IDriverFactory + /// Hands out the given drivers in order, one per spawn (the last one repeats); a null is "no factory for + /// this type", which makes the host spawn a stub. + private sealed class SequenceDriverFactory(params IDriver?[] drivers) : IDriverFactory { private int _next;