Resolve blocking I/O finding and complete Historian lifecycle test coverage
Move subscribe/unsubscribe I/O outside lock(Lock) in SyncAddressSpace to avoid blocking all OPC UA operations during rebuilds. Replace blocking ReadAsync calls for alarm priority/description in dispatch loop with cached subscription values. Extract IHistorianConnectionFactory so EnsureConnected can be tested without the SDK runtime — adds 5 connection lifecycle tests (failure, timeout, reconnect, state resilience, dispose-after-failure). All stability review findings and test coverage gaps are now fully resolved. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -25,6 +25,9 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
|
||||
// Alarm tracking: maps InAlarm tag reference → alarm source info
|
||||
private readonly Dictionary<string, AlarmInfo> _alarmInAlarmTags = new(StringComparer.OrdinalIgnoreCase);
|
||||
// Reverse lookups: priority/description tag reference → alarm info for cache updates
|
||||
private readonly Dictionary<string, AlarmInfo> _alarmPriorityTags = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, AlarmInfo> _alarmDescTags = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly bool _alarmTrackingEnabled;
|
||||
private readonly bool _anonymousCanWrite;
|
||||
private readonly AutoResetEvent _dataChangeSignal = new(false);
|
||||
@@ -172,6 +175,8 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
_tagMetadata.Clear();
|
||||
_alarmInAlarmTags.Clear();
|
||||
_alarmAckedTags.Clear();
|
||||
_alarmPriorityTags.Clear();
|
||||
_alarmDescTags.Clear();
|
||||
_nodeMap.Clear();
|
||||
_gobjectToTagRefs.Clear();
|
||||
VariableNodeCount = 0;
|
||||
@@ -357,6 +362,10 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
};
|
||||
_alarmInAlarmTags[inAlarmTagRef] = alarmInfo;
|
||||
_alarmAckedTags[alarmInfo.AckedTagReference] = alarmInfo;
|
||||
if (!string.IsNullOrEmpty(alarmInfo.PriorityTagReference))
|
||||
_alarmPriorityTags[alarmInfo.PriorityTagReference] = alarmInfo;
|
||||
if (!string.IsNullOrEmpty(alarmInfo.DescAttrNameTagReference))
|
||||
_alarmDescTags[alarmInfo.DescAttrNameTagReference] = alarmInfo;
|
||||
hasAlarms = true;
|
||||
}
|
||||
|
||||
@@ -530,6 +539,9 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
/// <param name="attributes">The latest Galaxy attribute snapshot to compare against the currently published variables.</param>
|
||||
public void SyncAddressSpace(List<GalaxyObjectInfo> hierarchy, List<GalaxyAttributeInfo> attributes)
|
||||
{
|
||||
var tagsToUnsubscribe = new List<string>();
|
||||
var tagsToResubscribe = new List<string>();
|
||||
|
||||
lock (Lock)
|
||||
{
|
||||
if (_lastHierarchy == null || _lastAttributes == null)
|
||||
@@ -565,29 +577,22 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
if (_subscriptionRefCounts.TryGetValue(tagRef, out var count))
|
||||
affectedSubscriptions[tagRef] = count;
|
||||
|
||||
// Tear down changed subtrees
|
||||
TearDownGobjects(changedIds);
|
||||
// Tear down changed subtrees (collects tags for deferred unsubscription)
|
||||
TearDownGobjects(changedIds, tagsToUnsubscribe);
|
||||
|
||||
// Rebuild changed subtrees from new data
|
||||
var changedHierarchy = hierarchy.Where(h => changedIds.Contains(h.GobjectId)).ToList();
|
||||
var changedAttributes = attributes.Where(a => changedIds.Contains(a.GobjectId)).ToList();
|
||||
BuildSubtree(changedHierarchy, changedAttributes);
|
||||
|
||||
// Restore subscriptions for surviving tags
|
||||
// Restore subscription bookkeeping for surviving tags
|
||||
foreach (var kvp in affectedSubscriptions)
|
||||
{
|
||||
if (!_tagToVariableNode.ContainsKey(kvp.Key))
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
_mxAccessClient.SubscribeAsync(kvp.Key, (_, _) => { }).GetAwaiter().GetResult();
|
||||
_subscriptionRefCounts[kvp.Key] = kvp.Value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning(ex, "Failed to restore subscription for {TagRef} after sync", kvp.Key);
|
||||
}
|
||||
_subscriptionRefCounts[kvp.Key] = kvp.Value;
|
||||
tagsToResubscribe.Add(kvp.Key);
|
||||
}
|
||||
|
||||
_lastHierarchy = new List<GalaxyObjectInfo>(hierarchy);
|
||||
@@ -596,9 +601,18 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
Log.Information("Incremental sync complete: {Objects} objects, {Variables} variables, {Alarms} alarms",
|
||||
ObjectNodeCount, VariableNodeCount, _alarmInAlarmTags.Count);
|
||||
}
|
||||
|
||||
// Perform subscribe/unsubscribe I/O outside Lock so read/write/browse operations are not blocked
|
||||
foreach (var tag in tagsToUnsubscribe)
|
||||
try { _mxAccessClient.UnsubscribeAsync(tag).GetAwaiter().GetResult(); }
|
||||
catch (Exception ex) { Log.Warning(ex, "Failed to unsubscribe {Tag} after sync", tag); }
|
||||
|
||||
foreach (var tag in tagsToResubscribe)
|
||||
try { _mxAccessClient.SubscribeAsync(tag, (_, _) => { }).GetAwaiter().GetResult(); }
|
||||
catch (Exception ex) { Log.Warning(ex, "Failed to restore subscription for {Tag} after sync", tag); }
|
||||
}
|
||||
|
||||
private void TearDownGobjects(HashSet<int> gobjectIds)
|
||||
private void TearDownGobjects(HashSet<int> gobjectIds, List<string> tagsToUnsubscribe)
|
||||
{
|
||||
foreach (var id in gobjectIds)
|
||||
{
|
||||
@@ -607,18 +621,10 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
{
|
||||
foreach (var tagRef in tagRefs.ToList())
|
||||
{
|
||||
// Unsubscribe if actively subscribed
|
||||
// Defer unsubscribe to outside lock
|
||||
if (_subscriptionRefCounts.ContainsKey(tagRef))
|
||||
{
|
||||
try
|
||||
{
|
||||
_mxAccessClient.UnsubscribeAsync(tagRef).GetAwaiter().GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
tagsToUnsubscribe.Add(tagRef);
|
||||
_subscriptionRefCounts.Remove(tagRef);
|
||||
}
|
||||
|
||||
@@ -630,20 +636,17 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
foreach (var alarmKey in alarmKeysToRemove)
|
||||
{
|
||||
var info = _alarmInAlarmTags[alarmKey];
|
||||
// Unsubscribe alarm auto-subscriptions
|
||||
// Defer alarm tag unsubscription to outside lock
|
||||
foreach (var alarmTag in new[]
|
||||
{ alarmKey, info.PriorityTagReference, info.DescAttrNameTagReference })
|
||||
if (!string.IsNullOrEmpty(alarmTag))
|
||||
try
|
||||
{
|
||||
_mxAccessClient.UnsubscribeAsync(alarmTag).GetAwaiter().GetResult();
|
||||
}
|
||||
catch
|
||||
{
|
||||
/* ignore */
|
||||
}
|
||||
tagsToUnsubscribe.Add(alarmTag);
|
||||
|
||||
_alarmInAlarmTags.Remove(alarmKey);
|
||||
if (!string.IsNullOrEmpty(info.PriorityTagReference))
|
||||
_alarmPriorityTags.Remove(info.PriorityTagReference);
|
||||
if (!string.IsNullOrEmpty(info.DescAttrNameTagReference))
|
||||
_alarmDescTags.Remove(info.DescAttrNameTagReference);
|
||||
if (!string.IsNullOrEmpty(info.AckedTagReference))
|
||||
_alarmAckedTags.Remove(info.AckedTagReference);
|
||||
}
|
||||
@@ -871,6 +874,10 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
};
|
||||
_alarmInAlarmTags[inAlarmTagRef] = alarmInfo;
|
||||
_alarmAckedTags[alarmInfo.AckedTagReference] = alarmInfo;
|
||||
if (!string.IsNullOrEmpty(alarmInfo.PriorityTagReference))
|
||||
_alarmPriorityTags[alarmInfo.PriorityTagReference] = alarmInfo;
|
||||
if (!string.IsNullOrEmpty(alarmInfo.DescAttrNameTagReference))
|
||||
_alarmDescTags[alarmInfo.DescAttrNameTagReference] = alarmInfo;
|
||||
hasAlarms = true;
|
||||
}
|
||||
|
||||
@@ -2075,6 +2082,23 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
alarmInfo = null;
|
||||
}
|
||||
|
||||
// Cache alarm priority/description values as they arrive via subscription
|
||||
if (_alarmPriorityTags.TryGetValue(address, out var priorityInfo))
|
||||
{
|
||||
if (vtq.Value is int ipCache)
|
||||
priorityInfo.CachedSeverity =
|
||||
(ushort)Math.Min(Math.Max(ipCache, 1), 1000);
|
||||
else if (vtq.Value is short spCache)
|
||||
priorityInfo.CachedSeverity =
|
||||
(ushort)Math.Min(Math.Max((int)spCache, 1), 1000);
|
||||
}
|
||||
|
||||
if (_alarmDescTags.TryGetValue(address, out var descInfo))
|
||||
{
|
||||
if (vtq.Value is string descCache && !string.IsNullOrEmpty(descCache))
|
||||
descInfo.CachedMessage = descCache;
|
||||
}
|
||||
|
||||
// Check for Acked transitions — skip if state hasn't changed
|
||||
if (_alarmAckedTags.TryGetValue(address, out ackedAlarmInfo))
|
||||
{
|
||||
@@ -2095,31 +2119,11 @@ namespace ZB.MOM.WW.LmxOpcUa.Host.OpcUa
|
||||
|
||||
if (newInAlarm)
|
||||
{
|
||||
try
|
||||
{
|
||||
var pVtq = _mxAccessClient.ReadAsync(alarmInfo.PriorityTagReference).GetAwaiter()
|
||||
.GetResult();
|
||||
if (pVtq.Value is int ip)
|
||||
severity = (ushort)Math.Min(Math.Max(ip, 1), 1000);
|
||||
else if (pVtq.Value is short sp)
|
||||
severity = (ushort)Math.Min(Math.Max((int)sp, 1), 1000);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Keep the previously cached severity when refresh reads fail.
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var dVtq = _mxAccessClient.ReadAsync(alarmInfo.DescAttrNameTagReference).GetAwaiter()
|
||||
.GetResult();
|
||||
if (dVtq.Value is string desc && !string.IsNullOrEmpty(desc))
|
||||
message = desc;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Keep the previously cached message when refresh reads fail.
|
||||
}
|
||||
// Use cached values from subscription data changes instead of blocking reads
|
||||
severity = alarmInfo.CachedSeverity > 0 ? alarmInfo.CachedSeverity : (ushort?)null;
|
||||
message = !string.IsNullOrEmpty(alarmInfo.CachedMessage)
|
||||
? alarmInfo.CachedMessage
|
||||
: null;
|
||||
}
|
||||
|
||||
pendingAlarmEvents.Add((address, alarmInfo, newInAlarm, severity, message));
|
||||
|
||||
Reference in New Issue
Block a user