diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy/AbLegacyDriver.Coalescing.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy/AbLegacyDriver.Coalescing.cs index 9af09443c..a2bbe33ed 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy/AbLegacyDriver.Coalescing.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy/AbLegacyDriver.Coalescing.cs @@ -170,7 +170,7 @@ public sealed partial class AbLegacyDriver } /// The planned, device-bucketed form of one read's reference list (#1073). - private sealed record ReadPlan((int Index, uint Status)[] Unresolved, DevicePlan[] Devices); + private sealed record ReadPlan((int Index, uint Status)[] Unresolved, DevicePlan[] Devices, bool HasTransient = false); /// One device's units, planned with no block refusal applied (see ). private sealed record DevicePlan(DeviceState Device, List Units); @@ -221,9 +221,10 @@ public sealed partial class AbLegacyDriver if (cache.Plans.TryGetValue(key, out var hit)) return hit; var plan = BuildReadPlan(devices, key); Interlocked.Increment(ref _readPlanBuilds); - // Only a plan with no Unresolved entries is cached: an unresolved reference may resolve later (a transient - // equipment-tag parse), and caching would pin the Bad answer. - if (plan.Unresolved.Length == 0) + // A plan is cached unless it holds a TRANSIENT unresolved reference (resolver miss / unknown device: it may + // resolve later, and caching would pin the Bad answer). Deterministic refusals - a malformed address or a + // config-rejected tag - are cached (#1087); both only change on re-initialise, which drops the cache. + if (!plan.HasTransient) { if (cache.Plans.Count >= MaxCachedReadPlans) cache.Plans.Clear(); cache.Plans[key] = plan; @@ -244,6 +245,7 @@ public sealed partial class AbLegacyDriver private ReadPlan BuildReadPlan(Dictionary devices, string[] references) { var unresolved = new List<(int, uint)>(); + var transient = false; var buckets = new Dictionary>(ReferenceEqualityComparer.Instance); for (var i = 0; i < references.Length; i++) { @@ -251,6 +253,7 @@ public sealed partial class AbLegacyDriver if (!_resolver.TryResolve(reference, out var def) || !devices.TryGetValue(def.DeviceHostAddress, out var device)) { unresolved.Add((i, AbLegacyStatusMapper.BadNodeIdUnknown)); + transient = true; continue; } // A tag whose data type its device's PLC family does not ship is a per-tag configuration error: answered @@ -271,7 +274,8 @@ public sealed partial class AbLegacyDriver list.Add(new ReadItem(i, reference, def, parsed)); } return new ReadPlan([.. unresolved], - [.. buckets.Select(kv => new DevicePlan(kv.Key, PlanReadUnits(kv.Key, kv.Value, applyRefusals: false)))]); + [.. buckets.Select(kv => new DevicePlan(kv.Key, PlanReadUnits(kv.Key, kv.Value, applyRefusals: false)))], + transient); } /// What a block read concluded; = read the members singly instead. diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/AbLegacyReadPlanCacheTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/AbLegacyReadPlanCacheTests.cs index 8399906ed..1c1ad6d40 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/AbLegacyReadPlanCacheTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Tests/AbLegacyReadPlanCacheTests.cs @@ -74,6 +74,36 @@ public sealed class AbLegacyReadPlanCacheTests drv.CachedReadPlanCount.ShouldBe(0); } + [Fact] + public async Task A_malformed_address_plan_is_cached_but_a_transient_unresolved_reference_is_not() + { + var factory = new FakeAbLegacyTagFactory { Customise = p => new FakeAbLegacyTag(p) { Value = 1 } }; + var drv = new AbLegacyDriver(new AbLegacyDriverOptions + { + Devices = [new AbLegacyDeviceOptions(Host)], + RawTags = AbLegacyRawTags.Entries([ + new AbLegacyTagDefinition("Good", Host, "N7:0", AbLegacyDataType.Int), + new AbLegacyTagDefinition("Bad", Host, "not-an-address", AbLegacyDataType.Int)]), + Probe = new AbLegacyProbeOptions { Enabled = false }, + }, "drv-plan", factory); + await drv.InitializeAsync("{}", CancellationToken.None); + + // Deterministic: malformed address -> cached, built once. + string[] malformed = ["Good", "Bad"]; + await drv.ReadAsync(malformed, CancellationToken.None); + await drv.ReadAsync(malformed, CancellationToken.None); + drv.ReadPlanBuilds.ShouldBe(1); + drv.CachedReadPlanCount.ShouldBe(1); + + // Positive control: an unknown reference (transient resolver miss) is never pinned. + string[] unknown = ["Good", "NoSuchTag"]; + await drv.ReadAsync(unknown, CancellationToken.None); + await drv.ReadAsync(unknown, CancellationToken.None); + drv.ReadPlanBuilds.ShouldBe(3); + drv.CachedReadPlanCount.ShouldBe(1); + await drv.ShutdownAsync(CancellationToken.None); + } + [Fact] public async Task The_cache_is_bounded() {