fix(ablegacy): #1087 cache read plans whose only unresolved refs are deterministic (malformed address / config-rejected); transient misses still uncached
This commit is contained in:
@@ -170,7 +170,7 @@ public sealed partial class AbLegacyDriver
|
||||
}
|
||||
|
||||
/// <summary>The planned, device-bucketed form of one read's reference list (#1073).</summary>
|
||||
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);
|
||||
|
||||
/// <summary>One device's units, planned with no block refusal applied (see <see cref="ApplyRefusedBlocks"/>).</summary>
|
||||
private sealed record DevicePlan(DeviceState Device, List<ReadUnit> 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<string, DeviceState> devices, string[] references)
|
||||
{
|
||||
var unresolved = new List<(int, uint)>();
|
||||
var transient = false;
|
||||
var buckets = new Dictionary<DeviceState, List<ReadItem>>(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);
|
||||
}
|
||||
|
||||
/// <summary>What a block read concluded; <see cref="Fallback"/> = read the members singly instead.</summary>
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user