fix(dashboard): prime new /hubs/alarms connections with the current snapshot (DSH-005, #254)
The publisher's single monitor subscription is primed once, normally before any browser connects, so a later client saw only subsequent transitions and never the standing alarms or provider status. AlarmsHub.OnConnectedAsync now joins the group, sends the caller a generation-0 AlarmFeedReset, then the status frames, active alarms and snapshot_complete from a fresh subscription, redacted by ShowTagValues.
This commit is contained in:
@@ -9,6 +9,8 @@ namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
|
||||
/// </summary>
|
||||
/// <param name="Generation">
|
||||
/// Monotonic subscription counter for this gateway process, starting at 1. Lets a
|
||||
/// client tell a resubscribe from a duplicate delivery.
|
||||
/// client tell a resubscribe from a duplicate delivery. Generation 0 is the per-connection
|
||||
/// prime sent to a client when it connects, ahead of the current snapshot; a client handles
|
||||
/// it exactly like any other reset.
|
||||
/// </param>
|
||||
public sealed record AlarmFeedReset(long Generation);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.MxGateway.Server.Alarms;
|
||||
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
|
||||
|
||||
@@ -19,9 +22,17 @@ namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
|
||||
/// read-only environment bypasses (<c>Authentication:Mode=Disabled</c>, anonymous localhost) no
|
||||
/// longer receive the feed. The <c>/alarms</c> page carries the same policy — the two readers of
|
||||
/// one monitor must not disagree about who may see it.
|
||||
/// <para>
|
||||
/// On connect the client is primed with the current state (a generation-0
|
||||
/// <see cref="ResetMessage"/>, provider and snapshot status, the active alarms, then
|
||||
/// <c>snapshot_complete</c>) before it relies on the group's live frames; see
|
||||
/// <see cref="AlarmsHubPublisher.PrimeConnectionAsync"/>.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
/// <param name="alarmService">The gateway's central alarm feed.</param>
|
||||
/// <param name="options">Gateway options supplying <c>Dashboard:ShowTagValues</c>.</param>
|
||||
[Authorize(Policy = DashboardAuthenticationDefaults.AlarmsPolicy)]
|
||||
public sealed class AlarmsHub : Hub
|
||||
public sealed class AlarmsHub(IGatewayAlarmService alarmService, IOptions<GatewayOptions> options) : Hub
|
||||
{
|
||||
/// <summary>Group every admitted connection joins on connect.</summary>
|
||||
public const string AllAlarmsGroup = "__alarms__";
|
||||
@@ -40,6 +51,14 @@ public sealed class AlarmsHub : Hub
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, AllAlarmsGroup).ConfigureAwait(false);
|
||||
|
||||
// Joined first, so nothing broadcast after the snapshot is read can be missed (DSH-005).
|
||||
await AlarmsHubPublisher.PrimeConnectionAsync(
|
||||
alarmService,
|
||||
Clients.Caller,
|
||||
options.Value.Dashboard.ShowTagValues,
|
||||
Context.ConnectionAborted)
|
||||
.ConfigureAwait(false);
|
||||
await base.OnConnectedAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,6 +146,56 @@ public sealed class AlarmsHubPublisher : BackgroundService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Primes one newly connected client with the feed's current state: a
|
||||
/// <see cref="AlarmsHub.ResetMessage"/> marker with generation 0, then the provider
|
||||
/// status, the snapshot status, one <c>active_alarm</c> frame per active alarm and the
|
||||
/// <c>snapshot_complete</c> sentinel — the same preamble a <c>StreamAlarms</c> subscriber
|
||||
/// gets, redacted by the same <c>ShowTagValues</c> rule as the group broadcast.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The publisher holds one monitor subscription for the process lifetime and group-sends
|
||||
/// the snapshot only when that subscription (re)opens, which is normally before any client
|
||||
/// is connected; without this a client joining later saw only subsequent transitions
|
||||
/// (DSH-005). The caller joins the group before priming and the reset is sent before the
|
||||
/// snapshot is read, so a transition is either already in the snapshot or arrives as a
|
||||
/// live frame after the reset. A transition applied while the snapshot frames are being
|
||||
/// sent can arrive ahead of the (older) snapshot row for the same alarm; the next
|
||||
/// transition or reconcile repair for that alarm corrects it, consistent with the feed's
|
||||
/// at-least-once, apply-as-state contract.
|
||||
/// </remarks>
|
||||
/// <param name="alarmService">The gateway's central alarm feed.</param>
|
||||
/// <param name="caller">The connecting client.</param>
|
||||
/// <param name="showTagValues">Whether alarm values may reach browser clients.</param>
|
||||
/// <param name="cancellationToken">The connection-aborted token.</param>
|
||||
/// <returns>A task that completes once the snapshot sentinel has been sent.</returns>
|
||||
internal static async Task PrimeConnectionAsync(
|
||||
IGatewayAlarmService alarmService,
|
||||
IClientProxy caller,
|
||||
bool showTagValues,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(alarmService);
|
||||
ArgumentNullException.ThrowIfNull(caller);
|
||||
|
||||
await caller.SendAsync(AlarmsHub.ResetMessage, new AlarmFeedReset(0), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
using CancellationTokenSource stop = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
await foreach (AlarmFeedMessage message in alarmService
|
||||
.StreamAsync(alarmFilterPrefix: null, stop.Token)
|
||||
.ConfigureAwait(false))
|
||||
{
|
||||
AlarmFeedMessage outbound = showTagValues ? message : RedactValues(message);
|
||||
await caller.SendAsync(AlarmsHub.AlarmMessage, outbound, cancellationToken).ConfigureAwait(false);
|
||||
if (message.PayloadCase == AlarmFeedMessage.PayloadOneofCase.SnapshotComplete)
|
||||
{
|
||||
// The preamble is complete; live frames reach this client through the group.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends one frame to the alarms group within the send timeout. A slow or failed
|
||||
/// send is logged and dropped so the feed keeps draining; only host shutdown
|
||||
@@ -193,7 +243,7 @@ public sealed class AlarmsHubPublisher : BackgroundService
|
||||
/// names only the arms that have something to redact, so adding a valueless
|
||||
/// arm to the contract needs no change here.
|
||||
/// </returns>
|
||||
private static AlarmFeedMessage RedactValues(AlarmFeedMessage source)
|
||||
internal static AlarmFeedMessage RedactValues(AlarmFeedMessage source)
|
||||
{
|
||||
switch (source.PayloadCase)
|
||||
{
|
||||
|
||||
@@ -287,6 +287,47 @@ public sealed class AlarmsHubPublisherTests
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DSH-005: a client connecting long after the publisher subscribed is primed with the
|
||||
/// current state itself — a generation-0 reset, then the status frames, every active
|
||||
/// alarm (values redacted) and the snapshot sentinel — without any new transition.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task PrimeConnectionAsync_SendsResetThenTheCurrentSnapshot()
|
||||
{
|
||||
AlarmFeedMessage activeOne = BuildActiveAlarm();
|
||||
AlarmFeedMessage activeTwo = BuildActiveAlarm();
|
||||
activeTwo.ActiveAlarm.AlarmFullReference = "Tank03.Level.Hi";
|
||||
ScriptedAlarmService service = new(
|
||||
[
|
||||
new AlarmFeedMessage { ProviderStatus = new AlarmProviderStatus { Mode = AlarmProviderMode.Subtag, Degraded = true } },
|
||||
new AlarmFeedMessage { SnapshotStatus = new AlarmSnapshotStatus { Truncated = false } },
|
||||
activeOne,
|
||||
activeTwo,
|
||||
new AlarmFeedMessage { SnapshotComplete = true },
|
||||
]);
|
||||
CapturingClientProxy caller = new();
|
||||
|
||||
using CancellationTokenSource cts = new(TestTimeout);
|
||||
await AlarmsHubPublisher.PrimeConnectionAsync(service, caller, showTagValues: false, cts.Token);
|
||||
|
||||
IReadOnlyList<(string Method, object? Argument)> frames = caller.Frames;
|
||||
Assert.Equal(6, frames.Count);
|
||||
Assert.Equal(AlarmsHub.ResetMessage, frames[0].Method);
|
||||
Assert.Equal(new AlarmFeedReset(0), frames[0].Argument);
|
||||
Assert.All(frames.Skip(1), frame => Assert.Equal(AlarmsHub.AlarmMessage, frame.Method));
|
||||
|
||||
IReadOnlyList<AlarmFeedMessage> sent = caller.Sent;
|
||||
Assert.Equal(AlarmProviderMode.Subtag, sent[0].ProviderStatus.Mode);
|
||||
Assert.Equal(AlarmFeedMessage.PayloadOneofCase.SnapshotStatus, sent[1].PayloadCase);
|
||||
Assert.Equal("Tank02.Level.Lo", sent[2].ActiveAlarm.AlarmFullReference);
|
||||
Assert.Equal("Tank03.Level.Hi", sent[3].ActiveAlarm.AlarmFullReference);
|
||||
Assert.Null(sent[2].ActiveAlarm.CurrentValue);
|
||||
Assert.Null(sent[3].ActiveAlarm.CurrentValue);
|
||||
Assert.True(sent[4].SnapshotComplete);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Yields one scripted batch per subscription. Every batch but the last ends its
|
||||
/// stream — the monitor completing a subscription — so the publisher resubscribes;
|
||||
|
||||
Reference in New Issue
Block a user