添加并发线程控制查询结构合并

This commit is contained in:
xuejiaming 2021-10-04 06:28:26 +08:00
parent 38efd9ea3e
commit ab31ffd83b
9 changed files with 57 additions and 92 deletions

View File

@ -1,7 +1,7 @@
<h1 align="center"> ShardingCore </h1>
`ShardingCore` 易用、简单、高性能、普适性是一款扩展针对efcore生态下的分表分库的扩展解决方案,支持efcore2+的所有版本,支持efcore2+的所有数据库、支持自定义路由、动态路由、高性能分页、读写分离的一款组件,如果你喜欢这组件或者这个组件对你有帮助请[点我star github 地址](https://github.com/xuejmnet/sharding-core)
`ShardingCore` 易用、简单、高性能、普适性是一款扩展针对efcore生态下的分表分库的扩展解决方案,支持efcore2+的所有版本,支持efcore2+的所有数据库、支持自定义路由、动态路由、高性能分页、读写分离的一款组件,如果你喜欢这组件或者这个组件对你有帮助请点击下发star让更多的.neter可以看到使用
---

View File

@ -1,17 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Exceptions;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.Enumerators;
using ShardingCore.Sharding.MergeEngines.ParallelControl;
using ShardingCore.Sharding.StreamMergeEngines;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace ShardingCore.Sharding.MergeEngines.Abstractions
{
@ -22,7 +17,7 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public abstract class AbstractBaseMergeEngine<TEntity>
public abstract class AbstractBaseMergeEngine<TEntity>: IAsyncParallelLimit
{
private readonly SemaphoreSlim _semaphore;
@ -44,51 +39,13 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions
_parallelQueryTimeOut = streamMergeContext.GetParallelQueryTimeOut();
}
/// <summary>
/// 执行异步并发
/// </summary>
/// <param name="queryable"></param>
/// <param name="dataSourceName"></param>
/// <param name="routeResult"></param>
/// <param name="efQuery"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<RouteQueryResult<TResult>> AsyncParallelResultExecuteAsync<TResult>(IQueryable queryable, string dataSourceName, TableRouteResult routeResult, Func<IQueryable, Task<TResult>> efQuery, CancellationToken cancellationToken = new CancellationToken())
{
return AsyncParallelControlExecuteAsync(async () =>
{
var queryResult =
await AsyncParallelResultExecuteAsync0<TResult>(queryable, efQuery, cancellationToken);
return new RouteQueryResult<TResult>(dataSourceName, routeResult, queryResult);
}, cancellationToken);
}
/// <summary>
/// 异步执行并发的实际方法
/// </summary>
/// <param name="queryable"></param>
/// <param name="efQuery"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public abstract Task<TResult> AsyncParallelResultExecuteAsync0<TResult>(IQueryable queryable, Func<IQueryable, Task<TResult>> efQuery, CancellationToken cancellationToken = new CancellationToken());
/// <summary>
/// 执行异步并发
/// </summary>
/// <param name="queryable"></param>
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<IStreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumeratorExecuteAsync(IQueryable<TEntity> queryable, bool async, CancellationToken cancellationToken = new CancellationToken())
{
return AsyncParallelControlExecuteAsync(async () => await AsyncParallelEnumeratorExecuteAsync0(queryable, async, cancellationToken), cancellationToken);
}
/// <summary>
/// 异步多线程控制并发
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="executeAsync"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<TResult> AsyncParallelControlExecuteAsync<TResult>(Func<Task<TResult>> executeAsync,CancellationToken cancellationToken=new CancellationToken())
public Task<TResult> AsyncParallelLimitExecuteAsync<TResult>(Func<Task<TResult>> executeAsync,CancellationToken cancellationToken=new CancellationToken())
{
var parallelTimeOut = _parallelQueryTimeOut.TotalMilliseconds;
var acquired = this._semaphore.Wait((int)parallelTimeOut);
@ -120,14 +77,5 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions
throw new ShardingCoreParallelQueryTimeOutException(_executeExpression.ShardingPrint());
}
}
/// <summary>
/// 异步执行并发的实际方法
/// </summary>
/// <param name="queryable"></param>
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public abstract Task<IStreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumeratorExecuteAsync0(IQueryable<TEntity> queryable, bool async,
CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ShardingCore.Sharding.MergeEngines.Abstractions
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/10/4 6:25:02
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public interface IAsyncParallelLimit
{
Task<TResult> AsyncParallelLimitExecuteAsync<TResult>(Func<Task<TResult>> executeAsync,
CancellationToken cancellationToken = new CancellationToken());
}
}

View File

@ -87,7 +87,7 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge
{
var asyncExecuteQueryable = CreateAsyncExecuteQueryable<TResult>(dataSourceName, routeResult);
return AsyncParallelResultExecuteAsync(asyncExecuteQueryable,dataSourceName,routeResult,efQuery, cancellationToken);
return AsyncParallelResultExecute(asyncExecuteQueryable,dataSourceName,routeResult,efQuery, cancellationToken);
});
}).ToArray();
@ -98,25 +98,24 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions.InMemoryMerge
/// <summary>
/// 异步并发查询
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="queryable"></param>
/// <param name="dataSourceName"></param>
/// <param name="routeResult"></param>
/// <param name="efQuery"></param>
/// <param name="cancellationToken"></param>
/// <typeparam name="TResult"></typeparam>
/// <returns></returns>
public override async Task<TResult> AsyncParallelResultExecuteAsync0<TResult>(IQueryable queryable, Func<IQueryable, Task<TResult>> efQuery,
public Task<RouteQueryResult<TResult>> AsyncParallelResultExecute<TResult>(IQueryable queryable,string dataSourceName,TableRouteResult routeResult, Func<IQueryable, Task<TResult>> efQuery,
CancellationToken cancellationToken = new CancellationToken())
{
var queryResult = await efQuery(queryable);
return queryResult;
}
return AsyncParallelLimitExecuteAsync(async () =>
{
var queryResult = await efQuery(queryable);
public override Task<IStreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumeratorExecuteAsync0(IQueryable<TEntity> queryable, bool async,
CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
return new RouteQueryResult<TResult>(dataSourceName, routeResult, queryResult);
},cancellationToken);
}
public virtual IQueryable DoCombineQueryable<TResult>(IQueryable<TEntity> queryable)
{
return queryable;

View File

@ -100,25 +100,22 @@ namespace ShardingCore.Sharding.MergeEngines.Abstractions.StreamMerge
/// <param name="async"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IStreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumeratorExecuteAsync0(IQueryable<TEntity> queryable, bool async,
public Task<StreamMergeAsyncEnumerator<TEntity>> AsyncParallelEnumerator(IQueryable<TEntity> queryable, bool async,
CancellationToken cancellationToken = new CancellationToken())
{
if (async)
return AsyncParallelLimitExecuteAsync(async () =>
{
var asyncEnumerator = await GetAsyncEnumerator0(queryable);
return new StreamMergeAsyncEnumerator<TEntity>(asyncEnumerator);
}
else
{
var enumerator = GetEnumerator0(queryable);
return new StreamMergeAsyncEnumerator<TEntity>(enumerator);
}
}
public override Task<TResult> AsyncParallelResultExecuteAsync0<TResult>(IQueryable queryable, Func<IQueryable, Task<TResult>> efQuery,
CancellationToken cancellationToken = new CancellationToken())
{
throw new NotImplementedException();
if (async)
{
var asyncEnumerator = await GetAsyncEnumerator0(queryable);
return new StreamMergeAsyncEnumerator<TEntity>(asyncEnumerator);
}
else
{
var enumerator = GetEnumerator0(queryable);
return new StreamMergeAsyncEnumerator<TEntity>(enumerator);
}
}, cancellationToken);
}
/// <summary>

View File

@ -105,7 +105,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
var enumeratorTasks = sequenceResults.Select(sequenceResult =>
{
var newQueryable = CreateAsyncExecuteQueryable(sequenceResult.DSName, noPaginationQueryable, sequenceResult, reSetOrders);
return AsyncParallelEnumeratorExecuteAsync(newQueryable, async, cancellationToken);
return AsyncParallelEnumerator(newQueryable, async, cancellationToken);
}).ToArray();
var streamEnumerators = Task.WhenAll(enumeratorTasks).WaitAndUnwrapException();

View File

@ -35,7 +35,7 @@ namespace ShardingCore.Sharding.MergeEngines.EnumeratorStreamMergeEngines.Enumer
return tableRouteResults.Select(routeResult =>
{
var newQueryable = CreateAsyncExecuteQueryable(dataSourceName, routeResult);
return AsyncParallelEnumeratorExecuteAsync(newQueryable, async,cancellationToken);
return AsyncParallelEnumerator(newQueryable, async,cancellationToken);
});
}).ToArray();

View File

@ -51,7 +51,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
return StreamMergeContext.TableRouteResults.Select(routeResult =>
{
var newQueryable = CreateAsyncExecuteQueryable(dataSourceName, reverseOrderQueryable, routeResult);
return AsyncParallelEnumeratorExecuteAsync(newQueryable, async,cancellationToken);
return AsyncParallelEnumerator(newQueryable, async,cancellationToken);
});
}).ToArray();;

View File

@ -92,7 +92,7 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
var enumeratorTasks = sequenceResults.Select(sequenceResult =>
{
var newQueryable = CreateAsyncExecuteQueryable(sequenceResult.DSName, noPaginationQueryable, sequenceResult);
return AsyncParallelEnumeratorExecuteAsync(newQueryable, async,cancellationToken);
return AsyncParallelEnumerator(newQueryable, async,cancellationToken);
}).ToArray();
var streamEnumerators = Task.WhenAll(enumeratorTasks).WaitAndUnwrapException();