fix(store-and-forward): immediate delivery never overtakes buffered rows of its lane (SF-1 follow-up, decision B)
SF-1 kept the retry sweep per-(category,target) FIFO, but a NEW message could still be delivered inline while older rows of the same lane were buffered and backing off: EnqueueAsync's immediate attempt, and the first attempt that ExternalSystem.CachedCall / Database.CachedWrite make themselves before handing a transient failure to S&F. A recovered target then received B before A, and A's later retry overwrote B. Now each of those paths first probes the lane (StoreAndForwardService. LaneHasPendingAsync: SELECT EXISTS seek on the covering idx_sf_messages_lane, pinned by an EXPLAIN QUERY PLAN test; fails open to the old behaviour). An occupied lane gets no inline attempt: the message is buffered due-immediately (LastAttemptAt null, deferToSweep) behind the older rows and the sweep delivers in order. Empty lanes (or parked-only) are unchanged. Notify.Send already deferred to the sweep. Return contract unchanged: handle/TrackedOperationId at once, WasBuffered=true, tracking reads Pending. Regression tests (fail before, pass after): service, CachedCall, CachedWrite; plus lane-probe + query-plan storage tests. Component-StoreAndForward.md and Component-ExternalSystemGateway.md updated.
This commit is contained in:
@@ -942,6 +942,55 @@ public class StoreAndForwardStorageTests : IAsyncLifetime, IDisposable
|
||||
Assert.DoesNotContain(plan, p => p.Contains("TEMP B-TREE") && !p.Contains("LAST TERM"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decision B (2026-12-02 review follow-up): the immediate-delivery lane probe is answered
|
||||
/// by a seek on the covering <c>idx_sf_messages_lane</c> (leading status, category, target)
|
||||
/// — no table scan, no payload read, no sort — so it is cheap on every enqueue even with a
|
||||
/// large backlog. Runs the production SQL verbatim.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task LaneHasPending_SeeksTheCoveringLaneIndex()
|
||||
{
|
||||
await using var connection = _localDb.Db.CreateConnection();
|
||||
await using var cmd = connection.CreateCommand();
|
||||
cmd.CommandText = "EXPLAIN QUERY PLAN " + StoreAndForwardStorage.LaneHasPendingSql;
|
||||
cmd.Parameters.AddWithValue("@pending", 0);
|
||||
cmd.Parameters.AddWithValue("@category", 0);
|
||||
cmd.Parameters.AddWithValue("@target", "t");
|
||||
var plan = new List<string>();
|
||||
await using (var reader = await cmd.ExecuteReaderAsync())
|
||||
while (await reader.ReadAsync())
|
||||
plan.Add(reader.GetString(3));
|
||||
|
||||
Assert.Contains(plan, p => p.Contains("SEARCH")
|
||||
&& p.Contains("COVERING INDEX idx_sf_messages_lane")
|
||||
&& p.Contains("status=? AND category=? AND target=?"));
|
||||
// The only scan is the EXISTS wrapper's "SCAN CONSTANT ROW"; sf_messages is never scanned.
|
||||
Assert.DoesNotContain(plan, p => p.StartsWith("SCAN sf_messages", StringComparison.Ordinal));
|
||||
Assert.DoesNotContain(plan, p => p.Contains("TEMP B-TREE"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decision B: the lane probe is true only for a Pending row of the exact
|
||||
/// <c>(category, target)</c>; Parked rows and other lanes do not count.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task LaneHasPending_CountsOnlyPendingRowsOfThatLane()
|
||||
{
|
||||
// NewMsg rows are ExternalSystem / "t".
|
||||
Assert.False(await _storage.LaneHasPendingAsync(StoreAndForwardCategory.ExternalSystem, "t"));
|
||||
|
||||
await _storage.EnqueueAsync(NewMsg("parked", StoreAndForwardMessageStatus.Parked, DateTimeOffset.UtcNow));
|
||||
Assert.False(await _storage.LaneHasPendingAsync(StoreAndForwardCategory.ExternalSystem, "t"));
|
||||
|
||||
await _storage.EnqueueAsync(NewMsg("pending", StoreAndForwardMessageStatus.Pending, DateTimeOffset.UtcNow));
|
||||
Assert.True(await _storage.LaneHasPendingAsync(StoreAndForwardCategory.ExternalSystem, "t"));
|
||||
Assert.False(await _storage.LaneHasPendingAsync(StoreAndForwardCategory.ExternalSystem, "t-other"));
|
||||
Assert.False(await _storage.LaneHasPendingAsync(StoreAndForwardCategory.CachedDbWrite, "t"));
|
||||
}
|
||||
|
||||
// ReplaceAll_SwapsTheEntireBuffer_Atomically was DELETED with ReplaceAllAsync in
|
||||
// LocalDb Phase 2, and deliberately not replaced. It asserted a destructive
|
||||
// delete-all-then-insert-all, which was the standby's anti-entropy apply: a resync
|
||||
|
||||
Reference in New Issue
Block a user