fix(dcl): retry failed alarm re-subscribes, check alarm item create status, retry connection-level re-subscribe rows (DCL-2, DCL-3, DCL-8, WP3 task 3.2)

This commit is contained in:
Joseph Doherty
2026-12-03 09:53:44 -05:00
parent 6adfc9f449
commit 2e8bede3b2
5 changed files with 355 additions and 22 deletions
@@ -962,11 +962,29 @@ public class RealOpcUaClient : IOpcUaClient
HandleAlarmEvent(handle, sourceNodeId, efl, onTransition);
};
await _applyGate.RunAsync(async () =>
try
{
eventShard.AddItem(item);
await eventShard.ApplyChangesAsync(cancellationToken);
}, cancellationToken);
await _applyGate.RunAsync(async () =>
{
eventShard.AddItem(item);
await eventShard.ApplyChangesAsync(cancellationToken);
}, cancellationToken);
}
catch
{
await DiscardFailedAlarmItemAsync(handle, eventShard, item);
throw;
}
// DCL-3: ApplyChangesAsync does NOT throw for a per-item create failure — the server's
// verdict is on the item's status, exactly as on the data path. An unknown node, a node
// that is not an event notifier or a rejected event filter used to be recorded as a live
// feed (SubscribeAlarmsResponse Success) that never delivers anything.
if (AlarmItemCreateError(item) is { } createError)
{
await DiscardFailedAlarmItemAsync(handle, eventShard, item);
throw new ServiceResultException(createError);
}
_alarmItems[handle] = item;
// Replay currently-active conditions as a Snapshot…SnapshotComplete sequence.
@@ -974,6 +992,42 @@ public class RealOpcUaClient : IOpcUaClient
return handle;
}
/// <summary>
/// The server's per-item verdict on an alarm event monitored item after
/// <c>ApplyChangesAsync</c>: the bad <see cref="ServiceResult"/> when the create was rejected,
/// else <c>null</c> (DCL-3).
/// </summary>
internal static ServiceResult? AlarmItemCreateError(MonitoredItem item)
{
var error = item.Status?.Error;
return error != null && ServiceResult.IsBad(error) ? error : null;
}
/// <summary>
/// Unwinds a half-created alarm feed: removes the item from the event shard (best effort —
/// a dead session must not mask the original failure) and drops the per-handle bookkeeping
/// <see cref="CreateAlarmSubscriptionAsync"/> created up front.
/// </summary>
private async Task DiscardFailedAlarmItemAsync(string handle, Subscription eventShard, MonitoredItem item)
{
try
{
await _applyGate.RunAsync(async () =>
{
eventShard.RemoveItem(item);
await eventShard.ApplyChangesAsync(CancellationToken.None);
}, CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Removing a failed alarm event item from the event shard failed (ignored).");
}
_alarmInRefresh.TryRemove(handle, out _);
_alarmSnapshotIds.TryRemove(handle, out _);
_alarmLastState.TryRemove(handle, out _);
}
/// <inheritdoc />
public async Task RemoveAlarmSubscriptionAsync(string subscriptionHandle, CancellationToken cancellationToken = default)
{