Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk: Adds retry on RequestEntityTooLarge #2124

Merged
merged 8 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -84,7 +84,7 @@ public virtual async Task<TransactionalBatchOperationResult> AddAsync(

string resolvedPartitionKeyRangeId = await this.ResolvePartitionKeyRangeIdAsync(operation, cancellationToken).ConfigureAwait(false);
BatchAsyncStreamer streamer = this.GetOrAddStreamerForPartitionKeyRange(resolvedPartitionKeyRangeId);
ItemBatchOperationContext context = new ItemBatchOperationContext(resolvedPartitionKeyRangeId, BatchAsyncContainerExecutor.GetRetryPolicy(this.cosmosContainer, this.retryOptions));
ItemBatchOperationContext context = new ItemBatchOperationContext(resolvedPartitionKeyRangeId, BatchAsyncContainerExecutor.GetRetryPolicy(this.cosmosContainer, operation.OperationType, this.retryOptions));
operation.AttachContext(context);
streamer.Add(operation);
return await context.OperationTask;
Expand Down Expand Up @@ -133,10 +133,12 @@ internal virtual async Task ValidateOperationAsync(

private static IDocumentClientRetryPolicy GetRetryPolicy(
ContainerInternal containerInternal,
OperationType operationType,
RetryOptions retryOptions)
{
return new BulkPartitionKeyRangeGoneRetryPolicy(
return new BulkExecutionRetryPolicy(
containerInternal,
operationType,
new ResourceThrottleRetryPolicy(
retryOptions.MaxRetryAttemptsOnThrottledRequests,
retryOptions.MaxRetryWaitTimeInSeconds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
/// <see cref="BatchAsyncBatcher"/>
/// <see cref="ItemBatchOperationContext"/>
internal sealed class BulkPartitionKeyRangeGoneRetryPolicy : IDocumentClientRetryPolicy
internal sealed class BulkExecutionRetryPolicy : IDocumentClientRetryPolicy
{
private readonly IDocumentClientRetryPolicy nextRetryPolicy;
private readonly OperationType operationType;
private readonly ContainerInternal container;

public BulkPartitionKeyRangeGoneRetryPolicy(
public BulkExecutionRetryPolicy(
ContainerInternal container,
OperationType operationType,
IDocumentClientRetryPolicy nextRetryPolicy)
{
this.container = container ?? throw new ArgumentNullException(nameof(container));
this.operationType = operationType;
this.nextRetryPolicy = nextRetryPolicy;
}

Expand Down Expand Up @@ -80,6 +83,8 @@ public void OnBeforeSendRequest(DocumentServiceRequest request)
this.nextRetryPolicy.OnBeforeSendRequest(request);
}

private bool IsReadRequest => this.operationType == OperationType.Read;
j82w marked this conversation as resolved.
Show resolved Hide resolved

private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
HttpStatusCode? statusCode,
SubStatusCodes? subStatusCode,
Expand All @@ -103,6 +108,14 @@ private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
}
}

// Batch API can return 413 which means the response is bigger than 4Mb.
// Operations that exceed the 4Mb limit are returned as 413, while the operations within the 4Mb limit will be 200
if (this.IsReadRequest
ealsur marked this conversation as resolved.
Show resolved Hide resolved
&& statusCode == HttpStatusCode.RequestEntityTooLarge)
{
return ShouldRetryResult.RetryAfter(TimeSpan.Zero);
}

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,59 @@ private BatchAsyncBatcherExecuteDelegate ExecutorWithCompletingPartitionMigratio
return new PartitionKeyRangeBatchExecutionResult(request.PartitionKeyRangeId, request.Operations, batchresponse);
};

private readonly BatchAsyncBatcherExecuteDelegate ExecutorWith413
= async (PartitionKeyRangeServerBatchRequest request, CancellationToken cancellationToken) =>
{
List<TransactionalBatchOperationResult> results = new List<TransactionalBatchOperationResult>();
ItemBatchOperation[] arrayOperations = new ItemBatchOperation[request.Operations.Count];
int index = 0;
foreach (ItemBatchOperation operation in request.Operations)
{
if (index == 0)
{
// First operation is fine
results.Add(
new TransactionalBatchOperationResult(HttpStatusCode.OK)
{
ETag = operation.Id
});
}
else
{
// second operation is too big
results.Add(
new TransactionalBatchOperationResult(HttpStatusCode.RequestEntityTooLarge)
{
ETag = operation.Id
});
}

arrayOperations[index++] = operation;
}

MemoryStream responseContent = await new BatchResponsePayloadWriter(results).GeneratePayloadAsync();

SinglePartitionKeyServerBatchRequest batchRequest = await SinglePartitionKeyServerBatchRequest.CreateAsync(
partitionKey: null,
operations: new ArraySegment<ItemBatchOperation>(arrayOperations),
serializerCore: MockCosmosUtil.Serializer,
cancellationToken: cancellationToken);

ResponseMessage responseMessage = new ResponseMessage((HttpStatusCode)207)
{
Content = responseContent
};

TransactionalBatchResponse batchresponse = await TransactionalBatchResponse.FromResponseMessageAsync(
responseMessage,
batchRequest,
MockCosmosUtil.Serializer,
true,
CancellationToken.None);

return new PartitionKeyRangeBatchExecutionResult(request.PartitionKeyRangeId, request.Operations, batchresponse);
};

// The response will include all but 2 operation responses
private BatchAsyncBatcherExecuteDelegate ExecutorWithLessResponses
= async (PartitionKeyRangeServerBatchRequest request, CancellationToken cancellationToken) =>
Expand Down Expand Up @@ -448,12 +501,14 @@ public async Task CannotAddToDispatchedBatch()
[TestMethod]
public async Task RetrierGetsCalledOnSplit()
{
IDocumentClientRetryPolicy retryPolicy1 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy1 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

IDocumentClientRetryPolicy retryPolicy2 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy2 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

ItemBatchOperation operation1 = this.CreateItemBatchOperation();
Expand All @@ -477,12 +532,14 @@ public async Task RetrierGetsCalledOnSplit()
[TestMethod]
public async Task RetrierGetsCalledOnCompletingSplit()
{
IDocumentClientRetryPolicy retryPolicy1 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy1 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

IDocumentClientRetryPolicy retryPolicy2 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy2 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

ItemBatchOperation operation1 = this.CreateItemBatchOperation();
Expand All @@ -506,12 +563,14 @@ public async Task RetrierGetsCalledOnCompletingSplit()
[TestMethod]
public async Task RetrierGetsCalledOnCompletingPartitionMigration()
{
IDocumentClientRetryPolicy retryPolicy1 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy1 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

IDocumentClientRetryPolicy retryPolicy2 = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy2 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

ItemBatchOperation operation1 = this.CreateItemBatchOperation();
Expand Down Expand Up @@ -552,6 +611,64 @@ public async Task RetrierGetsCalledOnOverFlow()
retryDelegate.Verify(a => a(It.IsAny<ItemBatchOperation>(), It.IsAny<CancellationToken>()), Times.Once);
}

[TestMethod]
public async Task RetrierGetsCalledOn413_OnRead()
{
IDocumentClientRetryPolicy retryPolicy1 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

IDocumentClientRetryPolicy retryPolicy2 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));

ItemBatchOperation operation1 = this.CreateItemBatchOperation();
ItemBatchOperation operation2 = this.CreateItemBatchOperation();
operation1.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy1));
operation2.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy2));

Mock<BatchAsyncBatcherRetryDelegate> retryDelegate = new Mock<BatchAsyncBatcherRetryDelegate>();

BatchAsyncBatcher batchAsyncBatcher = new BatchAsyncBatcher(2, 1000, MockCosmosUtil.Serializer, this.ExecutorWith413, retryDelegate.Object);
Assert.IsTrue(batchAsyncBatcher.TryAdd(operation1));
Assert.IsTrue(batchAsyncBatcher.TryAdd(operation2));
await batchAsyncBatcher.DispatchAsync(metric);
retryDelegate.Verify(a => a(It.Is<ItemBatchOperation>(o => o == operation1), It.IsAny<CancellationToken>()), Times.Never);
retryDelegate.Verify(a => a(It.Is<ItemBatchOperation>(o => o == operation2), It.IsAny<CancellationToken>()), Times.Once);
retryDelegate.Verify(a => a(It.IsAny<ItemBatchOperation>(), It.IsAny<CancellationToken>()), Times.Once);
}

