fix(historian): #691.5 a reverse walk proves the rest empty once and carries it page to page

The last pages of a deep reverse page-through on a forward-only backend
each walked down to 1970 one x4 growth step at a time (~14-16 reads,
~2.5 s live). After two empty segments the walk now asks once per page
for the whole remainder at the page budget (a truncated answer is
discarded and the walk carries on), and the stretch the page's last
complete read proved empty rides the continuation point
(HistoryContinuationState.EmptyFromTicks/EmptyToTicks, session-held, so
no versioning) so the next page steps over it. Fuzz now pages with the
walk's own hints and with true random empty stretches.
This commit is contained in:
Joseph Doherty
2026-10-12 16:32:00 -04:00
parent d33992034d
commit 186b7d5ea5
7 changed files with 222 additions and 29 deletions
@@ -55,6 +55,13 @@ namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
/// <see cref="HistoryCursorKind.Raw"/> reverse reads on a forward-only backend only (Gitea #686): the segment width the
/// walk-back ended on, so the next page starts at the observed density instead of re-discovering it. 0 = none.
/// </param>
/// <param name="EmptyFromTicks">
/// Walked reverse reads only (Gitea #691): with <paramref name="EmptyToTicks"/>, a stretch <c>(from, to)</c> the previous
/// page's walk proved holds no row, so the next page steps over it rather than re-walking it (to the 1970 floor, ~14
/// reads, at the end of a deep page-through). 0/0 = none. The cursor lives in the session's in-memory store (never
/// serialized), so a new field with a "none" default is all the versioning it needs.
/// </param>
/// <param name="EmptyToTicks">The exclusive upper end of that stretch.</param>
internal sealed record HistoryContinuationState(
string Tagname,
DateTime NextStartUtc,
@@ -65,7 +72,13 @@ internal sealed record HistoryContinuationState(
long LastModifiedId = 0,
DateTime WindowStartUtc = default,
bool Reverse = false,
long SegmentWidthTicks = 0);
long SegmentWidthTicks = 0,
long EmptyFromTicks = 0,
long EmptyToTicks = 0)
{
/// <summary>The walk-back hint this cursor carries (#686/#689/#691).</summary>
public ReverseWalkHint WalkHint => new(SegmentWidthTicks, EmptyFromTicks, EmptyToTicks);
}
/// <summary>The HistoryRead kind that issued a <see cref="HistoryContinuationState"/>.</summary>
internal enum HistoryCursorKind
@@ -6184,7 +6184,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
DateTime startUtc;
var boundarySkip = 0;
long segmentWidth = 0; // #686: the walk-back's width, carried page to page
var walkHint = default(ReverseWalkHint); // #686/#689/#691: the walk-back's width + proven-empty stretch, page to page
string tagname;
if (inboundCp is { Length: > 0 })
@@ -6205,7 +6205,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
boundarySkip = state.BoundarySkipCount;
endUtc = state.EndUtc;
numValuesPerNode = state.NumValuesPerNode;
segmentWidth = state.SegmentWidthTicks;
walkHint = state.WalkHint;
}
else
{
@@ -6243,9 +6243,9 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
var page = await ReverseRawReader.ReadAsync(
source, tagname, startUtc, endUtc, numValuesPerNode, MaxTieClusterOverfetch,
Clock.GetUtcNow().UtcDateTime, segmentWidth, ct)
Clock.GetUtcNow().UtcDateTime, walkHint, ct)
.ConfigureAwait(false);
segmentWidth = page.WidthHintTicks;
walkHint = page.Hint;
sourceResult = new HistorianRead(page.Samples, ContinuationPoint: null);
}
else
@@ -6324,7 +6324,8 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
if (nextStartUtc is { } resumeAt)
{
var clusterState = new HistoryContinuationState(
tagname, resumeAt, endUtc, nextSkip, numValuesPerNode, SegmentWidthTicks: segmentWidth);
tagname, resumeAt, endUtc, nextSkip, numValuesPerNode, SegmentWidthTicks: walkHint.WidthTicks,
EmptyFromTicks: walkHint.EmptyFromTicks, EmptyToTicks: walkHint.EmptyToTicks);
clusterCp = _historyContinuationStore.Save(session, clusterState);
}
@@ -6347,7 +6348,8 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2
{
HistoryPaging.ComputeResumeCursor(samples, out var nextStart, out var skip);
var nextState = new HistoryContinuationState(
tagname, nextStart, endUtc, skip, numValuesPerNode, SegmentWidthTicks: segmentWidth);
tagname, nextStart, endUtc, skip, numValuesPerNode, SegmentWidthTicks: walkHint.WidthTicks,
EmptyFromTicks: walkHint.EmptyFromTicks, EmptyToTicks: walkHint.EmptyToTicks);
// Save may return null (no session on this request) ⇒ degrade to single-shot for this node.
// Built AFTER historyData so a failure projecting samples can never orphan a stored cursor.
outboundCp = _historyContinuationStore.Save(session, nextState);
@@ -2,7 +2,17 @@ namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
/// <summary>What one walked-back page tells the next page of the same read (carried in the continuation point).</summary>
/// <param name="WidthTicks">The segment width the walk ended on, aimed at the page's density — the next page's first width (0 = none).</param>
internal readonly record struct ReverseWalkHint(long WidthTicks);
/// <param name="EmptyFromTicks">
/// With <paramref name="EmptyToTicks"/>: a stretch <c>(EmptyFrom, EmptyTo)</c> (both exclusive) a complete segment read
/// proved holds no row (#691) — typically from the oldest row the page saw down to the walk's floor. A resumed page
/// steps straight over it instead of re-walking it. Both 0 = none.
/// </param>
/// <param name="EmptyToTicks">The exclusive upper end of the empty stretch.</param>
internal readonly record struct ReverseWalkHint(long WidthTicks, long EmptyFromTicks = 0, long EmptyToTicks = 0)
{
/// <summary>Whether an empty stretch is carried.</summary>
public bool HasEmptyStretch => EmptyToTicks > EmptyFromTicks && EmptyToTicks > 0;
}
/// <summary>One forward read of a segment: the rows (ascending by time) and whether a cap cut the read short.</summary>
/// <typeparam name="T">Row type.</typeparam>
@@ -46,6 +56,15 @@ internal readonly record struct ForwardSegmentRead<T>(IReadOnlyList<T> Rows, boo
/// fit at the tie budget — is refused.
/// </para>
/// <para>
/// <b>Proving the rest empty (#691).</b> Walking down to the floor through empty history used to take one read per ×4
/// growth step (~14 reads from a minute to the epoch) — on every page that ran out of rows. After two consecutive empty
/// segments the walk asks once per page for the WHOLE remainder <c>(lo, segHi]</c> at the page budget: a complete
/// answer ends the walk in one read; a truncated one (deep history) is discarded and the walk carries on as before. And
/// the stretch a page's last complete segment proved empty below its oldest row rides the continuation point
/// (<see cref="ReverseWalkHint.EmptyFromTicks"/>), so the next page steps over it — the rows of a page are often the
/// last ones before a stretch the previous page already read.
/// </para>
/// <para>
/// <b>Edges.</b> A segment is requested as <c>[⌊segLo⌋, ⌈segHi + 1 tick⌉]</c>, both rounded outward to the millisecond
/// (the gateway's / AVEVA's resolution), and filtered to <c>(segLo, segHi]</c> on the rows' own timestamps — exact
/// whether the backend's forward end is exclusive or inclusive and whether it truncates its bounds; a row fabricated at
@@ -141,14 +160,27 @@ internal static class ReverseWalk
var knownWidth = 0L;
var segHi = hiTicks;
// #691: the remainder probe (once per page, after two empty segments) and the empty stretches.
var emptyRun = 0;
var probed = false;
var carried = hint.HasEmptyStretch ? (hint.EmptyFromTicks, hint.EmptyToTicks) : (0L, 0L);
(long From, long To) proven = (0L, 0L);
while (segHi > stopTicks && result.Count < want)
{
var w = width;
if (carried.Item2 > carried.Item1 && segHi > carried.Item1 && segHi < carried.Item2)
{
segHi = carried.Item1; // (from, segHi] lies inside a stretch a previous page proved empty
continue;
}
var probe = !probed && known == 0 && emptyRun >= 2 && segHi - stopTicks > width;
var w = probe ? segHi - stopTicks : width;
if (known > 0) w = Math.Min(w, Math.Max(1, (segHi - known) / 2));
var segLo = segHi - stopTicks <= w ? stopTicks : segHi - w;
var start = Utc(AlignDown(segLo, granularity));
var end = Utc(AlignUp(segHi + 1, granularity));
var segBudget = discovering ? discoveryBudget : pageBudget;
var segBudget = discovering && !probe ? discoveryBudget : pageBudget;
var cap = segBudget + 2;
IReadOnlyList<T> rows;
var narrowed = false;
@@ -160,6 +192,14 @@ internal static class ReverseWalk
rows = segment.Rows;
if (!segment.Truncated || rows.Count == 0 || ticksOf(rows[^1]) > segHi) break; // complete for (segLo, segHi]
if (probe)
{
// Deep history below: the probe saw only its oldest rows. Carry on walking from the same segHi.
probed = true;
narrowed = true;
break;
}
var leading = 0;
var inner = 0;
var firstInner = 0L;
@@ -226,8 +266,18 @@ internal static class ReverseWalk
if (narrowed) continue; // re-read the same segHi, narrower
var count = AppendNewestFirst(rows, ticksOf, tieOrder, segLo, segHi, result, (int)Math.Min(int.MaxValue, want));
var count = AppendNewestFirst(rows, ticksOf, tieOrder, segLo, segHi, result, (int)Math.Min(int.MaxValue, want),
out var oldest);
// This complete read proves (segLo, oldest) empty — or the whole segment when it held nothing.
proven = count > 0 ? (segLo, oldest) : (segLo, segHi + 1);
if (probe)
{
probed = true;
segHi = segLo; // the whole remainder is read
continue;
}
emptyRun = count == 0 ? emptyRun + 1 : 0;
var grown = width > maxWidth / 4 ? maxWidth : width * 4;
if (count == 0)
{
@@ -253,13 +303,31 @@ internal static class ReverseWalk
}
}
return (result, new ReverseWalkHint(width));
// Carry the stretch the last complete read proved empty, joined to the one carried in when they touch; when they
// do not, keep the one reaching the floor (it saves a whole walk), else the newer.
var next = carried;
if (proven.To > proven.From)
{
if (carried.Item2 > carried.Item1 && proven.From < carried.Item2 && carried.Item1 < proven.To)
next = (Math.Min(proven.From, carried.Item1), Math.Max(proven.To, carried.Item2));
else if (!(carried.Item2 > carried.Item1 && carried.Item1 <= stopTicks) || proven.From <= stopTicks)
next = proven;
}
return (result, new ReverseWalkHint(width, next.Item1, next.Item2));
}
/// <summary>Appends the rows in <c>(segLo, segHi]</c> newest-first (until <paramref name="limit"/>) and counts them.</summary>
private static int AppendNewestFirst<T>(
IReadOnlyList<T> rows, Func<T, long> ticksOf, IComparer<T>? tieOrder, long segLo, long segHi, List<T> result, int limit)
IReadOnlyList<T> rows, Func<T, long> ticksOf, IComparer<T>? tieOrder, long segLo, long segHi, List<T> result, int limit) =>
AppendNewestFirst(rows, ticksOf, tieOrder, segLo, segHi, result, limit, out _);
/// <summary>As above, also returning the oldest in-range timestamp (meaningless when none).</summary>
private static int AppendNewestFirst<T>(
IReadOnlyList<T> rows, Func<T, long> ticksOf, IComparer<T>? tieOrder, long segLo, long segHi, List<T> result, int limit,
out long oldest)
{
oldest = long.MaxValue;
IReadOnlyList<T> ordered = rows;
if (tieOrder is not null)
{
@@ -278,6 +346,7 @@ internal static class ReverseWalk
var ts = ticksOf(ordered[i]);
if (ts <= segLo || ts > segHi) continue;
count++;
if (ts < oldest) oldest = ts;
if (result.Count < limit) result.Add(ordered[i]);
}