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:
@@ -427,31 +427,7 @@ public sealed class SitePairChannelProvider : IDisposable
|
||||
while (await timer.WaitForNextTickAsync(ct).ConfigureAwait(false))
|
||||
{
|
||||
SweepGraveyard();
|
||||
var now = DateTime.UtcNow;
|
||||
foreach (var kvp in _sites)
|
||||
{
|
||||
var pair = kvp.Value;
|
||||
bool due;
|
||||
lock (pair.Gate)
|
||||
{
|
||||
due = !pair.CurrentIsA && pair.ChannelA is not null && now >= pair.NextProbeUtc;
|
||||
}
|
||||
if (due)
|
||||
{
|
||||
try
|
||||
{
|
||||
await TryFailbackAsync(kvp.Key, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failback probe for site {TargetSiteId} faulted", kvp.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
await RunDueFailbackProbesAsync(DateTime.UtcNow, ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
@@ -460,6 +436,54 @@ public sealed class SitePairChannelProvider : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>COMM-8: how many failback probes one sweep runs concurrently.</summary>
|
||||
internal const int FailbackProbeParallelism = 8;
|
||||
|
||||
/// <summary>
|
||||
/// COMM-8: probes every site that is due for failback at <paramref name="nowUtc"/>,
|
||||
/// concurrently up to <see cref="FailbackProbeParallelism"/>. The probes used to run one
|
||||
/// after another, each up to its 5 s deadline, so 30 black-holed NodeAs made one pass take
|
||||
/// ~150 s: a site whose NodeA had recovered waited minutes past its backoff, and the
|
||||
/// graveyard sweep (which runs once per pass) stalled with it. Per-site state is guarded by
|
||||
/// each pair's own gate, so concurrent probes of different sites do not interact.
|
||||
/// </summary>
|
||||
/// <param name="nowUtc">The time the due check is evaluated at.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>A task that completes when every due probe has finished.</returns>
|
||||
internal async Task RunDueFailbackProbesAsync(DateTime nowUtc, CancellationToken ct)
|
||||
{
|
||||
var due = new List<string>();
|
||||
foreach (var kvp in _sites)
|
||||
{
|
||||
var pair = kvp.Value;
|
||||
lock (pair.Gate)
|
||||
{
|
||||
if (!pair.CurrentIsA && pair.ChannelA is not null && nowUtc >= pair.NextProbeUtc)
|
||||
due.Add(kvp.Key);
|
||||
}
|
||||
}
|
||||
if (due.Count == 0)
|
||||
return;
|
||||
|
||||
await Parallel.ForEachAsync(due,
|
||||
new ParallelOptions { MaxDegreeOfParallelism = FailbackProbeParallelism, CancellationToken = ct },
|
||||
async (siteId, token) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await TryFailbackAsync(siteId, token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (token.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogDebug(ex, "Failback probe for site {TargetSiteId} faulted", siteId);
|
||||
}
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parks a channel that is no longer reachable through <see cref="_sites"/> instead of
|
||||
/// disposing it inline.
|
||||
|
||||
@@ -679,16 +679,36 @@ public sealed class RealMxGatewayClient : IMxGatewayClient
|
||||
/// Maps MXAccess quality. A failing status proxy is authoritative bad; otherwise
|
||||
/// the OPC-style quality byte: ≥192 Good, ≥64 Uncertain, else Bad.
|
||||
/// </summary>
|
||||
private static QualityCode MapQuality(int quality, IEnumerable<MxStatusProxy> statuses)
|
||||
/// <remarks>
|
||||
/// DCL-10: on the per-value event path (~37 500 events/s per site at target). Takes the
|
||||
/// concrete repeated field and index-loops it — <c>IEnumerable.Any</c> boxed the
|
||||
/// repeated-field enumerator on every event although the list is normally empty or one
|
||||
/// entry — and maps the quality byte through a static table (no per-call work beyond a
|
||||
/// bounds check and an array read).
|
||||
/// </remarks>
|
||||
internal static QualityCode MapQuality(int quality, IReadOnlyList<MxStatusProxy> statuses)
|
||||
{
|
||||
if (statuses.Any(s => !s.IsSuccess()))
|
||||
return QualityCode.Bad;
|
||||
return quality switch
|
||||
for (var i = 0; i < statuses.Count; i++)
|
||||
{
|
||||
>= 192 => QualityCode.Good,
|
||||
>= 64 => QualityCode.Uncertain,
|
||||
_ => QualityCode.Bad,
|
||||
};
|
||||
if (!statuses[i].IsSuccess())
|
||||
return QualityCode.Bad;
|
||||
}
|
||||
if (quality < 0) return QualityCode.Bad;
|
||||
return quality < QualityByByte.Length ? QualityByByte[quality] : QualityCode.Good;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DCL-10: OPC-style quality byte → quality: ≥192 Good, ≥64 Uncertain, else Bad. Values above
|
||||
/// 255 map to Good and negatives to Bad, exactly as the former range switch did.
|
||||
/// </summary>
|
||||
private static readonly QualityCode[] QualityByByte = BuildQualityMap();
|
||||
|
||||
private static QualityCode[] BuildQualityMap()
|
||||
{
|
||||
var map = new QualityCode[256];
|
||||
for (var q = 0; q < map.Length; q++)
|
||||
map[q] = q >= 192 ? QualityCode.Good : q >= 64 ? QualityCode.Uncertain : QualityCode.Bad;
|
||||
return map;
|
||||
}
|
||||
|
||||
private static MxValue ToMxValue(object? value) => value switch
|
||||
|
||||
Reference in New Issue
Block a user