[TestMethod]
public async Task RetrierGetsCalledOn413_OnWrite()
{
IDocumentClientRetryPolicy retryPolicy1 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Create,
new ResourceThrottleRetryPolicy(1));

IDocumentClientRetryPolicy retryPolicy2 = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Create,
new ResourceThrottleRetryPolicy(1));

ItemBatchOperation operation1 = this.CreateItemBatchOperation();
ItemBatchOperation operation2 = this.CreateItemBatchOperation();
operation1.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy1));
operation2.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy2));

Mock<BatchAsyncBatcherRetryDelegate> retryDelegate = new Mock<BatchAsyncBatcherRetryDelegate>();

BatchAsyncBatcher batchAsyncBatcher = new BatchAsyncBatcher(2, 1000, MockCosmosUtil.Serializer, this.ExecutorWith413, retryDelegate.Object);
Assert.IsTrue(batchAsyncBatcher.TryAdd(operation1));
Assert.IsTrue(batchAsyncBatcher.TryAdd(operation2));
await batchAsyncBatcher.DispatchAsync(metric);
retryDelegate.Verify(a => a(It.Is<ItemBatchOperation>(o => o == operation1), It.IsAny<CancellationToken>()), Times.Never);
retryDelegate.Verify(a => a(It.Is<ItemBatchOperation>(o => o == operation2), It.IsAny<CancellationToken>()), Times.Never);
retryDelegate.Verify(a => a(It.IsAny<ItemBatchOperation>(), It.IsAny<CancellationToken>()), Times.Never);
}

