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

CreateItem will only retry for auto-extracted partition key in-case of container re-creation #1070

Merged
merged 5 commits into from
Dec 2, 2019
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 @@ -6,7 +6,6 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -48,7 +47,6 @@ public override Task<ResponseMessage> CreateItemStreamAsync(
streamPayload,
OperationType.Create,
requestOptions,
extractPartitionKeyIfNeeded: false,
cancellationToken: cancellationToken);
}

Expand All @@ -66,7 +64,7 @@ public override Task<ItemResponse<T>> CreateItemAsync<T>(
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: null,
streamPayload: this.ClientContext.CosmosSerializer.ToStream<T>(item),
item: item,
operationType: OperationType.Create,
requestOptions: requestOptions,
cancellationToken: cancellationToken);
Expand All @@ -86,7 +84,6 @@ public override Task<ResponseMessage> ReadItemStreamAsync(
null,
OperationType.Read,
requestOptions,
extractPartitionKeyIfNeeded: false,
cancellationToken: cancellationToken);
}

Expand Down Expand Up @@ -117,7 +114,6 @@ public override Task<ResponseMessage> UpsertItemStreamAsync(
streamPayload,
OperationType.Upsert,
requestOptions,
extractPartitionKeyIfNeeded: false,
cancellationToken: cancellationToken);
}

Expand All @@ -135,7 +131,7 @@ public override Task<ItemResponse<T>> UpsertItemAsync<T>(
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: null,
streamPayload: this.ClientContext.CosmosSerializer.ToStream<T>(item),
item: item,
operationType: OperationType.Upsert,
requestOptions: requestOptions,
cancellationToken: cancellationToken);
Expand All @@ -156,7 +152,6 @@ public override Task<ResponseMessage> ReplaceItemStreamAsync(
streamPayload,
OperationType.Replace,
requestOptions,
extractPartitionKeyIfNeeded: false,
cancellationToken: cancellationToken);
}

Expand All @@ -180,7 +175,7 @@ public override Task<ItemResponse<T>> ReplaceItemAsync<T>(
Task<ResponseMessage> response = this.ExtractPartitionKeyAndProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: id,
streamPayload: this.ClientContext.CosmosSerializer.ToStream<T>(item),
item: item,
operationType: OperationType.Replace,
requestOptions: requestOptions,
cancellationToken: cancellationToken);
Expand All @@ -200,7 +195,6 @@ public override Task<ResponseMessage> DeleteItemStreamAsync(
null,
OperationType.Delete,
requestOptions,
extractPartitionKeyIfNeeded: false,
cancellationToken: cancellationToken);
}

