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

Perf: unset EnableContentResponseOnWrite dotnet#22999 #23322

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -126,7 +126,7 @@ public virtual CosmosDbContextOptionsBuilder MaxRequestsPerTcpConnection(int req
/// This reduces networking and CPU load by not sending the resource back over the network and serializing it on the client.
/// </summary>
/// <param name="enabled"><see langword="false" /> to have null resource</param>
public virtual CosmosDbContextOptionsBuilder ContentResponseOnWriteEnabled(bool enabled = false)
public virtual CosmosDbContextOptionsBuilder ContentResponseOnWriteEnabled(bool enabled = true)
=> WithOption(e => e.ContentResponseOnWriteEnabled(Check.NotNull(enabled, nameof(enabled))));


AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
20 changes: 13 additions & 7 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class CosmosClientWrapper
private readonly string _databaseId;
private readonly IExecutionStrategyFactory _executionStrategyFactory;
private readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _commandLogger;
private readonly bool? _enableContentResponseOnWrite;

static CosmosClientWrapper()
{
Expand All @@ -89,6 +90,7 @@ public CosmosClientWrapper(
_databaseId = options.DatabaseName;
_executionStrategyFactory = executionStrategyFactory;
_commandLogger = commandLogger;
_enableContentResponseOnWrite = options.EnableContentResponseOnWrite;
}

private CosmosClient Client
Expand Down Expand Up @@ -292,8 +294,7 @@ private async Task<bool> CreateItemOnceAsync(

var entry = parameters.Entry;
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var enableContentResponseOnWrite = ((ICosmosSingletonOptions)Client.ClientOptions).EnableContentResponseOnWrite;
var itemRequestOptions = CreateItemRequestOptions(entry, enableContentResponseOnWrite);
var itemRequestOptions = CreateItemRequestOptions(entry, _enableContentResponseOnWrite);
var partitionKey = CreatePartitionKey(entry);

using var response = await container.CreateItemStreamAsync(stream, partitionKey, itemRequestOptions, cancellationToken)
Expand Down Expand Up @@ -355,8 +356,7 @@ private async Task<bool> ReplaceItemOnceAsync(

var entry = parameters.Entry;
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var enableContentResponseOnWrite = ((ICosmosSingletonOptions)Client.ClientOptions).EnableContentResponseOnWrite;
var itemRequestOptions = CreateItemRequestOptions(entry, enableContentResponseOnWrite);
var itemRequestOptions = CreateItemRequestOptions(entry, _enableContentResponseOnWrite);
var partitionKey = CreatePartitionKey(entry);

using var response = await container.ReplaceItemStreamAsync(
Expand Down Expand Up @@ -418,8 +418,8 @@ public virtual async Task<bool> DeleteItemOnceAsync(
{
var entry = parameters.Entry;
var items = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var enableContentResponseOnWrite = ((ICosmosSingletonOptions)Client.ClientOptions).EnableContentResponseOnWrite;
var itemRequestOptions = CreateItemRequestOptions(entry, enableContentResponseOnWrite);

var itemRequestOptions = CreateItemRequestOptions(entry, _enableContentResponseOnWrite);
var partitionKey = CreatePartitionKey(entry);

using var response = await items.DeleteItemStreamAsync(
Expand All @@ -445,7 +445,13 @@ private static ItemRequestOptions CreateItemRequestOptions(IUpdateEntry entry, b
etag = converter.ConvertToProvider(etag);
}

return new ItemRequestOptions { IfMatchEtag = (string)etag, EnableContentResponseOnWrite = enableContentResponseOnWrite };
var jObjectProperty = entry.EntityType.FindProperty(StoreKeyConvention.JObjectPropertyName);

var enabledContentResponse = enableContentResponseOnWrite.HasValue
? enableContentResponseOnWrite
: jObjectProperty?.ValueGenerated == ValueGenerated.OnAddOrUpdate;
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved

return new ItemRequestOptions { IfMatchEtag = (string)etag, EnableContentResponseOnWrite = enabledContentResponse };
}

private static PartitionKey CreatePartitionKey(IUpdateEntry entry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ public SingletonCosmosClientWrapper([NotNull] ICosmosSingletonOptions options)
configuration.MaxRequestsPerTcpConnection = options.MaxRequestsPerTcpConnection.Value;
}

if (options.EnableContentResponseOnWrite != null)
{
configuration.EnableTcpConnectionEndpointRediscovery = options.EnableContentResponseOnWrite.Value;
}

_options = configuration;
}

Expand Down
119 changes: 119 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/ContentResponseEtagTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

namespace Microsoft.EntityFrameworkCore.Cosmos
{
public class ContentResponseEtagTest
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved
{
[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_false()
{
await using var testDatabase = CosmosTestStore.Create("CustomerDemo");

var customer = new CustomerWithEtag
{
Id = Guid.NewGuid(),
Name = "Theon",
};

using (var context = new CustomerContextWithContentResponse(testDatabase, false))
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);

await context.SaveChangesAsync();
}

using (var context = new CustomerContextWithContentResponse(testDatabase, false))
{
var customerFromStore = await context.Set<CustomerWithEtag>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal("Theon", customerFromStore.Name);
Assert.Equal(customer.ETag, customerFromStore.ETag);

context.Remove(customerFromStore);

context.SaveChanges();
}
}

[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_true()
{
await using var testDatabase = CosmosTestStore.Create("CustomerDemo");

var customer = new CustomerWithEtag
{
Id = Guid.NewGuid(),
Name = "Theon",
};

using (var context = new CustomerContextWithContentResponse(testDatabase, true))
{
await context.Database.EnsureCreatedAsync();

context.Add(customer);

await context.SaveChangesAsync();
}

using (var context = new CustomerContextWithContentResponse(testDatabase, true))
{
var customerFromStore = await context.Set<CustomerWithEtag>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal("Theon", customerFromStore.Name);
Assert.Equal(customer.ETag, customerFromStore.ETag);

context.Remove(customerFromStore);

context.SaveChanges();
}
}

private class CustomerContextWithContentResponse : DbContext
{
private readonly string _connectionString;
private readonly string _name;
private readonly bool _contentResponseOnWriteEnabled;

public CustomerContextWithContentResponse(CosmosTestStore testStore, bool contentResponseOnWriteEnabled)
{
_connectionString = testStore.ConnectionString;
_name = testStore.Name;
_contentResponseOnWriteEnabled = contentResponseOnWriteEnabled;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCosmos(_connectionString, _name, b => b.ApplyConfiguration().ContentResponseOnWriteEnabled(_contentResponseOnWriteEnabled));
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomerWithEtag>(
b =>
{
b.HasKey(c => c.Id);
b.Property(c => c.ETag).IsETagConcurrency();
});
}

public DbSet<CustomerWithEtag> Customers { get; set; }
}

private class CustomerWithEtag
{
public Guid Id { get; set; }
public string Name { get; set; }
public string ETag { get; set; }
}
}
}