This commit is contained in:
xuejiaming 2022-10-19 23:13:20 +08:00
parent 441f0dbca3
commit 5eebc2aba2
13 changed files with 40 additions and 99 deletions

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

@ -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

@ -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;
}
}
}