Merge branch 'main' of github.com:dotnetcore/sharding-core

This commit is contained in:
xuejiaming 2022-10-20 13:20:34 +08:00
commit 0f06a7a2b2
22 changed files with 185 additions and 181 deletions

View File

@ -65,7 +65,6 @@ namespace Sample.MySql.Controllers
// //一定要先在路由里面添加尾巴
// virtualTableRoute.Append("20220921");
// shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
return _defaultTableDbContext.Set<SysTest>();
}
[HttpGet]

View File

@ -179,7 +179,6 @@ namespace Sample.MySql
{
app.UseDeveloperExceptionPage();
}
app.ApplicationServices.UseAutoShardingCreate();
// app.ApplicationServices.UseAutoTryCompensateTable();
// app.ApplicationServices.UseAutoShardingCreate();

View File

@ -5,7 +5,6 @@ using Sample.ShardingConsole;
using ShardingCore;
using ShardingCore.Extensions;
ShardingProvider.ShardingRuntimeContext.UseAutoShardingCreate();
ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable();
var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

View File

@ -10,7 +10,7 @@ namespace ShardingCore.Bootstrappers
/// 主要的分片初始化器,需要手动调用,如果你的分片路由存在定时执行的job譬如
/// 系统默认的时间分片的情况下那么需要调用<code>IShardingRuntimeContext初始化的时候会调用</code>
/// </summary>
public interface IShardingBootstrapper
internal interface IShardingBootstrapper
{
void AutoShardingCreate();
}

View File

@ -25,21 +25,18 @@ namespace ShardingCore.Bootstrappers
* @Date: Monday, 21 December 2020 09:10:07
* @Email: 326308290@qq.com
*/
public class ShardingBootstrapper : IShardingBootstrapper
internal class ShardingBootstrapper : IShardingBootstrapper
{
private readonly IShardingProvider _shardingProvider;
private readonly IDbContextCreator _dbContextCreator;
private readonly DoOnlyOnce _onlyOnce=new DoOnlyOnce();
public ShardingBootstrapper(IShardingProvider shardingProvider,IDbContextCreator dbContextCreator)
public ShardingBootstrapper(IShardingProvider shardingProvider)
{
_shardingProvider = shardingProvider;
_dbContextCreator = dbContextCreator;
}
public void AutoShardingCreate()
{
if (!_onlyOnce.IsUnDo())
return;
CheckRequirement();
StartAutoShardingJob();
}
@ -51,29 +48,6 @@ namespace ShardingCore.Bootstrappers
await jobRunnerService.StartAsync();
}, TaskCreationOptions.LongRunning);
}
private void CheckRequirement()
{
try
{
using (var scope = _shardingProvider.CreateScope())
{
using (var dbContext = _dbContextCreator.GetShellDbContext(scope.ServiceProvider))
{
if (dbContext == null)
{
throw new ShardingCoreInvalidOperationException(
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}");
}
}
}
}
catch (Exception ex)
{
throw new ShardingCoreInvalidOperationException(
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
ex);
}
}
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Core.ServiceProviders;
using ShardingCore.Exceptions;
using ShardingCore.Helpers;
using ShardingCore.Sharding.Abstractions;
@ -45,8 +46,17 @@ namespace ShardingCore.Core.DbContextCreator
}
public virtual DbContext GetShellDbContext(IShardingProvider shardingProvider)
{
try
{
return shardingProvider.GetService<TShardingDbContext>();
}
catch (Exception ex)
{
throw new ShardingCoreInvalidOperationException(
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
ex);
}
}
}
}

View File

@ -56,11 +56,8 @@ namespace ShardingCore.Core.RuntimeContexts
IShardingPageManager GetShardingPageManager();
IDataSourceInitializer GetDataSourceInitializer();
void CheckRequirement();
void GetOrCreateShardingRuntimeModel(DbContext dbContext);
void Initialize();
void AutoShardingCreate();
object GetService(Type serviceType);
TService GetService<TService>();
object GetRequiredService(Type serviceType);

View File

