perf(communication,dcl): bounded-parallel failback probes; allocation-free MxGateway quality map (COMM-8, DCL-10, WP6 task 6.10)

COMM-8: the failback sweep collects the due sites and probes them through
Parallel.ForEachAsync (FailbackProbeParallelism = 8) instead of awaiting each
probe (up to its 5 s deadline) in turn; per-site state stays under each
pair's own gate. The graveyard sweep still runs at the start of each pass,
which now ends after ceil(due/8) probe rounds instead of due rounds.
  Test: 16 failed-over sites, 400 ms probe each: serial 6.4 s -> one pass
  < 3 s asserted (~0.8 s, 2 rounds), max 8 in flight. 30 black-holed NodeAs:
  ~150 s -> ~20 s per pass.

DCL-10: MapQuality takes IReadOnlyList<MxStatusProxy> (the RepeatedField),
index-loops the statuses and maps the quality byte through a static
256-entry table (negatives Bad, >255 Good, as before).
  Throwaway Stopwatch, Release, 1M calls with one Ok status:
  old 40 B + 34.3 ns per event -> new 0 B + 0.8 ns (test pins zero allocation).
This commit is contained in:
Joseph Doherty
2026-12-03 10:25:50 -05:00
parent 2ef2d751cb
commit 790db4ff94
4 changed files with 165 additions and 33 deletions
@@ -65,6 +65,49 @@ public class SitePairChannelProviderFailbackTests
Assert.Equal(TimeSpan.FromSeconds(4), provider.GetFailbackBackoff(Site));
}
/// <summary>
/// COMM-8 (WP6 6.10): due failback probes run concurrently (bounded), so one slow NodeA per
/// site no longer serialises the pass — 16 sites with a 400 ms probe each finish in well
/// under the 6.4 s a serial pass takes, and never more than the bound run at once.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DueProbes_RunConcurrently_UpToTheBound()
{
var inFlight = 0;
var maxInFlight = 0;
using var provider = Build(async (_, ct) =>
{
var now = Interlocked.Increment(ref inFlight);
int seen;
while ((seen = Volatile.Read(ref maxInFlight)) < now && Interlocked.CompareExchange(ref maxInFlight, now, seen) != seen) { }
await Task.Delay(400, ct);
Interlocked.Decrement(ref inFlight);
return false;
});
const int sites = 16;
for (var i = 0; i < sites; i++)
{
var site = $"site-{i}";
provider.UpdateSite(site, $"http://localhost:{16000 + i}", $"http://localhost:{17000 + i}");
GrpcChannel? first = null;
await provider.ExecuteAsync(site, (channel, _) =>
{
if (first is null) { first = channel; throw GrpcRetrySafety.NotDispatched("not ready"); }
return Task.FromResult(1);
}, CancellationToken.None);
Assert.False(provider.IsOnPreferredNode(site));
}
var sw = System.Diagnostics.Stopwatch.StartNew();
await provider.RunDueFailbackProbesAsync(DateTime.UtcNow.AddMinutes(1), CancellationToken.None);
sw.Stop();
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(3), $"probe pass took {sw.ElapsedMilliseconds} ms");
Assert.True(maxInFlight > 1, "probes did not run concurrently");
Assert.True(maxInFlight <= SitePairChannelProvider.FailbackProbeParallelism);
}
/// <summary>Verifies that a successful real call on the preferred node after failback resets the backoff.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
@@ -0,0 +1,45 @@
using Google.Protobuf.Collections;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Adapters;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests;
/// <summary>DCL-10 (WP6 6.10): MxGateway quality mapping is table-driven and allocation-free.</summary>
public class MxGatewayQualityMapTests
{
/// <summary>The quality byte maps exactly as the former range switch did.</summary>
[Theory]
[InlineData(-5, QualityCode.Bad)]
[InlineData(0, QualityCode.Bad)]
[InlineData(63, QualityCode.Bad)]
[InlineData(64, QualityCode.Uncertain)]
[InlineData(191, QualityCode.Uncertain)]
[InlineData(192, QualityCode.Good)]
[InlineData(255, QualityCode.Good)]
[InlineData(1000, QualityCode.Good)]
public void QualityByte_Maps(int quality, QualityCode expected)
=> Assert.Equal(expected, RealMxGatewayClient.MapQuality(quality, new RepeatedField<MxStatusProxy>()));
/// <summary>A failing status is authoritative Bad; a success status defers to the byte.</summary>
[Fact]
public void FailingStatus_IsBad()
{
var ok = new RepeatedField<MxStatusProxy> { new MxStatusProxy { Category = MxStatusCategory.Ok } };
var failing = new RepeatedField<MxStatusProxy> { new MxStatusProxy { Category = MxStatusCategory.Ok }, new MxStatusProxy() };
Assert.Equal(QualityCode.Good, RealMxGatewayClient.MapQuality(192, ok));
Assert.Equal(QualityCode.Bad, RealMxGatewayClient.MapQuality(192, failing));
}
/// <summary>The per-event path allocates nothing (the old Any() boxed the enumerator per call).</summary>
[Fact]
public void MapQuality_DoesNotAllocate()
{
var statuses = new RepeatedField<MxStatusProxy> { new MxStatusProxy { Category = MxStatusCategory.Ok } };
RealMxGatewayClient.MapQuality(192, statuses); // warm
var before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 10_000; i++)
RealMxGatewayClient.MapQuality(192, statuses);
Assert.Equal(0, GC.GetAllocatedBytesForCurrentThread() - before);
}
}