perf(opcua-acl): #1033 flag-off revalidation answers local items before the pass memo

With Security:Acl:Enabled=false a revalidation pass over local Raw/UNS items is an unconditional allow; it still paid a
(session, ns, id) memo entry and the full Permits walk per item. Measured over 10k items: ~6.5-7 ms and ~1.4 MB per pass
-> ~0.5-1 ms and ~0.2 KB. IsLocalNodeUngated mirrors exactly what Permits concludes there and excludes the one wiring
where it differs (mapped resolver without the projected predicate). The same shortcut inside the per-request Permits was
measured and NOT taken: a 1,000-node flag-off Read stayed ~1.5-1.9 ms either way (SDK read dominates).

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 15:30:13 -05:00
parent 6ea0f09614
commit 7cda8dbb81
3 changed files with 60 additions and 1 deletions
+3 -1
View File
@@ -848,7 +848,9 @@ Behaviours worth knowing before enabling it:
the creation gate's own seam (`Permits` → `AuthorizeIfRequired`), which bypasses local grants — so an
ungranted (e.g. Anonymous) subscription admitted with the flag off is **never** stood down by a tick or a
deploy; only gRPC-data projected / imported-condition items, gated in both modes, can lapse (#972 — between
`8ad03e9c5` and the fix, the pass enforced grants with the flag off). Without the timer a key that expired by the
`8ad03e9c5` and the fix, the pass enforced grants with the flag off). With the flag off a local Raw/UNS item is
answered allow before the pass's memo (#1033: a 10k-item pass ~6.5–7 ms / ~1.4 MB ⇒ ~0.5–1 ms / ~0.2 KB; the same
shortcut in the per-request `Permits` was measured and not taken — a 1,000-node Read did not change). Without the timer a key that expired by the
clock kept streaming until a deployment happened to land — **indefinitely** on a fleet that has been
deployed once and left running, which is exactly the shape a production fleet settles into.
@@ -6286,6 +6286,29 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
(_authorizer is not null || _mappedSubjectResolver is null);
}
/// <summary>
/// #1033: whether the revalidation pass's <c>CreateMonitoredItems</c> verdict on <paramref name="nodeId"/> is an
/// unconditional allow — the node is in this manager's own Raw or UNS namespace and <c>Security:Acl:Enabled</c> is
/// off. That is exactly what <see cref="Permits"/> concludes there for a non-Browse operation, minus its lookups:
/// the imported-condition classifier answers only namespace [4], the projected predicate
/// (<see cref="IsProjectedDescendant"/>) only [2]/[3], and <c>AuthorizeIfRequired</c> then takes the
/// <c>localBypass</c>. The one wiring where the full path differs — a mapped-subject resolver with no projected
/// predicate, which asks the resolver about every node and denies the local ones it cannot place — is excluded, so
/// that configuration keeps its existing verdict.
/// </summary>
/// <remarks>
/// Measured (10k flag-off items): a pass went from ~6.5–7 ms and ~1.4 MB (the pass-local memo's tuple keys and
/// dictionary growth, plus the per-item authorizer walk) to ~0.5–1 ms and ~0.2 KB. The same shortcut in
/// <see cref="Permits"/> itself was measured too and NOT taken: a 1,000-node flag-off Read stayed ~1.5–1.9 ms either
/// way (the SDK read dominates).
/// </remarks>
/// <param name="nodeId">The target node.</param>
/// <returns><see langword="true"/> when the decision is an allow without consulting the authorizer.</returns>
private bool IsLocalNodeUngated(NodeId nodeId) =>
_authorizer is { Enabled: false } authorizer
&& (nodeId.NamespaceIndex == NamespaceIndexes[0] || nodeId.NamespaceIndex == NamespaceIndexes[1])
&& (authorizer.MappedSubjectResolver is null || authorizer.MappedNamespacePredicate is not null);
/// <summary>Guard against a corrupt (cyclic) parent chain in <see cref="ResolveUnsAclSubject"/>.</summary>
private const int MaxUnsAncestry = 64;
@@ -9290,6 +9313,9 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
// `Security:Acl:Enabled=false` bypass (AuthorizeIfRequired), the mapped/imported classifiers, and the
// no-authorizer case. The bypass-less `IsAllowedCached` used here before enforced grants with the flag off,
// standing every ungranted subscription (Anonymous included) down on the next tick or deploy.
// #1033: the flag-off answer for a local node needs no memo entry (the tuple key + dictionary growth were most of a
// 10k-item pass's ~1.4 MB / ~43 ms).
if (IsLocalNodeUngated(nodeId)) return true;
if (identity is null
|| (identity.ApiKeyId ?? identity.Principal) is not { Length: > 0 } sessionKey
|| !nodeId.TryGetValue(out string identifier) || identifier.Length == 0)
@@ -840,6 +840,37 @@ public sealed class NodeManagerStage6EnforcementTests : IDisposable
finally { await host.DisposeAsync(); }
}
/// <summary>
/// #1033: with the flag off a revalidation pass over local-namespace items is an unconditional allow, so it must not
/// pay the pass-local memo (a tuple key + dictionary slot per item) or the per-item authorizer walk. Measured over
/// 10k items: ~6.5–7 ms and ~1.4 MB per pass before, ~0.5–1 ms and ~0.2 KB after. Budgeted here on allocation (a
/// 2,000-item pass allocated ~290 KB before). <see cref="Revalidation_with_enforcement_on_stands_down_an_ungranted_item"/>
/// is the control that the pass still decides when the flag is on.
/// </summary>
[Fact]
public async Task A_flag_off_revalidation_pass_over_local_items_allocates_almost_nothing()
{
var (host, server) = await BootAsync();
try
{
var nm = server.NodeManager!;
nm.Authorizer = Authorizer(enabled: false, GrantOn(NodePermissions.None));
const int n = 2_000;
var items = new List<StubSampledItem>(n);
for (var i = 0; i < n; i++)
items.Add(TrackItem(nm, Variable(nm, $"Plant/Modbus/dev1/T{i}"), i % 2 == 0 ? Identity() : AnonymousIdentity()));
nm.RevalidateMonitoredItems(); // warm-up
var before = GC.GetAllocatedBytesForCurrentThread();
for (var pass = 0; pass < 5; pass++) nm.RevalidateMonitoredItems();
var perPass = (GC.GetAllocatedBytesForCurrentThread() - before) / 5;
items.ShouldAllBe(i => i.Denials == 0);
perPass.ShouldBeLessThan(8 * 1024, $"a flag-off pass over {n} local items allocated {perPass} bytes");
}
finally { await host.DisposeAsync(); }
}
// ───────────────────────────── helpers ─────────────────────────────
/// <summary>An Anonymous session's identity, as the activation handler builds it (reserved principal, no key).</summary>