dd1d02e25f
G-9: a gateway-answered InvalidArgument/OutOfRange/NotFound/Unimplemented read is a connectivity observation (open, streak ends), not a failure; outages still degrade. G-17: capped reverse/end-only QuestDB events reads find the n+1-th latest ts with an early-exit backward scan, then read ts >= cut ordered (ts DESC, event_id DESC). G-18: Distribute takes a bridged/nonGood flag instead of per-sample delegates (O(buckets) allocation). QuestDB integration fixture waits up to 60 s for WAL visibility (shared host is CPU-saturated; the 5 s production default made alarm-writer tests read back fewer rows). Co-Authored-By: Claude <noreply@anthropic.com>
112 lines
4.8 KiB
C#
112 lines
4.8 KiB
C#
using System.Text;
|
|
using Npgsql;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Conformance;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// G-17 (#993): a capped reverse / end-only events read finds its cut with a backward scan that stops early, then reads
|
|
/// <c>ts >= cut</c>. The cut must never change the answer: ties AT the cut are still chosen by <c>event_id</c> descending.
|
|
/// </summary>
|
|
[Collection(QuestDbCollection.Name)]
|
|
public sealed class ReverseEventsTests(QuestDbFixture fx) : IAsyncLifetime
|
|
{
|
|
private static readonly CancellationToken Ct = CancellationToken.None;
|
|
private QuestDbConformanceBackend _b = null!;
|
|
|
|
private static DateTime At(double s) => ConformanceData.At(s);
|
|
|
|
public async ValueTask InitializeAsync() => _b = await QuestDbConformanceBackend.CreateConcreteAsync(fx);
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_b is not null) await _b.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Three events per second (ties), alternating between two sources; ids sort differently from insertion order.</summary>
|
|
private async Task<List<(DateTime T, string Id, string Source)>> SeedAsync(int seconds)
|
|
{
|
|
await _b.Boot.EnsureAsync(Ct);
|
|
var rows = new List<(DateTime, string, string)>();
|
|
for (var s = 0; s < seconds; s++)
|
|
for (var k = 0; k < 3; k++)
|
|
rows.Add((_b.Round(At(s)), $"e{s:D3}-{(2 - k)}", (s + k) % 2 == 0 ? "srcA" : "srcB")); // inserted id-descending within a tie
|
|
foreach (var chunk in rows.Chunk(50))
|
|
{
|
|
var sql = new StringBuilder($"INSERT INTO {_b.Conn.EventTable} (ts, event_id, source, severity, message, received_utc) VALUES ");
|
|
for (var i = 0; i < chunk.Length; i++)
|
|
{
|
|
if (i > 0) sql.Append(", ");
|
|
var n = i * 3;
|
|
sql.Append($"(${n + 1}, ${n + 2}, ${n + 3}, 500, 'm', ${n + 1})");
|
|
}
|
|
await _b.ExecuteAsync(sql.ToString(), cmd =>
|
|
{
|
|
foreach (var (t, id, src) in chunk)
|
|
{
|
|
QuestDbConnection.BindTimestamp(cmd, t);
|
|
QuestDbConnection.BindText(cmd, id);
|
|
QuestDbConnection.BindText(cmd, src);
|
|
}
|
|
});
|
|
}
|
|
await _b.Wal.WaitAsync(_b.Conn.EventTable, Ct);
|
|
return rows;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(null)]
|
|
[InlineData("srcA")]
|
|
[InlineData("srcB")]
|
|
public async Task A_capped_reverse_read_equals_the_head_of_the_uncapped_one_for_every_cut_including_ties(string? source)
|
|
{
|
|
var rows = await SeedAsync(20);
|
|
var src = _b.CreateDataSource();
|
|
var expected = rows
|
|
.Where(r => source is null || r.Source == source)
|
|
.OrderByDescending(r => r.T).ThenByDescending(r => r.Id, StringComparer.Ordinal)
|
|
.Select(r => r.Id).ToList();
|
|
|
|
var start = At(100); // reversed window: (end, start]
|
|
var end = At(-1);
|
|
var all = await src.ReadEventsAsync(source, start, end, 0, reverse: true, Ct);
|
|
all.Events.Select(e => e.EventId).ShouldBe(expected);
|
|
all.ContinuationPoint.ShouldBeNull();
|
|
|
|
for (var n = 1; n <= expected.Count + 1; n++)
|
|
{
|
|
var page = await src.ReadEventsAsync(source, start, end, n, reverse: true, Ct);
|
|
page.Events.Select(e => e.EventId).ShouldBe(expected.Take(n), $"n = {n}");
|
|
if (n < expected.Count)
|
|
Encoding.UTF8.GetString(page.ContinuationPoint!).ShouldContain(expected[n], Case.Sensitive, $"n = {n}: the first event not returned");
|
|
else
|
|
page.ContinuationPoint.ShouldBeNull($"n = {n}");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task The_cut_statement_is_a_backward_scan_not_a_forward_scan_of_the_window()
|
|
{
|
|
await SeedAsync(5);
|
|
foreach (var withSource in new[] { false, true })
|
|
{
|
|
var sql = "EXPLAIN " + QuestDbHistorianDataSource.ReverseEventCutSql(_b.Conn.EventTable, withSource);
|
|
await using var conn = await _b.Conn.OpenAsync(Ct);
|
|
await using var cmd = _b.Conn.CreateCommand(conn, sql);
|
|
QuestDbConnection.BindTimestamp(cmd, At(-1));
|
|
QuestDbConnection.BindTimestamp(cmd, At(100));
|
|
if (withSource) QuestDbConnection.BindText(cmd, "srcA");
|
|
QuestDbConnection.BindLong(cmd, 11);
|
|
var plan = new StringBuilder();
|
|
await using (var r = await cmd.ExecuteReaderAsync(Ct))
|
|
while (await r.ReadAsync(Ct)) plan.AppendLine(r.GetString(0));
|
|
|
|
plan.ToString().ShouldContain("backward", Case.Insensitive, $"withSource={withSource}:\n{plan}");
|
|
}
|
|
}
|
|
}
|