@ -23,6 +23,7 @@ using ShardingCore.DynamicDataSources;
using ShardingCore.Exceptions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.MergeEngines.ParallelControl;
using ShardingCore.Sharding.ParallelTables;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
@ -67,10 +68,11 @@ namespace ShardingCore.Core.RuntimeContexts
_serviceProvider = _serviceMap.BuildServiceProvider();
_serviceProvider.GetRequiredService<IShardingInitializer>().Initialize();
InitFieldValue();
AutoShardingCreate();
}
}
public void AutoShardingCreate()
private void AutoShardingCreate()
{
GetRequiredService<IShardingBootstrapper>().AutoShardingCreate();
}
@ -217,40 +219,6 @@ namespace ShardingCore.Core.RuntimeContexts
return _dataSourceInitializer??=GetRequiredService<IDataSourceInitializer>();
}
public void CheckRequirement()
{
if (isCheckRequirement)
return;
lock (CHECK_REQUIREMENT)
{
if (isCheckRequirement)
return;
isCheckRequirement = true;
try
{
var shardingProvider = GetShardingProvider();
using (var scope = shardingProvider.CreateScope())
{
using (var dbContext = _dbContextCreator.GetShellDbContext(scope.ServiceProvider))
{
if (dbContext == null)
{
throw new ShardingCoreInvalidOperationException(
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}");
}
}
}
}
catch (Exception ex)
{
throw new ShardingCoreInvalidOperationException(
$"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}",
ex);
}
}
}
public void GetOrCreateShardingRuntimeModel(DbContext dbContext)
{

View File

@ -92,13 +92,19 @@ namespace ShardingCore.EFCores
await _shardingDbContext.CommitAsync(cancellationToken);
_shardingDbContext.NotifyShardingTransaction();
}
// #if !NETCOREAPP3_0
// public override void CreateSavepoint(string name)
// {
// AAA
// base.CreateSavepoint(name);
// }
// #endif
#if !NETCOREAPP3_0&&!NETSTANDARD2_0
// public override void CreateSavepoint(string name)
// {
// base.CreateSavepoint(name);
// _shardingDbContext.CreateSavepoint(name);
// }
//
// public override async Task CreateSavepointAsync(string name, CancellationToken cancellationToken = new CancellationToken())
// {
// await base.CreateSavepointAsync(name, cancellationToken);
// await _shardingDbContext.CreateSavepointAsync(name,cancellationToken);
// }
#endif
#endif
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -10,11 +11,6 @@ namespace ShardingCore.Extensions
public static class ShardingRuntimeExtension
{
public static void UseAutoShardingCreate(this IShardingRuntimeContext shardingRuntimeContext)
{
shardingRuntimeContext.CheckRequirement();
shardingRuntimeContext.AutoShardingCreate();
}
/// <summary>
/// 自动尝试补偿表
/// </summary>
@ -22,7 +18,6 @@ namespace ShardingCore.Extensions
/// <param name="parallelCount"></param>
public static void UseAutoTryCompensateTable(this IShardingRuntimeContext shardingRuntimeContext, int? parallelCount = null)
{
shardingRuntimeContext.CheckRequirement();
var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource();
var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer();
var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions();

View File

@ -91,6 +91,15 @@ namespace ShardingCore.Sharding.Abstractions
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task CommitAsync(CancellationToken cancellationToken = new CancellationToken());
#if !NETCOREAPP3_0&&!NETSTANDARD2_0
// void CreateSavepoint(string name);
// Task CreateSavepointAsync(string name, CancellationToken cancellationToken = new CancellationToken());
// void RollbackToSavepoint(string name);
// Task RollbackToSavepointAsync(string name,CancellationToken cancellationToken = default(CancellationToken));
// void ReleaseSavepoint(string name);
// Task ReleaseSavepointAsync(string name, CancellationToken cancellationToken = default(CancellationToken));
#endif
#endif
}
}

View File

