Skip to content

Commit

Permalink
Make ConcurrencyDetector optional (dotnet#24125)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Feb 19, 2021
1 parent a2b9a70 commit 2eeaee5
Show file tree
Hide file tree
Showing 42 changed files with 857 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private sealed class QueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>
private readonly string _partitionKey;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly bool _concurrencyDetectionEnabled;

public QueryingEnumerable(
CosmosQueryContext cosmosQueryContext,
Expand All @@ -44,7 +45,8 @@ public QueryingEnumerable(
Func<CosmosQueryContext, JObject, T> shaper,
Type contextType,
string partitionKeyFromExtension,
bool standAloneStateManager)
bool standAloneStateManager,
bool concurrencyDetectionEnabled)
{
_cosmosQueryContext = cosmosQueryContext;
_sqlExpressionFactory = sqlExpressionFactory;
Expand All @@ -54,6 +56,7 @@ public QueryingEnumerable(
_contextType = contextType;
_queryLogger = cosmosQueryContext.QueryLogger;
_standAloneStateManager = standAloneStateManager;
_concurrencyDetectionEnabled = concurrencyDetectionEnabled;

var partitionKey = selectExpression.GetPartitionKey(cosmosQueryContext.ParameterValues);
if (partitionKey != null && partitionKeyFromExtension != null && partitionKeyFromExtension != partitionKey)
Expand Down Expand Up @@ -113,6 +116,7 @@ private sealed class Enumerator : IEnumerator<T>
private readonly string _partitionKey;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly IConcurrencyDetector _concurrencyDetector;

private IEnumerator<JObject> _enumerator;

Expand All @@ -126,6 +130,10 @@ public Enumerator(QueryingEnumerable<T> queryingEnumerable)
_partitionKey = queryingEnumerable._partitionKey;
_queryLogger = queryingEnumerable._queryLogger;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;

_concurrencyDetector = queryingEnumerable._concurrencyDetectionEnabled
? _cosmosQueryContext.ConcurrencyDetector
: null;
}

public T Current { get; private set; }
Expand All @@ -137,7 +145,9 @@ public bool MoveNext()
{
try
{
using (_cosmosQueryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
if (_enumerator == null)
{
Expand All @@ -163,6 +173,10 @@ public bool MoveNext()

return hasNext;
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand Down Expand Up @@ -193,6 +207,7 @@ private sealed class AsyncEnumerator : IAsyncEnumerator<T>
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;
private readonly IConcurrencyDetector _concurrencyDetector;

private IAsyncEnumerator<JObject> _enumerator;

Expand All @@ -207,6 +222,10 @@ public AsyncEnumerator(QueryingEnumerable<T> queryingEnumerable, CancellationTok
_queryLogger = queryingEnumerable._queryLogger;
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;

_concurrencyDetector = queryingEnumerable._concurrencyDetectionEnabled
? _cosmosQueryContext.ConcurrencyDetector
: null;
}

public T Current { get; private set; }
Expand All @@ -215,7 +234,9 @@ public async ValueTask<bool> MoveNextAsync()
{
try
{
using (_cosmosQueryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
if (_enumerator == null)
{
Expand All @@ -241,6 +262,10 @@ public async ValueTask<bool> MoveNextAsync()

return hasNext;
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ private sealed class ReadItemQueryingEnumerable<T> : IEnumerable<T>, IAsyncEnume
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly bool _concurrencyDetectionEnabled;

public ReadItemQueryingEnumerable(
CosmosQueryContext cosmosQueryContext,
ReadItemExpression readItemExpression,
Func<CosmosQueryContext, JObject, T> shaper,
Type contextType,
bool standAloneStateManager)
bool standAloneStateManager,
bool concurrencyDetectionEnabled)
{
_cosmosQueryContext = cosmosQueryContext;
_readItemExpression = readItemExpression;
_shaper = shaper;
_contextType = contextType;
_queryLogger = _cosmosQueryContext.QueryLogger;
_standAloneStateManager = standAloneStateManager;
_concurrencyDetectionEnabled = concurrencyDetectionEnabled;
}

public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -173,6 +176,7 @@ private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly IConcurrencyDetector _concurrencyDetector;
private readonly ReadItemQueryingEnumerable<T> _readItemEnumerable;
private readonly CancellationToken _cancellationToken;

Expand All @@ -189,6 +193,10 @@ public Enumerator(ReadItemQueryingEnumerable<T> readItemEnumerable, Cancellation
_standAloneStateManager = readItemEnumerable._standAloneStateManager;
_readItemEnumerable = readItemEnumerable;
_cancellationToken = cancellationToken;

_concurrencyDetector = readItemEnumerable._concurrencyDetectionEnabled
? _cosmosQueryContext.ConcurrencyDetector
: null;
}

object IEnumerator.Current
Expand All @@ -200,7 +208,9 @@ public bool MoveNext()
{
try
{
using (_cosmosQueryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
if (!_hasExecuted)
{
Expand All @@ -226,6 +236,10 @@ public bool MoveNext()

return false;
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand All @@ -239,7 +253,9 @@ public async ValueTask<bool> MoveNextAsync()
{
try
{
using (_cosmosQueryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
if (!_hasExecuted)
{
Expand Down Expand Up @@ -267,6 +283,10 @@ public async ValueTask<bool> MoveNextAsync()

return false;
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class CosmosShapedQueryCompilingExpressionVisitor : ShapedQueryCo
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IQuerySqlGeneratorFactory _querySqlGeneratorFactory;
private readonly Type _contextType;
private readonly bool _concurrencyDetectionEnabled;
private readonly string _partitionKeyFromExtension;

/// <summary>
Expand All @@ -40,6 +41,7 @@ public CosmosShapedQueryCompilingExpressionVisitor(
_sqlExpressionFactory = sqlExpressionFactory;
_querySqlGeneratorFactory = querySqlGeneratorFactory;
_contextType = cosmosQueryCompilationContext.ContextType;
_concurrencyDetectionEnabled = dependencies.CoreSingletonOptions.IsConcurrencyDetectionEnabled;
_partitionKeyFromExtension = cosmosQueryCompilationContext.PartitionKeyFromExtension;
}

Expand Down Expand Up @@ -85,7 +87,8 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
Expression.Constant(_contextType),
Expression.Constant(_partitionKeyFromExtension, typeof(string)),
Expression.Constant(
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution),
Expression.Constant(_concurrencyDetectionEnabled));

case ReadItemExpression readItemExpression:

Expand All @@ -108,7 +111,8 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
Expression.Constant(shaperReadItemLambda.Compile()),
Expression.Constant(_contextType),
Expression.Constant(
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution),
Expression.Constant(_concurrencyDetectionEnabled));

default:
throw new NotSupportedException(CoreStrings.UnhandledExpressionNode(shapedQueryExpression.QueryExpression));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ private sealed class QueryingEnumerable<T> : IAsyncEnumerable<T>, IEnumerable<T>
private readonly Type _contextType;
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly bool _concurrencyDetectionEnabled;

public QueryingEnumerable(
QueryContext queryContext,
IEnumerable<ValueBuffer> innerEnumerable,
Func<QueryContext, ValueBuffer, T> shaper,
Type contextType,
bool standAloneStateManager)
bool standAloneStateManager,
bool concurrencyDetectionEnabled)
{
_queryContext = queryContext;
_innerEnumerable = innerEnumerable;
_shaper = shaper;
_contextType = contextType;
_queryLogger = queryContext.QueryLogger;
_standAloneStateManager = standAloneStateManager;
_concurrencyDetectionEnabled = concurrencyDetectionEnabled;
}

public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
Expand All @@ -70,6 +73,7 @@ private sealed class Enumerator : IEnumerator<T>, IAsyncEnumerator<T>
private readonly IDiagnosticsLogger<DbLoggerCategory.Query> _queryLogger;
private readonly bool _standAloneStateManager;
private readonly CancellationToken _cancellationToken;
private readonly IConcurrencyDetector? _concurrencyDetector;

private IEnumerator<ValueBuffer>? _enumerator;

Expand All @@ -83,6 +87,10 @@ public Enumerator(QueryingEnumerable<T> queryingEnumerable, CancellationToken ca
_standAloneStateManager = queryingEnumerable._standAloneStateManager;
_cancellationToken = cancellationToken;
Current = default!;

_concurrencyDetector = queryingEnumerable._concurrencyDetectionEnabled
? _queryContext.ConcurrencyDetector
: null;
}

public T Current { get; private set; }
Expand All @@ -94,10 +102,16 @@ public bool MoveNext()
{
try
{
using (_queryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
return MoveNextHelper();
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand All @@ -111,12 +125,18 @@ public ValueTask<bool> MoveNextAsync()
{
try
{
using (_queryContext.ConcurrencyDetector.EnterCriticalSection())
_concurrencyDetector?.EnterCriticalSection();

try
{
_cancellationToken.ThrowIfCancellationRequested();

return new ValueTask<bool>(MoveNextHelper());
}
finally
{
_concurrencyDetector?.ExitCriticalSection();
}
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
public partial class InMemoryShapedQueryCompilingExpressionVisitor : ShapedQueryCompilingExpressionVisitor
{
private readonly Type _contextType;
private readonly bool _concurrencyDetectionEnabled;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -31,6 +32,7 @@ public InMemoryShapedQueryCompilingExpressionVisitor(
: base(dependencies, queryCompilationContext)
{
_contextType = queryCompilationContext.ContextType;
_concurrencyDetectionEnabled = dependencies.CoreSingletonOptions.IsConcurrencyDetectionEnabled;
}

/// <summary>
Expand Down Expand Up @@ -93,7 +95,8 @@ protected override Expression VisitShapedQuery(ShapedQueryExpression shapedQuery
Expression.Constant(shaperLambda.Compile()),
Expression.Constant(_contextType),
Expression.Constant(
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution));
QueryCompilationContext.QueryTrackingBehavior == QueryTrackingBehavior.NoTrackingWithIdentityResolution),
Expression.Constant(_concurrencyDetectionEnabled));
}

private static readonly MethodInfo _tableMethodInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,14 @@ public static int ExecuteSqlRaw(
Check.NotNull(parameters, nameof(parameters));

var facadeDependencies = GetFacadeDependencies(databaseFacade);
var concurrencyDetector = facadeDependencies.ConcurrencyDetector;
var concurrencyDetector = facadeDependencies.CoreOptions.IsConcurrencyDetectionEnabled
? facadeDependencies.ConcurrencyDetector
: null;
var logger = facadeDependencies.CommandLogger;

using (concurrencyDetector.EnterCriticalSection())
concurrencyDetector?.EnterCriticalSection();

try
{
var rawSqlCommand = facadeDependencies.RawSqlCommandBuilder
.Build(sql, parameters);
Expand All @@ -239,6 +243,10 @@ public static int ExecuteSqlRaw(
((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context,
logger));
}
finally
{
concurrencyDetector?.ExitCriticalSection();
}
}

/// <summary>
Expand Down Expand Up @@ -393,10 +401,14 @@ public static async Task<int> ExecuteSqlRawAsync(
Check.NotNull(parameters, nameof(parameters));

var facadeDependencies = GetFacadeDependencies(databaseFacade);
var concurrencyDetector = facadeDependencies.ConcurrencyDetector;
var concurrencyDetector = facadeDependencies.CoreOptions.IsConcurrencyDetectionEnabled
? facadeDependencies.ConcurrencyDetector
: null;
var logger = facadeDependencies.CommandLogger;

using (concurrencyDetector.EnterCriticalSection())
concurrencyDetector?.EnterCriticalSection();

try
{
var rawSqlCommand = facadeDependencies.RawSqlCommandBuilder
.Build(sql, parameters);
Expand All @@ -413,6 +425,10 @@ public static async Task<int> ExecuteSqlRawAsync(
cancellationToken)
.ConfigureAwait(false);
}
finally
{
concurrencyDetector?.ExitCriticalSection();
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 2eeaee5

Please sign in to comment.