fix: preserve SDK role identity and continuation authority lifecycle

This commit is contained in:
Joseph Doherty
2026-11-10 10:29:23 -05:00
parent a5bed28765
commit b71620145b
5 changed files with 105 additions and 43 deletions
@@ -698,6 +698,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
private volatile SemaphoreSlim? _historyReadBatchLimiter;
private readonly object _historyReadBatchLimiterLock = new();
private long _historyContinuationEpoch;
private volatile IHistoryContinuationStore _historyContinuationStore = new SessionHistoryContinuationStore();
/// <summary>
@@ -2315,6 +2316,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
var refuseArrayOnScalar = _arrayOnScalarRefused.ContainsKey(MapKey(node.NodeId));
var range = EvaluateEquipmentWriteRange(indexRange);
if (range is not null) return range;
if (value.TypeInfo.ValueRank > 1) return StatusCodes.BadTypeMismatch;
var structure = EvaluateEquipmentWriteStructure(SdkValueBoundary.ToClr(value), node, refuseArrayOnScalar, out var coerced);
if (structure is not null) return structure;
value = SdkValueBoundary.FromClr(coerced, node is BaseDataVariableState { DataType: var dt, ValueRank: ValueRanks.OneDimension } && dt == DataTypeIds.Byte);
@@ -3688,6 +3690,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
lock (Lock)
{
Interlocked.Increment(ref _historyContinuationEpoch);
// Review 01 L2 — every monitored item on a node this rebuild discards is told and forgotten BEFORE
// the nodes go, so a later create on the same NodeId binds to the re-created node (see
// TombstoneMonitoredNode). The shared root survives the rebuild and keeps its items.
@@ -3812,6 +3815,8 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// caller (AddressSpaceApplier) falls back to a full rebuild (resync). Mirrors the per-node
// teardown inside RebuildAddressSpace, scoped to this one id.
if (!_variables.TryRemove(key, out var variable)) return false;
// Conservatively invalidate every outstanding history cursor on topology removal.
Interlocked.Increment(ref _historyContinuationEpoch);
UnindexChild(key);
TombstoneMonitoredNode(variable.NodeId, UtcNow);
// 2026-08-15 review I3: sever the INVERSE half of every cross-tree Organizes edge this node holds
@@ -3880,6 +3885,8 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// lingering notifier folder is harmless (RemoveEquipmentSubtree demotes it when the whole
// equipment goes).
if (!_alarmConditions.TryRemove(key, out var condition)) return false;
// Conservatively invalidate every outstanding history cursor on topology removal.
Interlocked.Increment(ref _historyContinuationEpoch);
UnindexChild(key);
TombstoneMonitoredSubtree(condition, UtcNow);
// unwire this condition's extra equipment-folder notifiers bidirectionally BEFORE
@@ -3908,6 +3915,8 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
// Unknown equipment id ⇒ map drift ⇒ false (caller rebuilds).
if (!_folders.ContainsKey(equipKey)) return false;
// Conservatively invalidate every outstanding history cursor on topology removal.
Interlocked.Increment(ref _historyContinuationEpoch);
// Review 01 P2 — walk the parent→children index the materialise calls maintain instead of
// prefix-scanning every map (variables, conditions, folders, notifier folders, event sources and
@@ -4868,9 +4877,9 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (e is AuditEventState && !IsAuditDeliverableTo(item)) continue;
if (scopes is not null && !IsConditionVisibleTo(item, scopes)) continue;
if (ServiceResult.IsBad(ValidateEventRolePermissions(item, e))) continue;
if (reportingSession is not null
if (reportingSession is { IsNull: false } reportingId
&& item.Session?.Id is { } own
&& !own.Equals(reportingSession))
&& !own.Equals(reportingId))
{
continue;
}
@@ -5825,6 +5834,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
IList<ServiceResult> errors,
CancellationToken ct)
{
var historyEpoch = Volatile.Read(ref _historyContinuationEpoch);
var inboundCp = nodesToRead[handle.Index].ContinuationPoint;
var resumed = inboundCp is { Length: > 0 };
// One snapshot of the (hot-swappable) source for the whole read: the capability check and every call must agree.
@@ -5844,7 +5854,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// for THIS node only.
var state = _historyContinuationStore.TryTake(session, inboundCp.ToArray());
// A point is bound to the node that issued it: another node's source name is a miss too.
if (state is null || state.Kind != HistoryCursorKind.Events || state.Tagname != (sourceName ?? string.Empty))
if (state is null || !HistoryContinuationMatches(state, handle.NodeId, historyEpoch) || state.Kind != HistoryCursorKind.Events || state.Tagname != (sourceName ?? string.Empty))
{
errors[handle.Index] = StatusCodes.BadContinuationPointInvalid;
results[handle.Index] = new SdkHistoryReadResult { StatusCode = StatusCodes.BadContinuationPointInvalid };
@@ -6019,7 +6029,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// boundary skip) is exactly as without a filter: a page may come back with fewer events — even none — and a
// continuation point, never skipping or repeating one.
var historyEvent = ProjectEvents(events, selectClauses, where, filterContext);
var outboundCp = pendingCursor is null ? null : _historyContinuationStore.Save(session, pendingCursor);
var outboundCp = pendingCursor is null ? null : SaveHistoryContinuation(session, handle.NodeId, historyEpoch, pendingCursor);
results[handle.Index] = new SdkHistoryReadResult
{
// No events ⇒ GoodNoData (the notifier is historized, the window just held no events — or none matched).
@@ -6888,6 +6898,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
CancellationToken ct,
bool returnBounds = false)
{
var historyEpoch = Volatile.Read(ref _historyContinuationEpoch);
var inboundCp = nodesToRead[handle.Index].ContinuationPoint;
try
@@ -6904,7 +6915,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
// Resume read: take the stored cursor. A miss (unknown / evicted / malformed point) ⇒
// BadContinuationPointInvalid for THIS node.
var state = _historyContinuationStore.TryTake(session, inboundCp.ToArray());
if (state is null || state.Kind != HistoryCursorKind.Raw
if (state is null || !HistoryContinuationMatches(state, handle.NodeId, historyEpoch) || state.Kind != HistoryCursorKind.Raw
|| !TryGetHistorizedTagname(handle.NodeId, out var boundTag) || boundTag != state.Tagname)
{
errors[handle.Index] = StatusCodes.BadContinuationPointInvalid;
@@ -6971,7 +6982,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
return null;
}
return _historyContinuationStore.Save(session, new HistoryContinuationState(
return SaveHistoryContinuation(session, handle.NodeId, historyEpoch, new HistoryContinuationState(
tagname, previous, endUtc, 0, numValuesPerNode, SegmentWidthTicks: walkHint.WidthTicks,
EmptyFromTicks: walkHint.EmptyFromTicks, EmptyToTicks: walkHint.EmptyToTicks,
PendingEndBound: HistoryBounds.Carry(endBound!, page)! with { DataExhausted = true }));
@@ -7037,7 +7048,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (startBound is not null && numValuesPerNode == 1 && anything)
{
// The page is the start bound alone; the data starts at StartTime on the next page.
var boundOnlyCp = _historyContinuationStore.Save(session, new HistoryContinuationState(
var boundOnlyCp = SaveHistoryContinuation(session, handle.NodeId, historyEpoch, new HistoryContinuationState(
tagname, startUtc, endUtc, 0, numValuesPerNode, SegmentWidthTicks: walkHint.WidthTicks,
EmptyFromTicks: walkHint.EmptyFromTicks, EmptyToTicks: walkHint.EmptyToTicks,
PendingEndBound: HistoryBounds.Carry(pending, [startBound])));
@@ -7224,7 +7235,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
EmptyFromTicks: walkHint.EmptyFromTicks, EmptyToTicks: walkHint.EmptyToTicks,
PendingEndBound: HistoryBounds.Carry(endBound, slice),
WithinTieCluster: resumeAt == startUtc);
clusterCp = _historyContinuationStore.Save(session, clusterState);
clusterCp = SaveHistoryContinuation(session, handle.NodeId, historyEpoch, clusterState);
}
else if (endBound is not null)
{
@@ -7279,7 +7290,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
// Save may return null (no session on this request) ⇒ degrade to single-shot for this node.
// Built AFTER historyData so a failure projecting samples can never orphan a stored cursor.
outboundCp = _historyContinuationStore.Save(session, nextState);
outboundCp = SaveHistoryContinuation(session, handle.NodeId, historyEpoch, nextState);
}
else if (endBound is not null)
{
@@ -7366,6 +7377,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
uint numValuesPerNode,
CancellationToken ct)
{
var historyEpoch = Volatile.Read(ref _historyContinuationEpoch);
var inboundCp = nodesToRead[handle.Index].ContinuationPoint;
try
{
@@ -7381,7 +7393,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (inboundCp is { Length: > 0 })
{
var state = _historyContinuationStore.TryTake(session, inboundCp.ToArray());
if (state is null || state.Kind != HistoryCursorKind.Modified || state.Tagname != tagname)
if (state is null || !HistoryContinuationMatches(state, handle.NodeId, historyEpoch) || state.Kind != HistoryCursorKind.Modified || state.Tagname != tagname)
{
errors[handle.Index] = StatusCodes.BadContinuationPointInvalid;
results[handle.Index] = new SdkHistoryReadResult { StatusCode = StatusCodes.BadContinuationPointInvalid };
@@ -7411,7 +7423,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (page.HasMore && numValuesPerNode > 0 && page.Values.Count > 0)
{
var last = page.Values[^1];
outboundCp = _historyContinuationStore.Save(session, new HistoryContinuationState(
outboundCp = SaveHistoryContinuation(session, handle.NodeId, historyEpoch, new HistoryContinuationState(
tagname, last.SourceTimeUtc, endUtc, 0, numValuesPerNode,
HistoryCursorKind.Modified, last.ModifiedId, startUtc));
}
@@ -7489,6 +7501,18 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
}
}
private byte[]? SaveHistoryContinuation(ISession? session, NodeId nodeId, long epoch, HistoryContinuationState state)
{
if (epoch != Volatile.Read(ref _historyContinuationEpoch)) return null;
return _historyContinuationStore.Save(session, state with
{
BoundNodeKey = nodeId.ToString(), AddressSpaceEpoch = epoch,
});
}
private static bool HistoryContinuationMatches(HistoryContinuationState state, NodeId nodeId, long epoch) =>
state.BoundNodeKey == nodeId.ToString() && state.AddressSpaceEpoch == epoch;
/// <summary>Transport failures are retryable; unsupported operations retain their distinct status.</summary>
private static StatusCode HistoryReadFailureStatus(Exception error)
{