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:
@@ -412,6 +412,74 @@ public class ExternalSystemClientTests : IDisposable
|
||||
Assert.Equal(0, handlerInvocations);
|
||||
}
|
||||
|
||||
// ── Decision B (2026-12-02 review follow-up): a CachedCall never overtakes its lane ──
|
||||
|
||||
/// <summary>
|
||||
/// A CachedCall must not overtake an older buffered call to the same external system.
|
||||
/// Call A fails transiently and is buffered; the system recovers; call B arrives. Pre-fix B
|
||||
/// made its HTTP request inline at once, ahead of A. B must make NO inline request: it is
|
||||
/// buffered behind A (WasBuffered, the TrackedOperationId pinned as the message id) for
|
||||
/// the sweep to deliver in order. A different system's lane is unaffected.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task CachedCall_LaneHasBufferedCall_BuffersBehindItWithoutHttpRequest()
|
||||
{
|
||||
var system = new ExternalSystemDefinition("TestAPI", "https://api.example.com", "none") { Id = 1 };
|
||||
var method = new ExternalSystemMethod("postData", "POST", "/post") { Id = 1, ExternalSystemDefinitionId = 1 };
|
||||
var other = new ExternalSystemDefinition("OtherAPI", "https://other.example.com", "none") { Id = 2 };
|
||||
var otherMethod = new ExternalSystemMethod("postData", "POST", "/post") { Id = 2, ExternalSystemDefinitionId = 2 };
|
||||
StubResolution(system, method);
|
||||
StubResolution(other, otherMethod);
|
||||
|
||||
var status = HttpStatusCode.InternalServerError;
|
||||
var requests = 0;
|
||||
_httpClientFactory.CreateClient(Arg.Any<string>()).Returns(_ => new HttpClient(new CallbackHttpMessageHandler(() =>
|
||||
{
|
||||
Interlocked.Increment(ref requests);
|
||||
return status;
|
||||
})));
|
||||
|
||||
var localDb = CreateLocalDb("EsgLaneOrder");
|
||||
var storage = new StoreAndForwardStorage(localDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
||||
await storage.InitializeAsync();
|
||||
var sf = new StoreAndForwardService(storage, new StoreAndForwardOptions
|
||||
{
|
||||
DefaultRetryInterval = TimeSpan.FromMinutes(10),
|
||||
RetryTimerInterval = TimeSpan.FromMinutes(10),
|
||||
}, NullLogger<StoreAndForwardService>.Instance);
|
||||
var client = new ExternalSystemClient(
|
||||
_httpClientFactory, _repository, NullLogger<ExternalSystemClient>.Instance, storeAndForward: sf);
|
||||
|
||||
var a = await client.CachedCallAsync("TestAPI", "postData");
|
||||
Assert.True(a.WasBuffered);
|
||||
Assert.Equal(1, requests);
|
||||
|
||||
status = HttpStatusCode.OK;
|
||||
var bId = ZB.MOM.WW.ScadaBridge.Commons.Types.TrackedOperationId.New();
|
||||
var b = await client.CachedCallAsync("TestAPI", "postData", trackedOperationId: bId);
|
||||
|
||||
Assert.True(b.Success);
|
||||
Assert.True(b.WasBuffered); // pre-fix: false (HTTP request made inline, overtaking A)
|
||||
Assert.Equal(1, requests); // pre-fix: 2
|
||||
var bRow = await sf.GetMessageByIdAsync(bId.ToString());
|
||||
Assert.NotNull(bRow);
|
||||
Assert.Null(bRow!.LastAttemptAt);
|
||||
|
||||
// Another system's lane is empty — its call still goes out inline.
|
||||
var c = await client.CachedCallAsync("OtherAPI", "postData");
|
||||
Assert.False(c.WasBuffered);
|
||||
Assert.Equal(2, requests);
|
||||
}
|
||||
|
||||
/// <summary>Test helper: answers every request with the status the callback returns.</summary>
|
||||
private sealed class CallbackHttpMessageHandler(Func<HttpStatusCode> respond) : HttpMessageHandler
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
=> Task.FromResult(new HttpResponseMessage(respond()) { Content = new StringContent("{}") });
|
||||
}
|
||||
|
||||
// ── per-system call timeout ──
|
||||
|
||||
/// <summary>Verifies that a call to a slow system times out as a transient error within the configured window.</summary>
|
||||
|
||||
Reference in New Issue
Block a user