perf(sql): rate-limit per-poll warnings and cache poll plans per reference list (R06:S-6, S-7)
S-6: a frozen group's BadTimeout warning and a failing group's query-error warning (with SQL text) logged every poll per group indefinitely; both are now rate-limited per plan (once a minute), and an uncoercible cell warns once per tag instead of once per poll. Every poll still publishes the Bad status. S-7: plans and the slot map were rebuilt every poll although a poll loop passes the same reference list each cycle. They are cached per list instance (weakly keyed) and rebuilt only when a reference resolves to a different definition instance, which a catalog-gate swap produces. Refs #744 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -605,6 +605,126 @@ public sealed class SqlPollReaderTests
|
||||
dialect.Factory, " ", dialect, CommandTimeout, OperationTimeout, 4, false, _ => null));
|
||||
}
|
||||
|
||||
// ---- R06:S-7: plans are cached per subscription reference list ----
|
||||
|
||||
/// <summary>
|
||||
/// A poll loop hands the reader the SAME reference list every cycle; re-planning it (and rebuilding the
|
||||
/// slot dictionary keyed by a 12-field record) every poll was pure per-poll garbage. Planned once,
|
||||
/// and the cached plan still reads correct values every time — the positive control.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task ReadAsync_theSameReferenceList_isPlannedOnce()
|
||||
{
|
||||
using var fixture = new SqlitePollFixture();
|
||||
var reader = Reader(fixture,
|
||||
KvTag("Speed", SqlitePollFixture.PresentKey), KvTag("Pressure", SqlitePollFixture.SecondPresentKey));
|
||||
IReadOnlyList<string> refs = ["Speed", "Pressure"];
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var snapshots = await reader.ReadAsync(refs, CancellationToken.None);
|
||||
snapshots[0].Value.ShouldBe(SqlitePollFixture.PresentValue);
|
||||
snapshots[1].Value.ShouldBe(SqlitePollFixture.SecondPresentValue);
|
||||
}
|
||||
|
||||
reader.PlansBuiltForTests.ShouldBe(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cache is invalidated by the definitions themselves: a catalog-gate swap (or any re-resolve)
|
||||
/// hands back different definition instances, and the next poll re-plans against them.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task ReadAsync_whenAReferenceResolvesToADifferentDefinition_rePlans()
|
||||
{
|
||||
using var fixture = new SqlitePollFixture();
|
||||
var table = new Dictionary<string, SqlTagDefinition>(StringComparer.Ordinal)
|
||||
{
|
||||
["Speed"] = KvTag("Speed", SqlitePollFixture.PresentKey),
|
||||
};
|
||||
var reader = new SqlPollReader(fixture.Factory, fixture.ConnectionString, new SqliteDialect(),
|
||||
commandTimeout: CommandTimeout, operationTimeout: OperationTimeout,
|
||||
maxConcurrentGroups: 4, nullIsBad: false, resolve: r => table.GetValueOrDefault(r));
|
||||
IReadOnlyList<string> refs = ["Speed"];
|
||||
|
||||
(await reader.ReadAsync(refs, CancellationToken.None))[0].Value.ShouldBe(SqlitePollFixture.PresentValue);
|
||||
table["Speed"] = KvTag("Speed", SqlitePollFixture.SecondPresentKey); // re-pointed by a new resolve
|
||||
|
||||
(await reader.ReadAsync(refs, CancellationToken.None))[0].Value.ShouldBe(SqlitePollFixture.SecondPresentValue);
|
||||
reader.PlansBuiltForTests.ShouldBe(2);
|
||||
}
|
||||
|
||||
// ---- R06:S-6: per-poll warnings are rate-limited ----
|
||||
|
||||
/// <summary>
|
||||
/// A dropped table polled every second used to log a warning — with the full SQL text — on every
|
||||
/// poll, per group, forever. One per minute per plan now; every poll still publishes the Bad status.
|
||||
/// The first poll's warning is the positive control.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task ReadAsync_aPersistentlyFailingGroup_warnsOncePerInterval()
|
||||
{
|
||||
using var fixture = new SqlitePollFixture();
|
||||
var logger = new RecordingLogger();
|
||||
var reader = Reader(fixture, logger,
|
||||
new SqlTagDefinition("Gone", SqlTagModel.KeyValue, "NoSuchTable",
|
||||
KeyColumn: SqlitePollFixture.KeyColumn, KeyValue: "k", ValueColumn: SqlitePollFixture.ValueColumn));
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
(await reader.ReadAsync(["Gone"], CancellationToken.None))[0]
|
||||
.StatusCode.ShouldBe(SqlStatusCodes.BadCommunicationError);
|
||||
}
|
||||
|
||||
logger.Entries.Count(e => e.Level == LogLevel.Warning).ShouldBe(1);
|
||||
}
|
||||
|
||||
/// <summary>A cell that will not coerce is a per-tag refusal: one warning per tag, not one per poll.</summary>
|
||||
[Fact]
|
||||
public async Task ReadAsync_anUncoercibleCell_warnsOncePerTag()
|
||||
{
|
||||
using var fixture = new SqlitePollFixture();
|
||||
var logger = new RecordingLogger();
|
||||
var reader = Reader(fixture, logger,
|
||||
KvTag("Speed", SqlitePollFixture.PresentKey, declaredType: DriverDataType.DateTime));
|
||||
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
(await reader.ReadAsync(["Speed"], CancellationToken.None))[0]
|
||||
.StatusCode.ShouldBe(SqlStatusCodes.BadTypeMismatch);
|
||||
}
|
||||
|
||||
logger.Entries.Count(e => e.Level == LogLevel.Warning).ShouldBe(1);
|
||||
}
|
||||
|
||||
/// <summary>A frozen group's BadTimeout warning is rate-limited too; both polls still time out.</summary>
|
||||
[Fact]
|
||||
public async Task ReadAsync_aRepeatedlyTimedOutGroup_warnsOncePerInterval()
|
||||
{
|
||||
using var fixture = new SqlitePollFixture();
|
||||
var logger = new RecordingLogger();
|
||||
var reader = new SqlPollReader(fixture.Factory, fixture.ConnectionString, new SqliteDialect(),
|
||||
commandTimeout: TimeSpan.FromSeconds(30), operationTimeout: TimeSpan.FromMilliseconds(500),
|
||||
maxConcurrentGroups: 4, nullIsBad: false,
|
||||
resolve: Resolver(KvTag("Speed", SqlitePollFixture.PresentKey)), logger: logger);
|
||||
|
||||
var locker = fixture.OpenNewConnection();
|
||||
Execute(locker, "BEGIN EXCLUSIVE");
|
||||
try
|
||||
{
|
||||
(await reader.ReadAsync(["Speed"], CancellationToken.None))[0].StatusCode.ShouldBe(SqlStatusCodes.BadTimeout);
|
||||
(await reader.ReadAsync(["Speed"], CancellationToken.None))[0].StatusCode.ShouldBe(SqlStatusCodes.BadTimeout);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Execute(locker, "ROLLBACK");
|
||||
locker.Dispose();
|
||||
}
|
||||
|
||||
logger.Entries.Count(e => e.Level == LogLevel.Warning && e.Message.Contains("operation timeout"))
|
||||
.ShouldBe(1);
|
||||
}
|
||||
|
||||
// ---- R06:S-3: the concurrency gate is released ----
|
||||
|
||||
/// <summary>An idle reader releases its gate on Dispose, and a disposed reader starts no new group.</summary>
|
||||
|
||||
Reference in New Issue
Block a user