fix(historian): pending-appends gauge follows the append queue back to zero (lmxopcua#746)
v2-ci / unit-tests (push) Waiting to run

otopcua.historization.recorder.pending_appends was written only from the
actor thread: when a burst was accepted (just after the increment, so
never 0) and on GetStatus, which nothing in production sends. Once ingest
stopped the gauge froze at the depth the last burst saw, so after a
backpressure episode it kept reporting a queue near the 64-burst cap that
had long drained.

The append loop now publishes the depth after every decrement
(ReportPendingAppends). The mailbox-depth gauge stays actor-thread only;
its description now says when it is sampled. New test (non-parallel
collection, the gauge is process-wide): the gauge reads 2 with one append
stuck and one queued, and 0 after the queue drains with no status
request. With the loop's report removed it fails (stays 2).

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:38:02 -05:00
parent 0961b3ed93
commit b051bdc87c
3 changed files with 69 additions and 6 deletions
@@ -0,0 +1,50 @@
using Akka.Actor;
using Akka.TestKit.Xunit2;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.Historian;
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Historian;
/// <summary>
/// The recorder's process-wide gauges. Their backing values are static, so these tests run alone (the collection
/// disables parallelization): any other recorder test running at the same time would overwrite what is asserted.
/// </summary>
[CollectionDefinition(Name, DisableParallelization = true)]
public sealed class RecorderGaugeCollection
{
/// <summary>The collection name.</summary>
public const string Name = "historization-recorder-gauges";
}
/// <summary>The <c>otopcua.historization.recorder.pending_appends</c> gauge tracks the append queue, not the last burst.</summary>
[Collection(RecorderGaugeCollection.Name)]
public sealed class ContinuousHistorizationRecorderGaugeTests : TestKit
{
private static readonly DateTime T0 = new(2026, 11, 9, 8, 0, 0, DateTimeKind.Utc);
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(10);
private static VirtualTagActor.DependencyValueBatch Burst(int value) =>
new([new VirtualTagActor.DependencyValueChanged("A", (double)value, T0.AddSeconds(value))]);
[Fact]
public async Task The_pending_appends_gauge_returns_to_zero_when_the_queue_drains_without_a_status_request()
{
var log = new EventLog();
var outbox = new LoggingOutbox(log) { AppendGate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously) };
var rec = Sys.ActorOf(ContinuousHistorizationRecorder.Props(
CreateTestProbe().Ref, new LoggingPassWriter(log), outbox, historizedRefs: ["A"], drainInterval: TimeSpan.FromSeconds(30)));
rec.Tell(Burst(0));
await outbox.AppendEntered.Task.WaitAsync(Wait);
rec.Tell(Burst(1));
// The second burst is accepted while the first append is stuck: the gauge shows the queue.
await AwaitAssertAsync(() => ContinuousHistorizationMetrics.PendingAppendsGauge.ShouldBe(2), Wait);
// No GetStatus from here on (production never asks one): the append loop itself must publish the drained depth.
outbox.AppendGate.SetResult();
await AwaitAssertAsync(() => outbox.Appended.Count.ShouldBe(2), Wait);
await AwaitAssertAsync(() => ContinuousHistorizationMetrics.PendingAppendsGauge.ShouldBe(0), Wait);
}
}