Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Historian/IHistorianPassWriter.cs
T
Joseph Doherty ee314d1b9e perf(historian): one QuestDB WAL wait per drain pass, with an exponential poll (lmxopcua#746)
The QuestDB live writer waited for WAL visibility after every tag group, and
the wait polled wal_tables() every 10 ms. With DrainConcurrency = 8 that was
up to eight pollers at ~100 queries/s each against a database whose WAL
apply was already behind.

New Runtime seam IHistorianPassWriter (WriteLiveValuesInPassAsync +
FlushAsync). The recorder writes every group of a pass through it and calls
FlushAsync once after all groups report, before the ack, whenever any group
wrote (an un-acked pass is redelivered and its insert-if-absent check must
see this pass's rows). A flush failure does not fail the pass. The QuestDB
writer implements it: in-pass writes only mark a flush as owed; FlushAsync
waits once. The poll now backs off 10 -> 20 -> ... -> 200 ms, never past the
timeout. Direct WriteLiveValuesAsync callers still wait per call.

Also updates the QuestDB cancelled-write integration test to the rethrow
contract from the previous commit.

Rebased from the unpushed remed/lane-H2 branch (originally authored 2026-10-22
in session_01Ub9hGmYLQvn9XETzw7CMsT) onto current master.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 15:40:46 -05:00

37 lines
2.2 KiB
C#

using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Historian;
/// <summary>
/// An <see cref="IHistorianValueWriter"/> whose per-write completion work can be done ONCE per drain pass instead of
/// once per tag group — QuestDB's WAL visibility wait, which a backend needs so that an immediate redelivery's
/// insert-if-absent check sees the rows just written. The continuous-historization recorder writes every group of a
/// pass with <see cref="WriteLiveValuesInPassAsync"/> and then calls <see cref="FlushAsync"/> once, after every group
/// has reported and before it acks the pass.
/// </summary>
/// <remarks>
/// The cancellation contract is the recorder's for both members: a cancellation of the caller's token (the pass) is
/// rethrown; any other failure of a write is <c>false</c>. A <see cref="FlushAsync"/> failure does not fail the pass —
/// the rows are committed; only their visibility to the next pass is unproven.
/// </remarks>
public interface IHistorianPassWriter : IHistorianValueWriter
{
/// <summary>
/// Writes a batch of live values for a single tag as part of a drain pass, deferring the per-write completion
/// work to <see cref="FlushAsync"/>. Same result contract as <see cref="IHistorianValueWriter.WriteLiveValuesAsync"/>.
/// </summary>
/// <param name="tag">Fully-qualified historian tag the values are recorded against.</param>
/// <param name="values">The samples to write, in append order.</param>
/// <param name="ct">The pass's cancellation token.</param>
/// <returns><c>true</c> when the backend stored the batch; <c>false</c> on a retryable failure.</returns>
Task<bool> WriteLiveValuesInPassAsync(string tag, IReadOnlyList<HistorizationValue> values, CancellationToken ct);
/// <summary>
/// Completes the pass: does the deferred completion work for every write since the last flush (QuestDB: one WAL
/// visibility wait). A no-op when nothing was written.
/// </summary>
/// <param name="ct">The pass's cancellation token.</param>
/// <returns>A task that completes when the pass's writes are visible (or the wait gave up).</returns>
Task FlushAsync(CancellationToken ct);
}