fix(grpc-data): #967 a rich write whose invoker fails before dispatch on an externally-rejected claim releases its slot
When the invoker threw without running the dispatch delegate (open breaker / bulkhead) after the claim had already been completed RejectedBeforeDispatch by someone else, Complete(RichRejected) returned false, the path answered OutcomeUnknown and never completed the physical task. The late-write slot, the (driver, reference) target and the ticket's byte lease stayed reserved for the process lifetime, so every later write to that tag was refused as 'Outstanding driver write capacity exhausted'. Both catch paths now go through FailRichAttempt: an undispatched terminal claim can never dispatch (dispatch is a CAS from pending), so the physical task completes with the claim's pre-dispatch outcome and the caller hears RejectedBeforeDispatch. Only a dispatched claim stays Unknown and is left to the driver's own completion.
This commit is contained in:
@@ -1519,10 +1519,7 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
var outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown;
|
return FailRichAttempt(msg.Claim, physical);
|
||||||
if (outcome == RichRejected) physical.TrySetResult(outcome);
|
|
||||||
else msg.Claim.Complete(outcome);
|
|
||||||
return outcome;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
try
|
try
|
||||||
@@ -1534,9 +1531,7 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
outcome = msg.Claim.Complete(RichRejected) ? RichRejected : RichUnknown;
|
outcome = FailRichAttempt(msg.Claim, physical);
|
||||||
if (outcome == RichRejected) physical.TrySetResult(outcome);
|
|
||||||
else msg.Claim.Complete(outcome);
|
|
||||||
}
|
}
|
||||||
// #964: when the physical write has finished, its late-write slot must be back BEFORE the caller
|
// #964: when the physical write has finished, its late-write slot must be back BEFORE the caller
|
||||||
// hears the outcome or the actor frees the tag — both let a next write to the same target reach
|
// hears the outcome or the actor frees the tag — both let a next write to the same target reach
|
||||||
@@ -1551,6 +1546,29 @@ public sealed class DriverInstanceActor : ReceiveActor, IWithTimers
|
|||||||
finally { self.Tell(new WriteCompleted(msg.Ticket.Proof.DriverReference, incarnation)); }
|
finally { self.Tell(new WriteCompleted(msg.Ticket.Proof.DriverReference, incarnation)); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Settles a rich write whose invoker attempt failed or was abandoned. A claim that never dispatched can never
|
||||||
|
/// dispatch any more once terminal (dispatch is a CAS from pending), so no driver call exists or can start:
|
||||||
|
/// the physical task is completed with the claim's pre-dispatch outcome, releasing the late-write slot,
|
||||||
|
/// target and ticket. That holds whether THIS path rejected the claim or someone else (an external
|
||||||
|
/// pre-dispatch deadline) already had — #967: the latter used to leave the physical task pending forever,
|
||||||
|
/// reserving the slot and target for the process lifetime. Only a DISPATCHED claim is left to the driver's
|
||||||
|
/// own completion (it may still be running) and answered Unknown.
|
||||||
|
/// </summary>
|
||||||
|
private static DataWriteOutcome FailRichAttempt(WriteClaim claim, TaskCompletionSource<DataWriteOutcome> physical)
|
||||||
|
{
|
||||||
|
if (claim.Complete(RichRejected) || !claim.IsDispatched)
|
||||||
|
{
|
||||||
|
// CurrentOutcome is null only while another completer is mid-publish; any pre-dispatch completion is a
|
||||||
|
// RejectedBeforeDispatch, so RichRejected is the same state.
|
||||||
|
var outcome = claim.CurrentOutcome ?? RichRejected;
|
||||||
|
physical.TrySetResult(outcome);
|
||||||
|
return outcome;
|
||||||
|
}
|
||||||
|
claim.Complete(RichUnknown);
|
||||||
|
return RichUnknown;
|
||||||
|
}
|
||||||
|
|
||||||
private static async Task ObserveRichDriver(Task<IReadOnlyList<DataWriteOutcome>> task,
|
private static async Task ObserveRichDriver(Task<IReadOnlyList<DataWriteOutcome>> task,
|
||||||
TaskCompletionSource<DataWriteOutcome> physical)
|
TaskCompletionSource<DataWriteOutcome> physical)
|
||||||
{
|
{
|
||||||
|
|||||||
+35
@@ -231,6 +231,34 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
|
|||||||
driver.Calls.ShouldBe(0);
|
driver.Calls.ShouldBe(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Invoker_failing_before_the_call_site_on_an_externally_rejected_claim_releases_the_slot_and_target()
|
||||||
|
{
|
||||||
|
// #967: the invoker (e.g. an open breaker) throws without ever running the dispatch delegate, after the
|
||||||
|
// claim was already completed RejectedBeforeDispatch by someone else. Nothing reached the driver, but the
|
||||||
|
// physical task was never completed, so the late-write slot, the target and the ticket stayed reserved
|
||||||
|
// for the process lifetime and every later write to the tag was refused as capacity exhausted.
|
||||||
|
var driver = new RichDriver();
|
||||||
|
var invoker = new PausedWriteInvoker { FailFirstWithoutCalling = true };
|
||||||
|
var actor = Connect(driver, new RecordingGate(), new LateWriteObserver(1, TimeSpan.FromMinutes(1)), invoker);
|
||||||
|
var (ticket, claim) = NewWrite("breaker-open", 1);
|
||||||
|
var response = actor.Ask<DataWriteOutcome>(new DriverInstanceActor.WriteAttributeWithOutcome(ticket, claim), Timeout, TestContext.Current.CancellationToken);
|
||||||
|
await invoker.Entered.Task.WaitAsync(Timeout, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
claim.Complete(new(DataWriteState.RejectedBeforeDispatch, 0x800A0000u, "deadline before dispatch")).ShouldBeTrue();
|
||||||
|
invoker.Release.TrySetResult();
|
||||||
|
// Nothing was dispatched, so the caller hears the claim's own pre-dispatch rejection, not "unknown".
|
||||||
|
(await response.WaitAsync(Timeout, TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.RejectedBeforeDispatch);
|
||||||
|
claim.IsDispatched.ShouldBeFalse();
|
||||||
|
driver.Calls.ShouldBe(0);
|
||||||
|
|
||||||
|
var (nextTicket, nextClaim) = NewWrite("after-breaker", 2);
|
||||||
|
var next = await actor.Ask<DataWriteOutcome>(new DriverInstanceActor.WriteAttributeWithOutcome(nextTicket, nextClaim),
|
||||||
|
Timeout, TestContext.Current.CancellationToken);
|
||||||
|
next.State.ShouldBe(DataWriteState.DriverConfirmedSuccess, next.Diagnostic);
|
||||||
|
driver.Calls.ShouldBe(1);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Queued_write_revalidates_after_an_earlier_write_finishes()
|
public async Task Queued_write_revalidates_after_an_earlier_write_finishes()
|
||||||
{
|
{
|
||||||
@@ -688,6 +716,10 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
|
|||||||
public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
public bool SawIdempotent { get; private set; }
|
public bool SawIdempotent { get; private set; }
|
||||||
|
/// <summary>When set, the FIRST write call fails after release without ever invoking the call site — the
|
||||||
|
/// shape of an open circuit breaker / bulkhead rejection. Later calls pass straight through.</summary>
|
||||||
|
public bool FailFirstWithoutCalling { get; init; }
|
||||||
|
private int _writeCalls;
|
||||||
public ValueTask<TResult> ExecuteAsync<TResult>(DriverCapability capability, string hostName,
|
public ValueTask<TResult> ExecuteAsync<TResult>(DriverCapability capability, string hostName,
|
||||||
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken) => callSite(cancellationToken);
|
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken) => callSite(cancellationToken);
|
||||||
public ValueTask ExecuteAsync(DriverCapability capability, string hostName,
|
public ValueTask ExecuteAsync(DriverCapability capability, string hostName,
|
||||||
@@ -696,8 +728,11 @@ public sealed class GrpcDataDriverOutcomeTests : RuntimeActorTestBase
|
|||||||
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken)
|
Func<CancellationToken, ValueTask<TResult>> callSite, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
SawIdempotent = isIdempotent;
|
SawIdempotent = isIdempotent;
|
||||||
|
var first = Interlocked.Increment(ref _writeCalls) == 1;
|
||||||
|
if (FailFirstWithoutCalling && !first) return await callSite(cancellationToken);
|
||||||
Entered.TrySetResult();
|
Entered.TrySetResult();
|
||||||
await Release.Task;
|
await Release.Task;
|
||||||
|
if (FailFirstWithoutCalling) throw new InvalidOperationException("circuit open");
|
||||||
return await callSite(cancellationToken);
|
return await callSite(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user