fix(galaxy): #991 review — a re-advise pass never dials on a swapped session; dropped-event resync is rate-limited and coalesced
- ReadviseSubscriptionAsync: a reconnect completing between two subscriptions of a pass (dropped-event resync loop had no per-iteration check) sent the next SubscribeBulk to the NEW gateway session under the OLD session number; TryRebind refused it (SessionChanged) and the live new-session advises were never bound or released, then duplicated by the replay. The pass now refuses to dial once its session moved. Releasing afterwards was rejected: once the session moved, which session issued the reply's handles is ambiguous and old numbers are reused (#861); not dialling is the unambiguous fix. - Dropped-event resync passes are at least 5 s apart (DropResyncMinInterval); drops reported meanwhile coalesce into the next pass, so repeated overflow bursts no longer churn re-advises into the overflowing channel. - Test seams: ResyncDroppedHandlesForTestsAsync, NotifyEventsDroppedForTests, DropResyncMinIntervalForTests; AdvisingSubscriber gained after-subscribe/unsubscribe hooks.
This commit is contained in:
@@ -235,7 +235,7 @@ Every (re)open of `StreamAlarms` replays the gateway's active-alarm snapshot. `G
|
||||
|
||||
- **MXAccess `ClientName` collisions**: two OtOpcUa instances sharing a `ClientName` cause the older Wonderware session to lose subscription state. Redundancy pairs (decision #149) enforce uniqueness via install scripts.
|
||||
- **Channel saturation**: `galaxy.events.dropped > 0` indicates `EventPump` is back-pressured. Raise `EventPumpChannelCapacity` or investigate downstream slowness in the server-side fan-out.
|
||||
- **A dropped change is re-read (#991 G-4).** MXAccess sends a value only when it changes, so a data change the full channel drops (newest-dropped) used to leave its tag at the previous value, Good, until the attribute next changed. The pump records the item handles of dropped data changes and, once its channel has drained (so the resync is not lost to the same congestion), the driver re-advises exactly those tags — an advise is answered with the current value — rebinds them, and releases the superseded handles. One coalescing background worker; serialised with the replay-gap re-snapshot. The `galaxy.events.dropped` counter is unchanged; each burst logs one Warning ("re-reading them") and one Information line with the re-advised count.
|
||||
- **A dropped change is re-read (#991 G-4).** MXAccess sends a value only when it changes, so a data change the full channel drops (newest-dropped) used to leave its tag at the previous value, Good, until the attribute next changed. The pump records the item handles of dropped data changes and, once its channel has drained (so the resync is not lost to the same congestion), the driver re-advises exactly those tags — an advise is answered with the current value — rebinds them, and releases the superseded handles. One coalescing background worker; serialised with the replay-gap re-snapshot. The `galaxy.events.dropped` counter is unchanged; each burst logs one Warning ("re-reading them") and one Information line with the re-advised count. **Rate-limited (#991 review):** passes are at least 5 s apart and drops reported meanwhile coalesce into the next pass — repeated overflow bursts used to start a pass as soon as the previous one ended, re-advising (and pushing initial values into) the very channel that was overflowing. **A pass whose gateway session is gone no longer dials:** a reconnect completing between two subscriptions of a re-advise pass (resync, replay-gap re-snapshot, reconnect replay) used to send the next SubscribeBulk to the NEW session under the OLD session number; the rebind refused it and those live advises were never bound or released (then duplicated by the new session's replay). They cannot simply be released afterwards — once the session has moved, which session issued a reply's handles is ambiguous and old numbers are reused (#861) — so the pass stops before dialling.
|
||||
- **OPC DA limit bits do not warn (#991 G-2).** `StatusCodeMap.FromQualityByte` masks the two limit bits (`QQSSSSLL`) before its table lookup, so `Good | LowLimited` (0xC1) and friends map like the unlimited quality. A byte still unlisted after the mask logs one Warning per distinct value per process — it used to log one per event on the pump's dispatch thread, enough to back the channel up into drops.
|
||||
- **The deploy watcher paces every stream end (#991 G-3).** A `WatchDeployEvents` stream the gateway completes cleanly is re-subscribed after the same capped exponential backoff as a faulted one (1 s doubling to 30 s), and the backoff resets only after a stream stayed open 60 s — an event alone (the bootstrap every stream delivers) no longer resets it. It used to re-open immediately, hammering a gateway that ends its streams after the bootstrap.
|
||||
- **Secured / verified writes are REFUSED `BadUserAccessDenied`, locally (#991 G-1 + review).** The driver has no
|
||||
|
||||
@@ -1650,6 +1650,16 @@ public sealed class GalaxyDriver
|
||||
IReadOnlyList<SubscribeResult> results;
|
||||
using (_subscriptions.BeginPendingSubscribe())
|
||||
{
|
||||
// #991 review: a pass whose session is gone must not dial. A reconnect that completes between two
|
||||
// subscriptions of one pass (the dropped-event resync loop, or the replay loop between its own check and
|
||||
// here) left this SubscribeBulk going to the NEW gateway session under the OLD session number: the rebind
|
||||
// below refused it (SessionChanged) and the fresh, live advises were never bound or released — leaked on
|
||||
// the gateway, then duplicated by the new session's replay. Releasing them afterwards is not an option in
|
||||
// general: once the session has moved, which session issued a reply's handles is ambiguous, and an
|
||||
// old-session number may be reused by the new one (#861). Not dialling is the only unambiguous fix. The
|
||||
// check and the dial are synchronous back to back; ReopenAsync's session swap awaits network I/O between
|
||||
// its two fences, so a swap cannot complete in between.
|
||||
if (_subscriptions.CurrentSession != session) return (false, 0);
|
||||
results = await _subscriber!
|
||||
.SubscribeBulkAsync(refs, _options.MxAccess.PublishingIntervalMs, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
@@ -2942,6 +2952,41 @@ public sealed class GalaxyDriver
|
||||
private int _droppedSession;
|
||||
private int _dropResyncRunning;
|
||||
|
||||
/// <summary>
|
||||
/// #991 review: the floor between two dropped-event resync passes. Repeated overflow bursts used to start a
|
||||
/// pass as soon as the previous one finished — re-advising (and pushing initial values into) the very channel
|
||||
/// that was overflowing. Drops reported inside the window coalesce into the next pass.
|
||||
/// </summary>
|
||||
internal static readonly TimeSpan DefaultDropResyncMinInterval = TimeSpan.FromSeconds(5);
|
||||
|
||||
private TimeSpan _dropResyncMinInterval = DefaultDropResyncMinInterval;
|
||||
|
||||
// Stopwatch timestamp of the last resync pass start (0 = none yet). Worker-owned.
|
||||
private long _lastDropResyncTimestamp;
|
||||
|
||||
/// <summary>Test seam: the floor between two dropped-event resync passes.</summary>
|
||||
internal TimeSpan DropResyncMinIntervalForTests
|
||||
{
|
||||
get => _dropResyncMinInterval;
|
||||
set => _dropResyncMinInterval = value;
|
||||
}
|
||||
|
||||
/// <summary>Test seam: report dropped events for <paramref name="itemHandles"/> as the current pump would.</summary>
|
||||
/// <param name="itemHandles">The handles whose events were dropped.</param>
|
||||
internal void NotifyEventsDroppedForTests(IReadOnlyCollection<int> itemHandles)
|
||||
{
|
||||
EventPump? pump;
|
||||
lock (_pumpLock) pump = _eventPump;
|
||||
OnEventPumpEventsDropped(pump, itemHandles);
|
||||
}
|
||||
|
||||
/// <summary>Test seam: one dropped-event resync pass, run inline.</summary>
|
||||
/// <param name="itemHandles">The dropped handles.</param>
|
||||
/// <param name="session">The session that issued them.</param>
|
||||
/// <returns>A task completing when the pass does.</returns>
|
||||
internal Task ResyncDroppedHandlesForTestsAsync(IReadOnlyCollection<int> itemHandles, int session) =>
|
||||
ResyncDroppedHandlesAsync(itemHandles, session);
|
||||
|
||||
// Serialises the same-session re-advise passes (replay-gap re-snapshot, dropped-event resync): both rebind a
|
||||
// subscription from a snapshot of its bindings, so two interleaved passes could rebind over each other and
|
||||
// release handles the other one bound.
|
||||
@@ -2976,6 +3021,15 @@ public sealed class GalaxyDriver
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// Rate limit (#991 review): at most one pass per DropResyncMinInterval; drops reported while waiting
|
||||
// join the pending set and ride the next pass together.
|
||||
var last = Volatile.Read(ref _lastDropResyncTimestamp);
|
||||
if (last != 0)
|
||||
{
|
||||
var wait = _dropResyncMinInterval - System.Diagnostics.Stopwatch.GetElapsedTime(last);
|
||||
if (wait > TimeSpan.Zero) await Task.Delay(wait).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
int[] batch;
|
||||
int batchSession;
|
||||
lock (_droppedLock)
|
||||
@@ -2990,6 +3044,7 @@ public sealed class GalaxyDriver
|
||||
_droppedPending.Clear();
|
||||
batchSession = _droppedSession;
|
||||
}
|
||||
Volatile.Write(ref _lastDropResyncTimestamp, System.Diagnostics.Stopwatch.GetTimestamp());
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
+81
@@ -141,6 +141,79 @@ public sealed class DroppedEventResyncTests
|
||||
await TestWait.UntilAsync(() => values.Contains(("Tank.B", 9.0)), "data must flow on the post-gap handles");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #991 review: a reconnect that completes in the MIDDLE of a multi-subscription re-advise pass (here: between
|
||||
/// the dropped-event resync's first and second subscription) made the pass dial its next SubscribeBulk on the
|
||||
/// NEW gateway session with the OLD session number — the rebind then refused it (SessionChanged) and the fresh,
|
||||
/// live new-session advises were never bound nor released: a leak on the gateway, doubled by the replay that
|
||||
/// re-advises the same tags. A pass whose session is gone must not dial at all.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_session_swap_mid_resync_leaves_no_unbound_new_session_advises()
|
||||
{
|
||||
var subscriber = new AdvisingSubscriber();
|
||||
subscriber.Current["Tank.A"] = 1.0;
|
||||
subscriber.Current["Tank.B"] = 2.0;
|
||||
using var driver = NewDriver(subscriber);
|
||||
await driver.SubscribeAsync(["Tank.A"], TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
await driver.SubscribeAsync(["Tank.B"], TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
var hA = subscriber.LiveHandlesFor("Tank.A").ShouldHaveSingleItem();
|
||||
var hB = subscriber.LiveHandlesFor("Tank.B").ShouldHaveSingleItem();
|
||||
var session = driver.SubscriptionsForTests.CurrentSession;
|
||||
|
||||
var swapped = 0;
|
||||
var createdAfterSwap = new ConcurrentQueue<int>();
|
||||
subscriber.AfterSubscribe = handles =>
|
||||
{
|
||||
if (Volatile.Read(ref swapped) == 1) foreach (var h in handles) createdAfterSwap.Enqueue(h);
|
||||
};
|
||||
// Tank.A's re-advise ends by releasing hA: the reconnect completes right there, before Tank.B's re-advise.
|
||||
subscriber.AfterUnsubscribe = handles =>
|
||||
{
|
||||
if (handles.Contains(hA) && Interlocked.Exchange(ref swapped, 1) == 0) driver.SubscriptionsForTests.StartNewSession();
|
||||
};
|
||||
|
||||
await driver.ResyncDroppedHandlesForTestsAsync([hA, hB], session);
|
||||
|
||||
Volatile.Read(ref swapped).ShouldBe(1, "the swap must have happened mid-pass");
|
||||
createdAfterSwap.Where(h => !subscriber.Unsubscribed.Contains(h))
|
||||
.ShouldBeEmpty("no advise issued after the swap may be left unbound and unreleased");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #991 review: repeated overflow bursts each started a resync pass as soon as the previous one finished —
|
||||
/// re-advising (and pushing initial values into) the very channel that was overflowing. Passes are now at least
|
||||
/// <c>DropResyncMinInterval</c> apart, and the drops reported meanwhile coalesce into the next pass.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Repeated_drop_bursts_are_rate_limited_and_coalesced()
|
||||
{
|
||||
var subscriber = new AdvisingSubscriber();
|
||||
subscriber.Current["Tank.A"] = 1.0;
|
||||
using var driver = NewDriver(subscriber);
|
||||
driver.DropResyncMinIntervalForTests = TimeSpan.FromSeconds(1.5);
|
||||
await driver.SubscribeAsync(["Tank.A"], TimeSpan.Zero, TestContext.Current.CancellationToken);
|
||||
var baseline = subscriber.SubscribeCalls;
|
||||
|
||||
driver.NotifyEventsDroppedForTests([subscriber.LiveHandlesFor("Tank.A").Single()]);
|
||||
await TestWait.UntilAsync(() => subscriber.SubscribeCalls == baseline + 1, "the first burst resyncs at once");
|
||||
await TestWait.UntilAsync(() => subscriber.LiveHandlesFor("Tank.A").Length == 1, "superseded handle released");
|
||||
|
||||
// Five more bursts in quick succession, each against the tag's current handle.
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
driver.NotifyEventsDroppedForTests([subscriber.LiveHandlesFor("Tank.A").Single()]);
|
||||
await Task.Delay(50, TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
await Task.Delay(500, TestContext.Current.CancellationToken);
|
||||
subscriber.SubscribeCalls.ShouldBe(baseline + 1, "no second pass inside the minimum interval");
|
||||
|
||||
await TestWait.UntilAsync(() => subscriber.SubscribeCalls == baseline + 2, "the coalesced bursts resync once the interval passes");
|
||||
await Task.Delay(500, TestContext.Current.CancellationToken);
|
||||
subscriber.SubscribeCalls.ShouldBe(baseline + 2, "five bursts cost one pass, not five");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Behaves like the gateway + MXAccess for the parts these tests need: every SubscribeBulk issues fresh item
|
||||
/// handles and pushes each one's current value (an advise's initial value), a change is pushed on every live
|
||||
@@ -185,6 +258,12 @@ public sealed class DroppedEventResyncTests
|
||||
SourceTimestamp = Timestamp.FromDateTime(DateTime.UtcNow),
|
||||
}).ShouldBeTrue();
|
||||
|
||||
/// <summary>Called with the handles each SubscribeBulk issued, after issuing them.</summary>
|
||||
public Action<IReadOnlyList<int>>? AfterSubscribe { get; set; }
|
||||
|
||||
/// <summary>Called with the handles each UnsubscribeBulk retired, after retiring them.</summary>
|
||||
public Action<IReadOnlyList<int>>? AfterUnsubscribe { get; set; }
|
||||
|
||||
public Task<IReadOnlyList<SubscribeResult>> SubscribeBulkAsync(
|
||||
IReadOnlyList<string> fullReferences, int bufferedUpdateIntervalMs, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -197,6 +276,7 @@ public sealed class DroppedEventResyncTests
|
||||
results.Add(new SubscribeResult { TagAddress = r, ItemHandle = handle, WasSuccessful = true });
|
||||
if (Current.TryGetValue(r, out var v)) Emit(handle, v); // the advise's initial value
|
||||
}
|
||||
AfterSubscribe?.Invoke([.. results.Select(x => x.ItemHandle)]);
|
||||
return Task.FromResult<IReadOnlyList<SubscribeResult>>(results);
|
||||
}
|
||||
|
||||
@@ -207,6 +287,7 @@ public sealed class DroppedEventResyncTests
|
||||
Unsubscribed.Enqueue(h);
|
||||
_live.TryRemove(h, out _);
|
||||
}
|
||||
AfterUnsubscribe?.Invoke(itemHandles);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user