From 04c772268a0bc0de21d498d125ce9fc1901a2338 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:53:39 -0500 Subject: [PATCH] perf(worker): wnwrap poll skips the parse and diff when the alarm XML is unchanged (MXA-008, #256) --- .../MxAccess/WnWrapAlarmConsumerXmlTests.cs | 25 +++++++ .../MxAccess/WnWrapAlarmConsumer.cs | 69 +++++++++++++++++-- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/WnWrapAlarmConsumerXmlTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/WnWrapAlarmConsumerXmlTests.cs index 555cc9ae..77ec3b32 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/WnWrapAlarmConsumerXmlTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/WnWrapAlarmConsumerXmlTests.cs @@ -724,6 +724,31 @@ public sealed class WnWrapAlarmConsumerXmlTests // out of one call, produced under one lock acquisition. // ------------------------------------------------------------------------- + /// + /// MXA-008: an alarm document identical to the previous poll's is not parsed or diffed again and + /// emits nothing; a changed document is parsed and diffed as before. + /// + [Fact] + public void ProcessFetchedXml_WhenTheDocumentIsUnchanged_SkipsTheParseAndEmitsNothing() + { + using WnWrapAlarmConsumer consumer = new WnWrapAlarmConsumer(1024); + string xml = BuildAlarmXml(3); + + IReadOnlyList first = consumer.ProcessFetchedXml(xml); + IReadOnlyList second = consumer.ProcessFetchedXml(new string(xml.ToCharArray())); + + Assert.NotEmpty(first); + Assert.Empty(second); + Assert.Equal(1, consumer.XmlParseCount); + Assert.Equal(3, consumer.SnapshotActiveAlarms(out bool truncated).Count); + Assert.False(truncated); + + IReadOnlyList changed = consumer.ProcessFetchedXml(BuildAlarmXml(4)); + Assert.Equal(2, consumer.XmlParseCount); + Assert.NotEmpty(changed); + Assert.Equal(4, consumer.SnapshotActiveAlarms(out _).Count); + } + /// /// A capped fetch sets the truncation verdict handed out with the /// snapshot. Without this the signal never leaves the consumer and the diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/WnWrapAlarmConsumer.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/WnWrapAlarmConsumer.cs index bb589a8b..9f645662 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/WnWrapAlarmConsumer.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/WnWrapAlarmConsumer.cs @@ -104,6 +104,9 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer private long lastTruncationWarningMilliseconds = -TruncationWarningIntervalMilliseconds; private long truncatedFetchCount; private bool lastSnapshotTruncated; + private string? lastFetchedXml; + private int lastFetchedRecordCount; + private int lastParsedRecordCount; private wwAlarmConsumerClass? client; private bool subscribed; private bool disposed; @@ -351,6 +354,9 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer /// internal Action? TruncationWarningSink { get; set; } + /// Gets how many fetched documents were actually parsed (diagnostic, for tests). + internal int XmlParseCount { get; private set; } + /// public void PollOnce() { @@ -367,7 +373,55 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer string xml = xmlObj?.ToString() ?? string.Empty; if (xml.Length == 0) return; - Dictionary next = ParseSnapshotXml(xml, out int fetchedRecordCount); + IReadOnlyList transitions = ProcessFetchedXml(xml); + + if (transitions.Count == 0) return; + EventHandler? handler = AlarmTransitionEmitted; + if (handler is null) return; + foreach (MxAlarmTransitionEvent transition in transitions) + { + handler.Invoke(this, transition); + } + } + + /// + /// Folds one fetched alarm XML document into the retained snapshot and returns the + /// transitions it implies. Split out of so the fetch-to-fold step is + /// testable without the wnwrap COM object. + /// + /// A document identical to the previous one cannot change the snapshot, the transitions + /// or the truncation verdict, so the parse (about 19 strings and a record per alarm) and + /// the diff are skipped; only the rate-limited truncation warning is still counted, so a + /// galaxy parked above the cap keeps being reported. The BSTR copy itself cannot be + /// avoided. + /// + /// + /// The non-empty GetXmlCurrentAlarms2 reply. + /// The transitions the fetch implies; empty when the document is unchanged. + internal IReadOnlyList ProcessFetchedXml(string xml) + { + int fetchedRecordCount; + int parsedCount; + bool truncated; + lock (syncRoot) + { + if (lastFetchedXml is not null && string.Equals(lastFetchedXml, xml, StringComparison.Ordinal)) + { + fetchedRecordCount = lastFetchedRecordCount; + parsedCount = lastParsedRecordCount; + truncated = lastSnapshotTruncated; + int retained = latestSnapshot.Count; + if (truncated) + { + WarnTruncatedFetch(fetchedRecordCount, parsedCount, retained); + } + + return Array.Empty(); + } + } + + Dictionary next = ParseSnapshotXml(xml, out fetchedRecordCount); + XmlParseCount++; // TRUNCATION CLIFF. GetXmlCurrentAlarms2 caps its reply at maxAlmCnt // and gives no *verified* "there is more" flag, so a reply holding @@ -381,7 +435,7 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer // matching the records in the reply every time. It reports the reply, // not the total, so it cannot tell a capped fetch from a complete one. // See docs/AlarmProbeFindings.md.) - bool truncated = IsTruncatedFetch(fetchedRecordCount, maxAlarmsPerFetch); + truncated = IsTruncatedFetch(fetchedRecordCount, maxAlarmsPerFetch); IReadOnlyList transitions = FoldFetch(next, truncated, out int retainedCount); @@ -391,13 +445,14 @@ public sealed class WnWrapAlarmConsumer : IMxAccessAlarmConsumer WarnTruncatedFetch(fetchedRecordCount, next.Count, retainedCount); } - if (transitions.Count == 0) return; - EventHandler? handler = AlarmTransitionEmitted; - if (handler is null) return; - foreach (MxAlarmTransitionEvent transition in transitions) + lock (syncRoot) { - handler.Invoke(this, transition); + lastFetchedXml = xml; + lastFetchedRecordCount = fetchedRecordCount; + lastParsedRecordCount = next.Count; } + + return transitions; } ///