fix(dashboard): per-viewer share of the live-data advise set

The 256-tag LRU was global, so two Browse circuits on different areas evicted
each other's whole sets every poll. Tags now record the viewer that read them
last; each active viewer (read within 30s) owns MaxSubscribedTags/active
(floor 32). Eviction takes other viewers' excess over their share first
(inactive viewers' share is zero), then the reader's own untouched tags, and
never strips a neighbour within its share (soft cap). Single-viewer behaviour
is unchanged. IDashboardLiveDataService gains a viewerId ReadAsync overload.
This commit is contained in:
Joseph Doherty
2026-10-22 06:22:17 -04:00
parent 2a7d6facce
commit 5d4aefc99c
3 changed files with 279 additions and 31 deletions
@@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Time.Testing;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Configuration;
using ZB.MOM.WW.MxGateway.Server.Dashboard;
@@ -135,6 +136,69 @@ public sealed class DashboardLiveDataServiceTests
Assert.Equal(5, worker.UnsubscribedHandles.Count);
}
/// <summary>
/// Verifies the per-viewer share: with two active circuits each owns half the cap,
/// so a second circuit's large read evicts only the first circuit's excess above
/// its share — never its whole set — and keeps every tag it read itself.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ReadAsync_WithTwoViewers_EvictsOnlyTheOtherViewersExcessOverItsShare()
{
RecordingWorkerClient worker = new();
await using FakeSessionManager sessionManager = new(worker);
await using DashboardLiveDataService service = CreateService(sessionManager);
string[] first = CreateTagAddresses(200, "First");
string[] second = CreateTagAddresses(200, "Second");
await service.ReadAsync(first, "circuit-a", CancellationToken.None);
DashboardLiveReadResult result = await service.ReadAsync(second, "circuit-b", CancellationToken.None);
Assert.Null(result.Error);
// Two active viewers -> share of 128 each. circuit-a drops its 72 least-recently-read
// tags and keeps 128; under the old global LRU it lost 144 (400 - 256).
int share = MaxSubscribedTags / 2;
Assert.Equal(first[..(first.Length - share)].Select(worker.HandleFor), worker.UnsubscribedHandles);
Assert.DoesNotContain(worker.UnsubscribedHandles, handle => second.Select(worker.HandleFor).Contains(handle));
// circuit-a re-reading its retained tags subscribes nothing new and evicts nothing.
worker.UnsubscribedHandles.Clear();
int subscribedBefore = worker.SubscribedTags.Count;
await service.ReadAsync(first[^share..], "circuit-a", CancellationToken.None);
Assert.Equal(subscribedBefore, worker.SubscribedTags.Count);
Assert.Empty(worker.UnsubscribedHandles);
}
/// <summary>
/// Verifies an inactive viewer (a closed circuit that stopped polling) loses its
/// share: its tags are the first eviction candidates once the activity window passes.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ReadAsync_WhenAViewerGoesInactive_ItsTagsAreEvictedFirst()
{
RecordingWorkerClient worker = new();
await using FakeSessionManager sessionManager = new(worker);
FakeTimeProvider clock = new();
await using DashboardLiveDataService service = CreateService(sessionManager, timeProvider: clock);
string[] stale = CreateTagAddresses(100, "Stale");
string[] live = CreateTagAddresses(156, "Live");
await service.ReadAsync(live, "open-circuit", CancellationToken.None);
await service.ReadAsync(stale, "closed-circuit", CancellationToken.None);
Assert.Empty(worker.UnsubscribedHandles);
clock.Advance(TimeSpan.FromMinutes(1));
// The stale tags were read after the live ones, so a global LRU would evict live[10]
// (the oldest tag this read does not touch). The stale owner is inactive, though, so
// its share is zero and its oldest tag goes first.
await service.ReadAsync([.. live[..10], "Live.New.PV"], "open-circuit", CancellationToken.None);
Assert.Equal([worker.HandleFor(stale[0])], worker.UnsubscribedHandles);
}
/// <summary>
/// Verifies a tag the worker failed to advise still occupies a slot but is evicted
/// without any unsubscribe command — there is no item handle to unadvise.
@@ -280,7 +344,8 @@ public sealed class DashboardLiveDataServiceTests
private static DashboardLiveDataService CreateService(
ISessionManager sessionManager,
bool showTagValues = false)
bool showTagValues = false,
TimeProvider? timeProvider = null)
{
GatewayOptions gatewayOptions = new()
{
@@ -291,7 +356,8 @@ public sealed class DashboardLiveDataServiceTests
sessionManager,
new FakeGatewayAlarmService(),
Options.Create(gatewayOptions),
NullLogger<DashboardLiveDataService>.Instance);
NullLogger<DashboardLiveDataService>.Instance,
timeProvider);
}
private static string[] CreateTagAddresses(int count, string prefix = "Tank")