@ -21,6 +21,14 @@ namespace ShardingCore.Sharding.Abstractions
#if !NETCOREAPP2_0
Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken());
Task CommitAsync(CancellationToken cancellationToken = new CancellationToken());
#if !NETCOREAPP3_0 && !NETSTANDARD2_0
// void CreateSavepoint(string name);
// Task CreateSavepointAsync(string name, CancellationToken cancellationToken = new CancellationToken());
// void RollbackToSavepoint(string name);
// Task RollbackToSavepointAsync(string name,CancellationToken cancellationToken = default(CancellationToken));
// void ReleaseSavepoint(string name);
// Task ReleaseSavepointAsync(string name, CancellationToken cancellationToken = default(CancellationToken));
#endif
#endif
}
}

View File

@ -59,6 +59,15 @@ namespace ShardingCore.Sharding.MergeContexts
var orders = orderByContext.PropertyOrders;
var combineQueryable = mergeQueryCompilerContext.GetQueryCombineResult().GetCombineQueryable();
if (skip is < 0)
{
throw new ShardingCoreException($"queryable:{mergeQueryCompilerContext.GetQueryCombineResult().GetQueryCompilerContext().GetQueryExpression().ShardingPrint()} skip should >= 0");
}
if (take is < 0)
{
throw new ShardingCoreException($"queryable:{mergeQueryCompilerContext.GetQueryCombineResult().GetQueryCompilerContext().GetQueryExpression().ShardingPrint()} take should >= 0");
}
//去除分页,获取前Take+Skip数量
var reWriteQueryable = combineQueryable;
if (take.HasValue || skip.HasValue)

View File

@ -1,15 +0,0 @@
using System;
/*
* @Author: xjm
* @Description:
* @Date: DATE TIME
* @Email: 326308290@qq.com
*/
namespace ShardingCore.Sharding.ParseEngines
{
public class ParseEngine
{
}
}

View File

@ -18,7 +18,6 @@ using ShardingCore.Core.RuntimeContexts;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Infrastructures;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Sharding.ShardingDbContextExecutors
@ -32,11 +31,10 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
*/
public class DataSourceDbContext : IDataSourceDbContext
{
private static readonly IComparer<string> _comparer = new NoShardingFirstComparer();
private readonly ILogger<DataSourceDbContext> _logger;
public Type DbContextType { get; }
/// <summary>
/// 当前是否是默认的dbcontext 也就是不分片的dbcontext
/// </summary>
@ -82,6 +80,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
/// shell dbcontext最外面的壳
/// </summary>
private readonly DbContext _shardingShellDbContext;
private readonly IShardingRuntimeContext _shardingRuntimeContext;
/// <summary>
@ -117,8 +116,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
bool isDefault,
DbContext shardingShellDbContext,
IDbContextCreator dbContextCreator,
ActualConnectionStringManager actualConnectionStringManager,
ILogger<DataSourceDbContext> logger)
ActualConnectionStringManager actualConnectionStringManager)
{
var shardingDbContext = (IShardingDbContext)shardingShellDbContext;
DataSourceName = dataSourceName;
@ -126,11 +124,10 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
_shardingShellDbContext = shardingShellDbContext;
_shardingRuntimeContext = shardingShellDbContext.GetShardingRuntimeContext();
DbContextType = shardingShellDbContext.GetType();
_virtualDataSource =shardingDbContext
_virtualDataSource = shardingDbContext
.GetVirtualDataSource();
_dbContextCreator = dbContextCreator;
_actualConnectionStringManager = actualConnectionStringManager;
this._logger = logger;
}
/// <summary>
@ -155,7 +152,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
{
//先创建dbcontext option builder
var dbContextOptionBuilderCreator = _shardingRuntimeContext.GetDbContextOptionBuilderCreator();
var dbContextOptionsBuilder = dbContextOptionBuilderCreator.CreateDbContextOptionBuilder().UseShardingOptions(_shardingRuntimeContext);
var dbContextOptionsBuilder = dbContextOptionBuilderCreator.CreateDbContextOptionBuilder()
.UseShardingOptions(_shardingRuntimeContext);
if (IsDefault)
{
@ -342,70 +340,88 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
{
if (IsDefault)
return;
try
{
CurrentDbContextTransaction?.Rollback();
}
catch (Exception e)
{
_logger.LogError(e, "rollback error.");
}
}
/// <summary>
/// 提交数据
/// </summary>
/// <param name="dataSourceCount">如果只有一个数据源那么就直接报错否则就忽略</param>
public void Commit(int dataSourceCount)
public void Commit()
{
if (IsDefault)
return;
try
{
CurrentDbContextTransaction?.Commit();
}
catch (Exception e)
{
_logger.LogError(e, "commit error.");
if (dataSourceCount == 1)
throw;
}
}
#if !NETCOREAPP2_0
public async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
if (IsDefault)
return;
try
{
if (CurrentDbContextTransaction != null)
await CurrentDbContextTransaction.RollbackAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "rollback error.");
}
}
public async Task CommitAsync(int dataSourceCount, CancellationToken cancellationToken =
public async Task CommitAsync(CancellationToken cancellationToken =
new CancellationToken())
{
cancellationToken.ThrowIfCancellationRequested();
if (IsDefault)
return;
try
{
if (CurrentDbContextTransaction != null)
await CurrentDbContextTransaction.CommitAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "commit error.");
if (dataSourceCount == 1)
throw;
}
}
#if !NETCOREAPP3_0&&!NETSTANDARD2_0
// public void CreateSavepoint(string name)
// {
// if (IsDefault)
// return;
// CurrentDbContextTransaction?.CreateSavepoint(name);
// }
//
// public async Task CreateSavepointAsync(string name,
// CancellationToken cancellationToken = new CancellationToken())
// {
// cancellationToken.ThrowIfCancellationRequested();
// if (IsDefault)
// return;
// if (CurrentDbContextTransaction != null)
// await CurrentDbContextTransaction.CreateSavepointAsync(name, cancellationToken);
// }
//
// public void RollbackToSavepoint(string name)
// {
// if (IsDefault)
// return;
// CurrentDbContextTransaction?.RollbackToSavepoint(name);
// }
//
// public async Task RollbackToSavepointAsync(string name,
// CancellationToken cancellationToken = default(CancellationToken))
// {
// cancellationToken.ThrowIfCancellationRequested();
// if (IsDefault)
// return;
// if (CurrentDbContextTransaction != null)
// await CurrentDbContextTransaction.RollbackToSavepointAsync(name, cancellationToken);
// }
//
// public void ReleaseSavepoint(string name)
// {
// if (IsDefault)
// return;
// CurrentDbContextTransaction?.ReleaseSavepoint(name);
// }
//
// public async Task ReleaseSavepointAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
// {
// cancellationToken.ThrowIfCancellationRequested();
// if (IsDefault)
// return;
// if (CurrentDbContextTransaction != null)
// await CurrentDbContextTransaction.ReleaseSavepointAsync(name, cancellationToken);
// }
#endif
#endif
public void Dispose()

