fix(grpc-data): size projected unary reservations from the node's shape (#960); per-item status for projected items refused at create (#961)
#960: a projected read reserves 16 KiB for a fixed-size scalar (16 KiB + 256 B/element for a fixed-length fixed-size array) instead of the whole MaxPendingBytes, so the default 1 MiB admits 64 concurrent scalar reads; String/Bytes/variable arrays keep the whole cap, and a waiting whole-cap read holds back smaller ones for 100 ms (anti-starvation). Writes reserve 16 KiB + identifiers + 3 x the value bound (what the origin's DataWriteExecutor admits the batch against). Monitored-item renewals reserve 256 KiB. Measured: 4 x 250 ms maxAge=0 reads 1016 ms / 1 in flight -> 285 ms / 4 in flight. #961: TryAcquireProjectedItem reports BadResourceUnavailable for a stream-capacity refusal; the node manager's CreateMonitoredItem override turns the item manager's ServiceResultException into that item's status (was a whole-request fault), and an authority race is re-classified outside Lock like the per-item probe.
This commit is contained in:
+150
-8
@@ -175,32 +175,115 @@ public sealed partial class GrpcDataProjectedAccessTests
|
||||
access.ScheduleRead(demand!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeTrue();
|
||||
var update = await fixture.Published.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
update.Snapshot.Value.ShouldBe(42);
|
||||
fixture.ReadBudget.ShouldNotBeNull().MaxBytes.ShouldBe(1024 * 1024);
|
||||
// #960: a scalar read reserves (and may decode) only what its declared shape can answer, within the configured limit.
|
||||
fixture.ReadBudget.ShouldNotBeNull().MaxBytes.ShouldBe(ProjectedDataAccess.ProjectedEnvelopeBytes);
|
||||
await fixture.ConnectionDisposed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10")]
|
||||
public async Task Clamped_read_reserves_the_full_configured_budget_until_physical_completion()
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10"), Trait("Issue", "960")]
|
||||
public async Task Variable_size_read_reserves_the_full_configured_budget_until_physical_completion()
|
||||
{
|
||||
// A String value has no declared bound, so its read keeps the whole per-read cap (#960 sizes only bounded shapes).
|
||||
using var fixture = new AccessFixture(readOnly: true) { BlockAcquire = true }; using var access = fixture.Create(maxBytes: 1024 * 1024);
|
||||
fixture.Publish(42);
|
||||
UseValueKind(fixture, DataValueKind.String);
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var first).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var second).ShouldBeTrue();
|
||||
var sdkBudget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
try
|
||||
{
|
||||
access.ScheduleRead(first!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeTrue();
|
||||
first = null; // admitted work owns the lease
|
||||
await fixture.AcquireEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
access.ScheduleRead(second!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeFalse();
|
||||
second!.IsValid.ShouldBeTrue("a rejected admission leaves ownership with the caller");
|
||||
fixture.ReleaseAcquire.TrySetResult();
|
||||
await fixture.Published.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
SpinWait.SpinUntil(() => access.ScheduleRead(second, sdkBudget, fixture.Clock.GetTimestamp()), TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
||||
second = null; // admitted work owns the lease
|
||||
second = null;
|
||||
fixture.ReadBudget.ShouldNotBeNull().MaxBytes.ShouldBe(1024 * 1024);
|
||||
}
|
||||
finally { fixture.ReleaseAcquire.TrySetResult(); first?.Dispose(); second?.Dispose(); }
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10"), Trait("Issue", "960")]
|
||||
public async Task Scalar_reads_reserve_their_shape_bound_so_concurrent_reads_share_the_default_budget()
|
||||
{
|
||||
// The production default: DataConnections:MaxPendingBytes = 1 MiB. Before #960 every read reserved all of it, so only
|
||||
// ONE projected read (or write) could be in flight per node. The operation-count limit (4 here) is the bound now.
|
||||
using var fixture = new AccessFixture(readOnly: true) { BlockAcquire = true }; using var access = fixture.Create(maxDemands: 8, maxBytes: 1024 * 1024);
|
||||
fixture.Publish(42);
|
||||
var sdkBudget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
var demands = new List<IProjectedDemandLease?>();
|
||||
try
|
||||
{
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var demand).ShouldBeTrue();
|
||||
demands.Add(demand);
|
||||
}
|
||||
var admitted = 0;
|
||||
for (var i = 0; i < 4; i++)
|
||||
if (access.ScheduleRead(demands[i]!, sdkBudget, fixture.Clock.GetTimestamp())) { admitted++; demands[i] = null; }
|
||||
admitted.ShouldBe(4, "four scalar reads fit the 1 MiB budget at once");
|
||||
access.ScheduleRead(demands[4]!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeFalse("the operation-count limit still bounds them");
|
||||
fixture.ReleaseAcquire.TrySetResult();
|
||||
SpinWait.SpinUntil(() => access.ScheduleRead(demands[4]!, sdkBudget, fixture.Clock.GetTimestamp()), TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
||||
demands[4] = null;
|
||||
await fixture.Published.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
}
|
||||
finally { fixture.ReleaseAcquire.TrySetResult(); foreach (var demand in demands) demand?.Dispose(); }
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10"), Trait("Issue", "960")]
|
||||
public async Task A_waiting_whole_cap_read_holds_back_smaller_reads_until_it_is_admitted()
|
||||
{
|
||||
// Scalar reads now share the budget; a whole-cap (String) read that waits for capacity must not be starved by them.
|
||||
using var fixture = new AccessFixture(readOnly: true, streams: true) { BlockAcquire = true };
|
||||
using var access = fixture.Create(maxDemands: 8, maxBytes: 1024 * 1024);
|
||||
var stringMetadata = fixture.SecondProof!.Metadata with { ValueKind = DataValueKind.String };
|
||||
fixture.SecondProof = fixture.SecondProof with { Metadata = stringMetadata, OriginMetadata = fixture.SecondProof.OriginMetadata with { ValueKind = DataValueKind.String } };
|
||||
fixture.SecondDefinition = fixture.SecondDefinition! with { Metadata = stringMetadata };
|
||||
fixture.Publish(42);
|
||||
var sdkBudget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var first).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var second).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Read, out var third).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.SecondNodeId, fixture.Caller, DataOperations.Read, out var large).ShouldBeTrue();
|
||||
try
|
||||
{
|
||||
access.ScheduleRead(first!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeTrue();
|
||||
first = null;
|
||||
(await access.ReadNowAsync(large!, sdkBudget, fixture.Clock.GetTimestamp(), ct)).Status.ShouldBe(ProjectedOriginReadStatus.NotAdmitted);
|
||||
access.ScheduleRead(second!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeFalse("a waiting whole-cap read holds back smaller ones");
|
||||
// A waiter that gives up stops holding them back once the window lapses.
|
||||
fixture.Advance(ProjectedDataAccess.ReadDrainWindow + TimeSpan.FromMilliseconds(1));
|
||||
access.ScheduleRead(second!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeTrue();
|
||||
second = null;
|
||||
// It waits again; the scalar reads in flight finish and it is admitted ahead of the third.
|
||||
(await access.ReadNowAsync(large!, sdkBudget, fixture.Clock.GetTimestamp(), ct)).Status.ShouldBe(ProjectedOriginReadStatus.NotAdmitted);
|
||||
access.ScheduleRead(third!, sdkBudget, fixture.Clock.GetTimestamp()).ShouldBeFalse();
|
||||
fixture.ReleaseAcquire.TrySetResult();
|
||||
Task<ProjectedOriginRead>? read = null;
|
||||
SpinWait.SpinUntil(() =>
|
||||
{
|
||||
var attempt = access.ReadNowAsync(large!, sdkBudget, fixture.Clock.GetTimestamp(), ct);
|
||||
if (attempt.IsCompletedSuccessfully && attempt.Result.Status == ProjectedOriginReadStatus.NotAdmitted) return false;
|
||||
read = attempt; return true;
|
||||
}, TimeSpan.FromSeconds(5)).ShouldBeTrue("the waiting read is admitted once the scalar reads finish");
|
||||
await read!.WaitAsync(TimeSpan.FromSeconds(5), ct);
|
||||
SpinWait.SpinUntil(() => access.ScheduleRead(third!, sdkBudget, fixture.Clock.GetTimestamp()), TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
||||
third = null;
|
||||
}
|
||||
finally { fixture.ReleaseAcquire.TrySetResult(); first?.Dispose(); second?.Dispose(); third?.Dispose(); large?.Dispose(); }
|
||||
}
|
||||
|
||||
private static void UseValueKind(AccessFixture fixture, DataValueKind kind)
|
||||
{
|
||||
var metadata = fixture.Proof.Metadata with { ValueKind = kind };
|
||||
fixture.Proof = fixture.Proof with { Metadata = metadata, OriginMetadata = fixture.Proof.OriginMetadata with { ValueKind = kind } };
|
||||
fixture.Definition = fixture.Definition with { Metadata = metadata };
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000)]
|
||||
public async Task Read_without_subscribe_permission_preserves_origin_age_timestamps_and_source_proof()
|
||||
{
|
||||
@@ -250,12 +333,70 @@ public sealed partial class GrpcDataProjectedAccessTests
|
||||
var budget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
var result = await access.WriteAsync(demand!, 42, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken);
|
||||
result.State.ShouldBe(DataWriteState.OutcomeUnknown);
|
||||
fixture.SentWrite.ShouldNotBeNull().Budget.MaxBytes.ShouldBe(1024 * 1024);
|
||||
// #960: a scalar write reserves what it needs (envelope + the origin's write accounting), not the whole configured limit.
|
||||
var sent = fixture.SentWrite.ShouldNotBeNull();
|
||||
sent.Budget.MaxBytes.ShouldBeLessThan(ScalarWriteBytesBound);
|
||||
sent.Budget.MaxBytes.ShouldBeGreaterThanOrEqualTo(OriginWriteAccounting(sent, 288));
|
||||
fixture.WriteCalls.ShouldBe(1);
|
||||
}
|
||||
finally { demand?.Dispose(); }
|
||||
}
|
||||
|
||||
/// <summary>#960: more than one scalar write's reservation (envelope + identifiers + 3 × 288), less than two.</summary>
|
||||
private const long ScalarWriteBytesBound = 30 * 1024;
|
||||
|
||||
/// <summary>What the origin's DataWriteExecutor admits a one-item write batch against (3 × retained estimate + 512 + identifiers).</summary>
|
||||
private static long OriginWriteAccounting(WriteBatch sent, long valueEstimate)
|
||||
{
|
||||
var item = sent.Items.ShouldHaveSingleItem();
|
||||
return 3 * valueEstimate + 512 + 2L * ((item.CorrelationId?.Length ?? 0) + (item.OperationId?.Length ?? 0) +
|
||||
(item.View.Subject?.Length ?? 0) + (item.View.MappingId?.Length ?? 0) + (item.View.Address.OriginClusterId?.Length ?? 0) +
|
||||
(item.View.Address.NamespaceUri?.Length ?? 0) + (item.View.Address.IdentifierKind?.Length ?? 0) +
|
||||
(item.View.Address.Identifier?.Length ?? 0) + (item.ExpectedServingIncarnation?.Length ?? 0));
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10"), Trait("Issue", "960")]
|
||||
public async Task A_large_string_write_reserves_what_the_origin_admits_it_against()
|
||||
{
|
||||
// Sized below the cap, a write must still carry the budget the ORIGIN checks it against, or a long string that the
|
||||
// whole-cap reservation used to carry is refused there ("Write batch exceeds its byte limit").
|
||||
using var fixture = new AccessFixture(writable: true); using var access = fixture.Create(maxBytes: 1024 * 1024);
|
||||
UseValueKind(fixture, DataValueKind.String);
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var demand).ShouldBeTrue();
|
||||
try
|
||||
{
|
||||
var text = new string('x', 20_000);
|
||||
var budget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
(await access.WriteAsync(demand!, text, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken)).State
|
||||
.ShouldBe(DataWriteState.OutcomeUnknown);
|
||||
var sent = fixture.SentWrite.ShouldNotBeNull();
|
||||
sent.Budget.MaxBytes.ShouldBeGreaterThanOrEqualTo(OriginWriteAccounting(sent, 288 + 2L * text.Length));
|
||||
sent.Budget.MaxBytes.ShouldBeLessThan(1024 * 1024);
|
||||
}
|
||||
finally { demand?.Dispose(); }
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10"), Trait("Issue", "960")]
|
||||
public async Task Scalar_writes_are_dispatched_concurrently_within_the_default_budget()
|
||||
{
|
||||
using var fixture = new AccessFixture(writable: true) { BlockWrite = true }; using var access = fixture.Create(maxBytes: 1024 * 1024);
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var first).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var second).ShouldBeTrue();
|
||||
try
|
||||
{
|
||||
var budget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024);
|
||||
var one = access.WriteAsync(first!, 42, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken);
|
||||
await fixture.WriteEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
var two = access.WriteAsync(second!, 43, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken);
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref fixture.WriteCalls) == 2 || two.IsCompleted, TimeSpan.FromSeconds(5));
|
||||
fixture.WriteCalls.ShouldBe(2, "a second write is dispatched while the first is in flight, not rejected for capacity");
|
||||
fixture.ReleaseWrite.TrySetResult();
|
||||
(await one.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.OutcomeUnknown);
|
||||
(await two.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.OutcomeUnknown);
|
||||
}
|
||||
finally { fixture.ReleaseWrite.TrySetResult(); first?.Dispose(); second?.Dispose(); }
|
||||
}
|
||||
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10")]
|
||||
public async Task Clamped_write_rejects_oversized_value_before_dispatch()
|
||||
{
|
||||
@@ -276,7 +417,8 @@ public sealed partial class GrpcDataProjectedAccessTests
|
||||
[Fact(Timeout = 30000), Trait("Phase", "P10")]
|
||||
public async Task Cancelled_clamped_write_keeps_capacity_until_the_physical_send_finishes()
|
||||
{
|
||||
using var fixture = new AccessFixture(writable: true) { BlockWrite = true }; using var access = fixture.Create(maxBytes: 1024 * 1024);
|
||||
// A budget that holds one scalar write's reservation but not two (#960), so the held one leaves no room for another.
|
||||
using var fixture = new AccessFixture(writable: true) { BlockWrite = true }; using var access = fixture.Create(maxBytes: ScalarWriteBytesBound);
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var first).ShouldBeTrue();
|
||||
access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var second).ShouldBeTrue();
|
||||
using var cancel = new CancellationTokenSource();
|
||||
|
||||
Reference in New Issue
Block a user