Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,44 +197,57 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
RequiresFileUpload = false
};

SourceSchemaResult? result = null;
var hasSomeErrors = false;
var results = new SourceSchemaResultCollector(initialCapacity: 2);

try
{
var client = context.GetClient(schemaName, lookup.Operation.Type);
using var clientScope = diagnosticEvents.ExecuteSourceSchemaRequest(context, this, schemaName);

await foreach (var current in client.ExecuteAsync(context, request, cancellationToken)
.ConfigureAwait(false))
if (client is HttpSourceSchemaClient httpClient
&& HttpSourceSchemaClient.CanExecuteBuffered(lookup.Operation.Type, requiresFileUpload: false))
{
if (result is not null)
await httpClient
.ExecuteBufferedAsync(context, request, results, cancellationToken)
.ConfigureAwait(false);
}
else
{
await foreach (var current in client.ExecuteAsync(context, request, cancellationToken)
.ConfigureAwait(false))
{
current.Dispose();
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
}

result = current;
// A second result ends the read, so a source that keeps its stream open is not awaited.
if (results.Count > 0)
{
current.Dispose();
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
}

if (current.Errors is not null)
{
hasSomeErrors = true;
results.OnResult(0, current);
}
}

if (results.Count > 1)
{
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
result?.Dispose();
results.DisposeResults();
return ExecutionStatus.Failed;
}
catch (Exception exception)
{
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);
result?.Dispose();
results.DisposeResults();
context.AddErrors(exception, variables, operation.ResultSelectionSet);
return ExecutionStatus.Failed;
}

var result = results.Single;
var hasSomeErrors = results.HasErrors;

