Skip to content

Commit bfb98b4

Browse files
authored
Remove sync support from Cosmos provider (#37103)
Closes #37059
1 parent 89c5d0f commit bfb98b4

21 files changed

+272
-1078
lines changed

src/EFCore.Cosmos/Diagnostics/CosmosEventId.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class CosmosEventId
2525
private enum Id
2626
{
2727
// Database events
28+
[Obsolete("Synchronous I/O has been fully removed and now always throws.")]
2829
SyncNotSupported = CoreEventId.ProviderBaseId,
2930

3031
// Command events
@@ -44,19 +45,6 @@ private enum Id
4445
NoPartitionKeyDefined = CoreEventId.ProviderBaseId + 600,
4546
}
4647

47-
private static readonly string DatabasePrefix = DbLoggerCategory.Database.Name + ".";
48-
49-
/// <summary>
50-
/// Azure Cosmos DB does not support synchronous I/O. Make sure to use and correctly await only async
51-
/// methods when using Entity Framework Core to access Azure Cosmos DB.
52-
/// See https://aka.ms/ef-cosmos-nosync for more information.
53-
/// </summary>
54-
/// <remarks>
55-
/// This event is in the <see cref="DbLoggerCategory.Database" /> category.
56-
/// </remarks>
57-
public static readonly EventId SyncNotSupported
58-
= new((int)Id.SyncNotSupported, DatabasePrefix + Id.SyncNotSupported);
59-
6048
private static readonly string CommandPrefix = DbLoggerCategory.Database.Command.Name + ".";
6149

6250
/// <summary>

src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggerExtensions.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,6 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal;
1919
/// </summary>
2020
public static class CosmosLoggerExtensions
2121
{
22-
/// <summary>
23-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
24-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
25-
/// any release. You should only use it directly in your code with extreme caution and knowing that
26-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
27-
/// </summary>
28-
public static void SyncNotSupported(
29-
this IDiagnosticsLogger<DbLoggerCategory.Database> diagnostics)
30-
{
31-
var definition = CosmosResources.LogSyncNotSupported(diagnostics);
32-
33-
if (diagnostics.ShouldLog(definition))
34-
{
35-
definition.Log(diagnostics);
36-
}
37-
38-
if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
39-
{
40-
var eventData = new EventData(
41-
definition,
42-
(d, p) => ((EventDefinition)d).GenerateMessage());
43-
44-
diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
45-
}
46-
}
47-
4822
/// <summary>
4923
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
5024
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

src/EFCore.Cosmos/Diagnostics/Internal/CosmosLoggingDefinitions.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ public class CosmosLoggingDefinitions : LoggingDefinitions
7575
/// </summary>
7676
public EventDefinitionBase? LogExecutedDeleteItem;
7777

78-
/// <summary>
79-
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
80-
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
81-
/// any release. You should only use it directly in your code with extreme caution and knowing that
82-
/// doing so can result in application failures when updating to a new Entity Framework Core release.
83-
/// </summary>
84-
public EventDefinitionBase? LogSyncNotSupported;
85-
8678
/// <summary>
8779
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
8880
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

src/EFCore.Cosmos/Extensions/CosmosDbContextOptionsExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public static DbContextOptionsBuilder UseCosmos(
5252
Check.NotNull(optionsBuilder);
5353
Check.NotNull(cosmosOptionsAction);
5454

55-
ConfigureWarnings(optionsBuilder);
56-
5755
cosmosOptionsAction.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
5856

5957
return optionsBuilder;
@@ -120,8 +118,6 @@ public static DbContextOptionsBuilder UseCosmos(
120118
.WithAccountKey(accountKey)
121119
.WithDatabaseName(databaseName);
122120

123-
ConfigureWarnings(optionsBuilder);
124-
125121
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
126122

127123
cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
@@ -190,8 +186,6 @@ public static DbContextOptionsBuilder UseCosmos(
190186
.WithTokenCredential(tokenCredential)
191187
.WithDatabaseName(databaseName);
192188

193-
ConfigureWarnings(optionsBuilder);
194-
195189
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
196190

197191
cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
@@ -253,8 +247,6 @@ public static DbContextOptionsBuilder UseCosmos(
253247
.WithConnectionString(connectionString)
254248
.WithDatabaseName(databaseName);
255249

256-
ConfigureWarnings(optionsBuilder);
257-
258250
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
259251

260252
cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
@@ -268,10 +260,6 @@ var coreOptionsExtension
268260
= optionsBuilder.Options.FindExtension<CoreOptionsExtension>()
269261
?? new CoreOptionsExtension();
270262

271-
coreOptionsExtension = coreOptionsExtension.WithWarningsConfiguration(
272-
coreOptionsExtension.WarningsConfiguration.TryWithExplicit(
273-
CosmosEventId.SyncNotSupported, WarningBehavior.Throw));
274-
275263
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(coreOptionsExtension);
276264
}
277265
}

src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Lines changed: 6 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/EFCore.Cosmos/Properties/CosmosStrings.resx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,6 @@
250250
<value>The key property '{entityType}.{property}' is not configured to generate values and has the CLR default or sentinel value while saving a new entity to the database. The Azure Cosmos DB database provider for EF Core does not generate key values by default. This means key values must be explicitly set before saving new entities. See https://aka.ms/ef-cosmos-keys for more information.</value>
251251
<comment>Warning CosmosEventId.PrimaryKeyValueNotSet string string</comment>
252252
</data>
253-
<data name="LogSyncNotSupported" xml:space="preserve">
254-
<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>
255-
<comment>Error CosmosEventId.SyncNotSupported</comment>
256-
</data>
257253
<data name="MissingOrderingInSelectExpression" xml:space="preserve">
258254
<value>'Reverse' could not be translated to the server because there is no ordering on the server side.</value>
259255
</data>
@@ -353,6 +349,9 @@
353349
<data name="SingleFirstOrDefaultNotSupportedOnNonNullableQueries" xml:space="preserve">
354350
<value>SingleOrDefault and FirstOrDefault cannot be used Cosmos SQL does not allow Offset without Limit. Consider specifying a 'Take' operation on the query.</value>
355351
</data>
352+
<data name="SyncNotSupported" xml:space="preserve">
353+
<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.</value>
354+
</data>
356355
<data name="ThroughputMismatch" xml:space="preserve">
357356
<value>The provisioned throughput was configured to '{throughput1}' on '{entityType1}', but on '{entityType2}' it was configured to '{throughput2}'. All entity types mapped to the same container '{container}' must be configured with the same provisioned throughput.</value>
358357
</data>

src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.QueryingEnumerable.cs

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System.Collections;
77
using System.Text;
8+
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
89
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
910
using Newtonsoft.Json.Linq;
1011

@@ -64,7 +65,7 @@ public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToke
6465
=> new AsyncEnumerator(this, cancellationToken);
6566

6667
public IEnumerator<T> GetEnumerator()
67-
=> new Enumerator(this);
68+
=> throw new InvalidOperationException(CosmosStrings.SyncNotSupported);
6869

6970
IEnumerator IEnumerable.GetEnumerator()
7071
=> GetEnumerator();
@@ -99,95 +100,6 @@ public string ToQueryString()
99100
return builder.Append(sqlQuery.Query).ToString();
100101
}
101102

102-
private sealed class Enumerator : IEnumerator<T>
103-
{
104-
private readonly QueryingEnumerable<T> _queryingEnumerable;
105-
private readonly CosmosQueryContext _cosmosQueryContext;
106-
private readonly Func<CosmosQueryContext, JToken, T> _shaper;
107-
private readonly Type _contextType;
108-
private readonly string _cosmosContainer;
109-
private readonly PartitionKey _cosmosPartitionKey;
110-
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
111-
private readonly bool _standAloneStateManager;
112-
private readonly IConcurrencyDetector _concurrencyDetector;
113-
private readonly IExceptionDetector _exceptionDetector;
114-
115-
private IEnumerator<JToken> _enumerator;
116-
117-
public Enumerator(QueryingEnumerable<T> queryingEnumerable)
118-
{
119-
_queryingEnumerable = queryingEnumerable;
120-
_cosmosQueryContext = queryingEnumerable._cosmosQueryContext;
121-
_shaper = queryingEnumerable._shaper;
122-
_contextType = queryingEnumerable._contextType;
123-
_cosmosContainer = queryingEnumerable._cosmosContainer;
124-
_cosmosPartitionKey = queryingEnumerable._cosmosPartitionKey;
125-
_queryLogger = queryingEnumerable._queryLogger;
126-
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
127-
_exceptionDetector = _cosmosQueryContext.ExceptionDetector;
128-
129-
_concurrencyDetector = queryingEnumerable._threadSafetyChecksEnabled
130-
? _cosmosQueryContext.ConcurrencyDetector
131-
: null;
132-
}
133-
134-
public T Current { get; private set; }
135-
136-
object IEnumerator.Current
137-
=> Current;
138-
139-
public bool MoveNext()
140-
{
141-
try
142-
{
143-
using var _ = _concurrencyDetector?.EnterCriticalSection();
144-
145-
if (_enumerator == null)
146-
{
147-
var sqlQuery = _queryingEnumerable.GenerateQuery();
148-
149-
EntityFrameworkMetricsData.ReportQueryExecuting();
150-
151-
_enumerator = _cosmosQueryContext.CosmosClient
152-
.ExecuteSqlQuery(_cosmosContainer, _cosmosPartitionKey, sqlQuery)
153-
.GetEnumerator();
154-
_cosmosQueryContext.InitializeStateManager(_standAloneStateManager);
155-
}
156-
157-
var hasNext = _enumerator.MoveNext();
158-
159-
Current
160-
= hasNext
161-
? _shaper(_cosmosQueryContext, _enumerator.Current)
162-
: default;
163-
164-
return hasNext;
165-
}
166-
catch (Exception exception)
167-
{
168-
if (_exceptionDetector.IsCancellation(exception))
169-
{
170-
_queryLogger.QueryCanceled(_contextType);
171-
}
172-
else
173-
{
174-
_queryLogger.QueryIterationFailed(_contextType, exception);
175-
}
176-
177-
throw;
178-
}
179-
}
180-
181-
public void Dispose()
182-
{
183-
_enumerator?.Dispose();
184-
_enumerator = null;
185-
}
186-
187-
public void Reset()
188-
=> throw new NotSupportedException(CoreStrings.EnumerableResetNotSupported);
189-
}
190-
191103
private sealed class AsyncEnumerator : IAsyncEnumerator<T>
192104
{
193105
private readonly QueryingEnumerable<T> _queryingEnumerable;

0 commit comments

Comments
 (0)