test(grpc-data): H4 review 7 — diagnostics snapshot round-trips on the cluster serializer; old-shape payloads decode

Round-trips NodeDiagnosticsSnapshot (OriginHistory, HistoryVariants and the other H4 members) through
the serializer Akka picks for the fleet-diagnostics reply, and decodes payloads stripped of the H4
members. The origin flag is renamed NoPeerTrafficYet (default false): Akka's JSON serializer fills a
missing constructor argument with the CLR default, so a payload without it must never claim it.
This commit is contained in:
Joseph Doherty
2026-11-24 08:41:52 -05:00
parent 2fba036176
commit 7498f1ce82
6 changed files with 102 additions and 9 deletions
@@ -925,7 +925,7 @@
"project": "tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/ZB.MOM.WW.OtOpcUa.Runtime.Tests.csproj",
"filter": "(Feature=GrpcData&Phase=H4)|FullyQualifiedName~.GrpcDataProjectedHistoryCursorTests.|FullyQualifiedName~.GrpcDataOriginHistorySessionTests.",
"new_classes": [
"GrpcDataHistoryMetricsTests"
"GrpcDataHistoryMetricsTests", "GrpcDataDiagnosticsSerializationTests"
],
"existing_classes": [
"GrpcDataConfiguredDiscoveryWireTests",
@@ -57,9 +57,10 @@ public sealed record DataConnectionDiagnosticsSnapshot(
/// <param name="ReadsGood">Remote history items answered Good since process start.</param>
/// <param name="ReadsUncertain">Remote history items answered Uncertain since process start.</param>
/// <param name="ReadsBad">Remote history items answered Bad (including refusals) since process start.</param>
/// <param name="PeerServicesStarted">
/// False while no peer has called this node since start: the origin data services are created on the first peer call, so
/// the zeros are "no peer traffic yet", not missing evidence. True once they run (and from a sender that predates the flag).
/// <param name="NoPeerTrafficYet">
/// True while no peer has called this node since start: the origin data services are created on the first peer call, so
/// the zeros are "no peer traffic yet", not missing evidence. False once they run. Default false, so a payload without the
/// member (Akka's JSON serializer fills a missing constructor argument with the CLR default) never claims it.
/// </param>
public sealed record OriginHistoryDiagnostics(bool Available, int Sessions, int OpenContinuations, long ReadsGood,
long ReadsUncertain, long ReadsBad, bool PeerServicesStarted = true);
long ReadsUncertain, long ReadsBad, bool NoPeerTrafficYet = false);
@@ -53,7 +53,7 @@ else
<div class="small mb-2 data-connection-origin"><strong>@nodeId</strong>
@if (node.OriginHistory is { } origin)
{
<span> · remote history @(origin.Available ? "served" : "no historian backend") · peer history sessions @origin.Sessions · open origin continuations @origin.OpenContinuations · history items answered @DataConnectionDiagnostics.HistoryCounts(origin.ReadsGood, origin.ReadsUncertain, origin.ReadsBad)@(origin.PeerServicesStarted ? "" : " · no peer has called since start")</span>
<span> · remote history @(origin.Available ? "served" : "no historian backend") · peer history sessions @origin.Sessions · open origin continuations @origin.OpenContinuations · history items answered @DataConnectionDiagnostics.HistoryCounts(origin.ReadsGood, origin.ReadsUncertain, origin.ReadsBad)@(origin.NoPeerTrafficYet ? " · no peer has called since start" : "")</span>
}
else { <span> · remote history evidence unavailable</span> }
@if (node.DroppedExportOperations is { } dropped)
@@ -156,7 +156,7 @@ public sealed class DataConnectionHistoryDiagnostics
if (_originState is not { } state)
{
if (_provisionalAvailability is not { } provisional) return null;
try { return new(provisional(), 0, 0, 0, 0, 0, PeerServicesStarted: false); }
try { return new(provisional(), 0, 0, 0, 0, 0, NoPeerTrafficYet: true); }
catch (Exception) { return null; }
}
(bool Available, int Sessions, int Open) now;
@@ -0,0 +1,92 @@
using System.Text;
using Akka.TestKit.Xunit;
using Newtonsoft.Json.Linq;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
/// <summary>
/// H4 review item 7: the fleet-diagnostics reply crosses Akka remoting (<c>FleetDiagnosticsClient</c> asks the node's
/// driver host) on the serializer Akka picks for it — round-trip the new members through THAT serializer, and decode an
/// old-shape payload (a pre-H4 sender) to show mixed-version tolerance.
/// </summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H4")]
public sealed class GrpcDataDiagnosticsSerializationTests : TestKit
{
private static readonly string[] H4ConnectionMembers =
["NegotiatedFeatures", "HistoryVariants", "HistoryAggregateCount", "HistoryReadsGood", "HistoryReadsUncertain", "HistoryReadsBad",
"OpenRemoteContinuations"];
private static NodeDiagnosticsSnapshot Snapshot() => new(
// Arrays, as production builds them (CaptureDiagnostics .ToArray()s; RecordSelected builds string[]): a C# collection
// expression typed IReadOnlyList<T> is a compiler-synthesized type Newtonsoft cannot construct.
NodeId.Parse("consumer-1:4053"), null, Array.Empty<DriverInstanceDiagnostics>(), new DateTime(2026, 11, 24, 13, 0, 0, DateTimeKind.Utc),
DataConnections: new[]
{
new DataConnectionDiagnosticsSnapshot("link", 3, "ready", "https://source-2:4057", null, 0, 0, 7, "artifact-7",
new DateTime(2026, 11, 24, 13, 0, 0, DateTimeKind.Utc), DroppedUnknownOperations: DataOperations.None,
MappingsWithDroppedOperations: 0, NegotiatedFeatures: DataFeatures.History, HistoryVariants: new[] { "raw", "processed", "attime" },
HistoryAggregateCount: 19, HistoryReadsGood: 34, HistoryReadsUncertain: 1, HistoryReadsBad: 3, OpenRemoteContinuations: 2),
},
DroppedExportOperations: new Dictionary<string, DataOperations> { ["export-a"] = (DataOperations)(1 << 20) },
OriginHistory: new OriginHistoryDiagnostics(true, 1, 2, 7, 0, 2, NoPeerTrafficYet: true));
private (Akka.Serialization.Serializer Serializer, string Manifest) SerializerFor(object message)
{
var serializer = Sys.Serialization.FindSerializerFor(message);
return (serializer, Akka.Serialization.Serialization.ManifestFor(serializer, message));
}
[Fact]
public void A_snapshot_with_the_H4_members_round_trips_on_the_cluster_serializer()
{
var message = Snapshot();
var (serializer, manifest) = SerializerFor(message);
var round = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize(serializer.ToBinary(message), serializer.Identifier, manifest);
round.OriginHistory.ShouldBe(message.OriginHistory);
round.DroppedExportOperations!["export-a"].ShouldBe((DataOperations)(1 << 20));
var row = round.DataConnections.ShouldNotBeNull().ShouldHaveSingleItem();
row.NegotiatedFeatures.ShouldBe(DataFeatures.History);
row.HistoryVariants.ShouldBe(["raw", "processed", "attime"]);
(row.HistoryAggregateCount, row.HistoryReadsGood, row.HistoryReadsUncertain, row.HistoryReadsBad, row.OpenRemoteContinuations)
.ShouldBe((19, 34L, 1L, 3L, 2));
}
[Fact]
public void An_old_shape_payload_without_the_H4_members_decodes_with_them_unavailable()
{
var message = Snapshot();
var (serializer, manifest) = SerializerFor(message);
var json = JObject.Parse(Encoding.UTF8.GetString(serializer.ToBinary(message)));
json.Remove("OriginHistory").ShouldBeTrue("the H4 node member is on the wire");
var removed = 0;
foreach (var row in json.Descendants().OfType<JObject>().Where(o => o.ContainsKey("ConnectionId")).ToArray())
foreach (var member in H4ConnectionMembers)
if (row.Remove(member)) removed++;
removed.ShouldBe(H4ConnectionMembers.Length, "every H4 connection member was on the wire and is now stripped");
var legacy = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize(Encoding.UTF8.GetBytes(json.ToString(Newtonsoft.Json.Formatting.None)),
serializer.Identifier, manifest);
legacy.OriginHistory.ShouldBeNull("a pre-H4 node reports no origin evidence (rendered 'unavailable')");
// An origin record without the later NoPeerTrafficYet member decodes as live evidence, never "no peer traffic yet".
var withOrigin = JObject.Parse(Encoding.UTF8.GetString(serializer.ToBinary(message)));
((JObject)withOrigin["OriginHistory"]!).Remove("NoPeerTrafficYet").ShouldBeTrue();
var partial = (NodeDiagnosticsSnapshot)Sys.Serialization.Deserialize(
Encoding.UTF8.GetBytes(withOrigin.ToString(Newtonsoft.Json.Formatting.None)), serializer.Identifier, manifest);
partial.OriginHistory.ShouldNotBeNull().NoPeerTrafficYet.ShouldBeFalse();
partial.OriginHistory.ReadsGood.ShouldBe(7);
var row0 = legacy.DataConnections.ShouldNotBeNull().ShouldHaveSingleItem();
row0.ConnectionId.ShouldBe("link");
row0.NegotiatedFeatures.ShouldBeNull();
row0.HistoryVariants.ShouldBeNull();
row0.HistoryReadsGood.ShouldBeNull();
row0.OpenRemoteContinuations.ShouldBeNull();
row0.DroppedUnknownOperations.ShouldBe(DataOperations.None, "X0 members are untouched");
}
}
@@ -118,12 +118,12 @@ public sealed class GrpcDataHistoryMetricsTests
var backend = true;
diagnostics.AttachProvisionalOrigin(() => backend);
diagnostics.CaptureOrigin().ShouldBe(new ZB.MOM.WW.OtOpcUa.Commons.Interfaces.OriginHistoryDiagnostics(true, 0, 0, 0, 0, 0,
PeerServicesStarted: false));
NoPeerTrafficYet: true));
backend = false;
diagnostics.CaptureOrigin()!.Available.ShouldBeFalse("availability is probed live, not latched");
diagnostics.AttachOrigin(() => (true, 2, 1));
diagnostics.CaptureOrigin().ShouldBe(new ZB.MOM.WW.OtOpcUa.Commons.Interfaces.OriginHistoryDiagnostics(true, 2, 1, 0, 0, 0));
diagnostics.CaptureOrigin()!.PeerServicesStarted.ShouldBeTrue();
diagnostics.CaptureOrigin()!.NoPeerTrafficYet.ShouldBeFalse();
}
[Fact]