优化[#81]交由efcore执行的非聚合查询需判断是否使用读写分离且当前是否开启读写分离
This commit is contained in:
parent
9e45d71964
commit
ce42da428e
|
@ -74,5 +74,33 @@ namespace ShardingCore.Extensions
|
|||
shardingReadWriteContext.DefaultPriority = priority;
|
||||
shardingReadWriteContext.DefaultReadEnable = readOnly;
|
||||
}
|
||||
/// <summary>
|
||||
/// 当前dbcontext是否是启用了读写分离
|
||||
/// </summary>
|
||||
/// <param name="shardingDbContext"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CurrentIsReadWriteSeparation(this IShardingDbContext shardingDbContext)
|
||||
{
|
||||
if (shardingDbContext is ISupportShardingReadWrite shardingReadWrite)
|
||||
{
|
||||
var shardingReadWriteManager = ShardingContainer.GetService<IShardingReadWriteManager>();
|
||||
var shardingReadWriteContext = shardingReadWriteManager.GetCurrent(shardingDbContext.GetType());
|
||||
if (shardingReadWriteContext != null)
|
||||
{
|
||||
if (shardingReadWriteContext.DefaultPriority > shardingReadWrite.ReadWriteSeparationPriority)
|
||||
{
|
||||
return shardingReadWriteContext.DefaultReadEnable;
|
||||
}
|
||||
else
|
||||
{
|
||||
return shardingReadWrite.ReadWriteSeparation;
|
||||
}
|
||||
}
|
||||
|
||||
return shardingReadWrite.ReadWriteSeparation;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,5 +20,6 @@ namespace ShardingCore.Sharding.ShardingExecutors.Abstractions
|
|||
Type GetShardingDbContextType();
|
||||
QueryCompilerExecutor GetQueryCompilerExecutor();
|
||||
bool IsEnumerableQuery();
|
||||
bool IsParallelQuery();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,12 +29,12 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
/// <summary>
|
||||
/// 本次查询跨库
|
||||
/// </summary>
|
||||
public readonly bool _isCrossDataSource;
|
||||
private readonly bool _isCrossDataSource;
|
||||
|
||||
/// <summary>
|
||||
/// 本次查询跨表
|
||||
/// </summary>
|
||||
public readonly bool _isCrossTable;
|
||||
private readonly bool _isCrossTable;
|
||||
|
||||
|
||||
private QueryCompilerExecutor _queryCompilerExecutor;
|
||||
|
@ -92,6 +92,10 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
{
|
||||
return _queryCompilerContext.GetShardingDbContextType();
|
||||
}
|
||||
public bool IsParallelQuery()
|
||||
{
|
||||
return _queryCompilerContext.IsParallelQuery();
|
||||
}
|
||||
|
||||
public QueryCompilerExecutor GetQueryCompilerExecutor()
|
||||
{
|
||||
|
@ -100,7 +104,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
if (!IsMergeQuery())
|
||||
{
|
||||
var routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
|
||||
var dbContext = GetShardingDbContext().GetDbContext(_dataSourceRouteResult.IntersectDataSources.First(), false, routeTailFactory.Create(_tableRouteResults.First()));
|
||||
var dbContext = GetShardingDbContext().GetDbContext(_dataSourceRouteResult.IntersectDataSources.First(), IsParallelQuery(), routeTailFactory.Create(_tableRouteResults.First()));
|
||||
_queryCompilerExecutor = new QueryCompilerExecutor(dbContext, GetQueryExpression());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,8 +24,9 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
private readonly Expression _queryExpression;
|
||||
private readonly IEntityMetadataManager _entityMetadataManager;
|
||||
private readonly Type _shardingDbContextType;
|
||||
private readonly IShardingConfigOption _shardingConfigOption;
|
||||
private QueryCompilerExecutor _queryCompilerExecutor;
|
||||
private bool? hasQueryCompilerExecutor;
|
||||
private bool? hasQueryCompilerExecutor;
|
||||
|
||||
private QueryCompilerContext( IShardingDbContext shardingDbContext, Expression queryExpression)
|
||||
{
|
||||
|
@ -34,6 +35,9 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
_shardingDbContext = shardingDbContext;
|
||||
_queryExpression = queryExpression;
|
||||
_entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(_shardingDbContextType));
|
||||
|
||||
_shardingConfigOption = ShardingContainer.GetServices<IShardingConfigOption>()
|
||||
.FirstOrDefault(o => o.ShardingDbContextType == _shardingDbContextType);
|
||||
}
|
||||
|
||||
public static QueryCompilerContext Create(IShardingDbContext shardingDbContext, Expression queryExpression)
|
||||
|
@ -66,6 +70,10 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
return _shardingDbContextType;
|
||||
}
|
||||
|
||||
public bool IsParallelQuery()
|
||||
{
|
||||
return _shardingConfigOption.UseReadWrite&&_shardingDbContext.CurrentIsReadWriteSeparation();
|
||||
}
|
||||
public QueryCompilerExecutor GetQueryCompilerExecutor()
|
||||
{
|
||||
if (!hasQueryCompilerExecutor.HasValue)
|
||||
|
@ -75,7 +83,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
|
|||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(
|
||||
typeof(IVirtualDataSource<>).GetGenericType0(_shardingDbContextType));
|
||||
var routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>();
|
||||
var dbContext = _shardingDbContext.GetDbContext(virtualDataSource.DefaultDataSourceName, false, routeTailFactory.Create(string.Empty));
|
||||
var dbContext = _shardingDbContext.GetDbContext(virtualDataSource.DefaultDataSourceName, IsParallelQuery(), routeTailFactory.Create(string.Empty));
|
||||
_queryCompilerExecutor = new QueryCompilerExecutor(dbContext, _queryExpression);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -241,7 +241,7 @@ namespace ShardingCore.Sharding
|
|||
/// <returns></returns>
|
||||
private bool IsUseReadWriteSeparation()
|
||||
{
|
||||
return _shardingConfigOption.UseReadWrite;
|
||||
return _shardingConfigOption.UseReadWrite&&_shardingDbContext.CurrentIsReadWriteSeparation();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in New Issue