Merge driver debt follow-ups: AbLegacy caches plans with deterministic malformed references, never transient ones (#1087); no other private driver JSON options found (#1088); FailoverApiKey in the secrets table (#1086)

This commit is contained in:
Joseph Doherty
2026-12-04 14:30:47 -05:00
4 changed files with 41 additions and 6 deletions
+1 -1
View File
@@ -381,4 +381,4 @@ and the UI authoring flow.
## Read-plan cache (#1073)
`ReadAsync` memoises its plan (resolution, per-device bucketing, coalesced-block planning) per EXACT ordered reference list, up to 64 lists per device-table generation (a full cache is cleared). The cache is dropped on every initialise and teardown, so a config change re-plans. The plan is cached as if no block were refused; the device's current refused-block memory is applied at use time. Reads containing an unresolved reference are not cached.
`ReadAsync` memoises its plan (resolution, per-device bucketing, coalesced-block planning) per EXACT ordered reference list, up to 64 lists per device-table generation (a full cache is cleared). The cache is dropped on every initialise and teardown, so a config change re-plans. The plan is cached as if no block were refused; the device's current refused-block memory is applied at use time. A plan holding a TRANSIENT unresolved reference (resolver miss / unknown device) is never cached, so a late resolution is not pinned Bad; deterministic refusals (a malformed address, a config-rejected tag) are cached (#1087) because they only change on re-initialise, which drops the cache.
+1
View File
@@ -2121,6 +2121,7 @@ The five config secrets and their canonical secret names:
| `Security:Jwt:SigningKey` | `JwtOptions` | `otopcua/jwt/signing-key` |
| `Security:Ldap:ServiceAccountPassword` | `LdapOptions` | `otopcua/ldap/service-account-password` |
| `Security:DeployApiKey` | `DeployApiEndpoints` | `otopcua/deploy/api-key` |
| `Security:FailoverApiKey` | `ManualFailoverApiEndpoints` (#1078) | `otopcua/failover/api-key` |
| `ConnectionStrings:ConfigDb` | `AddOtOpcUaConfigDb` | `otopcua/sql/configdb-connstr` |
| `ServerHistorian:ApiKey` | `ServerHistorianOptions` | `otopcua/historian/api-key` |
@@ -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()
{