From 7148d7c5b754ba53ea622c896b1f24440bf2626b Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Wed, 6 Jul 2022 10:39:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=AF=8F=E4=B8=AA=E5=88=86=E7=89=87=E4=B8=8A=E4=B8=8B?= =?UTF-8?q?=E6=96=87=E7=8B=AC=E7=AB=8B=E4=BD=BF=E7=94=A8=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E7=9A=84=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/Sample.AutoCreateIfPresent/Program.cs | 2 +- samples/Sample.SqlServer/Startup.cs | 2 +- .../Sample.SqlServerShardingTable/Startup.cs | 4 +- .../EntityMetadataInitializer.cs | 1 - .../IShardingRuntimeContext.cs | 5 +- .../RuntimeContexts/ShardingRuntimeContext.cs | 25 +++++---- .../IShardingRouteConfigOptions.cs | 33 ------------ .../ShardingConfigOptions.cs | 9 ++++ .../ShardingRouteConfigOptions.cs | 54 ------------------- .../Abstractions/ITableRouteManager.cs | 9 ++++ .../TableRoutes/TableRouteManager.cs | 10 +++- .../DefaultDataSourceInitializer.cs | 16 +++--- .../EFCores/ShardingInternalDbSet.cs | 2 +- .../EFCores/ShardingModelCustomizer.cs | 3 -- .../Extensions/ShardingRuntimeExtension.cs | 50 +++++++++++++++++ .../Extensions/VirtualDataBaseExtension.cs | 4 +- src/ShardingCore/Jobs/Abstaractions/IJob.cs | 1 + src/ShardingCore/Jobs/JobRunnerService.cs | 6 +-- .../DataSourceDbContext.cs | 7 +-- .../ShardingDbContextExecutor.cs | 12 +++-- .../DefaultShardingCompilerExecutor.cs | 5 +- .../DefaultShardingQueryExecutor.cs | 1 - .../QueryCompilerContextFactory.cs | 6 +-- .../Sharding/StreamMergeContext.cs | 18 +++---- .../Sharding/StreamMergeContextFactory.cs | 14 ++--- src/ShardingCore/ShardingCoreExtension.cs | 27 +--------- src/ShardingCore/ShardingRuntimeBuilder.cs | 8 ++- .../TableCreator/ShardingTableCreator.cs | 14 ++--- ...dingAutoCreateOperatorVirtualTableRoute.cs | 16 +++--- test/ShardingCore.Test/Startup.cs | 28 ++++++++-- test/ShardingCore.Test2x/Startup.cs | 4 +- 31 files changed, 191 insertions(+), 205 deletions(-) create mode 100644 src/ShardingCore/Extensions/ShardingRuntimeExtension.cs diff --git a/samples/Sample.AutoCreateIfPresent/Program.cs b/samples/Sample.AutoCreateIfPresent/Program.cs index e2e6634a..8c5e5706 100644 --- a/samples/Sample.AutoCreateIfPresent/Program.cs +++ b/samples/Sample.AutoCreateIfPresent/Program.cs @@ -21,12 +21,12 @@ builder.Services.AddControllers(); builder.Services.AddShardingDbContext() .AddEntityConfig(o => { - o.ThrowIfQueryRouteNotMatch = false; // o.AddShardingTableRoute(); // o.AddShardingTableRoute(); }) .AddConfig(o => { + o.ThrowIfQueryRouteNotMatch = false; o.AddDefaultDataSource("ds0", "server=127.0.0.1;port=3306;database=shardingTest;userid=root;password=root;"); o.UseShardingQuery((conn, b) => { diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index 577e415c..d1b3be40 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -35,13 +35,13 @@ namespace Sample.SqlServer services.AddShardingDbContext() .UseRouteConfig(o => { - o.ThrowIfQueryRouteNotMatch = false; o.AddShardingTableRoute(); o.AddShardingTableRoute(); o.AddShardingTableRoute(); }) .UseConfig((sp,op) => { + op.ThrowIfQueryRouteNotMatch = false; op.MaxQueryConnectionsLimit = 5; op.UseSqlServer(builder => { diff --git a/samples/Sample.SqlServerShardingTable/Startup.cs b/samples/Sample.SqlServerShardingTable/Startup.cs index 1659221b..f711d6da 100644 --- a/samples/Sample.SqlServerShardingTable/Startup.cs +++ b/samples/Sample.SqlServerShardingTable/Startup.cs @@ -73,13 +73,13 @@ namespace Sample.SqlServerShardingTable // },ReadStrategyEnum.Loop,defaultEnable:true).End(); services.AddShardingDbContext().AddEntityConfig(op => { - //当无法获取路由时会返回默认值而不是报错 - op.ThrowIfQueryRouteNotMatch = false; op.AddShardingTableRoute(); op.AddShardingTableRoute(); op.AddShardingTableRoute(); }).AddConfig(op => { + //当无法获取路由时会返回默认值而不是报错 + op.ThrowIfQueryRouteNotMatch = false; op.UseShardingQuery((conStr, builder) => { builder.UseSqlServer(conStr).UseLoggerFactory(efLogger); diff --git a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs index 5c854b11..0b53a60a 100644 --- a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs +++ b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs @@ -31,7 +31,6 @@ namespace ShardingCore.Bootstrappers /// public class EntityMetadataInitializer: IEntityMetadataInitializer where TEntity:class { - private static readonly ILogger> _logger=ShardingLoggerFactory.CreateLogger>(); private readonly Type _shardingEntityType; private readonly IShardingProvider _shardingProvider; private readonly IShardingRouteConfigOptions _shardingRouteConfigOptions; diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs index 5d14b312..4cdd7132 100644 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs @@ -7,6 +7,7 @@ using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryTrackers; using ShardingCore.Core.ServiceProviders; using ShardingCore.Core.ShardingConfigurations; +using ShardingCore.Core.ShardingConfigurations.Abstractions; using ShardingCore.Core.ShardingMigrations.Abstractions; using ShardingCore.Core.ShardingPage.Abstractions; using ShardingCore.Core.TrackerManagers; @@ -28,6 +29,7 @@ namespace ShardingCore.Core.RuntimeContexts { IShardingProvider GetShardingProvider(); ShardingConfigOptions GetShardingConfigOptions(); + IShardingRouteConfigOptions GetShardingRouteConfigOptions(); IShardingMigrationManager GetShardingMigrationManager(); IShardingComparer GetShardingComparer(); IShardingCompilerExecutor GetShardingCompilerExecutor(); @@ -53,9 +55,6 @@ namespace ShardingCore.Core.RuntimeContexts void GetOrCreateShardingRuntimeModel(DbContext dbContext); - void UseLogfactory(ILoggerFactory loggerFactory); - - void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider); void Initialize(); void AutoShardingCreate(); object GetService(Type serviceType); diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index 160fb131..ee3201e2 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -9,6 +9,7 @@ using ShardingCore.Core.QueryRouteManagers.Abstractions; using ShardingCore.Core.QueryTrackers; using ShardingCore.Core.ServiceProviders; using ShardingCore.Core.ShardingConfigurations; +using ShardingCore.Core.ShardingConfigurations.Abstractions; using ShardingCore.Core.ShardingMigrations.Abstractions; using ShardingCore.Core.ShardingPage.Abstractions; using ShardingCore.Core.TrackerManagers; @@ -38,7 +39,7 @@ namespace ShardingCore.Core.RuntimeContexts private IServiceCollection _serviceMap = new ServiceCollection(); private IServiceProvider _serviceProvider; - private IServiceProvider _applicationServiceProvider; + // private ILoggerFactory _applicationLoggerFactory; public void AddServiceConfig(Action configure) { @@ -80,6 +81,12 @@ namespace ShardingCore.Core.RuntimeContexts } + private IShardingRouteConfigOptions _shardingRouteConfigOptions; + public IShardingRouteConfigOptions GetShardingRouteConfigOptions() + { + return _shardingRouteConfigOptions??= GetRequiredService(); + } + private IShardingMigrationManager _shardingMigrationManager; public IShardingMigrationManager GetShardingMigrationManager() { @@ -247,16 +254,13 @@ namespace ShardingCore.Core.RuntimeContexts } } } + // + // public void UseLogfactory(ILoggerFactory loggerFactory) + // { + // // ShardingLoggerFactory.DefaultFactory = loggerFactory; + // _applicationLoggerFactory = loggerFactory; + // } - public void UseLogfactory(ILoggerFactory loggerFactory) - { - ShardingLoggerFactory.DefaultFactory = loggerFactory; - } - - public void UseApplicationServiceProvider(IServiceProvider applicationServiceProvider) - { - _applicationServiceProvider = applicationServiceProvider; - } private void CheckIfBuild() { @@ -312,6 +316,7 @@ namespace ShardingCore.Core.RuntimeContexts { GetShardingProvider(); GetShardingConfigOptions(); + GetShardingRouteConfigOptions(); GetShardingMigrationManager(); GetShardingComparer(); GetShardingCompilerExecutor(); diff --git a/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingRouteConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingRouteConfigOptions.cs index 042fb986..7644da17 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingRouteConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/Abstractions/IShardingRouteConfigOptions.cs @@ -10,39 +10,6 @@ namespace ShardingCore.Core.ShardingConfigurations.Abstractions { public interface IShardingRouteConfigOptions { - /// - /// 当查询遇到没有路由被命中时是否抛出错误 - /// - bool ThrowIfQueryRouteNotMatch { get; set; } - - // /// - // /// 如果数据库不存在就创建并且创建表除了分表的 - // /// - // bool EnsureCreatedWithOutShardingTable { get; set; } - // - // /// - // /// 是否需要在启动时创建分表 - // /// - // bool? CreateShardingTableOnStart { get; set; } - // /// - // /// 是否在启动时创建数据库 - // /// - // public bool? CreateDataBaseOnlyOnStart { get; set; } - /// - /// 忽略建表时的错误 - /// - bool? IgnoreCreateTableError { get; set; } - - // ///// - // ///// 是否启用分表路由编译缓存(默认只缓存单个操作的也就是,>=,<,<=]]>) - // ///// default cache single filter route expression, ,>=,<,<=]]> with sharding property - // ///// - // //bool? EnableTableRouteCompileCache { get; set; } - // ///// - // ///// 是否启用分库路由编译缓存(默认只缓存单个操作的也就是,>=,<,<=]]>) - // ///// default cache single filter route expression, ,>=,<,<=]]> with sharding property - // ///// - // //bool? EnableDataSourceRouteCompileCache { get; set; } /// /// 添加分库路由 /// diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs index 4c65f60f..ddfeff00 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingConfigOptions.cs @@ -9,6 +9,15 @@ namespace ShardingCore.Core.ShardingConfigurations { public class ShardingConfigOptions { + /// + /// 当查询遇到没有路由被命中时是否抛出错误 + /// + public bool ThrowIfQueryRouteNotMatch { get; set; } = true; + + /// + /// 忽略建表时的错误 + /// + public bool? IgnoreCreateTableError { get; set; } = false; /// /// 配置全局迁移最大并行数,以data source为一个单元并行迁移保证在多数据库分库情况下可以大大提高性能 /// diff --git a/src/ShardingCore/Core/ShardingConfigurations/ShardingRouteConfigOptions.cs b/src/ShardingCore/Core/ShardingConfigurations/ShardingRouteConfigOptions.cs index a6f7c7af..ce28a208 100644 --- a/src/ShardingCore/Core/ShardingConfigurations/ShardingRouteConfigOptions.cs +++ b/src/ShardingCore/Core/ShardingConfigurations/ShardingRouteConfigOptions.cs @@ -21,37 +21,6 @@ namespace ShardingCore.Core.ShardingConfigurations private readonly IDictionary _virtualTableRoutes = new Dictionary(); private readonly ISet _parallelTables = new HashSet(); - // /// - // /// 如果数据库不存在就创建并且创建表除了分表的 - // /// - // public bool EnsureCreatedWithOutShardingTable { get; set; } - // - // /// - // /// 是否需要在启动时创建分表 - // /// - // public bool? CreateShardingTableOnStart { get; set; } - // /// - // /// 是否在启动时创建数据库 - // /// - // public bool? CreateDataBaseOnlyOnStart { get; set; } - // /// - // /// 当查询遇到没有路由被命中时是否抛出错误 - // /// - // public bool ThrowIfQueryRouteNotMatch { get; set; } = true; - // ///// - // ///// 全局启用分表路由表达式缓存,仅缓存单个表达式 - // ///// - // //public bool? EnableTableRouteCompileCache { get; set; } - // ///// - // ///// 全局启用分库路由表达式缓存,仅缓存单个表达式 - // ///// - // //public bool? EnableDataSourceRouteCompileCache { get; set; } - /// - /// 忽略建表时的错误 - /// - public bool? IgnoreCreateTableError { get; set; } = false; - - public bool ThrowIfQueryRouteNotMatch { get; set; } = true; /// /// 添加分库路由 @@ -154,28 +123,5 @@ namespace ShardingCore.Core.ShardingConfigurations { return _parallelTables; } - - // /// - // /// 仅内部DbContext生效的配置委托 - // /// - // public Action ExecutorDbContextConfigure { get; private set; } - // public Action ShellDbContextConfigure { get; private set; } - // - // /// - // /// 仅内部真实DbContext配置的方法 - // /// - // /// - // /// - // public void UseExecutorDbContextConfigure(Action executorDbContextConfigure) - // { - // ExecutorDbContextConfigure = executorDbContextConfigure ?? throw new ArgumentNullException(nameof(executorDbContextConfigure)); - // } - // - // public void UseShellDbContextConfigure(Action shellDbContextConfigure) - // { - // ShellDbContextConfigure = shellDbContextConfigure ?? throw new ArgumentNullException(nameof(shellDbContextConfigure)); - // } - - } } diff --git a/src/ShardingCore/Core/VirtualRoutes/Abstractions/ITableRouteManager.cs b/src/ShardingCore/Core/VirtualRoutes/Abstractions/ITableRouteManager.cs index 788d2c86..1bf67532 100644 --- a/src/ShardingCore/Core/VirtualRoutes/Abstractions/ITableRouteManager.cs +++ b/src/ShardingCore/Core/VirtualRoutes/Abstractions/ITableRouteManager.cs @@ -44,6 +44,15 @@ namespace ShardingCore.Core.VirtualRoutes.Abstractions List RouteTo(Type entityType, ShardingTableRouteConfig shardingTableRouteConfig); /// + /// 直接路由采用默认数据源 + /// + /// + /// + /// + /// + List RouteTo(Type entityType, string dataSourceName, + ShardingTableRouteConfig shardingTableRouteConfig); + /// /// 根据数据源路由进行分片路由 /// /// diff --git a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/TableRouteManager.cs b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/TableRouteManager.cs index 2afb9e86..f1e57254 100644 --- a/src/ShardingCore/Core/VirtualRoutes/TableRoutes/TableRouteManager.cs +++ b/src/ShardingCore/Core/VirtualRoutes/TableRoutes/TableRouteManager.cs @@ -18,11 +18,13 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes public class TableRouteManager : ITableRouteManager { private readonly IVirtualDataSource _virtualDataSource; + private readonly IDataSourceRouteManager _dataSourceRouteManager; private readonly ConcurrentDictionary _tableRoutes = new(); - public TableRouteManager(IVirtualDataSource virtualDataSource) + public TableRouteManager(IVirtualDataSource virtualDataSource,IDataSourceRouteManager dataSourceRouteManager) { _virtualDataSource = virtualDataSource; + _dataSourceRouteManager = dataSourceRouteManager; } public bool HasRoute(Type entityType) { @@ -53,7 +55,11 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes public List RouteTo(Type entityType, ShardingTableRouteConfig shardingTableRouteConfig) { - + return RouteTo(entityType, _virtualDataSource.DefaultDataSourceName, shardingTableRouteConfig); + } + + public List RouteTo(Type entityType, string dataSourceName, ShardingTableRouteConfig shardingTableRouteConfig) + { var dataSourceRouteResult = new DataSourceRouteResult(_virtualDataSource.DefaultDataSourceName); return RouteTo(entityType, dataSourceRouteResult, shardingTableRouteConfig); } diff --git a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs index b7c0469f..59b8603c 100644 --- a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs +++ b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Threading; using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.ServiceProviders; +using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Core.VirtualRoutes.Abstractions; using ShardingCore.Core.VirtualRoutes.TableRoutes; using ShardingCore.Exceptions; @@ -23,12 +24,11 @@ namespace ShardingCore.DynamicDataSources { public class DataSourceInitializer : IDataSourceInitializer { - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); + private readonly ILogger _logger ; private readonly IShardingProvider _shardingProvider; private readonly IDbContextCreator _dbContextCreator; - private readonly IShardingRouteConfigOptions _routeConfigOptions; + private readonly ShardingConfigOptions _shardingConfigOptions; private readonly IVirtualDataSource _virtualDataSource; private readonly IRouteTailFactory _routeTailFactory; private readonly IDataSourceRouteManager _dataSourceRouteManager; @@ -40,18 +40,19 @@ namespace ShardingCore.DynamicDataSources public DataSourceInitializer( IShardingProvider shardingProvider, IDbContextCreator dbContextCreator, - IShardingRouteConfigOptions routeConfigOptions, + ShardingConfigOptions shardingConfigOptions, IVirtualDataSource virtualDataSource, IRouteTailFactory routeTailFactory, IDataSourceRouteManager dataSourceRouteManager, ITableRouteManager tableRouteManager, IEntityMetadataManager entityMetadataManager, IShardingTableCreator shardingTableCreator, - ITableEnsureManager tableEnsureManager) + ITableEnsureManager tableEnsureManager, + ILogger logger ) { _shardingProvider = shardingProvider; _dbContextCreator = dbContextCreator; - _routeConfigOptions = routeConfigOptions; + _shardingConfigOptions = shardingConfigOptions; _virtualDataSource = virtualDataSource; _routeTailFactory = routeTailFactory; _dataSourceRouteManager = dataSourceRouteManager; @@ -59,6 +60,7 @@ namespace ShardingCore.DynamicDataSources _entityMetadataManager = entityMetadataManager; _tableCreator = shardingTableCreator; _tableEnsureManager = tableEnsureManager; + _logger = logger; } public void InitConfigure(string dataSourceName,bool createDatabase,bool createTable) @@ -150,7 +152,7 @@ namespace ShardingCore.DynamicDataSources } catch (Exception e) { - if (!_routeConfigOptions.IgnoreCreateTableError.GetValueOrDefault()) + if (!_shardingConfigOptions.IgnoreCreateTableError.GetValueOrDefault()) { _logger.LogWarning(e, $"table :{physicTableName} will created."); diff --git a/src/ShardingCore/EFCores/ShardingInternalDbSet.cs b/src/ShardingCore/EFCores/ShardingInternalDbSet.cs index e80ff4ed..6406b95a 100644 --- a/src/ShardingCore/EFCores/ShardingInternalDbSet.cs +++ b/src/ShardingCore/EFCores/ShardingInternalDbSet.cs @@ -413,7 +413,7 @@ namespace ShardingCore.EFCores if (primaryKeyValue != null) { var dataSourceName = GetDataSourceName(primaryKeyValue); - var tableTail = TableRouteManager.GetTableTail(primaryKeyValue); + var tableTail = TableRouteManager.GetTableTail(dataSourceName,primaryKeyValue); var routeTail = _shardingRuntimeContext.GetRouteTailFactory().Create(tableTail); ; return _context.GetShareDbContext(dataSourceName, routeTail); diff --git a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs index b916d75c..efe60b25 100644 --- a/src/ShardingCore/EFCores/ShardingModelCustomizer.cs +++ b/src/ShardingCore/EFCores/ShardingModelCustomizer.cs @@ -27,8 +27,6 @@ namespace ShardingCore.EFCores */ public class ShardingModelCustomizer : ModelCustomizer { - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); public ShardingModelCustomizer(ModelCustomizerDependencies dependencies) : base(dependencies) { @@ -83,7 +81,6 @@ namespace ShardingCore.EFCores var tableName = entityMetadata.LogicTableName; if (string.IsNullOrWhiteSpace(tableName)) throw new ArgumentNullException($"{shardingEntity}: not found logic table name。"); - _logger.LogDebug($"mapping table :[tableName]-->[{tableName}{tableSeparator}{tail}]"); entity.ToTable($"{tableName}{tableSeparator}{tail}"); } } diff --git a/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs new file mode 100644 index 00000000..23b61852 --- /dev/null +++ b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using ShardingCore.Core.RuntimeContexts; +using ShardingCore.DynamicDataSources; +using ShardingCore.Exceptions; + +namespace ShardingCore.Extensions +{ + + public static class ShardingRuntimeExtension + { + + /// + /// 自动尝试补偿表 + /// + /// + /// + public static void UseAutoTryCompensateTable(this IShardingRuntimeContext shardingRuntimeContext, int? parallelCount = null) + { + shardingRuntimeContext.CheckRequirement(); + var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); + var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer(); + var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); + var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount; + if (compensateTableParallelCount <= 0) + { + throw new ShardingCoreInvalidOperationException($"compensate table parallel count must >0"); + } + var allDataSourceNames = virtualDataSource.GetAllDataSourceNames(); + var partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount); + foreach (var migrationUnits in partitionMigrationUnits) + { + var migrateUnits = migrationUnits.Select(o => new InitConfigureUnit(o)).ToList(); + ExecuteInitConfigureUnit(dataSourceInitializer, migrateUnits); + } + } + + private static void ExecuteInitConfigureUnit(IDataSourceInitializer dataSourceInitializer, + List initConfigureUnits) + { + var initConfigureTasks = initConfigureUnits.Select(o => + { + return Task.Run(() => { dataSourceInitializer.InitConfigure(o.DataSourceName, true, true); }); + }).ToArray(); + Task.WaitAll(initConfigureTasks); + } + + } +} \ No newline at end of file diff --git a/src/ShardingCore/Extensions/VirtualDataBaseExtension.cs b/src/ShardingCore/Extensions/VirtualDataBaseExtension.cs index 02da9dc5..c8960dd3 100644 --- a/src/ShardingCore/Extensions/VirtualDataBaseExtension.cs +++ b/src/ShardingCore/Extensions/VirtualDataBaseExtension.cs @@ -67,13 +67,13 @@ namespace ShardingCore.Extensions - public static string GetTableTail(this ITableRouteManager tableRouteManager, + public static string GetTableTail(this ITableRouteManager tableRouteManager,string dataSourceName, TEntity entity) where TEntity : class { var shardingRouteUnit = tableRouteManager.RouteTo(entity.GetType(),new ShardingTableRouteConfig(shardingTable: entity))[0]; return shardingRouteUnit.Tail; } - public static string GetTableTail(this ITableRouteManager tableRouteManager, + public static string GetTableTail(this ITableRouteManager tableRouteManager,string dataSourceName, object shardingKeyValue) where TEntity : class { var shardingRouteUnit = tableRouteManager.RouteTo(typeof(TEntity),new ShardingTableRouteConfig(shardingKeyValue: shardingKeyValue))[0]; diff --git a/src/ShardingCore/Jobs/Abstaractions/IJob.cs b/src/ShardingCore/Jobs/Abstaractions/IJob.cs index 4c77e56d..d194bae3 100644 --- a/src/ShardingCore/Jobs/Abstaractions/IJob.cs +++ b/src/ShardingCore/Jobs/Abstaractions/IJob.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace ShardingCore.Jobs.Abstaractions { diff --git a/src/ShardingCore/Jobs/JobRunnerService.cs b/src/ShardingCore/Jobs/JobRunnerService.cs index 3dbc5baa..8b37fdae 100644 --- a/src/ShardingCore/Jobs/JobRunnerService.cs +++ b/src/ShardingCore/Jobs/JobRunnerService.cs @@ -19,8 +19,7 @@ namespace ShardingCore.Jobs [ExcludeFromCodeCoverage] internal class JobRunnerService { - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); + private readonly ILogger _logger; private readonly IJobManager _jobManager; private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private const long DEFAULT_MILLIS = 1000L; @@ -30,9 +29,10 @@ namespace ShardingCore.Jobs /// private const long MAX_DELAY_MILLIS = 30000L; - public JobRunnerService(IJobManager jobManager) + public JobRunnerService(IJobManager jobManager,ILogger logger) { _jobManager = jobManager; + _logger = logger; } public async Task StartAsync() diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs index f6b61dfc..dc52093e 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs @@ -32,11 +32,10 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors */ public class DataSourceDbContext : IDataSourceDbContext { - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); private static readonly IComparer _comparer = new NoShardingFirstComparer(); + private readonly ILogger _logger; public Type DbContextType { get; } /// /// 当前是否是默认的dbcontext 也就是不分片的dbcontext @@ -118,7 +117,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors bool isDefault, DbContext shardingShellDbContext, IDbContextCreator dbContextCreator, - ActualConnectionStringManager actualConnectionStringManager) + ActualConnectionStringManager actualConnectionStringManager, + ILogger logger) { var shardingDbContext = (IShardingDbContext)shardingShellDbContext; DataSourceName = dataSourceName; @@ -130,6 +130,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors .GetVirtualDataSource(); _dbContextCreator = dbContextCreator; _actualConnectionStringManager = actualConnectionStringManager; + this._logger = logger; } /// diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs index ecf19b48..7452329b 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using ShardingCore.Core; using ShardingCore.Core.EntityMetadatas; using ShardingCore.Core.VirtualDatabase.VirtualDataSources; @@ -31,6 +32,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors /// public class ShardingDbContextExecutor : IShardingDbContextExecutor { + private readonly ILoggerFactory _loggerFactory; private readonly DbContext _shardingDbContext; //private readonly ConcurrentDictionary> _dbContextCaches = new ConcurrentDictionary>(); @@ -71,6 +73,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors _entityMetadataManager = _shardingRuntimeContext.GetEntityMetadataManager(); _routeTailFactory = _shardingRuntimeContext.GetRouteTailFactory(); var shardingReadWriteManager = _shardingRuntimeContext.GetShardingReadWriteManager(); + var shardingProvider = _shardingRuntimeContext.GetShardingProvider(); + _loggerFactory=shardingProvider.GetService(); _actualConnectionStringManager = new ActualConnectionStringManager(shardingReadWriteManager,_virtualDataSource); } @@ -78,7 +82,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors private IDataSourceDbContext GetDataSourceDbContext(string dataSourceName) { - return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _dbContextCreator, _actualConnectionStringManager)); + return _dbContextCaches.GetOrAdd(dataSourceName, dsname => new DataSourceDbContext(dsname, _virtualDataSource.IsDefault(dsname), _shardingDbContext, _dbContextCreator, _actualConnectionStringManager,_loggerFactory.CreateLogger())); } /// @@ -116,7 +120,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors public DbContext CreateGenericDbContext(TEntity entity) where TEntity : class { var dataSourceName = GetDataSourceName(entity); - var tail = GetTableTail(entity); + var tail = GetTableTail(dataSourceName,entity); return CreateDbContext(CreateDbContextStrategyEnum.ShareConnection, dataSourceName, _routeTailFactory.Create(tail)); } @@ -131,11 +135,11 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors return _dataSourceRouteManager.GetDataSourceName(entity); } - private string GetTableTail(TEntity entity) where TEntity : class + private string GetTableTail(string dataSourceName,TEntity entity) where TEntity : class { if (!_entityMetadataManager.IsShardingTable(entity.GetType())) return string.Empty; - return _tableRouteManager.GetTableTail(entity); + return _tableRouteManager.GetTableTail(dataSourceName,entity); } #endregion diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs index 2a7cfc92..c4d34be0 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs @@ -19,7 +19,7 @@ namespace ShardingCore.Sharding.ShardingExecutors /// public class DefaultShardingCompilerExecutor: IShardingCompilerExecutor { - private static readonly ILogger _logger=ShardingLoggerFactory.CreateLogger(); + private readonly ILogger _logger; private readonly IShardingTrackQueryExecutor _shardingTrackQueryExecutor; private readonly IQueryCompilerContextFactory _queryCompilerContextFactory; private readonly IPrepareParser _prepareParser; @@ -27,12 +27,13 @@ namespace ShardingCore.Sharding.ShardingExecutors public DefaultShardingCompilerExecutor( IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory,IPrepareParser prepareParser, - IShardingRouteManager shardingRouteManager) + IShardingRouteManager shardingRouteManager,ILogger logger) { _shardingTrackQueryExecutor = shardingTrackQueryExecutor; _queryCompilerContextFactory = queryCompilerContextFactory; _prepareParser = prepareParser; _shardingRouteManager = shardingRouteManager; + _logger = logger; } public TResult Execute(IShardingDbContext shardingDbContext, Expression query) { diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs index 932c92c2..f9ade623 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs @@ -31,7 +31,6 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors */ public class DefaultShardingQueryExecutor : IShardingQueryExecutor { - private static readonly ILogger _logger=ShardingLoggerFactory.CreateLogger(); private readonly IStreamMergeContextFactory _streamMergeContextFactory; diff --git a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs index fa4acc80..808e8586 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/QueryCompilerContextFactory.cs @@ -21,8 +21,7 @@ namespace ShardingCore.Sharding.ShardingExecutors private readonly IDataSourceRouteRuleEngineFactory _dataSourceRouteRuleEngineFactory; private readonly ITableRouteRuleEngineFactory _tableRouteRuleEngineFactory; - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); + private readonly ILogger _logger; private static readonly IQueryableCombine _enumerableQueryableCombine; private static readonly IQueryableCombine _allQueryableCombine; private static readonly IQueryableCombine _constantQueryableCombine; @@ -38,10 +37,11 @@ namespace ShardingCore.Sharding.ShardingExecutors _whereQueryableCombine = new WhereQueryableCombine(); } - public QueryCompilerContextFactory(IDataSourceRouteRuleEngineFactory dataSourceRouteRuleEngineFactory,ITableRouteRuleEngineFactory tableRouteRuleEngineFactory) + public QueryCompilerContextFactory(IDataSourceRouteRuleEngineFactory dataSourceRouteRuleEngineFactory,ITableRouteRuleEngineFactory tableRouteRuleEngineFactory,ILogger logger) { _dataSourceRouteRuleEngineFactory = dataSourceRouteRuleEngineFactory; _tableRouteRuleEngineFactory = tableRouteRuleEngineFactory; + _logger = logger; } public IQueryCompilerContext Create(IPrepareParseResult prepareParseResult) diff --git a/src/ShardingCore/Sharding/StreamMergeContext.cs b/src/ShardingCore/Sharding/StreamMergeContext.cs index 8b5e61ea..6ce7055c 100644 --- a/src/ShardingCore/Sharding/StreamMergeContext.cs +++ b/src/ShardingCore/Sharding/StreamMergeContext.cs @@ -20,6 +20,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore.Infrastructure; using ShardingCore.Core.RuntimeContexts; +using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Core.VirtualRoutes; using ShardingCore.Sharding.MergeEngines.Abstractions; using ShardingCore.Sharding.MergeEngines.Common.Abstractions; @@ -72,7 +73,7 @@ namespace ShardingCore.Sharding public bool IsCrossTable => MergeQueryCompilerContext.IsCrossTable(); private readonly ITrackerManager _trackerManager; - private readonly IShardingRouteConfigOptions _shardingRouteConfigOptions; + private readonly ShardingConfigOptions _shardingConfigOptions; private readonly ConcurrentDictionary _parallelDbContexts; @@ -85,21 +86,18 @@ namespace ShardingCore.Sharding public StreamMergeContext(IMergeQueryCompilerContext mergeQueryCompilerContext, IParseResult parseResult, - IRewriteResult rewriteResult, IOptimizeResult optimizeResult, - IRouteTailFactory routeTailFactory, ITrackerManager trackerManager, - IShardingRouteConfigOptions shardingRouteConfigOptions) + IRewriteResult rewriteResult, IOptimizeResult optimizeResult) { MergeQueryCompilerContext = mergeQueryCompilerContext; - ShardingRuntimeContext = ((DbContext)mergeQueryCompilerContext.GetShardingDbContext()) - .GetRequireService(); ParseResult = parseResult; RewriteQueryable = rewriteResult.GetRewriteQueryable(); OptimizeResult = optimizeResult; _rewriteResult = rewriteResult; - _routeTailFactory = routeTailFactory; + ShardingRuntimeContext = mergeQueryCompilerContext.GetShardingDbContext().GetShardingRuntimeContext(); + _routeTailFactory = ShardingRuntimeContext.GetRouteTailFactory(); + _trackerManager = ShardingRuntimeContext.GetTrackerManager(); + _shardingConfigOptions = ShardingRuntimeContext.GetShardingConfigOptions(); QueryEntities = MergeQueryCompilerContext.GetQueryEntities().Keys.ToHashSet(); - _trackerManager = trackerManager; - _shardingRouteConfigOptions = shardingRouteConfigOptions; _parallelDbContexts = new ConcurrentDictionary(); Orders = parseResult.GetOrderByContext().PropertyOrders.ToArray(); Skip = parseResult.GetPaginationContext().Skip; @@ -349,7 +347,7 @@ namespace ShardingCore.Sharding private bool ThrowIfQueryRouteNotMatch() { - return _shardingRouteConfigOptions.ThrowIfQueryRouteNotMatch; + return _shardingConfigOptions.ThrowIfQueryRouteNotMatch; } public bool UseUnionAllMerge() diff --git a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs index f5eca5d3..123dd712 100644 --- a/src/ShardingCore/Sharding/StreamMergeContextFactory.cs +++ b/src/ShardingCore/Sharding/StreamMergeContextFactory.cs @@ -22,23 +22,17 @@ namespace ShardingCore.Sharding */ public class StreamMergeContextFactory : IStreamMergeContextFactory { - private readonly IRouteTailFactory _routeTailFactory; private readonly IQueryableParseEngine _queryableParseEngine; private readonly IQueryableRewriteEngine _queryableRewriteEngine; private readonly IQueryableOptimizeEngine _queryableOptimizeEngine; - private readonly ITrackerManager _trackerManager; - private readonly IShardingRouteConfigOptions _shardingRouteConfigOptions; - public StreamMergeContextFactory(IRouteTailFactory routeTailFactory - , IQueryableParseEngine queryableParseEngine, IQueryableRewriteEngine queryableRewriteEngine, IQueryableOptimizeEngine queryableOptimizeEngine, - ITrackerManager trackerManager,IShardingRouteConfigOptions shardingRouteConfigOptions) + public StreamMergeContextFactory(IQueryableParseEngine queryableParseEngine, + IQueryableRewriteEngine queryableRewriteEngine, + IQueryableOptimizeEngine queryableOptimizeEngine) { - _routeTailFactory = routeTailFactory; _queryableParseEngine = queryableParseEngine; _queryableRewriteEngine = queryableRewriteEngine; _queryableOptimizeEngine = queryableOptimizeEngine; - _trackerManager = trackerManager; - _shardingRouteConfigOptions = shardingRouteConfigOptions; } public StreamMergeContext Create(IMergeQueryCompilerContext mergeQueryCompilerContext) { @@ -47,7 +41,7 @@ namespace ShardingCore.Sharding var rewriteResult = _queryableRewriteEngine.GetRewriteQueryable(mergeQueryCompilerContext, parseResult); var optimizeResult = _queryableOptimizeEngine.Optimize(mergeQueryCompilerContext, parseResult, rewriteResult); CheckMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult, optimizeResult); - return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult,optimizeResult, _routeTailFactory,_trackerManager,_shardingRouteConfigOptions); + return new StreamMergeContext(mergeQueryCompilerContext, parseResult, rewriteResult,optimizeResult); } private void CheckMergeContext(IMergeQueryCompilerContext mergeQueryCompilerContext,IParseResult parseResult,IRewriteResult rewriteResult,IOptimizeResult optimizeResult) diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index 077711a6..e3fb953a 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -252,32 +252,7 @@ namespace ShardingCore public static void UseAutoTryCompensateTable(this IServiceProvider serviceProvider, int? parallelCount = null) { var shardingRuntimeContext = serviceProvider.GetRequiredService(); - shardingRuntimeContext.CheckRequirement(); - var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); - var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer(); - var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); - var compensateTableParallelCount = parallelCount ?? shardingConfigOptions.CompensateTableParallelCount; - if (compensateTableParallelCount <= 0) - { - throw new ShardingCoreInvalidOperationException($"compensate table parallel count must >0"); - } - var allDataSourceNames = virtualDataSource.GetAllDataSourceNames(); - var partitionMigrationUnits = allDataSourceNames.Partition(compensateTableParallelCount); - foreach (var migrationUnits in partitionMigrationUnits) - { - var migrateUnits = migrationUnits.Select(o => new InitConfigureUnit(o)).ToList(); - ExecuteInitConfigureUnit(dataSourceInitializer, migrateUnits); - } - } - - private static void ExecuteInitConfigureUnit(IDataSourceInitializer dataSourceInitializer, - List initConfigureUnits) - { - var initConfigureTasks = initConfigureUnits.Select(o => - { - return Task.Run(() => { dataSourceInitializer.InitConfigure(o.DataSourceName, true, true); }); - }).ToArray(); - Task.WaitAll(initConfigureTasks); + shardingRuntimeContext.UseAutoTryCompensateTable(parallelCount); } diff --git a/src/ShardingCore/ShardingRuntimeBuilder.cs b/src/ShardingCore/ShardingRuntimeBuilder.cs index bf805801..ec8c7b7b 100644 --- a/src/ShardingCore/ShardingRuntimeBuilder.cs +++ b/src/ShardingCore/ShardingRuntimeBuilder.cs @@ -73,8 +73,6 @@ namespace ShardingCore public IShardingRuntimeContext Build(IServiceProvider appServiceProvider, ILoggerFactory loggerFactory) { var shardingRuntimeContext = new ShardingRuntimeContext(); - shardingRuntimeContext.UseApplicationServiceProvider(appServiceProvider); - shardingRuntimeContext.UseLogfactory(loggerFactory); shardingRuntimeContext.AddServiceConfig(services => { // services.AddSingleton(sp => new DbContextTypeCollector()); @@ -94,6 +92,12 @@ namespace ShardingCore shardingConfigOptions.CheckArguments(); return shardingConfigOptions; }); + services.AddLogging(); + if (loggerFactory != null) + { + services.Replace(ServiceDescriptor.Singleton(sp => loggerFactory)); + } + services.AddSingleton(sp => new ShardingProvider(sp,appServiceProvider)); services.AddInternalShardingCore(); foreach (var serviceAction in _serviceActions) diff --git a/src/ShardingCore/TableCreator/ShardingTableCreator.cs b/src/ShardingCore/TableCreator/ShardingTableCreator.cs index f588ba79..2ac23676 100644 --- a/src/ShardingCore/TableCreator/ShardingTableCreator.cs +++ b/src/ShardingCore/TableCreator/ShardingTableCreator.cs @@ -12,6 +12,7 @@ using System; using System.Threading; using ShardingCore.Core.DbContextCreator; using ShardingCore.Core.ServiceProviders; +using ShardingCore.Core.ShardingConfigurations; using ShardingCore.Logger; using ShardingCore.Sharding; @@ -25,20 +26,21 @@ namespace ShardingCore.TableCreator */ public class ShardingTableCreator : IShardingTableCreator { - private static readonly ILogger _logger = - ShardingLoggerFactory.CreateLogger(); + private readonly ILogger _logger; private readonly IShardingProvider _shardingProvider; - private readonly IShardingRouteConfigOptions _routeConfigOptions; + private readonly ShardingConfigOptions _shardingConfigOptions; private readonly IRouteTailFactory _routeTailFactory; private readonly IDbContextCreator _dbContextCreator; - public ShardingTableCreator(IShardingProvider shardingProvider,IShardingRouteConfigOptions routeConfigOptions, IRouteTailFactory routeTailFactory,IDbContextCreator dbContextCreator) + public ShardingTableCreator(IShardingProvider shardingProvider,ShardingConfigOptions shardingConfigOptions, IRouteTailFactory routeTailFactory,IDbContextCreator dbContextCreator, + ILogger logger) { _shardingProvider = shardingProvider; - _routeConfigOptions = routeConfigOptions; + _shardingConfigOptions = shardingConfigOptions; _routeTailFactory = routeTailFactory; _dbContextCreator = dbContextCreator; + _logger = logger; } public void CreateTable(string dataSourceName, string tail) @@ -72,7 +74,7 @@ namespace ShardingCore.TableCreator } catch (Exception ex) { - if (!_routeConfigOptions.IgnoreCreateTableError.GetValueOrDefault()) + if (!_shardingConfigOptions.IgnoreCreateTableError.GetValueOrDefault()) { _logger.LogWarning(ex, $"create table error entity name:[{shardingEntityType.Name}]."); diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index 04c512cb..959c11e6 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -29,8 +29,6 @@ namespace ShardingCore.VirtualRoutes.Abstractions { private static readonly object APPEND_LOCK = new object(); - private static readonly ILogger> _logger = - ShardingLoggerFactory.CreateLogger>(); private readonly SafeReadAppendList _tails = new SafeReadAppendList(); @@ -103,7 +101,9 @@ namespace ShardingCore.VirtualRoutes.Abstractions public virtual Task ExecuteAsync() { - _logger.LogDebug($"get {typeof(TEntity).Name}'s route execute job "); + var logger=RouteShardingProvider + .GetService>>(); + logger.LogDebug($"get {typeof(TEntity).Name}'s route execute job "); var entityMetadataManager = RouteShardingProvider.GetRequiredService(); var tableCreator = RouteShardingProvider.GetRequiredService(); @@ -127,22 +127,22 @@ namespace ShardingCore.VirtualRoutes.Abstractions dataSources.Add(virtualDataSource.DefaultDataSourceName); } - _logger.LogInformation($"auto create table data source names:[{string.Join(",", dataSources)}]"); + logger.LogInformation($"auto create table data source names:[{string.Join(",", dataSources)}]"); foreach (var dataSource in dataSources) { try { - _logger.LogInformation($"begin table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); + logger.LogInformation($"begin table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); tableCreator.CreateTable(dataSource, typeof(TEntity), tail); - _logger.LogInformation($"succeed table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); + logger.LogInformation($"succeed table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); } catch (Exception e) { //ignore - _logger.LogInformation($"warning table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); + logger.LogInformation($"warning table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); if (DoLogError) - _logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error "); + logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error "); } } diff --git a/test/ShardingCore.Test/Startup.cs b/test/ShardingCore.Test/Startup.cs index 491c60bf..39b18c36 100644 --- a/test/ShardingCore.Test/Startup.cs +++ b/test/ShardingCore.Test/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using ShardingCore.Bootstrappers; +using ShardingCore.Core; using ShardingCore.Helpers; using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.TableExists; @@ -38,9 +39,6 @@ namespace ShardingCore.Test services.AddShardingDbContext() .UseRouteConfig(op => { - //当无法获取路由时会返回默认值而不是报错 - op.ThrowIfQueryRouteNotMatch = false; - op.AddShardingDataSourceRoute(); op.AddShardingTableRoute(); op.AddShardingTableRoute(); @@ -58,17 +56,32 @@ namespace ShardingCore.Test }) .UseConfig(op => { + //当无法获取路由时会返回默认值而不是报错 + op.ThrowIfQueryRouteNotMatch = false; + //忽略建表错误compensate table和table creator + op.IgnoreCreateTableError = true; + //迁移时使用的并行线程数(分库有效)defaultShardingDbContext.Database.Migrate() + op.MigrationParallelCount = Environment.ProcessorCount; + //补偿表创建并行线程数 调用UseAutoTryCompensateTable有效 + op.CompensateTableParallelCount = Environment.ProcessorCount; + //最大连接数限制 + op.MaxQueryConnectionsLimit = Environment.ProcessorCount; + //链接模式系统默认 + op.ConnectionMode = ConnectionModeEnum.SYSTEM_AUTO; + //如何通过字符串查询创建DbContext op.UseShardingQuery((conStr, builder) => { builder.UseSqlServer(conStr).UseLoggerFactory(efLogger); }); + //如何通过事务创建DbContext op.UseShardingTransaction((connection, builder) => { builder.UseSqlServer(connection).UseLoggerFactory(efLogger); }); - + //添加默认数据源 op.AddDefaultDataSource("A", "Data Source=localhost;Initial Catalog=ShardingCoreDBA;Integrated Security=True;"); + //添加额外数据源 op.AddExtraDataSource(sp => { return new Dictionary() @@ -77,6 +90,7 @@ namespace ShardingCore.Test { "C", "Data Source=localhost;Initial Catalog=ShardingCoreDBC;Integrated Security=True;" }, }; }); + //添加读写分离 op.AddReadWriteSeparation(sp => { return new Dictionary>() @@ -89,13 +103,17 @@ namespace ShardingCore.Test } }; }, ReadStrategyEnum.Loop, defaultEnable: false, readConnStringGetStrategy: ReadConnStringGetStrategyEnum.LatestEveryTime); - }).ReplaceService(ServiceLifetime.Singleton).AddShardingCore(); + }) + .ReplaceService() + .AddShardingCore(); } // 可以添加要用到的方法参数,会自动从注册的服务中获取服务实例,类似于 asp.net core 里 Configure 方法 public void Configure(IServiceProvider serviceProvider) { + //启动ShardingCore创建表任务 serviceProvider.UseAutoShardingCreate(); + //启动进行表补偿 serviceProvider.UseAutoTryCompensateTable(); // 有一些测试数据要初始化可以放在这里 InitData(serviceProvider).GetAwaiter().GetResult(); diff --git a/test/ShardingCore.Test2x/Startup.cs b/test/ShardingCore.Test2x/Startup.cs index c4400907..338043a8 100644 --- a/test/ShardingCore.Test2x/Startup.cs +++ b/test/ShardingCore.Test2x/Startup.cs @@ -38,8 +38,6 @@ namespace ShardingCore.Test2x services.AddShardingDbContext() .UseRouteConfig(op => { - //当无法获取路由时会返回默认值而不是报错 - op.ThrowIfQueryRouteNotMatch = false; op.AddShardingDataSourceRoute(); op.AddShardingTableRoute(); op.AddShardingTableRoute(); @@ -58,6 +56,8 @@ namespace ShardingCore.Test2x .UseConfig(op => { + //当无法获取路由时会返回默认值而不是报错 + op.ThrowIfQueryRouteNotMatch = false; op.UseShardingQuery((conStr, builder) => { builder.UseSqlServer(conStr).UseLoggerFactory(efLogger);