Skip to content

Commit

Permalink
Make Cosmos provider throw for sync I/O (#33386)
Browse files Browse the repository at this point in the history
Fixes #32563

This change adds an error via logging which is thrown on any use of sync I/O with Cosmos. This can currently be disabled to allow sync again, but ultimately this will be removed--probably in 11.

Also, makes all test infrastructure async-only. This means an EF provider only needs to support async APIs to use our test infra.
  • Loading branch information
ajcvickers authored Mar 25, 2024
1 parent 379127d commit eb0c2d2
Show file tree
Hide file tree
Showing 274 changed files with 15,877 additions and 13,895 deletions.
14 changes: 14 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/CosmosEventId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class CosmosEventId
private enum Id
{
// Database events
SyncNotSupported = CoreEventId.ProviderBaseId,

// Command events
ExecutingSqlQuery = CoreEventId.ProviderBaseId + 100,
Expand All @@ -36,6 +37,19 @@ private enum Id
ExecutedDeleteItem
}

private static readonly string DatabasePrefix = DbLoggerCategory.Database.Name + ".";

/// <summary>
/// Azure Cosmos DB does not support synchronous I/O. Make sure to use and correctly await only async
/// methods when using Entity Framework Core to access Azure Cosmos DB.
/// See https://aka.ms/ef-cosmos-nosync for more information.
/// </summary>
/// <remarks>
/// This event is in the <see cref="DbLoggerCategory.Database" /> category.
/// </remarks>
public static readonly EventId SyncNotSupported
= new((int)Id.SyncNotSupported, DatabasePrefix + Id.SyncNotSupported);

private static readonly string CommandPrefix = DbLoggerCategory.Database.Command.Name + ".";

/// <summary>
Expand Down
26 changes: 26 additions & 0 deletions src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal;
/// </summary>
public static class CosmosLoggerExtensions
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SyncNotSupported(
this IDiagnosticsLogger<DbLoggerCategory.Database> diagnostics)
{
var definition = CosmosResources.LogSyncNotSupported(diagnostics);

if (diagnostics.ShouldLog(definition))
{
definition.Log(diagnostics);
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
{
var eventData = new EventData(
definition,
(d, p) => ((EventDefinition)d).GenerateMessage());

diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public class CosmosLoggingDefinitions : LoggingDefinitions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public EventDefinitionBase? LogExecutedDeleteItem;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public EventDefinitionBase? LogSyncNotSupported;
}
21 changes: 21 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosDbContextOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static DbContextOptionsBuilder UseCosmos(
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotNull(cosmosOptionsAction, nameof(cosmosOptionsAction));

ConfigureWarnings(optionsBuilder);

cosmosOptionsAction.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));

return optionsBuilder;
Expand Down Expand Up @@ -118,6 +120,8 @@ public static DbContextOptionsBuilder UseCosmos(
.WithAccountKey(accountKey)
.WithDatabaseName(databaseName);

ConfigureWarnings(optionsBuilder);

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
Expand Down Expand Up @@ -186,6 +190,8 @@ public static DbContextOptionsBuilder UseCosmos(
.WithTokenCredential(tokenCredential)
.WithDatabaseName(databaseName);

ConfigureWarnings(optionsBuilder);

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
Expand Down Expand Up @@ -247,10 +253,25 @@ public static DbContextOptionsBuilder UseCosmos(
.WithConnectionString(connectionString)
.WithDatabaseName(databaseName);

ConfigureWarnings(optionsBuilder);

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));

return optionsBuilder;
}

private static void ConfigureWarnings(DbContextOptionsBuilder optionsBuilder)
{
var coreOptionsExtension
= optionsBuilder.Options.FindExtension<CoreOptionsExtension>()
?? new CoreOptionsExtension();

coreOptionsExtension = coreOptionsExtension.WithWarningsConfiguration(
coreOptionsExtension.WarningsConfiguration.TryWithExplicit(
CosmosEventId.SyncNotSupported, WarningBehavior.Throw));

((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(coreOptionsExtension);
}
}
25 changes: 25 additions & 0 deletions src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 31 additions & 27 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -187,6 +187,10 @@
<value>Executing SQL query for container '{containerId}' in partition '{partitionKey}' [Parameters=[{parameters}]]{newLine}{commandText}</value>
<comment>Information CosmosEventId.ExecutingSqlQuery string string? string string string</comment>
</data>
<data name="LogSyncNotSupported" xml:space="preserve">
<value>Azure Cosmos DB does not support synchronous I/O. Make sure to use and correctly await only async methods when using Entity Framework Core to access Azure Cosmos DB. See https://aka.ms/ef-cosmos-nosync for more information.</value>
<comment>Error CosmosEventId.SyncNotSupported</comment>
</data>
<data name="MissingOrderingInSelectExpression" xml:space="preserve">
<value>'Reverse' could not be translated to the server because there is no ordering on the server side.</value>
</data>
Expand Down
45 changes: 38 additions & 7 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CosmosClientWrapper : ICosmosClientWrapper
private readonly string _databaseId;
private readonly IExecutionStrategy _executionStrategy;
private readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _commandLogger;
private readonly IDiagnosticsLogger<DbLoggerCategory.Database> _databaseLogger;
private readonly bool? _enableContentResponseOnWrite;

static CosmosClientWrapper()
Expand All @@ -61,14 +62,16 @@ public CosmosClientWrapper(
ISingletonCosmosClientWrapper singletonWrapper,
IDbContextOptions dbContextOptions,
IExecutionStrategy executionStrategy,
IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger,
IDiagnosticsLogger<DbLoggerCategory.Database> databaseLogger)
{
var options = dbContextOptions.FindExtension<CosmosOptionsExtension>();

_singletonWrapper = singletonWrapper;
_databaseId = options!.DatabaseName;
_executionStrategy = executionStrategy;
_commandLogger = commandLogger;
_databaseLogger = databaseLogger;
_enableContentResponseOnWrite = options.EnableContentResponseOnWrite;
}

Expand All @@ -82,7 +85,11 @@ private CosmosClient Client
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool CreateDatabaseIfNotExists(ThroughputProperties? throughput)
=> _executionStrategy.Execute((throughput, this), CreateDatabaseIfNotExistsOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute((throughput, this), CreateDatabaseIfNotExistsOnce, null);
}

private static bool CreateDatabaseIfNotExistsOnce(
DbContext? context,
Expand Down Expand Up @@ -121,7 +128,11 @@ private static async Task<bool> CreateDatabaseIfNotExistsOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool DeleteDatabase()
=> _executionStrategy.Execute(this, DeleteDatabaseOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute(this, DeleteDatabaseOnce, null);
}

private static bool DeleteDatabaseOnce(
DbContext? context,
Expand Down Expand Up @@ -162,7 +173,11 @@ private static async Task<bool> DeleteDatabaseOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool CreateContainerIfNotExists(ContainerProperties properties)
=> _executionStrategy.Execute((properties, this), CreateContainerIfNotExistsOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute((properties, this), CreateContainerIfNotExistsOnce, null);
}

private static bool CreateContainerIfNotExistsOnce(
DbContext context,
Expand Down Expand Up @@ -215,7 +230,11 @@ public virtual bool CreateItem(
string containerId,
JToken document,
IUpdateEntry entry)
=> _executionStrategy.Execute((containerId, document, entry, this), CreateItemOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute((containerId, document, entry, this), CreateItemOnce, null);
}

private static bool CreateItemOnce(
DbContext context,
Expand Down Expand Up @@ -286,7 +305,11 @@ public virtual bool ReplaceItem(
string documentId,
JObject document,
IUpdateEntry entry)
=> _executionStrategy.Execute((collectionId, documentId, document, entry, this), ReplaceItemOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute((collectionId, documentId, document, entry, this), ReplaceItemOnce, null);
}

private static bool ReplaceItemOnce(
DbContext context,
Expand Down Expand Up @@ -358,7 +381,11 @@ public virtual bool DeleteItem(
string containerId,
string documentId,
IUpdateEntry entry)
=> _executionStrategy.Execute((containerId, documentId, entry, this), DeleteItemOnce, null);
{
_databaseLogger.SyncNotSupported();

return _executionStrategy.Execute((containerId, documentId, entry, this), DeleteItemOnce, null);
}

private static bool DeleteItemOnce(
DbContext context,
Expand Down Expand Up @@ -508,6 +535,8 @@ public virtual IEnumerable<JObject> ExecuteSqlQuery(
string? partitionKey,
CosmosSqlQuery query)
{
_databaseLogger.SyncNotSupported();

_commandLogger.ExecutingSqlQuery(containerId, partitionKey, query);

return new DocumentEnumerable(this, containerId, partitionKey, query);
Expand Down Expand Up @@ -540,6 +569,8 @@ public virtual IAsyncEnumerable<JObject> ExecuteSqlQueryAsync(
string? partitionKey,
string resourceId)
{
_databaseLogger.SyncNotSupported();

_commandLogger.ExecutingReadItem(containerId, partitionKey, resourceId);

var response = _executionStrategy.Execute((containerId, partitionKey, resourceId, this), CreateSingleItemQuery, null);
Expand Down
4 changes: 2 additions & 2 deletions test/EFCore.AspNet.Specification.Tests/GrpcTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public abstract class GrpcFixtureBase : SharedStoreFixtureBase<GrpcContext>
protected override string StoreName
=> "GrpcTest";

protected override void Seed(GrpcContext context)
protected override Task SeedAsync(GrpcContext context)
{
var post = new Post
{
Expand All @@ -153,7 +153,7 @@ protected override void Seed(GrpcContext context)

context.Add(post);

context.SaveChanges();
return context.SaveChangesAsync();
}
}
}
Expand Down
Loading

0 comments on commit eb0c2d2

Please sign in to comment.