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
5 changes: 3 additions & 2 deletions Microsoft.Azure.Cosmos/src/CosmosClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Handlers;
using Microsoft.Azure.Cosmos.Query;
using Microsoft.Azure.Documents;
using System.Text;

/// <summary>
/// Provides a client-side logical representation of the Azure Cosmos DB database account.
Expand Down Expand Up @@ -202,7 +203,7 @@ internal CosmosClient(

internal CosmosOffers Offers => this.offerSet.Value;
internal DocumentClient DocumentClient { get; set; }
internal CosmosRequestHandler RequestHandler { get; private set; }
internal RequestInvokerHandler RequestHandler { get; private set; }
internal ConsistencyLevel AccountConsistencyLevel { get; private set; }

internal CosmosResponseFactory ResponseFactory =>
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Handler/ClientPipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ private set

internal CosmosRequestHandler PartitionKeyRangeHandler { get; set; }

public CosmosRequestHandler Build()
public RequestInvokerHandler Build()
{
CosmosRequestHandler root = new RequestInvokerHandler(this.client);
RequestInvokerHandler root = new RequestInvokerHandler(this.client);

CosmosRequestHandler current = root;
if (this.CustomHandlers != null && this.CustomHandlers.Any())
Expand Down
3 changes: 1 addition & 2 deletions Microsoft.Azure.Cosmos/src/Handler/CosmosRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ internal async Task AssertPartitioningDetailsAsync(CosmosClient client, Cancella
{
Debug.Assert(this.AssertPartitioningPropertiesAndHeaders());
}
#endif
#if !DEBUG
#else
await Task.CompletedTask;
#endif
}
Expand Down
128 changes: 126 additions & 2 deletions Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Microsoft.Azure.Cosmos.Handlers
{
using Microsoft.Azure.Cosmos.Internal;
using Microsoft.Azure.Documents;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Routing;

/// <summary>
/// HttpMessageHandler can only be invoked by derived classed or internal classes inside http assembly
Expand Down Expand Up @@ -82,6 +85,127 @@ public override Task<CosmosResponseMessage> SendAsync(
.Unwrap();
}

public virtual async Task<T> SendAsync<T>(
Uri resourceUri,
ResourceType resourceType,
OperationType operationType,
CosmosRequestOptions requestOptions,
CosmosContainerCore cosmosContainerCore,
Object partitionKey,
Stream streamPayload,
Action<CosmosRequestMessage> requestEnricher,
Func<CosmosResponseMessage, T> responseCreator,
CancellationToken cancellation = default(CancellationToken))
{
if (responseCreator == null)
{
throw new ArgumentNullException(nameof(responseCreator));
}

CosmosResponseMessage responseMessage = await this.SendAsync(
resourceUri: resourceUri,
resourceType: resourceType,
operationType: operationType,
requestOptions: requestOptions,
cosmosContainerCore: cosmosContainerCore,
partitionKey: partitionKey,
streamPayload: streamPayload,
requestEnricher: requestEnricher,
cancellation: cancellation);

return responseCreator(responseMessage);
}

public virtual async Task<CosmosResponseMessage> SendAsync(
Uri resourceUri,
ResourceType resourceType,
OperationType operationType,
CosmosRequestOptions requestOptions,
CosmosContainerCore cosmosContainerCore,
Object partitionKey,
Stream streamPayload,
Action<CosmosRequestMessage> requestEnricher,
CancellationToken cancellation = default(CancellationToken))
{
if (resourceUri == null)
{
throw new ArgumentNullException(nameof(resourceUri));
}

HttpMethod method = RequestInvokerHandler.GetHttpMethod(operationType);

CosmosRequestMessage request = new CosmosRequestMessage(method, resourceUri);
request.OperationType = operationType;
request.ResourceType = resourceType;
request.RequestOptions = requestOptions;
request.Content = streamPayload;

if (partitionKey != null)
{
if (cosmosContainerCore == null && Object.ReferenceEquals(partitionKey, CosmosContainerSettings.NonePartitionKeyValue))
{
throw new ArgumentException($"{nameof(cosmosContainerCore)} can not be null with partition key as PartitionKey.None");
}
else if (Object.ReferenceEquals(partitionKey, CosmosContainerSettings.NonePartitionKeyValue))
{
try
{
PartitionKeyInternal partitionKeyInternal = await cosmosContainerCore.GetNonePartitionKeyValueAsync(cancellation);
request.Headers.PartitionKey = partitionKeyInternal.ToJsonString();
}
catch (DocumentClientException dce)
{
return dce.ToCosmosResponseMessage(request);
}
}
else
{
PartitionKey pk = new PartitionKey(partitionKey);
request.Headers.PartitionKey = pk.InternalKey.ToJsonString();
}
}

if (operationType == OperationType.Upsert)
{
request.Headers.IsUpsert = bool.TrueString;
}

requestEnricher?.Invoke(request);
return await this.SendAsync(request, cancellation);
}

internal static HttpMethod GetHttpMethod(
OperationType operationType)
{
HttpMethod httpMethod = HttpMethod.Head;
if (operationType == OperationType.Create ||
operationType == OperationType.Upsert ||
operationType == OperationType.Query ||
operationType == OperationType.SqlQuery ||
operationType == OperationType.Batch ||
operationType == OperationType.ExecuteJavaScript)
{
return HttpMethod.Post;
}
else if (operationType == OperationType.Read ||
operationType == OperationType.ReadFeed)
{
return HttpMethod.Get;
}
else if (operationType == OperationType.Replace)
{
return HttpMethod.Put;
}
else if (operationType == OperationType.Delete)
{
return HttpMethod.Delete;
}
else
{
throw new NotImplementedException();
}
}

private void FillMultiMasterContext(CosmosRequestMessage request)
{
if (this.client.DocumentClient.UseMultipleWriteLocations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ internal Task<CosmosOfferResult> ReplaceProvisionedThroughputIfExistsAsync(
internal async Task<CosmosContainerSettings> GetCachedContainerSettingsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
ClientCollectionCache collectionCache = await this.ClientContext.DocumentClient.GetCollectionCacheAsync();
return await collectionCache.GetByNameAsync(HttpConstants.Versions.CurrentVersion, this.LinkUri.OriginalString, cancellationToken);
return await collectionCache.ResolveByNameAsync(HttpConstants.Versions.CurrentVersion, this.LinkUri.OriginalString, cancellationToken);
}

// Name based look-up, needs re-computation and can't be cached
Expand All @@ -213,10 +213,12 @@ internal Task<string> GetRID(CancellationToken cancellationToken)
/// The function selects the right partition key constant for inserting documents that don't have
/// a value for partition key. The constant selection is based on whether the collection is migrated
/// or user partitioned
///
/// For non-existing container will throw <see cref="DocumentClientException"/> with 404 as status code
/// </remarks>
internal async Task<PartitionKeyInternal> GetNonePartitionKeyValue(CancellationToken cancellationToken = default(CancellationToken))
internal async Task<PartitionKeyInternal> GetNonePartitionKeyValueAsync(CancellationToken cancellation = default(CancellationToken))
{
CosmosContainerSettings containerSettings = await this.GetCachedContainerSettingsAsync(cancellationToken);
CosmosContainerSettings containerSettings = await this.GetCachedContainerSettingsAsync(cancellation);
return containerSettings.GetNoneValue();
}

Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Azure.Cosmos/src/Resource/CosmosClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.Azure.Cosmos
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Handlers;
using Microsoft.Azure.Cosmos.Query;
using Microsoft.Azure.Documents;

Expand All @@ -32,7 +33,7 @@ internal abstract class CosmosClientContext

internal abstract CosmosResponseFactory ResponseFactory { get; }

internal abstract CosmosRequestHandler RequestHandler { get; }
internal abstract RequestInvokerHandler RequestHandler { get; }

internal abstract CosmosClientConfiguration ClientConfiguration { get; }

Expand Down
20 changes: 9 additions & 11 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Handlers;
using Microsoft.Azure.Cosmos.Query;
using Microsoft.Azure.Documents;

Expand All @@ -20,7 +20,7 @@ internal CosmosClientContextCore(
CosmosClientConfiguration clientConfiguration,
CosmosJsonSerializer cosmosJsonSerializer,
CosmosResponseFactory cosmosResponseFactory,
CosmosRequestHandler requestHandler,
RequestInvokerHandler requestHandler,
DocumentClient documentClient,
IDocumentQueryClient documentQueryClient)
{
Expand All @@ -46,7 +46,7 @@ internal CosmosClientContextCore(

internal override CosmosResponseFactory ResponseFactory { get; }

internal override CosmosRequestHandler RequestHandler { get; }
internal override RequestInvokerHandler RequestHandler { get; }
Comment thread
kirankumarkolli marked this conversation as resolved.

internal override CosmosClientConfiguration ClientConfiguration { get; }

Expand Down Expand Up @@ -94,17 +94,16 @@ internal override Task<CosmosResponseMessage> ProcessResourceOperationStreamAsyn
Action<CosmosRequestMessage> requestEnricher,
CancellationToken cancellationToken)
{
return ExecUtils.ProcessResourceOperationStreamAsync(
requestHandler: this.RequestHandler,
cosmosContainerCore: cosmosContainerCore,
return this.RequestHandler.SendAsync(
resourceUri: resourceUri,
resourceType: resourceType,
operationType: operationType,
requestOptions: requestOptions,
cosmosContainerCore: cosmosContainerCore,
partitionKey: partitionKey,
streamPayload: streamPayload,
requestEnricher: requestEnricher,
cancellationToken: cancellationToken);
cancellation: cancellationToken);
}

internal override Task<T> ProcessResourceOperationAsync<T>(
Expand All @@ -117,10 +116,9 @@ internal override Task<T> ProcessResourceOperationAsync<T>(
Stream streamPayload,
Action<CosmosRequestMessage> requestEnricher,
Func<CosmosResponseMessage, T> responseCreator,
CancellationToken cancellationToken)
CancellationToken cancellation)
{
return ExecUtils.ProcessResourceOperationAsync<T>(
requestHandler: this.RequestHandler,
return this.RequestHandler.SendAsync<T>(
resourceUri: resourceUri,
resourceType: resourceType,
operationType: operationType,
Expand All @@ -130,7 +128,7 @@ internal override Task<T> ProcessResourceOperationAsync<T>(
streamPayload: streamPayload,
requestEnricher: requestEnricher,
responseCreator: responseCreator,
cancellationToken: cancellationToken);
cancellation: cancellation);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override Task<CosmosContainerSettings> GetByRidAsync(string apiVersion
cancellationToken);
}

internal override Task<CosmosContainerSettings> GetByNameAsync(string apiVersion, string resourceAddress, CancellationToken cancellationToken)
protected override Task<CosmosContainerSettings> GetByNameAsync(string apiVersion, string resourceAddress, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
IDocumentClientRetryPolicy retryPolicyInstance = new ClearingSessionContainerClientRetryPolicy(this.sessionContainer, this.retryPolicy.GetRequestPolicy());
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void Refresh(string resourceAddress, string apiVersion = null)

protected abstract Task<CosmosContainerSettings> GetByRidAsync(string apiVersion, string collectionRid, CancellationToken cancellationToken);

internal abstract Task<CosmosContainerSettings> GetByNameAsync(string apiVersion, string resourceAddress, CancellationToken cancellationToken);
protected abstract Task<CosmosContainerSettings> GetByNameAsync(string apiVersion, string resourceAddress, CancellationToken cancellationToken);

private async Task<CosmosContainerSettings> ResolveByPartitionKeyRangeIdentityAsync(string apiVersion, PartitionKeyRangeIdentity partitionKeyRangeIdentity, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ private Task<CosmosContainerSettings> ResolveByRidAsync(
cancellationToken);
}

private async Task<CosmosContainerSettings> ResolveByNameAsync(
internal virtual async Task<CosmosContainerSettings> ResolveByNameAsync(
string apiVersion,
string resourceAddress,
CancellationToken cancellationToken)
Expand Down
Loading