feat: expose mapped raw and uns data through grpc adapters

This commit is contained in:
Joseph Doherty
2026-11-16 20:43:15 -05:00
parent 47960a6d57
commit 8ad03e9c5d
118 changed files with 12645 additions and 227 deletions
@@ -25,7 +25,7 @@ namespace ZB.MOM.WW.OtOpcUa.Runtime.ScriptedAlarms;
/// snapshot — a concurrent unsubscribe can never corrupt an in-flight fan-out.
/// </para>
/// </remarks>
public sealed class DependencyMuxTagUpstreamSource : ITagUpstreamSource
public sealed class DependencyMuxTagUpstreamSource : ITagUpstreamSource, IAlarmInputEvidenceSource
{
// The status an "unknown path" snapshot carries — a path nothing has ever pushed to.
//
@@ -46,6 +46,51 @@ public sealed class DependencyMuxTagUpstreamSource : ITagUpstreamSource
// ONLY through RetainOnly — the host calls it with the loaded dependency-ref union after every
// (re)load, so a renamed/removed raw path (a RawTree rename is a surgical remove+add) stops
// costing a boxed value + two DateTimes for the process lifetime (R02:L3).
private readonly System.Runtime.CompilerServices.ConditionalWeakTable<DataValueSnapshot, IAlarmInputEvidence> _evidence = new();
private readonly ZB.MOM.WW.OtOpcUa.Runtime.DataConnections.ProjectedByteQuota _physicalQuota;
private sealed class ProjectedEvidence(ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.IProjectedSamplePublicationProof proof,
DataValueSnapshot snapshot, ZB.MOM.WW.OtOpcUa.Runtime.DataConnections.ProjectedByteQuota quota) : IAlarmInputPhysicalEvidence
{
private readonly WeakReference<DataValueSnapshot> _identity = new(snapshot);
public bool IsValid { get { try { return proof.IsValid; } catch (Exception) { return false; } } }
public bool TryRetainPhysical(DataValueSnapshot exactSnapshot, out IDisposable? lease)
{
lease = null;
if (!_identity.TryGetTarget(out var original) || !ReferenceEquals(original, exactSnapshot) || !IsValid) return false;
long charge;
try { charge = checked(512L + ZB.MOM.WW.OtOpcUa.Runtime.DataConnections.OwnedDataSample.Estimate(exactSnapshot.Value)); }
catch (Exception ex) when (ex is ArgumentException or OverflowException) { return false; }
if (!quota.TryAdjust(charge)) return false;
if (!ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ProjectedInputBudget.TryRetain([exactSnapshot], out var processLease) || processLease is null)
{ quota.TryAdjust(-charge); return false; }
if (!IsValid) { processLease.Dispose(); quota.TryAdjust(-charge); return false; }
lease = new PhysicalLease(quota, charge, exactSnapshot, processLease); return true;
}
private sealed class PhysicalLease(ZB.MOM.WW.OtOpcUa.Runtime.DataConnections.ProjectedByteQuota quota, long charge,
DataValueSnapshot snapshot, IDisposable processLease) : IDisposable
{
private DataValueSnapshot? _snapshot = snapshot;
public void Dispose()
{
if (Interlocked.Exchange(ref _snapshot, null) is null) return;
processLease.Dispose();
quota.TryAdjust(-charge);
}
}
}
public IAlarmInputEvidence? CaptureEvidence(DataValueSnapshot exactSnapshot)
{
ArgumentNullException.ThrowIfNull(exactSnapshot);
return _evidence.TryGetValue(exactSnapshot, out var evidence) ? evidence : null;
}
/// <summary>Copies a projected input and binds evidence to that exact immutable snapshot.</summary>
public void Push(string path, DataValueSnapshot snapshot, ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.IProjectedSamplePublicationProof proof)
{
ArgumentNullException.ThrowIfNull(snapshot); ArgumentNullException.ThrowIfNull(proof);
var owned = snapshot with { };
_evidence.Add(owned, new ProjectedEvidence(proof, owned, _physicalQuota));
Push(path, owned);
}
private readonly ConcurrentDictionary<string, DataValueSnapshot> _cache
= new(StringComparer.Ordinal);
private readonly ConcurrentDictionary<string, ImmutableList<Action<string, DataValueSnapshot>>> _observers
@@ -60,8 +105,12 @@ public sealed class DependencyMuxTagUpstreamSource : ITagUpstreamSource
/// snapshot returned for an unknown path. Defaults to <see cref="DateTime.UtcNow"/>,
/// mirroring how <see cref="ScriptedAlarmEngine"/> takes its clock.
/// </param>
public DependencyMuxTagUpstreamSource(Func<DateTime>? clock = null)
=> _clock = clock ?? (() => DateTime.UtcNow);
public DependencyMuxTagUpstreamSource(Func<DateTime>? clock = null, long maximumProjectedBytes = 16 * 1024 * 1024)
{
if (maximumProjectedBytes <= 0) throw new ArgumentOutOfRangeException(nameof(maximumProjectedBytes));
_clock = clock ?? (() => DateTime.UtcNow);
_physicalQuota = new(maximumProjectedBytes);
}
/// <summary>
/// Update the cached snapshot for <paramref name="path"/> and then notify every
@@ -314,6 +314,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
// burst. The engine upstream is fed per value, so push each item in order — the saving is the
// mailbox hop, not the push.
Receive<VirtualTagActor.DependencyValueBatch>(OnDependencyBatch);
Receive<DependencyMuxActor.ProjectedDependencyWake>(wake => wake.Consume(items => OnDependencyBatch(new(items))));
Receive<EngineEmission>(OnEngineEmission);
// Cluster redundancy snapshots (published on the `redundancy-state` topic, subscribed in PreStart)
// cache this node's role so OnEngineEmission can gate the cluster-wide alerts publish to the Primary.
@@ -512,8 +513,9 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
// the status code rides through verbatim. This used to convert the incoming value onto a
// 3-state enum and straight back again through a pair of mutually-inverse mappers, which is where
// the input's sub-code was lost before the engine ever cached it.
_upstream.Push(msg.TagId,
new DataValueSnapshot(msg.Value, msg.StatusCode, msg.TimestampUtc, msg.TimestampUtc));
var snapshot = new DataValueSnapshot(msg.Value, msg.StatusCode, msg.TimestampUtc, msg.TimestampUtc);
if (msg.PublicationProof is { } proof) _upstream.Push(msg.TagId, snapshot, proof);
else _upstream.Push(msg.TagId, snapshot);
}
/// <summary>One batched mux delivery — every item already matched this host's registered interest.
@@ -544,6 +546,11 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
private void OnEngineEmission(EngineEmission msg)
{
var e = msg.Event;
if (e.InputEvidence is { } evidence)
{
try { if (!evidence.IsValid) return; }
catch (Exception) { return; }
}
// None = no meaningful change; Suppressed = shelving ate the emission. Neither should reach a
// sink. (The engine already filters these out of BuildEmission, but guard defensively.)