perf(misc): cached hot-path lookups, bounded observer queue, alarm-priority stream path

WP2.6 (arch-review remediation, cross-cutting misc):
- SiteExternalSystemRepository: name/ID-indexed ExternalSystemDefinitionCache replaces
  the fetch-all + reverse-map scan on every by-ID/method lookup; loaded once per
  redeploy, invalidated by DeploymentManagerActor after HandleDeployArtifacts applies
  external-system changes. Static JsonSerializerOptions for method-list parsing.
- Inbound API: short-TTL ApiMethodCache fronts the per-request ApiMethod repository
  fetch; invalidated by name via the existing ScriptArtifactChangeSubscriber/
  IScriptArtifactChangeBus pipeline, self-healing via TTL for changes the bus
  doesn't cover (e.g. Management API edits).
- StoreAndForward: the cached-call audit-observer queue — the one unbounded channel
  left in the system — is now bounded (ObserverQueueCapacity, default 10,000) with
  DropOldest overflow and a dropped-notification counter.
- SiteStreamManager: alarm state changes now travel a dedicated publish
  source/broadcast hub, isolated from the (far higher-volume) attribute path, so an
  attribute storm can no longer evict a pending alarm transition; the alarm hand-off
  queue is bounded with a drop counter surfaced on the site health report
  (SiteStreamAlarmDropCount via the new SiteStreamAlarmDropReporter), and publishing
  is skipped entirely at zero subscribers on either path.
- CLI ManagementHttpClient: explicit 30s HttpClient.Timeout on the shared
  construction (was the 100s framework default), overridable via
  SCADABRIDGE_HTTP_TIMEOUT_SECONDS.

Deviation: the failback-probe heartbeat item is NOT included — its only viable
surface (CentralChannelProvider.cs / heartbeat consumers) lives entirely in the
Communication project, explicitly off-limits to this work package this phase.

Tests: SiteRuntime.Tests (550), InboundAPI.Tests (278), StoreAndForward.Tests (133),
CLI.Tests (390), HealthMonitoring.Tests (97) — all green after full solution build.
This commit is contained in:
Joseph Doherty
2026-08-14 20:59:43 -04:00
parent ee193cd2bb
commit a212283104
32 changed files with 1361 additions and 111 deletions
@@ -0,0 +1,85 @@
using System.Collections.Concurrent;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
namespace ZB.MOM.WW.ScadaBridge.InboundAPI;
/// <summary>
/// WP2.6b (arch-review misc — Inbound API per-request SQL): a short-TTL cache of
/// resolved <c>ApiMethod</c> rows, sitting in front of <c>IInboundApiRepository.GetMethodByNameAsync</c>
/// so a repeated <c>POST /api/{methodName}</c> burst for the same method does not each pay a
/// database round-trip.
/// <para>
/// Two invalidation paths, by design:
/// </para>
/// <list type="bullet">
/// <item><description><b>Fast path.</b> <see cref="ScriptArtifactChangeSubscriber"/> calls
/// <see cref="Invalidate"/> for every name in an <c>ApiMethod</c>
/// <c>ScriptArtifactsChanged</c> notification (bundle-import changes today).</description></item>
/// <item><description><b>Fallback.</b> Every entry — including a negative (not-found) result —
/// expires after <see cref="InboundApiOptions.ApiMethodCacheTtl"/> regardless of whether an
/// invalidation notification ever arrives. This is what keeps an operator edit via the
/// Management API/UI (which does not publish to the change bus) correct within a bounded
/// window, and is also the correctness backstop for the bus's at-least-once/in-process-only
/// contract — a missed or duplicate notification is harmless.</description></item>
/// </list>
/// <para>
/// A negative (method not found) result is cached too — bounded by the same short TTL — so a
/// caller with a provisioned-but-nonexistent method scope (a real misconfiguration case, not an
/// enumeration probe: <c>EndpointExtensions</c> only reaches this cache after the in-memory
/// scope check already passed) does not repeatedly pay a miss query.
/// </para>
/// </summary>
public sealed class ApiMethodCache
{
private sealed record Entry(ApiMethod? Method, DateTimeOffset ExpiresAtUtc);
private readonly ConcurrentDictionary<string, Entry> _entries = new(StringComparer.Ordinal);
private readonly TimeSpan _ttl;
private readonly TimeProvider _timeProvider;
/// <summary>Initializes the cache.</summary>
/// <param name="ttl">How long a resolved (or not-found) entry stays valid.</param>
/// <param name="timeProvider">Optional time source; defaults to <see cref="TimeProvider.System"/>.</param>
public ApiMethodCache(TimeSpan ttl, TimeProvider? timeProvider = null)
{
if (ttl <= TimeSpan.Zero)
throw new ArgumentOutOfRangeException(nameof(ttl), ttl, "ApiMethodCache TTL must be positive.");
_ttl = ttl;
_timeProvider = timeProvider ?? TimeProvider.System;
}
/// <summary>
/// Returns the cached entry for <paramref name="methodName"/> if it has not expired;
/// otherwise invokes <paramref name="fetch"/>, caches the result (found or not-found), and
/// returns it. Concurrent callers on a cache miss for the SAME name may each invoke
/// <paramref name="fetch"/> once (last write wins) — an acceptable, rare race in exchange
/// for not serializing every miss behind a lock; the point of the cache is to absorb
/// repeated hits, not to fully dedupe a first-request thundering herd.
/// </summary>
/// <param name="methodName">The method name to resolve.</param>
/// <param name="fetch">Loads the current row from the repository on a cache miss.</param>
/// <param name="cancellationToken">Cancellation token for the fetch.</param>
public async Task<ApiMethod?> GetOrFetchAsync(
string methodName,
Func<CancellationToken, Task<ApiMethod?>> fetch,
CancellationToken cancellationToken)
{
var now = _timeProvider.GetUtcNow();
if (_entries.TryGetValue(methodName, out var cached) && cached.ExpiresAtUtc > now)
{
return cached.Method;
}
var method = await fetch(cancellationToken).ConfigureAwait(false);
_entries[methodName] = new Entry(method, now + _ttl);
return method;
}
/// <summary>Drops the cached entry for one method name. Safe to call for an unknown name.</summary>
/// <param name="methodName">The method name to invalidate.</param>
public void Invalidate(string methodName) => _entries.TryRemove(methodName, out _);
/// <summary>Diagnostic helper — the number of currently-cached entries (including expired-but-not-evicted ones).</summary>
internal int Count => _entries.Count;
}