feat(grpc-data): A1 origin condition state in the node manager — delta callback, state_known, activation marker, sliced snapshot, MarkConditionsKnown sink seam, IAlarmInitialStateSignal
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
|
||||
/// <summary>
|
||||
/// The origin-owned activation marker of one condition (plan §5.6 step 3, T3): <c>(serving incarnation,
|
||||
/// per-condition sequence)</c>. The sequence moves only on a NEW occurrence — an Active false→true edge or any
|
||||
/// Acked true→false edge — never on a re-projection that leaves Active/Acked unchanged.
|
||||
/// </summary>
|
||||
/// <param name="Incarnation">The node manager's serving incarnation (one per process lifetime).</param>
|
||||
/// <param name="Sequence">The per-condition occurrence sequence.</param>
|
||||
public readonly record struct ConditionActivationMarker(string Incarnation, ulong Sequence);
|
||||
|
||||
/// <summary>
|
||||
/// One immutable, SDK-free copy of a live condition's state (plan §5.1), produced by the node manager under its
|
||||
/// <c>Lock</c> — either as a delta (raised for every condition state change) or inside a
|
||||
/// <see cref="ConditionSnapshotSlice"/>. Invisible until A2 puts it on the wire.
|
||||
/// </summary>
|
||||
/// <param name="Realm">The condition's realm (Raw = native RawPath, Uns = ScriptedAlarmId).</param>
|
||||
/// <param name="ConditionId">The condition node id within <paramref name="Realm"/>.</param>
|
||||
/// <param name="IsNative">Driver-fed (native) rather than scripted.</param>
|
||||
/// <param name="Version">The hub version: the delta's own version, or the slice watermark for a snapshot copy.</param>
|
||||
/// <param name="Removed">A removal record: the condition left the address space.</param>
|
||||
/// <param name="StateKnown">
|
||||
/// The condition's state has actually been established (§5.1 T2). A record with <c>false</c> never changes a
|
||||
/// consumer's last state; it only marks the condition Uncertain.
|
||||
/// </param>
|
||||
/// <param name="Active">Part 9 ActiveState.</param>
|
||||
/// <param name="Acknowledged">Part 9 AckedState.</param>
|
||||
/// <param name="Confirmed">Part 9 ConfirmedState.</param>
|
||||
/// <param name="Enabled">Part 9 EnabledState.</param>
|
||||
/// <param name="Shelving">The shelving kind.</param>
|
||||
/// <param name="Severity">The node's Part 9 severity (1..1000).</param>
|
||||
/// <param name="Message">The node's message text.</param>
|
||||
/// <param name="Quality">The condition's source-data Quality status code.</param>
|
||||
/// <param name="ConditionClassName">The Part 9 ConditionClassName text, if set.</param>
|
||||
/// <param name="TimeUtc">The condition's <c>Time</c>.</param>
|
||||
/// <param name="Retain">Part 9 Retain.</param>
|
||||
/// <param name="Activation">The origin-owned activation marker (T3).</param>
|
||||
/// <param name="Comment">The condition's Comment text, if any.</param>
|
||||
/// <param name="ClientUserId">Who left that comment, if recorded.</param>
|
||||
public sealed record ConditionStateRecord(
|
||||
AddressSpaceRealm Realm,
|
||||
string ConditionId,
|
||||
bool IsNative,
|
||||
long Version,
|
||||
bool Removed,
|
||||
bool StateKnown,
|
||||
bool Active,
|
||||
bool Acknowledged,
|
||||
bool Confirmed,
|
||||
bool Enabled,
|
||||
AlarmShelvingKind Shelving,
|
||||
ushort Severity,
|
||||
string Message,
|
||||
uint Quality,
|
||||
string? ConditionClassName,
|
||||
DateTime TimeUtc,
|
||||
bool Retain,
|
||||
ConditionActivationMarker Activation,
|
||||
string? Comment = null,
|
||||
string? ClientUserId = null);
|
||||
|
||||
/// <summary>
|
||||
/// The node manager's SDK-free delta callback (plan §5.1). Implemented by the Runtime's <c>ConditionStateHub</c>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Both members are called <b>under the node manager's <c>Lock</c></b>, which is what makes a snapshot cut atomic:
|
||||
/// every delta is either inside a slice or after its watermark. Implementations must therefore only enqueue —
|
||||
/// no I/O, never block, never call back into the node manager.
|
||||
/// </remarks>
|
||||
public interface IConditionStateSink
|
||||
{
|
||||
/// <summary>The version of the last appended record (the snapshot watermark). Read under the node manager's Lock.</summary>
|
||||
long Watermark { get; }
|
||||
|
||||
/// <summary>Append one delta record. The sink assigns its version; the record's own <c>Version</c> is ignored.</summary>
|
||||
/// <param name="record">The record.</param>
|
||||
/// <returns>The version assigned.</returns>
|
||||
long Append(ConditionStateRecord record);
|
||||
|
||||
/// <summary>
|
||||
/// The node manager's address-space apply state changed: <see langword="false"/> when a rebuild began (the
|
||||
/// condition set is being re-created), <see langword="true"/> when <c>CompleteAddressSpaceApply</c> ran. Part of
|
||||
/// the <c>snapshot_complete</c> readiness (§5.1: apply complete + Primary verdict).
|
||||
/// </summary>
|
||||
/// <param name="complete">Whether the current apply has completed.</param>
|
||||
void OnApplyStateChanged(bool complete);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One <c>Lock</c> hold of <see cref="OtOpcUaNodeManager.SnapshotConditions"/>: at most <c>MaxConditions</c>
|
||||
/// records plus the watermark at that instant. A delta for one of these conditions with a version above
|
||||
/// <see cref="Watermark"/> applies after the slice; one at or below it is already inside.
|
||||
/// </summary>
|
||||
/// <param name="Watermark">The sink's watermark taken under the same hold.</param>
|
||||
/// <param name="Records">The copied records (each carries <see cref="Watermark"/> as its version).</param>
|
||||
public sealed record ConditionSnapshotSlice(long Watermark, IReadOnlyList<ConditionStateRecord> Records);
|
||||
@@ -0,0 +1,310 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Opc.Ua;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
|
||||
/// <summary>
|
||||
/// A1 origin condition state (plan §5.1): the SDK-free delta callback, <c>state_known</c>, the activation marker
|
||||
/// (T3) and the sliced snapshot. Every member that touches these maps runs under <c>Lock</c>; nothing here does
|
||||
/// I/O or blocks — the sink only enqueues.
|
||||
/// </summary>
|
||||
public sealed partial class OtOpcUaNodeManager
|
||||
{
|
||||
/// <summary>The default most conditions one snapshot <c>Lock</c> hold copies (§5.5 <c>MaxConditions</c>).</summary>
|
||||
public const int DefaultMaxConditionsPerHold = 1000;
|
||||
|
||||
/// <summary>Per-condition activation bookkeeping: the occurrence sequence plus the Active/Acked basis it was
|
||||
/// last compared against. Mutable, only touched under <c>Lock</c>.</summary>
|
||||
private sealed class ActivationEntry
|
||||
{
|
||||
public ulong Sequence;
|
||||
public bool Active;
|
||||
public bool Acked = true;
|
||||
public ActivationEntry Clone() => new() { Sequence = Sequence, Active = Active, Acked = Acked };
|
||||
}
|
||||
|
||||
private IConditionStateSink? _conditionStateSink;
|
||||
private readonly HashSet<RealmKey> _conditionStateKnown = new();
|
||||
private readonly Dictionary<RealmKey, ActivationEntry> _activation = new();
|
||||
// Rebuild memory for SCRIPTED conditions (native ones ride NativeConditionCarryOver): their marker basis survives
|
||||
// the clear-and-re-materialise so the engine's re-assert of an unchanged active alarm is not a new occurrence.
|
||||
private readonly Dictionary<RealmKey, ActivationEntry> _rebuildScriptedActivation = new();
|
||||
// Conditions a rebuild discarded and no materialise has claimed yet; at CompleteAddressSpaceApply the remainder
|
||||
// gets a removal record. A condition that comes back is never announced as removed (T2: a rebuild must not make
|
||||
// a consumer retire a condition that is still active at the origin).
|
||||
private readonly HashSet<RealmKey> _rebuildPendingRemoval = new();
|
||||
private bool _conditionApplyComplete;
|
||||
|
||||
/// <summary>The serving incarnation the activation markers are minted under (one per node-manager lifetime).</summary>
|
||||
internal string ConditionIncarnation { get; set; } = Guid.NewGuid().ToString("N");
|
||||
|
||||
/// <summary>Test seam: invoked after each snapshot <c>Lock</c> hold (outside it) with the records that hold copied.</summary>
|
||||
internal Action<int>? OnConditionSnapshotHold { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The SDK-free condition-state delta callback (the Runtime's <c>ConditionStateHub</c>). Null = no hub; the
|
||||
/// known flags and activation markers are still maintained. Set once at host start.
|
||||
/// </summary>
|
||||
public IConditionStateSink? ConditionStateSink
|
||||
{
|
||||
get => _conditionStateSink;
|
||||
set
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
_conditionStateSink = value;
|
||||
value?.OnApplyStateChanged(_conditionApplyComplete);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Whether the last address-space apply has completed (<c>CompleteAddressSpaceApply</c> ran since the
|
||||
/// last rebuild began). Part of the <c>snapshot_complete</c> readiness.</summary>
|
||||
public bool IsConditionApplyComplete { get { lock (Lock) return _conditionApplyComplete; } }
|
||||
|
||||
/// <summary>Whether the condition's state is known (test + diagnostics read).</summary>
|
||||
/// <param name="conditionNodeId">The condition id.</param>
|
||||
/// <param name="realm">Its realm.</param>
|
||||
/// <returns><see langword="true"/> when its state has been established.</returns>
|
||||
public bool IsConditionStateKnown(string conditionNodeId, AddressSpaceRealm realm)
|
||||
{
|
||||
lock (Lock) return _conditionStateKnown.Contains(MapKey(realm, conditionNodeId));
|
||||
}
|
||||
|
||||
/// <summary>The condition's current activation marker, or null when no such condition is materialised.</summary>
|
||||
/// <param name="conditionNodeId">The condition id.</param>
|
||||
/// <param name="realm">Its realm.</param>
|
||||
/// <returns>The marker.</returns>
|
||||
public ConditionActivationMarker? GetActivationMarker(string conditionNodeId, AddressSpaceRealm realm)
|
||||
{
|
||||
lock (Lock)
|
||||
{
|
||||
var key = MapKey(realm, conditionNodeId);
|
||||
return _alarmConditions.ContainsKey(key) && _activation.TryGetValue(key, out var e)
|
||||
? new ConditionActivationMarker(ConditionIncarnation, e.Sequence) : null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Mark conditions whose state has been established (plan §5.1 T1/T2): sets the flag under <c>Lock</c> and raises
|
||||
/// one state-only record per NEWLY known condition. Repeats and unmaterialised ids are no-ops.
|
||||
/// </summary>
|
||||
/// <param name="source">The source (driver instance id, or the scripted host) — diagnostics only.</param>
|
||||
/// <param name="conditionIds">The condition ids.</param>
|
||||
/// <param name="realm">Their realm.</param>
|
||||
/// <returns>How many conditions became known.</returns>
|
||||
public int MarkConditionsKnown(string source, IReadOnlyList<string> conditionIds, AddressSpaceRealm realm)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(conditionIds);
|
||||
var marked = 0;
|
||||
lock (Lock)
|
||||
{
|
||||
foreach (var id in conditionIds)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id)) continue;
|
||||
var key = MapKey(realm, id);
|
||||
if (!_alarmConditions.TryGetValue(key, out var condition) || !_conditionStateKnown.Add(key)) continue;
|
||||
RaiseConditionRecord(key, condition);
|
||||
marked++;
|
||||
}
|
||||
}
|
||||
|
||||
if (marked > 0)
|
||||
_logger.LogDebug("OtOpcUaNodeManager: {Source} established the state of {Count} condition(s)", source, marked);
|
||||
return marked;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Withdraw "known" from conditions whose state can no longer be vouched for (a scripted alarm whose input
|
||||
/// reverted to never-pushed). Raises one state-only record per condition that was known.
|
||||
/// </summary>
|
||||
/// <param name="source">The source — diagnostics only.</param>
|
||||
/// <param name="conditionIds">The condition ids.</param>
|
||||
/// <param name="realm">Their realm.</param>
|
||||
/// <returns>How many conditions became unknown.</returns>
|
||||
public int MarkConditionsUnknown(string source, IReadOnlyList<string> conditionIds, AddressSpaceRealm realm)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(conditionIds);
|
||||
var cleared = 0;
|
||||
lock (Lock)
|
||||
{
|
||||
foreach (var id in conditionIds)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id)) continue;
|
||||
var key = MapKey(realm, id);
|
||||
if (!_alarmConditions.TryGetValue(key, out var condition) || !_conditionStateKnown.Remove(key)) continue;
|
||||
RaiseConditionRecord(key, condition);
|
||||
cleared++;
|
||||
}
|
||||
}
|
||||
|
||||
if (cleared > 0)
|
||||
_logger.LogDebug("OtOpcUaNodeManager: {Source} withdrew the state of {Count} condition(s)", source, cleared);
|
||||
return cleared;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copy every live condition in <paramref name="scope"/> (inactive, acknowledged and normal ones too — the
|
||||
/// snapshot defines the SET), never more than <paramref name="maxConditionsPerHold"/> per <c>Lock</c> hold.
|
||||
/// Each slice carries the sink's watermark taken under the same hold, so a delta is either inside the slice or
|
||||
/// after its watermark. A consumer must register for deltas before snapshotting.
|
||||
/// </summary>
|
||||
/// <param name="scope">Realm + id filter (an export's root); evaluated outside <c>Lock</c>.</param>
|
||||
/// <param name="maxConditionsPerHold">The most conditions one hold copies (≥ 1).</param>
|
||||
/// <returns>The slices in ascending (realm, id) order.</returns>
|
||||
public IReadOnlyList<ConditionSnapshotSlice> SnapshotConditions(
|
||||
Func<AddressSpaceRealm, string, bool> scope, int maxConditionsPerHold = DefaultMaxConditionsPerHold)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(scope);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(maxConditionsPerHold, 1);
|
||||
|
||||
// Key enumeration and the scope filter run OFF Lock (the map is concurrent and keys are immutable); a
|
||||
// condition added after this point is not in the snapshot but its materialise record is a delta.
|
||||
var keys = _alarmConditions.Keys.Where(k => scope(k.Realm, k.Id))
|
||||
.OrderBy(k => k.Realm).ThenBy(k => k.Id, StringComparer.Ordinal).ToList();
|
||||
var slices = new List<ConditionSnapshotSlice>((keys.Count + maxConditionsPerHold - 1) / maxConditionsPerHold);
|
||||
for (var start = 0; start < keys.Count; start += maxConditionsPerHold)
|
||||
{
|
||||
var end = Math.Min(start + maxConditionsPerHold, keys.Count);
|
||||
var records = new List<ConditionStateRecord>(end - start);
|
||||
long watermark;
|
||||
lock (Lock)
|
||||
{
|
||||
watermark = _conditionStateSink?.Watermark ?? 0;
|
||||
for (var i = start; i < end; i++)
|
||||
{
|
||||
if (_alarmConditions.TryGetValue(keys[i], out var condition))
|
||||
records.Add(BuildConditionRecord(keys[i], condition, watermark, removed: false));
|
||||
}
|
||||
}
|
||||
|
||||
OnConditionSnapshotHold?.Invoke(records.Count);
|
||||
slices.Add(new ConditionSnapshotSlice(watermark, records));
|
||||
}
|
||||
|
||||
return slices;
|
||||
}
|
||||
|
||||
/// <summary>Called under <c>Lock</c> when a condition is (re)materialised: clears known, seeds (or restores) its
|
||||
/// activation basis. The record is raised by <see cref="FinishConditionMaterialise"/>.</summary>
|
||||
private void BeginConditionMaterialise(RealmKey key, bool isNative)
|
||||
{
|
||||
_conditionStateKnown.Remove(key);
|
||||
_rebuildPendingRemoval.Remove(key);
|
||||
_activation[key] = !isNative && _rebuildScriptedActivation.Remove(key, out var kept) ? kept : new ActivationEntry();
|
||||
}
|
||||
|
||||
/// <summary>Under <c>Lock</c>, after registration and any carry-over restore: raise the condition's first record.</summary>
|
||||
/// <remarks>It does NOT advance the activation basis: a scripted condition re-created by a rebuild keeps the basis it
|
||||
/// had, so the engine's re-assert of the same still-active alarm onto the fresh (inactive) node is no new occurrence.</remarks>
|
||||
private void FinishConditionMaterialise(RealmKey key, AlarmConditionState alarm) =>
|
||||
_conditionStateSink?.Append(BuildConditionRecord(key, alarm, 0, removed: false));
|
||||
|
||||
/// <summary>Under <c>Lock</c>: a condition left the address space — drop its flags and raise a removal record.</summary>
|
||||
private void ForgetConditionState(RealmKey key, AlarmConditionState condition)
|
||||
{
|
||||
_conditionStateKnown.Remove(key);
|
||||
_activation.Remove(key, out var entry);
|
||||
_conditionStateSink?.Append(BuildConditionRecord(key, condition, 0, removed: true, entry));
|
||||
}
|
||||
|
||||
/// <summary>Under <c>Lock</c>, at the start of a full rebuild: remember what must survive the clear.</summary>
|
||||
private void CaptureConditionStateForRebuild()
|
||||
{
|
||||
_conditionApplyComplete = false;
|
||||
_conditionStateSink?.OnApplyStateChanged(false);
|
||||
_rebuildScriptedActivation.Clear();
|
||||
foreach (var key in _alarmConditions.Keys)
|
||||
{
|
||||
_rebuildPendingRemoval.Add(key);
|
||||
if (!_nativeAlarmNodeIds.Contains(key) && _activation.TryGetValue(key, out var e))
|
||||
_rebuildScriptedActivation[key] = e.Clone();
|
||||
}
|
||||
|
||||
_conditionStateKnown.Clear();
|
||||
_activation.Clear();
|
||||
}
|
||||
|
||||
/// <summary>Under <c>Lock</c>, at <c>CompleteAddressSpaceApply</c>: announce the conditions the rebuild dropped
|
||||
/// for good, forget the rebuild memory and mark the apply complete.</summary>
|
||||
private void CompleteConditionApply()
|
||||
{
|
||||
foreach (var key in _rebuildPendingRemoval)
|
||||
{
|
||||
if (_alarmConditions.ContainsKey(key)) continue;
|
||||
_conditionStateSink?.Append(new ConditionStateRecord(key.Realm, key.Id, false, 0, Removed: true, StateKnown: false,
|
||||
false, true, true, true, AlarmShelvingKind.Unshelved, 0, string.Empty, StatusCodes.Good.Code, null, UtcNow, false,
|
||||
new ConditionActivationMarker(ConditionIncarnation, 0)));
|
||||
}
|
||||
|
||||
_rebuildPendingRemoval.Clear();
|
||||
_rebuildScriptedActivation.Clear();
|
||||
_conditionApplyComplete = true;
|
||||
_conditionStateSink?.OnApplyStateChanged(true);
|
||||
}
|
||||
|
||||
/// <summary>Capture a native condition's marker basis + known flag for <see cref="NativeConditionCarryOver"/>.</summary>
|
||||
private (ulong Sequence, bool Active, bool Acked, bool Known) CaptureActivation(RealmKey key)
|
||||
{
|
||||
var known = _conditionStateKnown.Contains(key);
|
||||
return _activation.TryGetValue(key, out var e) ? (e.Sequence, e.Active, e.Acked, known) : (0, false, true, known);
|
||||
}
|
||||
|
||||
/// <summary>Restore a carried native condition's marker basis and (T2) its known flag — carried-over state is
|
||||
/// known at once, but only if it was known before the rebuild.</summary>
|
||||
private void RestoreActivation(RealmKey key, ulong sequence, bool active, bool acked, bool known)
|
||||
{
|
||||
_activation[key] = new ActivationEntry { Sequence = sequence, Active = active, Acked = acked };
|
||||
if (known) _conditionStateKnown.Add(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Under <c>Lock</c>: advance the activation marker against the node's current Active/Acked and append one
|
||||
/// record to the sink. Called for every condition state change — event-firing or silent.
|
||||
/// </summary>
|
||||
private void RaiseConditionRecord(RealmKey key, AlarmConditionState condition)
|
||||
{
|
||||
var entry = AdvanceActivation(key, condition);
|
||||
_conditionStateSink?.Append(BuildConditionRecord(key, condition, 0, removed: false, entry));
|
||||
}
|
||||
|
||||
/// <summary>The raise path for a condition known only by instance (ReportConditionEvent).</summary>
|
||||
private void RaiseConditionRecord(AlarmConditionState condition)
|
||||
{
|
||||
var key = MapKey(condition.NodeId);
|
||||
// A stale instance (replaced or removed) is not the live condition: its report never describes the set.
|
||||
if (!_alarmConditions.TryGetValue(key, out var live) || !ReferenceEquals(live, condition)) return;
|
||||
RaiseConditionRecord(key, condition);
|
||||
}
|
||||
|
||||
/// <summary>T3: bump on an Active false→true edge or ANY Acked true→false edge; never otherwise.</summary>
|
||||
private ActivationEntry AdvanceActivation(RealmKey key, AlarmConditionState condition)
|
||||
{
|
||||
if (!_activation.TryGetValue(key, out var e)) _activation[key] = e = new ActivationEntry();
|
||||
var active = condition.ActiveState?.Id?.Value ?? false;
|
||||
var acked = condition.AckedState?.Id?.Value ?? true;
|
||||
if ((active && !e.Active) || (e.Acked && !acked)) e.Sequence++;
|
||||
e.Active = active;
|
||||
e.Acked = acked;
|
||||
return e;
|
||||
}
|
||||
|
||||
private ConditionStateRecord BuildConditionRecord(
|
||||
RealmKey key, AlarmConditionState c, long version, bool removed, ActivationEntry? entry = null)
|
||||
{
|
||||
entry ??= _activation.TryGetValue(key, out var e) ? e : null;
|
||||
var delta = ReadConditionDelta(c);
|
||||
return new ConditionStateRecord(
|
||||
key.Realm, key.Id, _nativeAlarmNodeIds.Contains(key), version, removed,
|
||||
StateKnown: !removed && _conditionStateKnown.Contains(key),
|
||||
delta.Active, delta.Acknowledged, delta.Confirmed, delta.Enabled, delta.Shelving, delta.MappedSeverity,
|
||||
delta.Message, delta.Quality.Code,
|
||||
c.ConditionClassName?.Value.Text,
|
||||
c.Time?.Value.ToDateTime() is { } t && t != DateTime.MinValue ? DateTime.SpecifyKind(t, DateTimeKind.Utc) : UtcNow,
|
||||
c.Retain?.Value == true,
|
||||
new ConditionActivationMarker(ConditionIncarnation, entry?.Sequence ?? 0),
|
||||
c.Comment?.Value.Text,
|
||||
c.ClientUserId?.Value);
|
||||
}
|
||||
}
|
||||
@@ -1419,10 +1419,18 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
// WireMethodTransitionReporting) equals the node's current state → no delta → suppress, so
|
||||
// the transition is reported exactly once. ReportConditionEvent
|
||||
// stamps a fresh EventId, ClearChangeMasks, and ReportEvent — all still under this lock.
|
||||
// A1 (§5.1): a projected snapshot IS an established state. The record is raised by
|
||||
// ReportConditionEvent when the gate fires, otherwise here as a state-only record (ConditionClass and
|
||||
// the other silent fields).
|
||||
_conditionStateKnown.Add(key);
|
||||
if (fire)
|
||||
{
|
||||
ReportConditionEvent(condition, sourceTimestampUtc);
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseConditionRecord(key, condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1557,6 +1565,10 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
// the InstanceStateSnapshot captures the just-projected values.
|
||||
alarm.ClearChangeMasks(SystemContext, includeChildren: true);
|
||||
|
||||
// A1 (§5.1): the condition-state delta is raised BEFORE the Warm-Standby return, so a standby's hub stays
|
||||
// current and a promoted Primary starts correct.
|
||||
RaiseConditionRecord(alarm);
|
||||
|
||||
// Gitea #717 spec §4.5: on a Warm Standby only the STAMP half above runs — the condition node is current and a
|
||||
// later refresh carries a valid, ackable EventId — and the DELIVER half below does not. Suppressing here, before
|
||||
// the snapshot and every SDK field projection, covers the Server-object, folder and direct paths, ACL on or
|
||||
@@ -2640,6 +2652,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
|
||||
AddPredefinedNode(SystemContext, alarm);
|
||||
_alarmConditions[conditionKey] = alarm;
|
||||
BeginConditionMaterialise(conditionKey, isNative);
|
||||
if (!string.IsNullOrEmpty(equipmentNodeId)) IndexChild(MapKey(realm, equipmentNodeId), conditionKey);
|
||||
// Only the new condition can be missing its unit here, so raise just that one — a full reconcile
|
||||
// walks every condition, which made a deploy of N alarms O(N²) under Lock whenever any Server-object
|
||||
@@ -2659,6 +2672,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
// for a later re-add to resurrect.
|
||||
if (_rebuildCarryOver.Remove(conditionKey, out var carried) && isNative)
|
||||
RestoreCarryOver(alarm, conditionKey, carried);
|
||||
FinishConditionMaterialise(conditionKey, alarm);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2675,7 +2689,8 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
private sealed record NativeConditionCarryOver(
|
||||
AlarmConditionDelta State, DateTime Time, DateTime? ActiveTransitionTime, DateTime? QualityTimestamp,
|
||||
LocalizedText? Comment = null, string? ClientUserId = null, NodeId? ConditionClassId = null,
|
||||
LocalizedText? ConditionClassName = null);
|
||||
LocalizedText? ConditionClassName = null, ulong ActivationSequence = 0, bool ActivationActive = false,
|
||||
bool ActivationAcked = true, bool StateKnown = false);
|
||||
|
||||
/// <summary>
|
||||
/// Read what a native condition node holds right now, so the rebuild can put it back.
|
||||
@@ -2696,15 +2711,20 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
/// </remarks>
|
||||
/// <param name="condition">The live native condition, about to be discarded.</param>
|
||||
/// <returns>Its carried state.</returns>
|
||||
private NativeConditionCarryOver CaptureCarryOver(AlarmConditionState condition) => new(
|
||||
ReadConditionDelta(condition),
|
||||
condition.Time?.Value.ToDateTime() ?? UtcNow,
|
||||
condition.ActiveState?.TransitionTime?.Value.ToDateTime(),
|
||||
condition.Quality?.SourceTimestamp?.Value.ToDateTime(),
|
||||
condition.Comment?.Value,
|
||||
condition.ClientUserId?.Value,
|
||||
condition.ConditionClassId?.Value,
|
||||
condition.ConditionClassName?.Value);
|
||||
private NativeConditionCarryOver CaptureCarryOver(RealmKey key, AlarmConditionState condition)
|
||||
{
|
||||
var a = CaptureActivation(key);
|
||||
return new(
|
||||
ReadConditionDelta(condition),
|
||||
condition.Time?.Value.ToDateTime() ?? UtcNow,
|
||||
condition.ActiveState?.TransitionTime?.Value.ToDateTime(),
|
||||
condition.Quality?.SourceTimestamp?.Value.ToDateTime(),
|
||||
condition.Comment?.Value,
|
||||
condition.ClientUserId?.Value,
|
||||
condition.ConditionClassId?.Value,
|
||||
condition.ConditionClassName?.Value,
|
||||
a.Sequence, a.Active, a.Acked, a.Known);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Put a carried state back onto the freshly materialised native condition. MUST be called
|
||||
@@ -2733,6 +2753,8 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
private void RestoreCarryOver(AlarmConditionState alarm, RealmKey conditionKey, NativeConditionCarryOver carried)
|
||||
{
|
||||
var state = carried.State;
|
||||
// A1 (T3, T2): the activation marker and the known flag ride the carry-over, normal conditions included.
|
||||
RestoreActivation(conditionKey, carried.ActivationSequence, carried.ActivationActive, carried.ActivationAcked, carried.StateKnown);
|
||||
// #839: a driver-assigned ConditionClass is a property of the condition, not of its state — restored whether
|
||||
// or not the condition is off-normal (the materialise default is BaseConditionClass).
|
||||
if (alarm.ConditionClassId is { } classIdNode && carried.ConditionClassId is { } classId && !classId.IsNull)
|
||||
@@ -5583,8 +5605,9 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
_restoredConditionReports.Clear();
|
||||
foreach (var (key, alarm) in _alarmConditions)
|
||||
{
|
||||
if (_nativeAlarmNodeIds.Contains(key)) _rebuildCarryOver[key] = CaptureCarryOver(alarm);
|
||||
if (_nativeAlarmNodeIds.Contains(key)) _rebuildCarryOver[key] = CaptureCarryOver(key, alarm);
|
||||
}
|
||||
CaptureConditionStateForRebuild();
|
||||
|
||||
// tear down every native-alarm→equipment-folder notifier pair bidirectionally
|
||||
// BEFORE the conditions + folders are cleared below, so no inverse-notifier entry leaks across the
|
||||
@@ -5675,6 +5698,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
discarded = _rebuildCarryOver.Count;
|
||||
_restoredConditionReports.Clear();
|
||||
_rebuildCarryOver.Clear();
|
||||
CompleteConditionApply();
|
||||
}
|
||||
|
||||
if (reported > 0 || discarded > 0)
|
||||
@@ -5778,6 +5802,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
// the condition drops — the folders (equipment, UNS realm) SURVIVE this scoped remove, so without
|
||||
// this they would keep a dangling inverse-notifier entry to the removed condition.
|
||||
UnwireAlarmNotifiers(key);
|
||||
ForgetConditionState(key, condition);
|
||||
condition.Parent?.RemoveChild(condition);
|
||||
ForgetPredefinedSubtree(condition);
|
||||
_nativeAlarmNodeIds.Remove(key);
|
||||
@@ -5889,6 +5914,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
UnindexChild(key);
|
||||
TombstoneMonitoredSubtree(c, now);
|
||||
UnwireAlarmNotifiers(key);
|
||||
ForgetConditionState(key, c);
|
||||
if (c.Parent is { } parent && !removedParents.Contains(parent)) parent.RemoveChild(c);
|
||||
ForgetPredefinedSubtree(c);
|
||||
_nativeAlarmNodeIds.Remove(key);
|
||||
|
||||
@@ -141,6 +141,14 @@ public sealed class SdkAddressSpaceSink : IOpcUaAddressSpaceSink, ISurgicalAddre
|
||||
/// <inheritdoc />
|
||||
public void CompleteAddressSpaceApply() => _nodeManager.CompleteAddressSpaceApply();
|
||||
|
||||
/// <inheritdoc />
|
||||
public void MarkConditionsKnown(string source, IReadOnlyList<string> conditionIds, AddressSpaceRealm realm)
|
||||
=> _nodeManager.MarkConditionsKnown(source, conditionIds, realm);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void MarkConditionsUnknown(string source, IReadOnlyList<string> conditionIds, AddressSpaceRealm realm)
|
||||
=> _nodeManager.MarkConditionsUnknown(source, conditionIds, realm);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RaiseNodesAddedModelChange(string affectedNodeId, AddressSpaceRealm realm) => _nodeManager.RaiseNodesAddedModelChange(affectedNodeId, realm);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user