Expand Down Expand Up @@ -511,24 +505,39 @@ internal FeedIterator GetItemQueryStreamIteratorInternal(
// Extracted partition key might be invalid as CollectionCache might be stale.
// Stale collection cache is refreshed through PartitionKeyMismatchRetryPolicy
// and partition-key is extracted again.
internal async Task<ResponseMessage> ExtractPartitionKeyAndProcessItemStreamAsync(
internal async Task<ResponseMessage> ExtractPartitionKeyAndProcessItemStreamAsync<T>(
PartitionKey? partitionKey,
string itemId,
Stream streamPayload,
T item,
OperationType operationType,
RequestOptions requestOptions,
CancellationToken cancellationToken)
{
Stream streamPayload = this.ClientContext.CosmosSerializer.ToStream<T>(item);

// User specified PK value, no need to extract it
if (partitionKey.HasValue)
{
return await this.ProcessItemStreamAsync(
partitionKey,
itemId,
streamPayload,
operationType,
requestOptions,
cancellationToken: cancellationToken);
}

PartitionKeyMismatchRetryPolicy requestRetryPolicy = null;
while (true)
{
partitionKey = await this.GetPartitionKeyValueFromStreamAsync(streamPayload, cancellationToken);

ResponseMessage responseMessage = await this.ProcessItemStreamAsync(
partitionKey,
itemId,
streamPayload,
operationType,
requestOptions,
extractPartitionKeyIfNeeded: true,
cancellationToken: cancellationToken);

if (responseMessage.IsSuccessStatusCode)
Expand All @@ -555,33 +564,27 @@ internal async Task<ResponseMessage> ProcessItemStreamAsync(
Stream streamPayload,
OperationType operationType,
RequestOptions requestOptions,
bool extractPartitionKeyIfNeeded,
CancellationToken cancellationToken = default(CancellationToken))
{
if (requestOptions != null && requestOptions.IsEffectivePartitionKeyRouting)
{
partitionKey = null;
}

if (extractPartitionKeyIfNeeded && partitionKey == null)
{
partitionKey = await this.GetPartitionKeyValueFromStreamAsync(streamPayload, cancellationToken);
}

ContainerCore.ValidatePartitionKey(partitionKey, requestOptions);
Uri resourceUri = this.GetResourceUri(requestOptions, operationType, itemId);

return await this.ClientContext.ProcessResourceOperationStreamAsync(
resourceUri,
ResourceType.Document,
operationType,
requestOptions,
this,
partitionKey,
itemId,
streamPayload,
null,
cancellationToken);
resourceUri: resourceUri,
resourceType: ResourceType.Document,
operationType: operationType,
requestOptions: requestOptions,
cosmosContainerCore: this,
partitionKey: partitionKey,
itemId: itemId,
streamPayload: streamPayload,
requestEnricher: null,
cancellationToken: cancellationToken);
}

internal async Task<PartitionKey> GetPartitionKeyValueFromStreamAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Cleanup()
[TestMethod]
public async Task TestCustomPropertyWithHandler()
{
CustomHandler testHandler = new CustomHandler();
RequestHandlerHelper testHandler = new RequestHandlerHelper();

// Add the random guid to the property
Guid randomGuid = Guid.NewGuid();
Expand Down Expand Up @@ -111,20 +111,5 @@ public class ToDoActivity
public string description { get; set; }
public string status { get; set; }
}

public class CustomHandler : RequestHandler
{
public Action<RequestMessage> UpdateRequestMessage = null;

public override Task<ResponseMessage> SendAsync(RequestMessage request, CancellationToken cancellationToken)
{
if (this.UpdateRequestMessage != null)
{
this.UpdateRequestMessage(request);
}

return base.SendAsync(request, cancellationToken);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,52 @@ internal async Task TestPartitionKeyDefinitionOnContainerRecreateFromDifferentPa
}
}

[TestMethod]
public async Task TestInvalidPartitionKeyException()
{
RequestHandlerHelper testHandler = new RequestHandlerHelper();
int createItemCount = 0;
string partitionKey = null;
testHandler.UpdateRequestMessage = (requestMessage) =>
{
if (requestMessage.ResourceType == ResourceType.Document)
{
createItemCount++;
string pk = requestMessage.Headers.PartitionKey;
Assert.AreNotEqual(partitionKey, pk, $"Same PK value should not be sent again. PK value:{pk}");
partitionKey = pk;
}
};

using (CosmosClient client = TestCommon.CreateCosmosClient((builder) => builder.AddCustomHandlers(testHandler)))
{
Cosmos.Database database = null;
try
{
database = await client.CreateDatabaseAsync("TestInvalidPartitionKey" + Guid.NewGuid().ToString());
Container container = await database.CreateContainerAsync(id: "coll1", partitionKeyPath: "/doesnotexist");
ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();

ItemResponse<ToDoActivity> response = await container.CreateItemAsync(toDoActivity, partitionKey: new Cosmos.PartitionKey(toDoActivity.status));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test for other case where retry are expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test above this is testing the happy path. This test is for specifically testing that if a user provides a partition key value it is not retried on.

Assert.Fail("Create item should fail with wrong partition key value");
}
catch (CosmosException ce)
{
Assert.AreEqual(HttpStatusCode.BadRequest, ce.StatusCode);
Assert.AreEqual(SubStatusCodes.PartitionKeyMismatch, (SubStatusCodes)ce.SubStatusCode);
}
finally
{
if (database != null)
{
await database.DeleteAsync();
}
}

Assert.AreEqual(1, createItemCount, $"Request should use the custom handler, and it should only be used once. Count {createItemCount}");
}
}

/// <summary>
/// Tests that collection cache is refreshed when collection is recreated.
/// The test just ensures that client retries and completes successfully for query - request which doesn't target single partition key.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Threading;
using System.Threading.Tasks;

public class RequestHandlerHelper : RequestHandler
{
public Action<RequestMessage> UpdateRequestMessage = null;

public override Task<ResponseMessage> SendAsync(RequestMessage request, CancellationToken cancellationToken)
{
this.UpdateRequestMessage?.Invoke(request);

return base.SendAsync(request, cancellationToken);
}
}
}
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#1036](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1036) Fixed query responses to return null Content if it is a failure
- [#1045](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1045) Added stack trace and innner exception to CosmosException
- [#1050](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1050) Add mocking constructors to TransactionalBatchOperationResult

- [#1070](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1070) CreateItem will only retry for auto-extracted partition key in-case of collection re-creation

## <a name="3.4.1"/> [3.4.1](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.4.1) - 2019-11-06

### Fixed
Expand Down