fix(grpc-data): move the in-stream root re-resolve off the condition frame loop (#934)

RetryDueAsync awaited the root metadata request (10 s budget) inside the
frame loop, stalling every mapping's frames on that connection while the
origin's metadata was slow. The request now runs as a task (FetchRootsAsync
reads only the immutable root views); the task joins the loop's wake set
beside the retry timer, and its answer is applied on the loop
(ApplyResolvedAsync: RootView or MarkRefused, then the re-add). All runner
state stays loop-owned. While resolving the mapping is Unavailable (unknown,
T2) and is not re-scheduled. Test: a hung second metadata call no longer
blocks another mapping's delta; releasing it re-adds the item exactly once.
This commit is contained in:
Joseph Doherty
2026-11-25 10:58:28 -05:00
parent 2eb31f62d2
commit 5d946fde55
3 changed files with 142 additions and 32 deletions
@@ -1134,6 +1134,43 @@ public sealed class GrpcDataProjectedConditionAccessTests
await f.WaitAsync(() => f.Sink.Live.Contains(Key("x")));
}
[Fact]
public async Task Issue934_a_slow_in_stream_root_re_resolve_does_not_stall_frames_of_the_other_mappings()
{
await using var f = TimedFixture(300, ("map-a", DataRealm.Raw, 10), ("map-b", DataRealm.Raw, 10));
f.Connections.MetadataFails.Add(0); // map-a's root fails at the open: Unavailable, its item is not opened
f.Connections.MetadataBlockCall = 2; // …and its in-stream re-resolve (the 2nd metadata call) hangs until released
using var demand = f.Access.AcquireEventDemand(null);
var stream = await f.NextStreamAsync();
stream.Mappings.ShouldBe(["map-b"]);
stream.Ack();
stream.Begin("map-b", total: 1);
stream.Records("map-b", Record(Key("x"), 3));
stream.Complete("map-b");
await f.WaitAsync(() => f.Sink.Live.Contains(Key("x")));
f.Connections.MetadataFails.Clear();
await f.WaitAsync(() => Volatile.Read(ref f.Connections.MetadataCalls) == 2); // the re-resolve is in flight
// The frame loop is not blocked by it: map-b's delta is applied while the metadata call is still pending.
stream.Records("map-b", Record(Key("y"), 3));
await f.WaitAsync(() => f.Sink.Live.Contains(Key("y")));
f.Connections.MetadataRelease.Task.IsCompleted.ShouldBeFalse();
stream.AddsOf("map-a").ShouldBe(0, "not re-added before its root resolved");
f.Access.MappingStatuses(Connection)!["map-a"].ShouldBe(ProjectedConditionMappingStatus.Unavailable, "unknown while resolving (T2)");
// Positive control: once the answer arrives it is applied on the loop and the item is re-added — exactly once.
f.Connections.MetadataRelease.SetResult();
await f.WaitAsync(() => stream.AddsOf("map-a") == 1);
stream.AckAdd("map-a");
stream.Begin("map-a", total: 1);
stream.SnapshotRecords("map-a", Record(Key("w"), 3));
stream.Complete("map-a");
await f.WaitAsync(() => f.Sink.Live.Contains(Key("w")));
f.Access.MappingStatuses(Connection)!["map-a"].ShouldBe(ProjectedConditionMappingStatus.Complete);
stream.AddsOf("map-a").ShouldBe(1);
Volatile.Read(ref f.Connections.MetadataCalls).ShouldBe(2);
}
private sealed class Fixture : IAsyncDisposable
{
internal static readonly DataView DefaultRawPlacement =
@@ -1281,6 +1318,9 @@ public sealed class GrpcDataProjectedConditionAccessTests
public HashSet<int> MetadataFails { get; } = [];
/// <summary>#931 N4: metadata requests made (one per stream attempt, plus in-stream root retries).</summary>
public int MetadataCalls;
/// <summary>#934: when set, the metadata call with this 1-based number waits for <see cref="MetadataRelease"/>.</summary>
public int? MetadataBlockCall;
public TaskCompletionSource MetadataRelease { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>The serving roles of the next streams (Primary when empty).</summary>
public Queue<string> Roles { get; } = new();
public int PrimarySubscribes;
@@ -1307,11 +1347,18 @@ public sealed class GrpcDataProjectedConditionAccessTests
public TimeProvider Clock => TimeProvider.System;
public Task<DataCapabilities> GetCapabilitiesAsync(DataBudget budget, CancellationToken cancellationToken) => throw new NotSupportedException();
public Task<DataBrowseBranch> BrowseAsync(BrowseRequest request, CancellationToken cancellationToken) => throw new NotSupportedException();
public Task<IReadOnlyList<DataItemResult<DataMetadata>>> GetMetadataAsync(MetadataRequest request, CancellationToken cancellationToken) =>
Interlocked.Increment(ref owner.MetadataCalls) < 0 ? throw new InvalidOperationException() : Task.FromResult<IReadOnlyList<DataItemResult<DataMetadata>>>(request.Views.Select((v, i) => owner.MetadataFails.Contains(i)
public async Task<IReadOnlyList<DataItemResult<DataMetadata>>> GetMetadataAsync(MetadataRequest request, CancellationToken cancellationToken)
{
if (Interlocked.Increment(ref owner.MetadataCalls) == owner.MetadataBlockCall)
await owner.MetadataRelease.Task.WaitAsync(cancellationToken); // #934: a slow origin metadata answer
return Answer(request);
}
private IReadOnlyList<DataItemResult<DataMetadata>> Answer(MetadataRequest request) =>
request.Views.Select((v, i) => owner.MetadataFails.Contains(i)
? new DataItemResult<DataMetadata>("0", DataItemStatus.Unavailable, null, null)
: new DataItemResult<DataMetadata>("0", DataItemStatus.Good, new DataMetadata(v with { Subject = v.Address.Identifier }, v.Address.Identifier, "Folder", DataValueKind.Null, -1, [], false,
DataOperations.Browse, 1, null, null, false), null)).ToArray());
DataOperations.Browse, 1, null, null, false), null)).ToArray();
public Task<IReadOnlyList<ReadResult>> ReadAsync(ReadRequest request, CancellationToken cancellationToken) => throw new NotSupportedException();
public Task<IReadOnlyList<WriteItemResult>> WriteAsync(WriteBatch request, CancellationToken cancellationToken) => throw new NotSupportedException();
public Task<IDataSubscription> SubscribeAsync(CancellationToken cancellationToken)