fix(grpc-data): H4 review 2 — a fresh origin reports 'no peer traffic yet', not 'unavailable'

The origin services are composed on the first peer call (they need the driver host actor), so
CaptureOrigin returned null until then and a fresh H4 origin read like a pre-H4 build. The DI
singleton now probes the same deferred history port for backend availability and reports zeros
with PeerServicesStarted=false; the page says 'no peer has called since start'.
This commit is contained in:
Joseph Doherty
2026-11-24 08:37:38 -05:00
parent 2f11a9c609
commit 005720bd02
6 changed files with 53 additions and 6 deletions
+3 -1
View File
@@ -286,7 +286,9 @@ history sessions, the **open origin continuations** (page cursors and chunk rema
answered by status class, and its exports with dropped unknown operation bits.
All counts are since process start and reset on restart. "Unavailable" means the node did not report the measure (built
before H4, or no data-connection services) — never zero activity.
before H4, or no data-connection services) — never zero activity. The origin's data services are created on the first peer call,
so until a peer has called, a node's origin line shows its backend availability with zero sessions, continuations and answers and
"no peer has called since start".
Metrics, on the `ZB.MOM.WW.OtOpcUa` meter (exported on `/metrics` and OTLP like every OtOpcUa instrument), never carry a
connection, node or principal id in a label:
@@ -57,5 +57,9 @@ 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>
public sealed record OriginHistoryDiagnostics(bool Available, int Sessions, int OpenContinuations, long ReadsGood,
long ReadsUncertain, long ReadsBad);
long ReadsUncertain, long ReadsBad, bool PeerServicesStarted = true);
@@ -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)</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.PeerServicesStarted ? "" : " · no peer has called since start")</span>
}
else { <span> · remote history evidence unavailable</span> }
@if (node.DroppedExportOperations is { } dropped)
@@ -120,7 +120,15 @@ public static class DataConnectionRegistration
services.AddSingleton<IProjectedDataAccess>(sp => sp.GetRequiredService<ProjectedDataAccess>());
// H4: one dependency-free history diagnostics instance shared by the origin services, the consumer gateway and the
// diagnostics readers (it also backs the otopcua.dataconn.history.* metrics).
services.AddSingleton<DataConnectionHistoryDiagnostics>();
services.AddSingleton(sp =>
{
// Before the first peer call composes the origin services, report the backend's availability (and zeros) from a
// probe of the same deferred history port, so a fresh H4 origin never reads like a pre-H4 build (H4 review 2).
var diagnostics = new DataConnectionHistoryDiagnostics();
var probe = new DeferredOriginHistoryPort(sp.GetRequiredService<DeferredAddressSpaceSink>());
diagnostics.AttachProvisionalOrigin(() => probe.Serving is not null);
return diagnostics;
});
// H3: OPC UA HistoryRead on projected variables, forwarded over the mapping's deployed connection.
services.AddSingleton(sp => new ProjectedHistoryAccess(sp.GetRequiredService<ProjectedDataAccess>(),
sp.GetRequiredService<IGrpcDataAdapterConnectionSource>(),
@@ -75,6 +75,7 @@ public sealed class DataConnectionHistoryDiagnostics
private readonly ConcurrentDictionary<string, Counts> _consumer = new(StringComparer.OrdinalIgnoreCase);
private readonly Counts _origin = new();
private Func<(bool Available, int Sessions, int Open)>? _originState;
private Func<bool>? _provisionalAvailability;
public DataConnectionHistoryDiagnostics() => Live.AddOrUpdate(this, new object());
@@ -129,6 +130,13 @@ public sealed class DataConnectionHistoryDiagnostics
/// <summary>Connects the origin services whose live state (backend, sessions, open cursors) this reports.</summary>
internal void AttachOrigin(Func<(bool Available, int Sessions, int Open)> state) => _originState = state;
/// <summary>
/// H4 review: the origin's backend availability BEFORE its data services exist. They are composed on the first peer call
/// (the composition needs the driver host actor), so until then the evidence is "available/not, no peer traffic yet" —
/// zero sessions and continuations are facts, not unknowns — instead of null (which means a pre-H4 build).
/// </summary>
public void AttachProvisionalOrigin(Func<bool> available) => _provisionalAvailability = available;
/// <summary>The consumer evidence for one connection; zeros when it has seen no history traffic.</summary>
public ConsumerHistoryCounts CaptureConsumer(string connectionId) =>
_consumer.TryGetValue(connectionId, out var c)
@@ -138,10 +146,18 @@ public sealed class DataConnectionHistoryDiagnostics
/// <summary>Consumer continuations open across every connection.</summary>
public int OpenConsumerContinuations => _consumer.Values.Sum(x => Math.Max(0, Volatile.Read(ref x.Open)));
/// <summary>The origin evidence; null until origin services are attached (no data-connection origin on this node yet).</summary>
/// <summary>
/// The origin evidence: live once the origin services are attached; before that the provisional "no peer traffic yet"
/// state when one is attached; otherwise null (no data-connection origin on this node).
/// </summary>
public OriginHistoryDiagnostics? CaptureOrigin()
{
if (_originState is not { } state) return null;
if (_originState is not { } state)
{
if (_provisionalAvailability is not { } provisional) return null;
try { return new(provisional(), 0, 0, 0, 0, 0, PeerServicesStarted: false); }
catch (Exception) { return null; }
}
(bool Available, int Sessions, int Open) now;
try { now = state(); }
catch (Exception) { return null; }
@@ -109,6 +109,23 @@ public sealed class GrpcDataHistoryMetricsTests
diagnostics.CaptureOrigin().ShouldBeNull();
}
[Fact]
public void Before_any_peer_call_a_provisional_origin_reports_no_peer_traffic_yet_not_unavailable()
{
// H4 review item 2: the origin services are composed on the first peer call; until then a fresh H4 origin must not
// read like a pre-H4 build (null).
var diagnostics = new DataConnectionHistoryDiagnostics();
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));
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();
}
[Fact]
public void The_plan_unknown_bit_counter_is_the_existing_X0_instrument_not_a_second_one()
{