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:
@@ -492,6 +492,9 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
// No session yet — a probe would fail for every tag. ReSubscribeAll's chunk
|
||||
// completions re-arm the timer once the link is up.
|
||||
break;
|
||||
case RetryAlarmResubscribe:
|
||||
// No session — ReSubscribeAllAlarms re-issues every feed once the link is up.
|
||||
break;
|
||||
case ServiceLevelPollTick or ServiceLevelSampled or ServiceLevelProbed:
|
||||
// ScadaBridge#138: belongs to a session that is over; BecomeConnected restarts it.
|
||||
break;
|
||||
@@ -621,6 +624,9 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
case RetryTagResolution:
|
||||
HandleRetryTagResolution();
|
||||
break;
|
||||
case RetryAlarmResubscribe retryAlarm:
|
||||
HandleRetryAlarmResubscribe(retryAlarm);
|
||||
break;
|
||||
case BrowseNodeCommand browse:
|
||||
HandleBrowse(browse);
|
||||
break;
|
||||
@@ -985,6 +991,9 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
// Ignore — the adapter has no live session; ReSubscribeAll's chunk
|
||||
// completions re-arm the retry timer after reconnect.
|
||||
break;
|
||||
case RetryAlarmResubscribe:
|
||||
// Ignore — ReSubscribeAllAlarms re-issues every feed after reconnect.
|
||||
break;
|
||||
case ServiceLevelProbed { AfterDisconnect: true } lostLinkProbe:
|
||||
// ScadaBridge#141: the other endpoint's verdict for a link lost after a Degraded level.
|
||||
HandleDisconnectProbed(lostLinkProbe);
|
||||
@@ -2781,10 +2790,17 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
}
|
||||
else if (row.ConnectionLevelFailure)
|
||||
{
|
||||
// Connection-level fault: not a tag-resolution problem. The reconnect
|
||||
// cycle re-issues everything from _subscriptionsByInstance.
|
||||
// Connection-level fault on a re-subscribe / probe row. If the adapter also
|
||||
// raises Disconnected, the reconnect cycle re-issues everything from
|
||||
// _subscriptionsByInstance — but nothing here starts that cycle, and
|
||||
// ReSubscribeAll already cleared _subscriptionIds/_unresolvedTags, so a
|
||||
// chunk that failed while the session stayed healthy used to leave its tags
|
||||
// in NO retry set: Bad until some unrelated reconnect (DCL-8). Put them in
|
||||
// the resolution retry set; a reconnect clears it and re-issues them anyway.
|
||||
_log.Warning("[{0}] Batch subscribe for {1} failed at connection level: {2}",
|
||||
_connectionName, row.TagPath, row.Error);
|
||||
if (_instancesByTag.ContainsKey(row.TagPath) && !_subscriptionIds.ContainsKey(row.TagPath))
|
||||
_unresolvedTags.Add(row.TagPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3425,8 +3441,19 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
{
|
||||
_log.Warning("[{0}] Alarm subscribe failed for source {1}: {2}",
|
||||
_connectionName, msg.SourceReference, msg.Error);
|
||||
|
||||
// DCL-2: a reconnect re-subscribe (ReplyTo == null) has no requester to retry it —
|
||||
// the NativeAlarmActor already counts the feed as established and its
|
||||
// NativeAlarmSourceUnavailable handler deliberately does not re-subscribe — so the
|
||||
// DCL retries it itself on a backed-off timer while the source still has
|
||||
// subscribers. An interactive subscribe's failure is retried by its requester.
|
||||
if (msg.ReplyTo is null)
|
||||
ScheduleAlarmResubscribeRetry(msg.SourceReference);
|
||||
}
|
||||
|
||||
if (msg.Success)
|
||||
CancelAlarmResubscribeRetry(msg.SourceReference);
|
||||
|
||||
// ReplyTo is null for reconnect re-subscribes (no original requester to answer).
|
||||
msg.ReplyTo?.Tell(new SubscribeAlarmsResponse(
|
||||
msg.CorrelationId ?? string.Empty, msg.InstanceUniqueName ?? string.Empty,
|
||||
@@ -3595,6 +3622,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
UnindexAlarmSource(sourceReference);
|
||||
_alarmSourceFilter.Remove(sourceReference);
|
||||
_alarmSourceFilterPredicate.Remove(sourceReference);
|
||||
CancelAlarmResubscribeRetry(sourceReference);
|
||||
// Clear the in-flight marker so that if an adapter
|
||||
// subscribe is still in flight for this source, the late AlarmSubscribeCompleted
|
||||
// is recognized as orphaned (its guard checks _alarmSourceSubscribers, now empty)
|
||||
@@ -3616,24 +3644,89 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
_alarmSubscriptionIds.Clear();
|
||||
_alarmReplay.Clear();
|
||||
_alarmSubscribesInFlight.Clear();
|
||||
var self = Self;
|
||||
var generation = _adapterGeneration;
|
||||
// A fresh session re-issues every feed: retries armed against the previous one are moot
|
||||
// and their backoff starts over.
|
||||
foreach (var sourceRef in _alarmResubscribeInterval.Keys.ToList())
|
||||
CancelAlarmResubscribeRetry(sourceRef);
|
||||
|
||||
foreach (var sourceRef in _alarmSourceSubscribers.Keys.ToList())
|
||||
IssueAlarmResubscribe(alarmable, sourceRef);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Issues one requester-less (<c>ReplyTo == null</c>) alarm-feed subscribe for
|
||||
/// <paramref name="sourceRef"/> against <paramref name="alarmable"/> — the reconnect
|
||||
/// re-subscribe and its DCL-2 retry.
|
||||
/// </summary>
|
||||
private void IssueAlarmResubscribe(IAlarmSubscribableConnection alarmable, string sourceRef)
|
||||
{
|
||||
var self = Self;
|
||||
var generation = _adapterGeneration;
|
||||
var sr = sourceRef;
|
||||
var filter = _alarmSourceFilter.GetValueOrDefault(sourceRef);
|
||||
_alarmSubscribesInFlight.Add(sr);
|
||||
alarmable.SubscribeAlarmsAsync(sr, filter,
|
||||
t => self.Tell(new AlarmTransitionReceived(t, generation)))
|
||||
.ContinueWith(task => task.IsCompletedSuccessfully
|
||||
? new AlarmSubscribeCompleted(
|
||||
sr, true, task.Result, null, null, null, null, generation, alarmable) as object
|
||||
: new AlarmSubscribeCompleted(sr, false, null,
|
||||
task.Exception?.GetBaseException().Message ?? "Unknown error",
|
||||
null, null, null, generation, alarmable))
|
||||
.PipeTo(self);
|
||||
}
|
||||
|
||||
// ── DCL-2: alarm re-subscribe retry ──
|
||||
|
||||
/// <summary>
|
||||
/// Next retry delay per alarm source whose requester-less re-subscribe failed. Same floor,
|
||||
/// doubling and ceiling as the tag-resolution probe
|
||||
/// (<see cref="DataConnectionOptions.TagResolutionRetryInterval"/> →
|
||||
/// <see cref="DataConnectionOptions.TagResolutionRetryMaxInterval"/>).
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, TimeSpan> _alarmResubscribeInterval = new();
|
||||
|
||||
private static string AlarmResubscribeTimerKey(string sourceRef) => "alarm-resubscribe:" + sourceRef;
|
||||
|
||||
private void ScheduleAlarmResubscribeRetry(string sourceRef)
|
||||
{
|
||||
if (!_alarmSourceSubscribers.ContainsKey(sourceRef))
|
||||
return;
|
||||
|
||||
var delay = _alarmResubscribeInterval.TryGetValue(sourceRef, out var current)
|
||||
? current
|
||||
: _options.TagResolutionRetryInterval;
|
||||
_alarmResubscribeInterval[sourceRef] = NextTagResolutionInterval(
|
||||
delay, _options.TagResolutionRetryInterval, _options.TagResolutionRetryMaxInterval);
|
||||
Timers.StartSingleTimer(AlarmResubscribeTimerKey(sourceRef),
|
||||
new RetryAlarmResubscribe(sourceRef, _adapterGeneration), delay);
|
||||
_log.Info("[{0}] Alarm feed for source {1} will be re-subscribed in {2}",
|
||||
_connectionName, sourceRef, delay);
|
||||
}
|
||||
|
||||
private void CancelAlarmResubscribeRetry(string sourceRef)
|
||||
{
|
||||
_alarmResubscribeInterval.Remove(sourceRef);
|
||||
Timers.Cancel(AlarmResubscribeTimerKey(sourceRef));
|
||||
}
|
||||
|
||||
private void HandleRetryAlarmResubscribe(RetryAlarmResubscribe msg)
|
||||
{
|
||||
// A retry armed against an earlier session is moot: the reconnect re-issued every feed.
|
||||
if (msg.AdapterGeneration != _adapterGeneration)
|
||||
return;
|
||||
if (!_alarmSourceSubscribers.ContainsKey(msg.SourceReference))
|
||||
{
|
||||
var sr = sourceRef;
|
||||
var filter = _alarmSourceFilter.GetValueOrDefault(sourceRef);
|
||||
_alarmSubscribesInFlight.Add(sr);
|
||||
alarmable.SubscribeAlarmsAsync(sr, filter,
|
||||
t => self.Tell(new AlarmTransitionReceived(t, generation)))
|
||||
.ContinueWith(task => task.IsCompletedSuccessfully
|
||||
? new AlarmSubscribeCompleted(
|
||||
sr, true, task.Result, null, null, null, null, generation, alarmable) as object
|
||||
: new AlarmSubscribeCompleted(sr, false, null,
|
||||
task.Exception?.GetBaseException().Message ?? "Unknown error",
|
||||
null, null, null, generation, alarmable))
|
||||
.PipeTo(self);
|
||||
CancelAlarmResubscribeRetry(msg.SourceReference);
|
||||
return;
|
||||
}
|
||||
if (_alarmSubscriptionIds.ContainsKey(msg.SourceReference)
|
||||
|| _alarmSubscribesInFlight.Contains(msg.SourceReference)
|
||||
|| _adapter is not IAlarmSubscribableConnection alarmable)
|
||||
return;
|
||||
|
||||
_log.Info("[{0}] Retrying alarm feed subscribe for source {1}", _connectionName, msg.SourceReference);
|
||||
IssueAlarmResubscribe(alarmable, msg.SourceReference);
|
||||
}
|
||||
|
||||
/// <summary>Notifies alarm subscribers that the source feed is unavailable (connection lost).</summary>
|
||||
@@ -3693,6 +3786,9 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
|
||||
}
|
||||
internal record RetryTagResolution;
|
||||
|
||||
/// <summary>DCL-2: backed-off retry of a failed requester-less alarm-feed re-subscribe.</summary>
|
||||
internal record RetryAlarmResubscribe(string SourceReference, int AdapterGeneration);
|
||||
|
||||
/// <summary>Coalescing-timer tick for the tag-quality counter push.</summary>
|
||||
internal record QualityFlushTick;
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user