fix(grpc-data): A1 review round 2 N1 + N2 — withdrawals are never dropped; a discarded mark re-arms F5

N1: the deferral keeps every withdrawal (Known: false) regardless of capacity; a known mark beyond capacity is
dropped (unknown is safe) and its source remembered.
N2: a G3-discarded (or overflowed) deferred mark is reported to its sender as OpcUaPublishActor.ConditionMarksDiscarded
(the scripted host is skipped); DriverHostActor puts those conditions back in the F5 window from now, so a driver
that does not re-signal gets the diagnostic.
This commit is contained in:
Joseph Doherty
2026-11-24 11:34:12 -05:00
parent 4c8539b897
commit 4a6c894268
4 changed files with 127 additions and 6 deletions
@@ -49,6 +49,7 @@ public sealed partial class DriverHostActor
Receive<DriverInstanceActor.AlarmInitialStatePublished>(OnAlarmInitialState);
Receive<ConditionSignalTimedOut>(OnConditionSignalTimedOut);
Receive<ConditionBackstopElapsed>(OnConditionBackstopElapsed);
Receive<PublishActor.ConditionMarksDiscarded>(OnConditionMarksDiscarded);
}
private void ReceiveConditionSignalsWhileStale()
@@ -57,6 +58,7 @@ public sealed partial class DriverHostActor
_log.Debug("DriverHost {Node}: dropping an initial alarm-state signal from {Id} while Stale", _localNode, m.DriverInstanceId));
Receive<ConditionSignalTimedOut>(_ => { });
Receive<ConditionBackstopElapsed>(_ => { });
Receive<PublishActor.ConditionMarksDiscarded>(_ => { });
}
private ConditionSignalTracker Tracker(string driverInstanceId)
@@ -168,6 +170,30 @@ public sealed partial class DriverHostActor
}
}
/// <summary>
/// A1 review round 2 N2: the publish actor discarded a deferred mark of this driver (G3, or the deferral was full),
/// so those conditions are still unknown in the node manager. Put them back in the F5 window (from now), so a
/// driver that does not re-signal raises the "not signalled" diagnostic for them.
/// </summary>
private void OnConditionMarksDiscarded(PublishActor.ConditionMarksDiscarded msg)
{
if (!_conditionSignalTrackers.TryGetValue(msg.Source, out var t) || t.GoodSinceUtc is null) return;
if (!_alarmRefsByDriver.TryGetValue(msg.Source, out var refs)) return;
var owned = refs.Select(r => r.RawPath).ToHashSet(StringComparer.Ordinal);
IEnumerable<string> back = msg.ConditionIds is null
? owned
: msg.ConditionIds
.Where(id => _driverRefByAlarmNodeId.TryGetValue(id, out var target) && target.DriverInstanceId == msg.Source)
.Select(id => _driverRefByAlarmNodeId[id].RawPath)
.Where(owned.Contains);
var before = t.Pending.Count;
t.Pending.UnionWith(back);
if (t.Pending.Count == before) return;
_log.Warning("DriverHost {Node}: {Count} deferred condition-known mark(s) of {Driver} were discarded; back in the F5 window until it signals again",
_localNode, t.Pending.Count - before, msg.Source);
ArmSignalTimeout(msg.Source, t);
}
private void OnAlarmInitialState(DriverInstanceActor.AlarmInitialStatePublished msg)
{
if (!IsCurrentChild(msg.DriverInstanceId, Sender))
@@ -100,6 +100,16 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
/// <param name="Realm">Their realm.</param>
/// <param name="Known">True = mark known; false = withdraw (a scripted input reverted to never-pushed).</param>
public sealed record MarkConditionsKnown(string Source, IReadOnlyList<string> ConditionIds, AddressSpaceRealm Realm, bool Known = true);
/// <summary>
/// A1 review round 2 N2: sent back to the sender of a deferred <see cref="MarkConditionsKnown"/> the G3 rule (or
/// the deferral's capacity) discarded, so its F5 tracker counts those conditions as still unknown again.
/// </summary>
/// <param name="Source">The mark's source (driver instance id).</param>
/// <param name="ConditionIds">The discarded condition ids, or null when an overflowed mark's ids were not kept
/// (= every condition of <paramref name="Source"/>).</param>
/// <param name="Realm">Their realm.</param>
public sealed record ConditionMarksDiscarded(string Source, IReadOnlyList<string>? ConditionIds, AddressSpaceRealm Realm);
/// <summary>Annotate a materialised condition's source-data Quality out of band from any alarm
/// transition (the driver-connectivity path: comms lost → <see cref="OpcUaStatus.BadNoCommunication"/>,
/// restored → <see cref="OpcUaStatus.Good"/>). Routed to <see cref="IOpcUaAddressSpaceSink.WriteAlarmQuality"/>,
@@ -801,10 +811,17 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
private void HandleMarkConditionsKnown(MarkConditionsKnown msg)
{
if (PlanPipelineBusy) _deferred.Mark(msg);
if (PlanPipelineBusy) _deferred.Mark(msg, Sender);
ApplyMark(msg);
}
/// <summary>N2: tell a discarded mark's sender (a driver host; the scripted host has no F5 tracker and is skipped).</summary>
private static void TellDiscarded(IActorRef? sender, ConditionMarksDiscarded message)
{
if (sender is null || sender.IsNobody() || message.Source == ScriptedSource) return;
sender.Tell(message);
}
private void ApplyMark(MarkConditionsKnown msg)
{
try
@@ -1176,12 +1193,13 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
// until the source's next signal.
var marks = 0;
var discarded = 0;
foreach (var mark in _deferred.Marks)
foreach (var (mark, sender) in _deferred.Marks)
{
// A withdrawal (Known: false) is never discarded: it only makes conditions unknown, which is always safe.
if (mark.Known && _deferred.SourceLost(mark.Source))
{
discarded++;
TellDiscarded(sender, new ConditionMarksDiscarded(mark.Source, mark.ConditionIds, mark.Realm));
continue;
}
@@ -1189,6 +1207,13 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
ApplyMark(mark);
}
// N1: a KNOWN mark that did not fit the deferral was dropped (staying unknown is safe) — its sender hears it.
foreach (var (source, sender) in _deferred.OverflowedMarkSources)
{
discarded++;
TellDiscarded(sender, new ConditionMarksDiscarded(source, null, AddressSpaceRealm.Raw));
}
if (discarded > 0)
{
OtOpcUaTelemetry.DataConnConditionMarksDiscarded.Add(discarded);
@@ -1225,7 +1250,8 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
private readonly Dictionary<(AddressSpaceRealm, string), AlarmQualityUpdate> _qualities = new();
// A1: condition-known marks in arrival order (bounded by Capacity — a dropped mark only keeps conditions
// unknown, which is always safe), and the sources that lost a deferred alarm projection (G3).
private readonly List<MarkConditionsKnown> _marks = new();
private readonly List<(MarkConditionsKnown Msg, IActorRef? Sender)> _marks = new();
private readonly Dictionary<string, IActorRef?> _overflowedMarkSources = new(StringComparer.Ordinal);
private readonly HashSet<string> _lossySources = new(StringComparer.Ordinal);
private bool _unattributedLoss;
@@ -1235,13 +1261,23 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
public bool IsEmpty => Count == 0 && _marks.Count == 0;
public IEnumerable<AlarmStateUpdate> Alarms => _alarms.Values;
public IEnumerable<AlarmQualityUpdate> AlarmQualities => _qualities.Values;
public IReadOnlyList<MarkConditionsKnown> Marks => _marks;
public IReadOnlyList<(MarkConditionsKnown Msg, IActorRef? Sender)> Marks => _marks;
public void Mark(MarkConditionsKnown msg)
/// <summary>
/// Defer a mark. A withdrawal (<c>Known: false</c>) is never dropped — review round 2 N1: staying KNOWN over a
/// lost withdrawal is the unsafe direction, and withdrawals are bounded by connectivity edges. A known mark
/// beyond <see cref="Capacity"/> is dropped (unknown is safe) and its source is remembered so the sender hears
/// of it at replay (N2).
/// </summary>
public void Mark(MarkConditionsKnown msg, IActorRef? sender)
{
if (_marks.Count < Capacity) _marks.Add(msg);
if (!msg.Known || _marks.Count < Capacity) _marks.Add((msg, sender));
else _overflowedMarkSources[msg.Source] = sender;
}
public IEnumerable<(string Source, IActorRef? Sender)> OverflowedMarkSources =>
_overflowedMarkSources.Select(kv => (kv.Key, kv.Value));
/// <summary>G3: a deferred projection of <paramref name="source"/> was dropped or skipped. An unattributed
/// loss (null source) discards every source's marks.</summary>
public void NoteAlarmLoss(string? source)
@@ -1317,6 +1353,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers
_alarms.Clear();
_qualities.Clear();
_marks.Clear();
_overflowedMarkSources.Clear();
_lossySources.Clear();
_unattributedLoss = false;
Dropped = 0;