feat(mtconnect): /sample lag measurement + adaptive count (R06:T-5)
Subscription throughput was hard-capped at SampleCount / SampleIntervalMs with no lag detection: a machine producing more fell behind the ring buffer forever, re-baselining every buffer-length with every value backlog-stale. - The parser carries the header's optional lastSequence; each chunk measures lag = lastSequence + 1 - nextSequence and whether it was saturated (full count). - Three consecutive saturated chunks with a positive (or unknown) lag reopen the stream at the cursor reached with count doubled, up to 16x SampleCount (max 65536), through a new IMTConnectAgentClient.SampleAsync(from, count, ct) overload (default implementation ignores count). A raised stream that delivers nothing steps back to the last good count; at the ceiling one Warning per pump. - MTConnectDriver implements IDriverDiagnostics: sampleCount, sampleLag, sampleSaturatedChunks, sampleCountRaises on the /hosts bag. The cap is documented on SampleCount. Refs #743 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -1971,6 +1971,8 @@ public sealed partial class MTConnectDriver
|
||||
var cursor = from;
|
||||
var attempt = 0;
|
||||
_reconnectAttempts = 0;
|
||||
var counts = new SampleCountState(options.SampleCount);
|
||||
Volatile.Write(ref _effectiveSampleCount, counts.Count);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -2001,7 +2003,7 @@ public sealed partial class MTConnectDriver
|
||||
}
|
||||
}
|
||||
|
||||
var outcome = await ConsumeStreamAsync(client, options, cursor, instanceId, generation, ct).ConfigureAwait(false);
|
||||
var outcome = await ConsumeStreamAsync(client, options, cursor, instanceId, generation, counts, ct).ConfigureAwait(false);
|
||||
cursor = outcome.Cursor;
|
||||
instanceId = outcome.InstanceId;
|
||||
|
||||
@@ -2012,6 +2014,30 @@ public sealed partial class MTConnectDriver
|
||||
return;
|
||||
}
|
||||
|
||||
// R06:T-5 — the adaptive count. A stream that delivered vindicates the count it was opened with; a
|
||||
// RAISED count whose stream delivered nothing may be one the Agent refuses (count above its buffer),
|
||||
// so step back to the last count that worked and stop raising for this pump.
|
||||
if (outcome.DeliveredAny)
|
||||
{
|
||||
counts.LastDelivered = counts.Count;
|
||||
}
|
||||
else if (counts.Count > counts.LastDelivered && outcome.Verdict is not StreamVerdict.Cancelled)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
outcome.Error,
|
||||
"MTConnect driver {DriverInstanceId}: the /sample stream opened with the raised count {Count} delivered nothing; stepping back to {Previous} and not raising it again for this stream.",
|
||||
_driverInstanceId, counts.Count, counts.LastDelivered);
|
||||
counts.Count = counts.LastDelivered;
|
||||
counts.Ceiling = counts.Count;
|
||||
Volatile.Write(ref _effectiveSampleCount, counts.Count);
|
||||
}
|
||||
|
||||
if (outcome.RaiseCountTo is { } raised)
|
||||
{
|
||||
counts.Count = raised;
|
||||
Volatile.Write(ref _effectiveSampleCount, raised);
|
||||
}
|
||||
|
||||
switch (outcome.Verdict)
|
||||
{
|
||||
case StreamVerdict.Cancelled:
|
||||
@@ -2118,6 +2144,7 @@ public sealed partial class MTConnectDriver
|
||||
long cursor,
|
||||
long instanceId,
|
||||
long generation,
|
||||
SampleCountState counts,
|
||||
CancellationToken ct)
|
||||
{
|
||||
// The RUNNING cursor the gap check is made against: the previous chunk's nextSequence, equal
|
||||
@@ -2131,9 +2158,12 @@ public sealed partial class MTConnectDriver
|
||||
// that cannot work at all. They must not share a backoff ladder — see RunSampleStreamAsync.
|
||||
var delivered = false;
|
||||
|
||||
// Consecutive chunks that said this stream is behind the Agent (R06:T-5).
|
||||
var behindChunks = 0;
|
||||
|
||||
try
|
||||
{
|
||||
await foreach (var chunk in client.SampleAsync(cursor, ct).ConfigureAwait(false))
|
||||
await foreach (var chunk in client.SampleAsync(cursor, counts.Count, ct).ConfigureAwait(false))
|
||||
{
|
||||
if (chunk is null)
|
||||
{
|
||||
@@ -2198,6 +2228,21 @@ public sealed partial class MTConnectDriver
|
||||
_streamDegraded = false;
|
||||
_pumpLive = true;
|
||||
PublishHealthy(DateTime.UtcNow);
|
||||
|
||||
// R06:T-5 — a stream persistently behind the Agent reopens at the cursor it reached with a larger
|
||||
// count, rather than falling out of the ring buffer and re-baselining every buffer-length.
|
||||
if (!MeasureSampleLag(chunk, counts.Count))
|
||||
{
|
||||
behindChunks = 0;
|
||||
}
|
||||
else if (++behindChunks >= BehindChunksBeforeRaise)
|
||||
{
|
||||
behindChunks = 0;
|
||||
if (NextSampleCount(counts, options, expected) is { } next)
|
||||
{
|
||||
return new StreamOutcome(StreamVerdict.Reopen, expected, instanceId, delivered, null, next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The seam contracts that cancellation is the ONLY way this enumeration ends quietly, so
|
||||
@@ -3894,8 +3939,12 @@ public sealed partial class MTConnectDriver
|
||||
/// the reconnect backoff from ratcheting monotonically over a process lifetime.
|
||||
/// </param>
|
||||
/// <param name="Error">The failure, when there was one.</param>
|
||||
/// <param name="RaiseCountTo">
|
||||
/// Set on a <see cref="StreamVerdict.Reopen"/> the adaptive count asked for: the stream was persistently behind
|
||||
/// the Agent, and the next one opens with this <c>count</c> (R06:T-5).
|
||||
/// </param>
|
||||
private sealed record StreamOutcome(
|
||||
StreamVerdict Verdict, long Cursor, long InstanceId, bool DeliveredAny, Exception? Error);
|
||||
StreamVerdict Verdict, long Cursor, long InstanceId, bool DeliveredAny, Exception? Error, int? RaiseCountTo = null);
|
||||
|
||||
// ---- config DTOs ----
|
||||
|
||||
|
||||
Reference in New Issue
Block a user