private static ContainerInternal GetSplitEnabledContainer()
{
Mock<ContainerInternal> container = new Mock<ContainerInternal>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public async Task ShouldRetry_NoPolicy()
[TestMethod]
public async Task ShouldRetry_WithPolicy_OnSuccess()
{
IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
Mock.Of<ContainerInternal>(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.OK);
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
Expand All @@ -107,8 +108,9 @@ public async Task ShouldRetry_WithPolicy_OnSuccess()
[TestMethod]
public async Task ShouldRetry_WithPolicy_On429()
{
IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
Mock.Of<ContainerInternal>(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult((HttpStatusCode)StatusCodes.TooManyRequests);
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
Expand All @@ -117,11 +119,40 @@ public async Task ShouldRetry_WithPolicy_On429()
Assert.IsTrue(shouldRetryResult.ShouldRetry);
}

[TestMethod]
public async Task ShouldRetry_WithPolicy_On413_OnRead()
{
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
Mock.Of<ContainerInternal>(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.RequestEntityTooLarge);
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
operation.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy));
ShouldRetryResult shouldRetryResult = await operation.Context.ShouldRetryAsync(result, default);
Assert.IsTrue(shouldRetryResult.ShouldRetry);
}

[TestMethod]
public async Task ShouldRetry_WithPolicy_On413_OnWrite()
{
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
Mock.Of<ContainerInternal>(),
OperationType.Create,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.RequestEntityTooLarge);
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
operation.AttachContext(new ItemBatchOperationContext(string.Empty, retryPolicy));
ShouldRetryResult shouldRetryResult = await operation.Context.ShouldRetryAsync(result, default);
Assert.IsFalse(shouldRetryResult.ShouldRetry);
}

[TestMethod]
public async Task ShouldRetry_WithPolicy_OnSplit()
{
IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone) { SubStatusCode = SubStatusCodes.PartitionKeyRangeGone };
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
Expand All @@ -133,8 +164,9 @@ public async Task ShouldRetry_WithPolicy_OnSplit()
[TestMethod]
public async Task ShouldRetry_WithPolicy_OnCompletingSplit()
{
IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone) { SubStatusCode = SubStatusCodes.CompletingSplit };
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
Expand All @@ -146,8 +178,9 @@ public async Task ShouldRetry_WithPolicy_OnCompletingSplit()
[TestMethod]
public async Task ShouldRetry_WithPolicy_OnCompletingPartitionMigration()
{
IDocumentClientRetryPolicy retryPolicy = new BulkPartitionKeyRangeGoneRetryPolicy(
IDocumentClientRetryPolicy retryPolicy = new BulkExecutionRetryPolicy(
GetSplitEnabledContainer(),
OperationType.Read,
new ResourceThrottleRetryPolicy(1));
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Gone) { SubStatusCode = SubStatusCodes.CompletingPartitionMigration };
ItemBatchOperation operation = new ItemBatchOperation(OperationType.Create, 0, Cosmos.PartitionKey.Null);
Expand Down
Loading