e6bfbdb6e5
R-11: DriverHostActor skips the deploy bulk push for a kept child whose seeding fingerprint (value refs + raw/UNS fan-out + historization + alarm refs) is unchanged and none of whose refs gained a mux consumer (VirtualTag / scripted alarm / IDependencyConsumer driver). Skipped drivers are re-pushed when the address-space outcome reports a rebuild (new additive RebuildCompleted.AddressSpaceRebuilt, default true), degraded, timed out, or the apply threw. Boot/restore still push all. R-14: ParsedDeploymentArtifact memoises the data projection + its validation per node id (a throw re-thrown to every caller), and the row parsers skip it when the three data arrays are empty. OpcUaPublishActor hashes the artifact once (RebuildAddressSpace. ArtifactRevision) and reads the cluster redundancy mode from the plan step's parse instead of a second parse on the pinned thread. InjectScriptSource skips JsonNode.Parse for tag configs without a scriptId.
181 lines
9.7 KiB
C#
181 lines
9.7 KiB
C#
using Akka.Actor;
|
|
using Akka.TestKit;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Engines;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
|
|
|
|
/// <summary>
|
|
/// Gitea #995 (review 2026-12-02 R-11): every deploy used to re-subscribe every kept driver's whole data set —
|
|
/// upstream monitored-item delete/create, Galaxy unadvise/advise, a forced full re-read, a data gap — even when the
|
|
/// deploy did not touch that driver. A kept driver whose desired set and everything it seeds are unchanged is now
|
|
/// left alone on a NON-rebuild apply. The re-subscribe's seeding job is kept: a new UNS mirror, a new mux consumer
|
|
/// of one of the driver's refs, or an address-space REBUILD (nodes recreated empty) still re-push it.
|
|
/// </summary>
|
|
public sealed class DriverHostActorUnchangedResubscribeTests : RuntimeActorTestBase
|
|
{
|
|
private static readonly NodeId TestNode = NodeId.Parse("node-1:4053");
|
|
private const string SpeedPath = "Plant/Modbus/dev1/speed";
|
|
private const string Equipment = "EQ-filling-line1-station1";
|
|
|
|
private static readonly (string, string, string, string, string, bool)[] OneDriver =
|
|
[("drv-1", "Modbus", "dev1", "speed", "Float", false)];
|
|
|
|
private static readonly (string, string, string, string, string, bool)[] TwoDrivers =
|
|
[("drv-1", "Modbus", "dev1", "speed", "Float", false), ("drv-2", "Modbus2", "dev2", "pressure", "Float", false)];
|
|
|
|
private static readonly (string, string, string, string, string?)[] NoRefs = [];
|
|
|
|
/// <summary>An unrelated deploy no longer re-subscribes drv-1; a deploy adding a UNS mirror of its tag does
|
|
/// (the positive control that also orders drv-1's mailbox: had the first deploy pushed, the count would be 3).</summary>
|
|
[Fact]
|
|
public void A_kept_driver_is_not_resubscribed_by_an_unrelated_non_rebuild_deploy_but_is_by_a_new_mirror()
|
|
{
|
|
var rig = new Rig(this, NewInMemoryDbFactory());
|
|
rig.Deploy(OneDriver, NoRefs, Rev('a'));
|
|
var drv1 = rig.Driver("drv-1");
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
|
|
// Deploy B adds an unrelated driver. Its own push (drv-2 subscribes) is the presence half.
|
|
rig.Deploy(TwoDrivers, NoRefs, Rev('b'));
|
|
AwaitAssert(() => rig.Driver("drv-2").SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
|
|
// Deploy C adds a UNS reference to drv-1's tag: the new mirror needs a seed, so drv-1 IS re-pushed.
|
|
rig.Deploy(TwoDrivers, [("filling", "line1", "station1", "speed", null)], Rev('c'));
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBeGreaterThanOrEqualTo(2), PresenceBudget, cancellationToken: Ct);
|
|
// Mailbox order: B's push (had it been sent) precedes C's, so exactly 2 means B was skipped.
|
|
Thread.Sleep(300);
|
|
drv1.SubscribeCount.ShouldBe(2, "the unrelated deploy B re-subscribed drv-1");
|
|
rig.Driver("drv-2").SubscribeCount.ShouldBe(1, "deploy C did not touch drv-2");
|
|
factoryCreatedOnce(rig);
|
|
}
|
|
|
|
/// <summary>A full rebuild recreates every node without a value: the skipped driver is re-pushed once the
|
|
/// address-space outcome reports the rebuild.</summary>
|
|
[Fact]
|
|
public void A_skipped_driver_is_re_pushed_when_the_address_space_apply_was_a_rebuild()
|
|
{
|
|
var rig = new Rig(this, NewInMemoryDbFactory());
|
|
rig.Deploy(OneDriver, NoRefs, Rev('a'));
|
|
var drv1 = rig.Driver("drv-1");
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
|
|
rig.Rebuilt = true;
|
|
rig.Deploy(TwoDrivers, NoRefs, Rev('b'));
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(2), PresenceBudget, cancellationToken: Ct);
|
|
}
|
|
|
|
/// <summary>A degraded apply may have left nodes missing: re-push too.</summary>
|
|
[Fact]
|
|
public void A_skipped_driver_is_re_pushed_when_the_address_space_apply_was_degraded()
|
|
{
|
|
var rig = new Rig(this, NewInMemoryDbFactory());
|
|
rig.Deploy(OneDriver, NoRefs, Rev('a'));
|
|
var drv1 = rig.Driver("drv-1");
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
|
|
rig.Degraded = true;
|
|
rig.Deploy(TwoDrivers, NoRefs, Rev('b'), expectDegraded: true);
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(2), PresenceBudget, cancellationToken: Ct);
|
|
}
|
|
|
|
/// <summary>A new VirtualTag reading drv-1's tag registers with the mux, which replays a retained value only for refs
|
|
/// some replay-opting subscriber already named — so its first input has to come from a re-subscribe.</summary>
|
|
[Fact]
|
|
public void A_new_virtual_tag_consumer_of_a_kept_drivers_ref_re_pushes_that_driver()
|
|
{
|
|
var rig = new Rig(this, NewInMemoryDbFactory());
|
|
var equip = new[] { ("filling", "line1", "station1", "speed", (string?)null) };
|
|
rig.Deploy(OneDriver, equip, Rev('a'));
|
|
var drv1 = rig.Driver("drv-1");
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
|
|
rig.Deploy(OneDriver, equip, Rev('b'),
|
|
virtualTags: [("vt-1", Equipment, "Doubled", $"return (double)ctx.GetTag(\"{SpeedPath}\").Value * 2;")]);
|
|
AwaitAssert(() => drv1.SubscribeCount.ShouldBe(2), PresenceBudget, cancellationToken: Ct);
|
|
|
|
// ...and a further unrelated deploy with the same VirtualTag does not (the consumer is no longer new).
|
|
rig.Deploy(TwoDrivers, equip, Rev('c'),
|
|
virtualTags: [("vt-1", Equipment, "Doubled", $"return (double)ctx.GetTag(\"{SpeedPath}\").Value * 2;")]);
|
|
AwaitAssert(() => rig.Driver("drv-2").SubscribeCount.ShouldBe(1), PresenceBudget, cancellationToken: Ct);
|
|
rig.Deploy(TwoDrivers, equip, Rev('d'), virtualTags:
|
|
[
|
|
("vt-1", Equipment, "Doubled", $"return (double)ctx.GetTag(\"{SpeedPath}\").Value * 2;"),
|
|
("vt-2", Equipment, "Pressure2", "return (double)ctx.GetTag(\"Plant/Modbus2/dev2/pressure\").Value * 2;"),
|
|
]);
|
|
// Positive control: vt-2 is a new consumer of drv-2's ref, so drv-2 is re-pushed …
|
|
AwaitAssert(() => rig.Driver("drv-2").SubscribeCount.ShouldBe(2), PresenceBudget, cancellationToken: Ct);
|
|
Thread.Sleep(300);
|
|
// … while drv-1 (c and d both skipped) stays at 2.
|
|
drv1.SubscribeCount.ShouldBe(2, "an unchanged driver was re-subscribed by a deploy that added nothing it seeds");
|
|
}
|
|
|
|
private static void factoryCreatedOnce(Rig rig) =>
|
|
rig.Factory.Created.Count(d => d.DriverInstanceId == "drv-1").ShouldBe(1, "drv-1 must be the same kept driver throughout");
|
|
|
|
private static RevisionHash Rev(char c) => RevisionHash.Parse(new string(c, 64));
|
|
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
|
|
private sealed class Rig
|
|
{
|
|
private readonly RuntimeActorTestBase _kit;
|
|
private readonly IDbContextFactory<OtOpcUaConfigDbContext> _db;
|
|
private readonly IActorRef _host;
|
|
private readonly TestProbe _coordinator;
|
|
|
|
public RecordingSubscribableDriverFactory Factory { get; } = new("Modbus");
|
|
public volatile bool Rebuilt;
|
|
public volatile bool Degraded;
|
|
|
|
public Rig(RuntimeActorTestBase kit, IDbContextFactory<OtOpcUaConfigDbContext> db)
|
|
{
|
|
_kit = kit;
|
|
_db = db;
|
|
_coordinator = kit.CreateTestProbe();
|
|
var publish = kit.CreateTestProbe();
|
|
publish.SetAutoPilot(new DelegateAutoPilot((sender, message) =>
|
|
{
|
|
if (message is OpcUaPublishActor.RebuildAddressSpace { ReportOutcome: true } rebuild)
|
|
sender.Tell(new OpcUaPublishActor.RebuildCompleted(rebuild.Correlation, rebuild.DeploymentId,
|
|
Degraded: Degraded, DegradedReason: Degraded ? "test degraded" : null, AddressSpaceRebuilt: Rebuilt));
|
|
return AutoPilot.KeepRunning;
|
|
}));
|
|
_host = kit.Sys.ActorOf(DriverHostActor.Props(
|
|
_db, TestNode, _coordinator.Ref,
|
|
driverFactory: Factory,
|
|
localRoles: new HashSet<string> { "driver" },
|
|
dependencyMux: kit.CreateTestProbe().Ref,
|
|
opcUaPublishActor: publish.Ref,
|
|
virtualTagEvaluator: NullVirtualTagEvaluator.Instance,
|
|
virtualTagHostOverride: kit.CreateTestProbe().Ref));
|
|
}
|
|
|
|
public RecordingSubscribableDriver Driver(string id)
|
|
{
|
|
RecordingSubscribableDriver? found = null;
|
|
_kit.AwaitAssert(() => (found = Factory.Created.LastOrDefault(d => d.DriverInstanceId == id)).ShouldNotBeNull(),
|
|
PresenceBudget, cancellationToken: Ct);
|
|
return found!;
|
|
}
|
|
|
|
public void Deploy((string, string, string, string, string, bool)[] tags, (string, string, string, string, string?)[] refs,
|
|
RevisionHash rev, (string, string, string, string)[]? virtualTags = null, bool expectDegraded = false)
|
|
{
|
|
var id = V3ArtifactSeeder.SeedV3Deployment(_db, rev, tags, refs, enabled: true, virtualTags: virtualTags);
|
|
_host.Tell(new ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy.DispatchDeployment(id, rev, CorrelationId.NewId()));
|
|
var ack = _coordinator.FishForMessage<ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy.ApplyAck>(
|
|
m => m.DeploymentId == id, PresenceBudget, cancellationToken: Ct);
|
|
ack.Outcome.ShouldBe(ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy.ApplyAckOutcome.Applied);
|
|
ack.Degraded.ShouldBe(expectDegraded);
|
|
}
|
|
}
|
|
}
|