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

CosmosClientOptions and CosmosClientBuilder: Adds client level support for EnableContentResponseOnWrite #2145

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 20 additions & 10 deletions Microsoft.Azure.Cosmos/src/Batch/BatchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public override TransactionalBatch CreateItem<T>(
operationIndex: this.operations.Count,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -67,7 +68,8 @@ public override TransactionalBatch CreateItemStream(
operationIndex: this.operations.Count,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -86,7 +88,8 @@ public override TransactionalBatch ReadItem(
operationIndex: this.operations.Count,
id: id,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -105,7 +108,8 @@ public override TransactionalBatch UpsertItem<T>(
operationIndex: this.operations.Count,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -124,7 +128,8 @@ public override TransactionalBatch UpsertItemStream(
operationIndex: this.operations.Count,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -150,7 +155,8 @@ public override TransactionalBatch ReplaceItem<T>(
id: id,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -176,7 +182,8 @@ public override TransactionalBatch ReplaceItemStream(
id: id,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand All @@ -195,7 +202,8 @@ public override TransactionalBatch DeleteItem(
operationIndex: this.operations.Count,
id: id,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand Down Expand Up @@ -253,7 +261,8 @@ public virtual TransactionalBatch PatchItemStream(
id: id,
resourceStream: patchStream,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand Down Expand Up @@ -292,7 +301,8 @@ TransactionalBatch PatchItem(
id: id,
resource: patchOperations,
requestOptions: requestOptions,
containerCore: this.container));
containerCore: this.container,
cosmosClientContext: this.container.ClientContext));

return this;
}
Expand Down
36 changes: 29 additions & 7 deletions Microsoft.Azure.Cosmos/src/Batch/ItemBatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Serialization.HybridRow;
Expand All @@ -32,7 +33,8 @@ public ItemBatchOperation(
string id = null,
Stream resourceStream = null,
TransactionalBatchItemRequestOptions requestOptions = null,
CosmosDiagnosticsContext diagnosticsContext = null)
CosmosDiagnosticsContext diagnosticsContext = null,
CosmosClientContext cosmosClientContext = null)
j82w marked this conversation as resolved.
Show resolved Hide resolved
{
this.OperationType = operationType;
this.OperationIndex = operationIndex;
Expand All @@ -41,6 +43,7 @@ public ItemBatchOperation(
this.ResourceStream = resourceStream;
this.RequestOptions = requestOptions;
this.DiagnosticsContext = diagnosticsContext;
this.ClientContext = cosmosClientContext;
}

public ItemBatchOperation(
Expand All @@ -49,7 +52,8 @@ public ItemBatchOperation(
ContainerInternal containerCore,
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
string id = null,
Stream resourceStream = null,
TransactionalBatchItemRequestOptions requestOptions = null)
TransactionalBatchItemRequestOptions requestOptions = null,
CosmosClientContext cosmosClientContext = null)
{
this.OperationType = operationType;
this.OperationIndex = operationIndex;
Expand All @@ -58,6 +62,7 @@ public ItemBatchOperation(
this.ResourceStream = resourceStream;
this.RequestOptions = requestOptions;
this.DiagnosticsContext = null;
this.ClientContext = cosmosClientContext;
}

public PartitionKey? PartitionKey { get; internal set; }
Expand All @@ -80,6 +85,8 @@ public ItemBatchOperation(

internal Documents.PartitionKey ParsedPartitionKey { get; set; }

internal CosmosClientContext ClientContext { get; set; }
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved

internal Memory<byte> ResourceBody
{
get
Expand Down Expand Up @@ -166,7 +173,7 @@ internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg
return r;
}
}

if (ItemRequestOptions.ShouldSetNoContentHeader(
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
options.EnableContentResponseOnWrite,
options.EnableContentResponseOnRead,
Expand Down Expand Up @@ -250,6 +257,19 @@ internal static Result WriteOperation(ref RowWriter writer, TypeArgument typeArg
}
}

// If EnableContentResponse is set at Client level and not at Indivisual Operation Level
if (((operation.RequestOptions != null && !operation.RequestOptions.EnableContentResponseOnWrite.HasValue) || operation.RequestOptions == null)
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
&& operation.ClientContext != null
&& operation.ClientContext.ClientOptions != null
&& ItemRequestOptions.ShouldSetNoContentHeader(operation.ClientContext.ClientOptions.EnableContentResponseOnWrite, null, operation.OperationType))
{
r = writer.WriteBool("minimalReturnPreference", true);
if (r != Result.Success)
{
return r;
}
}

return Result.Success;
}

Expand Down Expand Up @@ -369,8 +389,9 @@ public ItemBatchOperation(
PartitionKey partitionKey,
T resource,
string id = null,
TransactionalBatchItemRequestOptions requestOptions = null)
: base(operationType, operationIndex, partitionKey: partitionKey, id: id, requestOptions: requestOptions)
TransactionalBatchItemRequestOptions requestOptions = null,
CosmosClientContext cosmosClientContext = null)
: base(operationType, operationIndex, partitionKey: partitionKey, id: id, requestOptions: requestOptions, cosmosClientContext: cosmosClientContext)
{
this.Resource = resource;
}
Expand All @@ -381,8 +402,9 @@ public ItemBatchOperation(
T resource,
ContainerInternal containerCore,
string id = null,
TransactionalBatchItemRequestOptions requestOptions = null)
: base(operationType, operationIndex, containerCore: containerCore, id: id, requestOptions: requestOptions)
TransactionalBatchItemRequestOptions requestOptions = null,
CosmosClientContext cosmosClientContext = null)
: base(operationType, operationIndex, containerCore: containerCore, id: id, requestOptions: requestOptions, cosmosClientContext: cosmosClientContext)
{
this.Resource = resource;
}
Expand Down
15 changes: 15 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ public ConnectionMode ConnectionMode
/// <seealso cref="CosmosClientBuilder.WithThrottlingRetryOptions(TimeSpan, int)"/>
public TimeSpan? MaxRetryWaitTimeOnRateLimitedRequests { get; set; }

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in
/// the Cosmos DB response for write item operation like Create, Upsert, Patch and Replace.
/// Setting the option to false will cause the response to have a null resource. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <remarks>
/// <para>This is optimal for workloads where the returned resource is not used.</para>
/// <para>This option can be overriden by similar property in ItemRequestOptions and TransactionalBatchItemRequestOptions</para>
/// </remarks>
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
/// <seealso cref="CosmosClientBuilder.WithContentResponseOnWriteEnabled(bool)"/>
/// <seealso cref="ItemRequestOptions.EnableContentResponseOnWrite"/>
/// <seealso cref="TransactionalBatchItemRequestOptions.EnableContentResponseOnWrite"/>
public bool? EnableContentResponseOnWrite { get; set; }

/// <summary>
/// (Direct/TCP) Controls the amount of idle time after which unused connections are closed.
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,27 @@ public CosmosClientBuilder WithHttpClientFactory(Func<HttpClient> httpClientFact
return this;
}

/// <summary>
/// Gets or sets the boolean to only return the headers and status code in
/// the Cosmos DB response for write item operation like Create, Upsert, Patch and Replace.
/// Setting the option to false will cause the response to have a null resource. This reduces networking and CPU load by not sending
/// the resource back over the network and serializing it on the client.
/// </summary>
/// <param name="contentResponseOnWriteEnabled">a boolean indicating whether payload will be included in the response or not.</param>
/// <remarks>
/// <para>
/// This option can be overriden by similar property in ItemRequestOptions and TransactionalBatchItemRequestOptions
/// </para>
/// </remarks>
/// <returns>The <see cref="CosmosClientBuilder"/> object</returns>
/// <seealso cref="ItemRequestOptions.EnableContentResponseOnWrite"/>
/// <seealso cref="TransactionalBatchItemRequestOptions.EnableContentResponseOnWrite"/>
public CosmosClientBuilder WithContentResponseOnWriteEnabled(bool contentResponseOnWriteEnabled)
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
j82w marked this conversation as resolved.
Show resolved Hide resolved
{
this.clientOptions.EnableContentResponseOnWrite = contentResponseOnWriteEnabled;
return this;
}

/// <summary>
/// The event handler to be invoked before the request is sent.
/// </summary>
Expand Down
36 changes: 36 additions & 0 deletions Microsoft.Azure.Cosmos/src/Handler/RequestInvokerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public override async Task<ResponseMessage> SendAsync(
promotedRequestOptions.PopulateRequestOptions(request);
}

// Adds the NoContent header if not already added based on
this.SetClientLevelNoContentResponseHeaders(request);

await this.ValidateAndSetConsistencyLevelAsync(request);
(bool isError, ResponseMessage errorResponse) = await this.EnsureValidClientAsync(request);
if (isError)
Expand Down Expand Up @@ -381,5 +384,38 @@ private async Task ValidateAndSetConsistencyLevelAsync(RequestMessage requestMes
}
}
}

private void SetClientLevelNoContentResponseHeaders(RequestMessage request)
{
// Sets the headers to only return the headers and status code in
// the Cosmos DB response for write item operation like Create, Upsert, Patch and Replace.
if (request.RequestOptions == null)
{
this.SetMinimalContentHeader(request);
}

if (!(request.RequestOptions is ItemRequestOptions))
{
return;
}

ItemRequestOptions itemRequestOptions = (ItemRequestOptions)request.RequestOptions;
if (request.ResourceType == ResourceType.Document
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
&& !itemRequestOptions.EnableContentResponseOnWrite.HasValue)
{
this.SetMinimalContentHeader(request);
}
}

private void SetMinimalContentHeader(RequestMessage request)
{
if (this.client.ClientOptions != null
&& ItemRequestOptions.ShouldSetNoContentHeader(enableContentResponseOnWrite: this.client.ClientOptions.EnableContentResponseOnWrite,
enableContentResponseOnRead: null,
operationType: request.OperationType))
{
request.Headers.Add(HttpConstants.HttpHeaders.Prefer, HttpConstants.HttpHeaderValues.PreferReturnMinimal);
}
}
}
}
3 changes: 2 additions & 1 deletion Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ private async Task<ResponseMessage> ProcessResourceOperationAsBulkStreamAsync(
id: itemId,
resourceStream: streamPayload,
requestOptions: batchItemRequestOptions,
diagnosticsContext: diagnosticsContext);
diagnosticsContext: diagnosticsContext,
cosmosClientContext: this);

TransactionalBatchOperationResult batchOperationResult = await cosmosContainerCore.BatchExecutor.AddAsync(
itemBatchOperation,
Expand Down
Loading