From 5eebc2aba249203c689384fe6c10fa6f80a8c2f2 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Wed, 19 Oct 2022 23:13:20 +0800 Subject: [PATCH] [#208] --- samples/Sample.MySql/Startup.cs | 1 - samples/Sample.ShardingConsole/Program.cs | 1 - .../Bootstrappers/IShardingBootstrapper.cs | 2 +- .../Bootstrappers/ShardingBootstrapper.cs | 30 +-------------- .../ActivatorDbContextCreator.cs | 12 +++++- .../IShardingRuntimeContext.cs | 3 -- .../RuntimeContexts/ShardingRuntimeContext.cs | 38 ++----------------- .../Extensions/ShardingRuntimeExtension.cs | 7 +--- .../ParallelControl/DoOnlyOnce.cs | 2 +- .../ShardingExecutors/CustomerQueryScope.cs | 5 +-- .../DefaultShardingCompilerExecutor.cs | 1 - src/ShardingCore/ShardingCoreExtension.cs | 5 +-- ...dingAutoCreateOperatorVirtualTableRoute.cs | 32 +++++++++------- 13 files changed, 40 insertions(+), 99 deletions(-) diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index c60a7b3b..ea292331 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -179,7 +179,6 @@ namespace Sample.MySql { app.UseDeveloperExceptionPage(); } - app.ApplicationServices.UseAutoShardingCreate(); // app.ApplicationServices.UseAutoTryCompensateTable(); // app.ApplicationServices.UseAutoShardingCreate(); diff --git a/samples/Sample.ShardingConsole/Program.cs b/samples/Sample.ShardingConsole/Program.cs index e31aa330..8ebf8ef6 100644 --- a/samples/Sample.ShardingConsole/Program.cs +++ b/samples/Sample.ShardingConsole/Program.cs @@ -5,7 +5,6 @@ using Sample.ShardingConsole; using ShardingCore; using ShardingCore.Extensions; -ShardingProvider.ShardingRuntimeContext.UseAutoShardingCreate(); ShardingProvider.ShardingRuntimeContext.UseAutoTryCompensateTable(); var dbContextOptionsBuilder = new DbContextOptionsBuilder(); diff --git a/src/ShardingCore/Bootstrappers/IShardingBootstrapper.cs b/src/ShardingCore/Bootstrappers/IShardingBootstrapper.cs index 886cbb39..d27b2e78 100644 --- a/src/ShardingCore/Bootstrappers/IShardingBootstrapper.cs +++ b/src/ShardingCore/Bootstrappers/IShardingBootstrapper.cs @@ -10,7 +10,7 @@ namespace ShardingCore.Bootstrappers /// 主要的分片初始化器,需要手动调用,如果你的分片路由存在定时执行的job譬如 /// 系统默认的时间分片的情况下那么需要调用IShardingRuntimeContext初始化的时候会调用 /// - public interface IShardingBootstrapper + internal interface IShardingBootstrapper { void AutoShardingCreate(); } diff --git a/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs b/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs index 777073ed..8e82a374 100644 --- a/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs +++ b/src/ShardingCore/Bootstrappers/ShardingBootstrapper.cs @@ -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); - } - } } } \ No newline at end of file diff --git a/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs b/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs index 67a322ff..c033f74b 100644 --- a/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs +++ b/src/ShardingCore/Core/DbContextCreator/ActivatorDbContextCreator.cs @@ -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; @@ -46,7 +47,16 @@ namespace ShardingCore.Core.DbContextCreator public virtual DbContext GetShellDbContext(IShardingProvider shardingProvider) { - return shardingProvider.GetService(); + try + { + return shardingProvider.GetService(); + } + catch (Exception ex) + { + throw new ShardingCoreInvalidOperationException( + $"cant get shell db context,plz override {nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)}", + ex); + } } } } diff --git a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs index 14cda773..4b1662dd 100644 --- a/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/IShardingRuntimeContext.cs @@ -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(); object GetRequiredService(Type serviceType); diff --git a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs index c37a3f43..75e32072 100644 --- a/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs +++ b/src/ShardingCore/Core/RuntimeContexts/ShardingRuntimeContext.cs @@ -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().Initialize(); InitFieldValue(); + AutoShardingCreate(); } } - public void AutoShardingCreate() + private void AutoShardingCreate() { GetRequiredService().AutoShardingCreate(); } @@ -217,40 +219,6 @@ namespace ShardingCore.Core.RuntimeContexts return _dataSourceInitializer??=GetRequiredService(); } - 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) { diff --git a/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs index 1ffbdda8..a21c5ed6 100644 --- a/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs +++ b/src/ShardingCore/Extensions/ShardingRuntimeExtension.cs @@ -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(); - } /// /// 自动尝试补偿表 /// @@ -22,7 +18,6 @@ namespace ShardingCore.Extensions /// public static void UseAutoTryCompensateTable(this IShardingRuntimeContext shardingRuntimeContext, int? parallelCount = null) { - shardingRuntimeContext.CheckRequirement(); var virtualDataSource = shardingRuntimeContext.GetVirtualDataSource(); var dataSourceInitializer = shardingRuntimeContext.GetDataSourceInitializer(); var shardingConfigOptions = shardingRuntimeContext.GetShardingConfigOptions(); diff --git a/src/ShardingCore/Sharding/MergeEngines/ParallelControl/DoOnlyOnce.cs b/src/ShardingCore/Sharding/MergeEngines/ParallelControl/DoOnlyOnce.cs index fc7be647..03d8d68b 100644 --- a/src/ShardingCore/Sharding/MergeEngines/ParallelControl/DoOnlyOnce.cs +++ b/src/ShardingCore/Sharding/MergeEngines/ParallelControl/DoOnlyOnce.cs @@ -13,7 +13,7 @@ namespace ShardingCore.Sharding.MergeEngines.ParallelControl private const int Did = 1; private const int UnDo = 0; - private int Status = UnDo; + private int Status = UnDo; public bool IsUnDo() { diff --git a/src/ShardingCore/Sharding/ShardingExecutors/CustomerQueryScope.cs b/src/ShardingCore/Sharding/ShardingExecutors/CustomerQueryScope.cs index 5bac1d6f..5853d46a 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/CustomerQueryScope.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/CustomerQueryScope.cs @@ -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 { diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs index 59adaf86..e75cc7f4 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs @@ -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 { diff --git a/src/ShardingCore/ShardingCoreExtension.cs b/src/ShardingCore/ShardingCoreExtension.cs index dd3b6f3b..d4278d7e 100644 --- a/src/ShardingCore/ShardingCoreExtension.cs +++ b/src/ShardingCore/ShardingCoreExtension.cs @@ -271,13 +271,12 @@ namespace ShardingCore /// - /// 启用定时任务自动创建表 + /// 当前接口可以直接移除掉,定时任务会在shardingcore初始化的时候自动调用 /// /// + [Obsolete("can remove this method,sharding core auto invoke.")] public static void UseAutoShardingCreate(this IServiceProvider serviceProvider) { - var shardingRuntimeContext = serviceProvider.GetRequiredService(); - shardingRuntimeContext.UseAutoShardingCreate(); } /// diff --git a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs index feca356f..fd8417e7 100644 --- a/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Abstractions/AbstractShardingAutoCreateOperatorVirtualTableRoute.cs @@ -81,6 +81,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions /// public abstract bool AutoCreateTableByTime(); + /// /// 显示错误日志 /// @@ -140,21 +141,24 @@ namespace ShardingCore.VirtualRoutes.Abstractions logger.LogInformation($"auto create table data source names:[{string.Join(",", dataSources)}]"); - foreach (var dataSource in dataSources) + if (AutoCreateTableByTime()) { - try + foreach (var dataSource in dataSources) { - 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}]"); - } - catch (Exception e) - { - //ignore - logger.LogInformation($"warning table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); - if (DoLogError) - logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error "); - } + try + { + 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}]"); + } + catch (Exception e) + { + //ignore + logger.LogInformation($"warning table tail:[{tail}],entity:[{typeof(TEntity).Name}]"); + if (DoLogError) + logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error "); + } + } } return Task.CompletedTask; @@ -162,7 +166,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions public bool AppendJob() { - return AutoCreateTableByTime(); + return true; } } } \ No newline at end of file