fix(galaxy): invalidate writer handle caches on session reconnect
Add IGalaxyDataWriter.InvalidateHandleCaches() and call it in GalaxyDriver.ReopenAsync after RecreateAsync succeeds. Prior to this fix, GatewayGalaxyDataWriter's _itemHandles and _supervisedHandles dictionaries survived across reconnects, causing the next write to skip AddItem and AdviseSupervisory against already-dead handles.
This commit is contained in:
@@ -293,6 +293,9 @@ public sealed class GalaxyDriver
|
||||
if (_ownedMxSession is null) return;
|
||||
var clientOptions = BuildClientOptions(_options.Gateway);
|
||||
await _ownedMxSession.RecreateAsync(clientOptions, cancellationToken).ConfigureAwait(false);
|
||||
// The recreated session invalidates every prior gw item handle; drop the writer's handle/advise
|
||||
// caches so the next write re-AddItems + re-AdviseSupervisory against the fresh session.
|
||||
_dataWriter?.InvalidateHandleCaches();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -42,6 +42,42 @@ public sealed class GatewayGalaxyDataWriter : IGalaxyDataWriter
|
||||
_logger = logger ?? NullLogger.Instance;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void InvalidateHandleCaches()
|
||||
{
|
||||
_itemHandles.Clear();
|
||||
_supervisedHandles.Clear();
|
||||
}
|
||||
|
||||
// ===== Test seams (internal — not part of the public contract) =====
|
||||
|
||||
/// <summary>
|
||||
/// Count of item-handle cache entries. Zero on a fresh instance or immediately after
|
||||
/// <see cref="InvalidateHandleCaches"/>. Used by unit tests to verify cache state
|
||||
/// without running a real gRPC round-trip.
|
||||
/// </summary>
|
||||
internal int CachedItemHandleCount => _itemHandles.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Count of supervisory-advised handle entries. Zero on a fresh instance or immediately
|
||||
/// after <see cref="InvalidateHandleCaches"/>. Used by unit tests to verify cache state.
|
||||
/// </summary>
|
||||
internal int CachedSupervisedHandleCount => _supervisedHandles.Count;
|
||||
|
||||
/// <summary>
|
||||
/// Pre-populate both caches as if a write had already occurred. Used by unit tests to
|
||||
/// simulate the "post-write" state without running a real gRPC gateway session (the SDK
|
||||
/// session types are sealed + internal-ctor and cannot be faked).
|
||||
/// </summary>
|
||||
/// <param name="fullRef">The tag full reference to add to the item-handle cache.</param>
|
||||
/// <param name="itemHandle">The item handle to cache for that reference.</param>
|
||||
/// <param name="supervised">When true, also records the handle in the supervised-handle cache.</param>
|
||||
internal void SeedHandleCachesForTest(string fullRef, int itemHandle, bool supervised)
|
||||
{
|
||||
_itemHandles[fullRef] = itemHandle;
|
||||
if (supervised) _supervisedHandles.TryAdd(itemHandle, 0);
|
||||
}
|
||||
|
||||
/// <summary>Writes values to Galaxy tags through the gateway.</summary>
|
||||
/// <param name="writes">The write requests.</param>
|
||||
/// <param name="securityResolver">Function to resolve security classification per tag.</param>
|
||||
|
||||
@@ -30,4 +30,8 @@ public interface IGalaxyDataWriter
|
||||
IReadOnlyList<WriteRequest> writes,
|
||||
Func<string, SecurityClassification> securityResolver,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>Drop cached gateway item handles + supervisory-advise state. Call after a session
|
||||
/// reconnect — the prior handles are dead, so the next write must re-AddItem + re-AdviseSupervisory.</summary>
|
||||
void InvalidateHandleCaches();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,10 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
||||
/// </summary>
|
||||
internal sealed class TracedGalaxyDataWriter(IGalaxyDataWriter inner, string clientName) : IGalaxyDataWriter
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <remarks>No span — this is a local cache-clear operation, not a gateway round-trip.</remarks>
|
||||
public void InvalidateHandleCaches() => inner.InvalidateHandleCaches();
|
||||
|
||||
/// <summary>Writes data to Galaxy while recording telemetry span.</summary>
|
||||
/// <param name="writes">The list of write requests to process.</param>
|
||||
/// <param name="securityResolver">Function to resolve security classification for tag references.</param>
|
||||
|
||||
Reference in New Issue
Block a user