perf(worker): alarm subtag source and ack writer tear down with Unregister alone (MXA-010, #256)

This commit is contained in:
Joseph Doherty
2026-12-03 09:54:11 -05:00
parent 04c772268a
commit c30ba69a6d
4 changed files with 32 additions and 27 deletions
@@ -388,6 +388,25 @@ public sealed class AlarmAckWriterTests
Assert.Equal(1UL, cache.CurrentVersion(8, 100)); Assert.Equal(1UL, cache.CurrentVersion(8, 100));
} }
[Fact]
public void Dispose_UnregistersOnceWithoutPerItemTeardown()
{
RecordingAckServer server = new RecordingAckServer { AuthenticateResult = 5 };
MxAccessWriteCompletionCache cache = new MxAccessWriteCompletionCache();
server.OnWrite = (handle, item) => cache.Record(handle, item, Rows(MxStatusCategory.Ok, 0));
AlarmAckWriter writer = new AlarmAckWriter(server, cache, () => { }, TimeSpan.FromSeconds(1), "MxGateway");
writer.Acknowledge("A.X.AckMsg", "1", Alice());
writer.Acknowledge("B.X.AckMsg", "2", Alice());
int unAdviseBefore = server.Operations.Count(op => op == "UnAdvise");
writer.Dispose();
// MXA-010: Unregister drops every cached ack item in one call.
Assert.Equal(1, server.Operations.Count(op => op == "Unregister"));
Assert.Equal(unAdviseBefore, server.Operations.Count(op => op == "UnAdvise"));
Assert.Empty(server.RemovedItemHandles);
}
[Fact] [Fact]
public void Acknowledge_WhenWriteDisconnected_ForgetsServerCompletions() public void Acknowledge_WhenWriteDisconnected_ForgetsServerCompletions()
{ {
@@ -94,26 +94,22 @@ public sealed class LmxSubtagAlarmSourceTests
} }
/// <summary> /// <summary>
/// Verifies <see cref="LmxSubtagAlarmSource.Dispose"/> UnAdvises and /// Verifies <see cref="LmxSubtagAlarmSource.Dispose"/> unregisters the server exactly once and
/// removes every advised handle and unregisters the server exactly once. /// makes no per-item UnAdvise/RemoveItem calls: <c>Unregister</c> already drops every item under
/// the handle (MXA-010).
/// </summary> /// </summary>
[Fact] [Fact]
public void Dispose_UnAdvisesAndRemovesAdvisedHandles_AndUnregistersOnce() public void Dispose_UnregistersOnceWithoutPerItemTeardown()
{ {
var server = new RecordingMxAccessServer(); var server = new RecordingMxAccessServer();
var source = new LmxSubtagAlarmSource(server, FakeServerHandle); var source = new LmxSubtagAlarmSource(server, FakeServerHandle);
source.Advise(new[] { "Tank1.Alarm.Subtag", "Tank2.Alarm.Subtag" }); source.Advise(new[] { "Tank1.Alarm.Subtag", "Tank2.Alarm.Subtag" });
int advised1 = server.LastItemHandleFor("Tank1.Alarm.Subtag");
int advised2 = server.LastItemHandleFor("Tank2.Alarm.Subtag");
source.Dispose(); source.Dispose();
Assert.Equal(new[] { advised1, advised2 }, server.UnAdvisedItemHandles); Assert.Empty(server.UnAdvisedItemHandles);
Assert.Equal( Assert.Empty(server.RemovedItemHandles);
new[] { advised1, advised2 }.OrderBy(h => h),
server.RemovedItemHandles.OrderBy(h => h));
Assert.Equal(1, server.UnregisterCount); Assert.Equal(1, server.UnregisterCount);
Assert.Empty(server.Writes); Assert.Empty(server.Writes);
} }
@@ -318,12 +318,9 @@ public sealed class AlarmAckWriter : IAlarmAckWriter
IMxAccessServer? mx = server; IMxAccessServer? mx = server;
if (mx is not null) if (mx is not null)
{ {
foreach (AckItem item in itemLru) // Unregister drops every cached ack item and its advice in one call (the teardown
{ // MxAccessSession relies on); per-item UnAdvise/RemoveItem ahead of it were redundant COM
try { mx.UnAdvise(serverHandle, item.Handle); } catch { /* best effort */ } // round trips on the STA. LRU eviction of a single item still removes it individually.
try { mx.RemoveItem(serverHandle, item.Handle); } catch { /* best effort */ }
}
try { mx.Unregister(serverHandle); } catch { /* best effort */ } try { mx.Unregister(serverHandle); } catch { /* best effort */ }
} }
@@ -197,17 +197,10 @@ public sealed class LmxSubtagAlarmSource : ISubtagAlarmSource
IMxAccessServer? mxServer = server; IMxAccessServer? mxServer = server;
if (mxServer is not null && registered) if (mxServer is not null && registered)
{ {
foreach (KeyValuePair<int, string> entry in addressesByItemHandle) // Unregister alone drops every item (and its advice) under the server handle — the same
{ // teardown MxAccessSession relies on. Walking the items first cost three COM calls per
// Only UnAdvise handles that were actually advised. // watched alarm on the STA (thousands for a large watch list) ahead of the one call that
if (advisedItemHandles.Contains(entry.Key)) // makes them redundant, with data callbacks and commands queued behind them.
{
try { mxServer.UnAdvise(serverHandle, entry.Key); } catch { /* swallow — best effort */ }
}
try { mxServer.RemoveItem(serverHandle, entry.Key); } catch { /* swallow — best effort */ }
}
try { mxServer.Unregister(serverHandle); } catch { /* swallow — best effort */ } try { mxServer.Unregister(serverHandle); } catch { /* swallow — best effort */ }
} }