fix(dashboard): debounced off-dispatcher Browse search, bounded disposal, circuit-aware polling

- Search: the oninput setter scanned the whole Galaxy synchronously per
  keystroke. It now debounces 150ms, runs on the thread pool with
  cancellation (newer keystroke supersedes), and reads a shared singleton
  DashboardBrowseSearchIndex of pre-lowercased references keyed on the
  Galaxy cache Sequence.
- Disposal: DisposeAsync awaited both loops unbounded and the loops caught
  only OCE. Loops now handle faults per iteration (poll faults surface in
  _readError, deploy-notifier faults in a retrying warning), mutate the tree
  on the dispatcher, drain concurrently with a 5s bound, and the CTSs are
  disposed in finally.
- Circuits: DisconnectedCircuitRetentionPeriod is 30s (was the 3 min
  default) and a scoped DashboardCircuitConnectionTracker (CircuitHandler)
  lets the 2s ReadBulk poll skip ticks while the browser is disconnected.
- Reads pass a per-page viewer id to the live-data per-viewer share.
This commit is contained in:
Joseph Doherty
2026-10-22 06:25:46 -04:00
parent 5d4aefc99c
commit db7833609d
6 changed files with 573 additions and 68 deletions
@@ -0,0 +1,95 @@
using ZB.MOM.WW.GalaxyRepository;
using ZB.MOM.WW.GalaxyRepository.Grpc;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
namespace ZB.MOM.WW.MxGateway.Tests.Dashboard;
/// <summary>
/// Verifies the Browse filter's shared index: case-insensitive matching on reference
/// and attribute name, the result limit, one build per Galaxy cache sequence, and
/// cancellation of a superseded scan.
/// </summary>
public sealed class DashboardBrowseSearchIndexTests
{
/// <summary>Matches either the full reference or the attribute name, ignoring case.</summary>
[Fact]
public void Search_MatchesReferenceOrAttributeNameCaseInsensitively()
{
DashboardBrowseSearchIndex index = new();
GalaxyHierarchyCacheEntry entry = CreateEntry(sequence: 1);
IReadOnlyList<GalaxyAttribute> byReference = index.Search(entry, " PUMP_001.p ", 10, CancellationToken.None);
IReadOnlyList<GalaxyAttribute> byName = index.Search(entry, "levels", 10, CancellationToken.None);
Assert.Equal(["Pump_001.PV"], byReference.Select(attr => attr.FullTagReference));
Assert.Equal(["Tank_001.Levels[]"], byName.Select(attr => attr.FullTagReference));
Assert.Empty(index.Search(entry, " ", 10, CancellationToken.None));
}
/// <summary>Stops at the limit, in Galaxy object order.</summary>
[Fact]
public void Search_StopsAtTheLimit()
{
DashboardBrowseSearchIndex index = new();
IReadOnlyList<GalaxyAttribute> matches = index.Search(CreateEntry(sequence: 1), "_001", 2, CancellationToken.None);
Assert.Equal(["Pump_001.PV", "Pump_001.Alarm"], matches.Select(attr => attr.FullTagReference));
}
/// <summary>The index is built once per cache sequence and rebuilt when the sequence moves.</summary>
[Fact]
public void Search_RebuildsOnlyWhenTheCacheSequenceChanges()
{
DashboardBrowseSearchIndex index = new();
GalaxyHierarchyCacheEntry first = CreateEntry(sequence: 1);
index.Search(first, "pump", 10, CancellationToken.None);
index.Search(first, "tank", 10, CancellationToken.None);
Assert.Equal(1, index.BuildCount);
GalaxyHierarchyCacheEntry redeployed = CreateEntry(sequence: 2, extraTag: "Valve_001.Open");
IReadOnlyList<GalaxyAttribute> matches = index.Search(redeployed, "valve", 10, CancellationToken.None);
Assert.Equal(2, index.BuildCount);
Assert.Equal(["Valve_001.Open"], matches.Select(attr => attr.FullTagReference));
}
/// <summary>A cancelled (superseded) search throws instead of finishing the scan.</summary>
[Fact]
public void Search_WhenCancelled_Throws()
{
DashboardBrowseSearchIndex index = new();
using CancellationTokenSource cancelled = new();
cancelled.Cancel();
Assert.ThrowsAny<OperationCanceledException>(
() => index.Search(CreateEntry(sequence: 1), "pump", 10, cancelled.Token));
}
private static GalaxyHierarchyCacheEntry CreateEntry(long sequence, string? extraTag = null)
{
GalaxyObject pump = new() { GobjectId = 1, TagName = "Pump_001" };
pump.Attributes.Add(new GalaxyAttribute { AttributeName = "PV", FullTagReference = "Pump_001.PV" });
pump.Attributes.Add(new GalaxyAttribute { AttributeName = "Alarm", FullTagReference = "Pump_001.Alarm" });
GalaxyObject tank = new() { GobjectId = 2, TagName = "Tank_001" };
tank.Attributes.Add(new GalaxyAttribute { AttributeName = "Levels", FullTagReference = "Tank_001.Levels[]" });
List<GalaxyObject> objects = [pump, tank];
if (extraTag is not null)
{
GalaxyObject extra = new() { GobjectId = 3, TagName = extraTag.Split('.')[0] };
extra.Attributes.Add(new GalaxyAttribute { AttributeName = extraTag.Split('.')[1], FullTagReference = extraTag });
objects.Add(extra);
}
return GalaxyHierarchyCacheEntry.Empty with
{
Status = GalaxyCacheStatus.Healthy,
Sequence = sequence,
Objects = objects,
Index = GalaxyHierarchyIndex.Build(objects),
};
}
}
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.MxGateway.Server;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
using ZB.MOM.WW.MxGateway.Server.Dashboard.Hubs;
@@ -61,4 +64,42 @@ public sealed class DashboardHubsRegistrationTests
Assert.Same(broadcaster, subscriber);
Assert.Same(app.Services.GetRequiredService<DashboardEventBroadcaster>(), broadcaster);
}
/// <summary>
/// A closed tab's circuit (and its Browse poll) is retained for 30 seconds, not the
/// framework's three minutes, and each circuit gets a connection tracker that is also
/// its <see cref="CircuitHandler"/>, so the Browse poll can pause while disconnected.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Build_WhenDashboardEnabled_ShortensCircuitRetentionAndTracksConnection()
{
await using WebApplication app = GatewayApplication.Build([]);
CircuitOptions circuitOptions = app.Services.GetRequiredService<IOptions<CircuitOptions>>().Value;
Assert.Equal(TimeSpan.FromSeconds(30), circuitOptions.DisconnectedCircuitRetentionPeriod);
Assert.Equal(DashboardServiceCollectionExtensions.DisconnectedCircuitRetention, circuitOptions.DisconnectedCircuitRetentionPeriod);
using IServiceScope scope = app.Services.CreateScope();
DashboardCircuitConnectionTracker tracker = scope.ServiceProvider.GetRequiredService<DashboardCircuitConnectionTracker>();
Assert.Contains(scope.ServiceProvider.GetServices<CircuitHandler>(), handler => ReferenceEquals(handler, tracker));
Assert.True(tracker.IsConnected);
await tracker.OnConnectionDownAsync(null!, CancellationToken.None);
Assert.False(tracker.IsConnected);
await tracker.OnConnectionUpAsync(null!, CancellationToken.None);
Assert.True(tracker.IsConnected);
}
/// <summary>The Browse filter index is one shared instance, built once per Galaxy deploy.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Build_WhenDashboardEnabled_SharesOneBrowseSearchIndex()
{
await using WebApplication app = GatewayApplication.Build([]);
Assert.Same(
app.Services.GetRequiredService<DashboardBrowseSearchIndex>(),
app.Services.GetRequiredService<DashboardBrowseSearchIndex>());
}
}