View File

@ -45,10 +45,18 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
IDictionary<string, DbContext> GetCurrentContexts();
void Rollback();
void Commit(int dataSourceCount);
void Commit();
#if !NETCOREAPP2_0
Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken());
Task CommitAsync(int dataSourceCount,CancellationToken cancellationToken = new CancellationToken());
Task CommitAsync(CancellationToken cancellationToken = new CancellationToken());
#if !NETCOREAPP3_0&&!NETSTANDARD2_0
// void CreateSavepoint(string name);
// Task CreateSavepointAsync(string name, CancellationToken cancellationToken = new CancellationToken());
// void RollbackToSavepoint(string name);
// Task RollbackToSavepointAsync(string name,CancellationToken cancellationToken = default(CancellationToken));
// void ReleaseSavepoint(string name);
// Task ReleaseSavepointAsync(string name, CancellationToken cancellationToken = default(CancellationToken));
#endif
#endif
}

View File

@ -34,7 +34,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
/// <typeparam name="TShardingDbContext"></typeparam>
public class ShardingDbContextExecutor : IShardingDbContextExecutor
{
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger<ShardingDbContextExecutor> _logger;
private readonly DbContext _shardingDbContext;
//private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>> _dbContextCaches = new ConcurrentDictionary<string, ConcurrentDictionary<string, DbContext>>();
@ -78,7 +78,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
_routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory();
var shardingReadWriteManager = _shardingRuntimeContext.GetShardingReadWriteManager();
var shardingProvider = _shardingRuntimeContext.GetShardingProvider();
_loggerFactory=shardingProvider.GetService<ILoggerFactory>();
var loggerFactory=shardingProvider.GetRequiredService<ILoggerFactory>();
_logger=loggerFactory.CreateLogger<ShardingDbContextExecutor>();
_actualConnectionStringManager = new ActualConnectionStringManager(shardingReadWriteManager,_virtualDataSource);
}
@ -86,7 +87,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
private IDataSourceDbContext GetDataSourceDbContext(string dataSourceName)
{
return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _dbContextCreator, _actualConnectionStringManager,_loggerFactory.CreateLogger<DataSourceDbContext>()));
return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _dbContextCreator, _actualConnectionStringManager));
}
/// <summary>
@ -194,9 +195,20 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
public void Commit()
{
int i = 0;
foreach (var dbContextCache in _dbContextCaches)
{
dbContextCache.Value.Commit(_dbContextCaches.Count);
try
{
dbContextCache.Value.Commit();
}
catch (Exception e)
{
_logger.LogError(e, "commit error.");
if (i == 0)
throw;
}
i++;
}
AutoUseWriteConnectionString();
@ -231,9 +243,20 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
public async Task CommitAsync(CancellationToken cancellationToken = new CancellationToken())
{
int i = 0;
foreach (var dbContextCache in _dbContextCaches)
{
await dbContextCache.Value.CommitAsync(_dbContextCaches.Count, cancellationToken);
try
{
await dbContextCache.Value.CommitAsync(cancellationToken);
}
catch (Exception e)
{
_logger.LogError(e, "commit error.");
if (i == 0)
throw;
}
i++;
}
AutoUseWriteConnectionString();

View File

@ -3,9 +3,6 @@ using ShardingCore.Core.QueryRouteManagers;
using ShardingCore.Core.QueryRouteManagers.Abstractions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Parsers.Abstractions;
using ShardingCore.Sharding.ReadWriteConfigurations.Abstractions;
using ShardingCore.Sharding.ShardingExecutors;
using ShardingCore.Sharding.ShardingExecutors.Abstractions;
/*
* @Author: xjm
@ -13,7 +10,7 @@ using ShardingCore.Sharding.ShardingExecutors.Abstractions;
* @Date: DATE TIME
* @Email: 326308290@qq.com
*/
namespace ShardingCore.ShardingExecutors
namespace ShardingCore.Sharding.ShardingExecutors
{
internal class CustomerQueryScope:IDisposable
{

View File

@ -10,7 +10,6 @@ using ShardingCore.Extensions;
using ShardingCore.Sharding.Parsers.Abstractions;
using ShardingCore.Sharding.ShardingExecutors.Abstractions;
using ShardingCore.Sharding.Visitors.ShardingExtractParameters;
using ShardingCore.ShardingExecutors;
namespace ShardingCore.Sharding.ShardingExecutors
{

View File

@ -271,13 +271,12 @@ namespace ShardingCore
/// <summary>
/// 启用定时任务自动创建表
/// 当前接口可以直接移除掉,定时任务会在shardingcore初始化的时候自动调用
/// </summary>
/// <param name="serviceProvider"></param>
[Obsolete("can remove this method,sharding core auto invoke.")]
public static void UseAutoShardingCreate(this IServiceProvider serviceProvider)
{
var shardingRuntimeContext = serviceProvider.GetRequiredService<IShardingRuntimeContext>();
shardingRuntimeContext.UseAutoShardingCreate();
}
/// <summary>

View File

@ -81,6 +81,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions
/// <returns></returns>
public abstract bool AutoCreateTableByTime();
/// <summary>
/// 显示错误日志
/// </summary>
@ -140,6 +141,8 @@ namespace ShardingCore.VirtualRoutes.Abstractions
logger.LogInformation($"auto create table data source names:[{string.Join(",", dataSources)}]");
if (AutoCreateTableByTime())
{
foreach (var dataSource in dataSources)
{
try
@ -156,13 +159,14 @@ namespace ShardingCore.VirtualRoutes.Abstractions
logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error ");
}
}
}
return Task.CompletedTask;
}
public bool AppendJob()
{
return AutoCreateTableByTime();
return true;
}
}
}