if (result is null)
{
context.AddErrors(
Expand All @@ -259,7 +272,10 @@ private async ValueTask<ExecutionStatus> ExecuteSingleAsync(
result,
hasSomeErrors);
hasPendingMerge = true;
context.EnqueuePendingMerge(pendingMerge);

// ExecuteAsync reports the completion of this node right after we return,
// and that signal wakes the executor for the merge as well.
context.EnqueuePendingMergeBeforeCompletion(pendingMerge);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -328,69 +344,44 @@ private async ValueTask<ExecutionStatus> ExecuteBatchAsync(
// the executor loop so downstream nodes can consume the data after completion.
var client = context.GetClient(schemaName, requests[0].OperationType);
receivedResults.AsSpan(0, operationCount).Clear();
var overallStatus = ExecutionStatus.Success;

var sink = new EntityBatchResultSink(
this,
context,
schemaName,
operationByIndex,
representationByIndex,
variablesByIndex,
receivedResults,
cancellationToken);

using var clientScope = diagnosticEvents.ExecuteSourceSchemaRequest(context, this, schemaName);

await foreach (var batchResult in client.ExecuteBatchAsync(context, requests, cancellationToken)
.ConfigureAwait(false))
if (client is HttpSourceSchemaClient httpClient && httpClient.CanExecuteBatchBuffered(requests))
{
var requestIndex = batchResult.RequestIndex;
var op = operationByIndex[requestIndex];
var result = batchResult.Result;
var hasErrors = result.Errors is not null;

receivedResults[requestIndex] = true;

var pendingMerge = default(PendingMerge);
var hasPendingMerge = false;

try
{
pendingMerge = PendingMerge.RepresentationSingle(
this,
schemaName,
s_entitiesSource,
op.ResultSelectionSet,
variablesByIndex[requestIndex],
representationByIndex[requestIndex],
result,
hasErrors);
hasPendingMerge = true;
context.EnqueuePendingMerge(pendingMerge);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
if (hasPendingMerge)
{
pendingMerge.DisposeUnmerged();
}

return ExecutionStatus.Failed;
}
catch (Exception exception)
await httpClient
.ExecuteBatchBufferedAsync(context, requests, sink, cancellationToken)
.ConfigureAwait(false);
}
else
{
await foreach (var batchResult in client.ExecuteBatchAsync(context, requests, cancellationToken)
.ConfigureAwait(false))
{
if (hasPendingMerge)
{
pendingMerge.DisposeUnmerged();
}
else
if (!sink.OnResult(batchResult.RequestIndex, batchResult.Result))
{
result.Dispose();
break;
}

diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
context.AddErrors(exception, variablesByIndex[requestIndex], op.ResultSelectionSet);
overallStatus = ExecutionStatus.Failed;
continue;
}
}

if (hasErrors && overallStatus == ExecutionStatus.Success)
{
overallStatus = ExecutionStatus.PartialSuccess;
}
if (sink.IsCanceled)
{
return ExecutionStatus.Failed;
}

var overallStatus = sink.Status;

// Verify that the downstream service returned a result for every
// operation in the batch. A missing result means the service did
// not implement the batch protocol correctly. We surface this as
Expand Down Expand Up @@ -681,6 +672,91 @@ private static SelectionPath GetPrimaryTarget(OperationDefinition operation)
_ => throw ThrowHelper.UnknownOperationDefinition(operation.GetType())
};

/// <summary>
/// Queues the merge of each result of a batch as it arrives and records which operations
/// received a result. A result that cannot be queued fails its operation but not the batch.
/// </summary>
private sealed class EntityBatchResultSink(
ApolloOperationBatchExecutionNode node,
OperationPlanContext context,
string schemaName,
OperationDefinition[] operationByIndex,
RepresentationValue[] representationByIndex,
ImmutableArray<VariableValues>[] variablesByIndex,
bool[] receivedResults,
CancellationToken cancellationToken)
: ISourceSchemaResultSink
{
/// <summary>
/// Gets the status of the batch from the results received so far.
/// </summary>
public ExecutionStatus Status { get; private set; } = ExecutionStatus.Success;

/// <summary>
/// Gets whether queueing a result was canceled, which stops reading the batch.
/// </summary>
public bool IsCanceled { get; private set; }

public bool OnResult(int requestIndex, SourceSchemaResult result)
{
var op = operationByIndex[requestIndex];
var hasErrors = result.Errors is not null;

receivedResults[requestIndex] = true;

var pendingMerge = default(PendingMerge);
var hasPendingMerge = false;

try
{
pendingMerge = PendingMerge.RepresentationSingle(
node,
schemaName,
s_entitiesSource,
op.ResultSelectionSet,
variablesByIndex[requestIndex],
representationByIndex[requestIndex],
result,
hasErrors);
hasPendingMerge = true;
context.EnqueuePendingMerge(pendingMerge);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
if (hasPendingMerge)
{
pendingMerge.DisposeUnmerged();
}

IsCanceled = true;
return false;
}
catch (Exception exception)
{
if (hasPendingMerge)
{
pendingMerge.DisposeUnmerged();
}
else
{
result.Dispose();
}

context.DiagnosticEvents.SourceSchemaStoreError(context, node, schemaName, exception);
context.AddErrors(exception, variablesByIndex[requestIndex], op.ResultSelectionSet);
Status = ExecutionStatus.Failed;
return true;
}

if (hasErrors && Status == ExecutionStatus.Success)
{
Status = ExecutionStatus.PartialSuccess;
}

return true;
}
}

private static string ValidateOperations(OperationDefinition[] operations)
{
if (operations.Length == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,47 +248,59 @@ protected override async ValueTask<ExecutionStatus> OnExecuteAsync(
LookupTypeName = _lookup.EntityTypeName
};

SourceSchemaResult? result = null;
var hasSomeErrors = false;
var results = new SourceSchemaResultCollector(initialCapacity: 2);

try
{
var client = context.GetClient(schemaName, _lookup.Operation.Type);
using var clientScope = diagnosticEvents.ExecuteSourceSchemaRequest(context, this, schemaName);

await foreach (var current in client.ExecuteAsync(context, request, cancellationToken).ConfigureAwait(false))
if (client is HttpSourceSchemaClient httpClient
&& HttpSourceSchemaClient.CanExecuteBuffered(_lookup.Operation.Type, requiresFileUpload: false))
{
if (result is not null)
await httpClient
.ExecuteBufferedAsync(context, request, results, cancellationToken)
.ConfigureAwait(false);
}
else
{
await foreach (var current in client.ExecuteAsync(context, request, cancellationToken).ConfigureAwait(false))
{
current.Dispose();
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
// A second result ends the read, so a source that keeps its stream open is not awaited.
if (results.Count > 0)
{
current.Dispose();
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
}

results.OnResult(0, current);
}
}

result = current;

if (current.Errors is not null)
{
hasSomeErrors = true;
}
if (results.Count > 1)
{
throw ThrowHelper.ApolloEntityFetchProducedMultipleResults();
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// If the execution of the node was cancelled, either the entire request was cancelled
// or the execution was halted. In both cases we do not want to produce any errors
// and just exit the node as quickly as possible.
result?.Dispose();
results.DisposeResults();
return ExecutionStatus.Failed;
}
catch (Exception exception)
{
diagnosticEvents.SourceSchemaTransportError(context, this, schemaName, exception);
result?.Dispose();
results.DisposeResults();

context.AddErrors(exception, variables, _resultSelectionSet);
return ExecutionStatus.Failed;
}

var result = results.Single;
var hasSomeErrors = results.HasErrors;
var pendingMerge = default(PendingMerge);
var hasPendingMerge = false;

Expand All @@ -306,14 +318,11 @@ protected override async ValueTask<ExecutionStatus> OnExecuteAsync(
result,
hasSomeErrors);
hasPendingMerge = true;
}

if (hasPendingMerge)
{
context.EnqueuePendingMerge(pendingMerge);
// ExecuteAsync reports the completion of this node right after we return,
// and that signal wakes the executor for the merge as well.
context.EnqueuePendingMergeBeforeCompletion(pendingMerge);
}

result = null;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Expand All @@ -335,7 +344,7 @@ protected override async ValueTask<ExecutionStatus> OnExecuteAsync(
}
else
{
result?.Dispose();
results.DisposeResults();
}

diagnosticEvents.SourceSchemaStoreError(context, this, schemaName, exception);
Expand Down
Loading
Loading