From 04ec7a467172c10d93153457e6e4bff6e8270a62 Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Fri, 24 Jun 2022 14:48:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=E6=94=AF=E6=8C=81order=20?= =?UTF-8?q?by=E7=9A=84=E6=97=B6=E5=80=99=E5=AD=98=E5=9C=A8=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E7=9A=84convert=E5=BC=BA=E8=BD=AC=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E6=9E=84=E9=80=A0=E5=87=BD=E6=95=B0=E5=BC=BA=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/Sample.MySql/Startup.cs | 2 +- .../Sample.MySql/appsettings.Development.json | 2 +- samples/Sample.MySql/appsettings.json | 2 +- .../EntityMetadataInitializer.cs | 7 ++- .../DefaultDataSourceInitializer.cs | 9 ++-- .../DataSourceDbContext.cs | 53 +++++++++++++------ .../DefaultShardingCompilerExecutor.cs | 6 +-- .../DefaultShardingQueryExecutor.cs | 7 +-- .../Visitors/QueryableExtraDiscoverVisitor.cs | 14 ++++- 9 files changed, 67 insertions(+), 35 deletions(-) diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 5dd5dff2..3ba52326 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -104,7 +104,7 @@ namespace Sample.MySql // { // conn.Open(); // } - // // DynamicShardingHelper.DynamicAppendDataSource($"c0",$"ds{i}",$"server=127.0.0.1;port=3306;database=dbdbd{i};userid=root;password=root;"); + // DynamicShardingHelper.DynamicAppendDataSource($"c0",$"ds{i}",$"server=127.0.0.1;port=3306;database=dbdbd{i};userid=root;password=root;"); // // } app.DbSeed(); diff --git a/samples/Sample.MySql/appsettings.Development.json b/samples/Sample.MySql/appsettings.Development.json index 8983e0fc..4f30a00f 100644 --- a/samples/Sample.MySql/appsettings.Development.json +++ b/samples/Sample.MySql/appsettings.Development.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/samples/Sample.MySql/appsettings.json b/samples/Sample.MySql/appsettings.json index 214d63f9..89a1c06e 100644 --- a/samples/Sample.MySql/appsettings.json +++ b/samples/Sample.MySql/appsettings.json @@ -1,7 +1,7 @@ { "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Information", "Microsoft.Hosting.Lifetime": "Information" } diff --git a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs index 0e06ddf9..3775b254 100644 --- a/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs +++ b/src/ShardingCore/Bootstrappers/EntityMetadataInitializer.cs @@ -19,6 +19,7 @@ using ShardingCore.Extensions; using ShardingCore.Helpers; using ShardingCore.Jobs; using ShardingCore.Jobs.Abstaractions; +using ShardingCore.Logger; using ShardingCore.Sharding.Abstractions; /* @@ -36,6 +37,7 @@ namespace ShardingCore.Bootstrappers /// public class EntityMetadataInitializer: IEntityMetadataInitializer where TShardingDbContext:DbContext,IShardingDbContext where TEntity:class { + private static readonly ILogger> _logger=InternalLoggerFactory.CreateLogger>(); private const string QueryFilter = "QueryFilter"; private readonly IEntityType _entityType; private readonly string _virtualTableName; @@ -45,15 +47,13 @@ namespace ShardingCore.Bootstrappers private readonly IVirtualDataSourceRouteManager _virtualDataSourceRouteManager; private readonly IVirtualTableManager _virtualTableManager; private readonly IEntityMetadataManager _entityMetadataManager; - private readonly ILogger> _logger; public EntityMetadataInitializer(EntityMetadataEnsureParams entityMetadataEnsureParams, IShardingEntityConfigOptions shardingEntityConfigOptions, IVirtualDataSourceManager virtualDataSourceManager, IVirtualDataSourceRouteManager virtualDataSourceRouteManager, IVirtualTableManager virtualTableManager, - IEntityMetadataManager entityMetadataManager, - ILogger> logger + IEntityMetadataManager entityMetadataManager ) { _entityType = entityMetadataEnsureParams.EntityType; @@ -64,7 +64,6 @@ namespace ShardingCore.Bootstrappers _virtualDataSourceRouteManager = virtualDataSourceRouteManager; _virtualTableManager = virtualTableManager; _entityMetadataManager = entityMetadataManager; - _logger = logger; } /// /// 初始化 diff --git a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs index 4ed93348..c083094d 100644 --- a/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs +++ b/src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs @@ -16,27 +16,29 @@ using ShardingCore.TableCreator; using System; using System.Collections.Generic; using System.Threading; +using ShardingCore.Logger; namespace ShardingCore.DynamicDataSources { public class DataSourceInitializer : IDataSourceInitializer where TShardingDbContext : DbContext, IShardingDbContext { + private static readonly ILogger> _logger = + InternalLoggerFactory.CreateLogger>(); + private readonly IShardingEntityConfigOptions _entityConfigOptions; private readonly IVirtualDataSourceManager _virtualDataSourceManager; private readonly IRouteTailFactory _routeTailFactory; private readonly IVirtualTableManager _virtualTableManager; private readonly IEntityMetadataManager _entityMetadataManager; private readonly IShardingTableCreator _tableCreator; - private readonly ILogger> _logger; public DataSourceInitializer( IShardingEntityConfigOptions entityConfigOptions, IVirtualDataSourceManager virtualDataSourceManager, IRouteTailFactory routeTailFactory, IVirtualTableManager virtualTableManager, IEntityMetadataManager entityMetadataManager, - IShardingTableCreator shardingTableCreator, - ILogger> logger) + IShardingTableCreator shardingTableCreator) { _entityConfigOptions = entityConfigOptions; _virtualDataSourceManager = virtualDataSourceManager; @@ -44,7 +46,6 @@ namespace ShardingCore.DynamicDataSources _virtualTableManager = virtualTableManager; _entityMetadataManager = entityMetadataManager; _tableCreator = shardingTableCreator; - _logger = logger; } public void InitConfigure(IVirtualDataSource virtualDataSource, string dataSourceName, diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs index f04274a7..f4ab487c 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/DataSourceDbContext.cs @@ -17,6 +17,7 @@ using ShardingCore.Core.DbContextCreator; using ShardingCore.Exceptions; using ShardingCore.Extensions; using ShardingCore.Infrastructures; +using ShardingCore.Logger; using ShardingCore.Sharding.Abstractions; namespace ShardingCore.Sharding.ShardingDbContextExecutors @@ -28,25 +29,34 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class DataSourceDbContext : IDataSourceDbContext where TShardingDbContext : DbContext, IShardingDbContext + public class DataSourceDbContext : IDataSourceDbContext + where TShardingDbContext : DbContext, IShardingDbContext { + private static readonly ILogger> _logger = + InternalLoggerFactory.CreateLogger>(); + private static readonly IComparer _comparer = new NoShardingFirstComparer(); + /// /// 当前是否是默认的dbcontext 也就是不分片的dbcontext /// public bool IsDefault { get; } + /// /// 当前同库有多少dbcontext了 /// public int DbContextCount => _dataSourceDbContexts.Count; + /// /// dbcontext 创建接口 /// private readonly IDbContextCreator _dbContextCreator; + /// /// 实际的链接字符串管理者 用来提供查询和插入dbcontext的创建链接的获取 /// private readonly ActualConnectionStringManager _actualConnectionStringManager; + /// /// 当前的数据源是什么默认单数据源可以支持多数据源配置 /// @@ -56,6 +66,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors /// 数据源名称 /// public string DataSourceName { get; } + /// /// 数据源排序默认提交将未分片的数据库最先提交 /// @@ -66,21 +77,23 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors /// 是否开启了事务 /// private bool _isBeginTransaction => _shardingShellDbContext.Database.CurrentTransaction != null; + /// /// shell dbcontext最外面的壳 /// private readonly DbContext _shardingShellDbContext; + /// /// 数据库事务 /// - private IDbContextTransaction _shardingContextTransaction => _shardingShellDbContext?.Database?.CurrentTransaction; + private IDbContextTransaction _shardingContextTransaction => + _shardingShellDbContext?.Database?.CurrentTransaction; - - private readonly ILogger> _logger; /// /// 同库下公用一个db context options /// private DbContextOptions _dbContextOptions; + /// /// 是否触发了并发如果是的话就报错 /// @@ -108,12 +121,13 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors DataSourceName = dataSourceName; IsDefault = isDefault; _shardingShellDbContext = shardingShellDbContext; - _virtualDataSource = (IVirtualDataSource)((IShardingDbContext)shardingShellDbContext).GetVirtualDataSource(); + _virtualDataSource = + (IVirtualDataSource)((IShardingDbContext)shardingShellDbContext) + .GetVirtualDataSource(); _dbContextCreator = dbContextCreator; _actualConnectionStringManager = actualConnectionStringManager; - _logger = ShardingContainer.GetService>>(); - } + /// /// 创建共享的数据源配置用来做事务 不支持并发后期发现直接报错 /// @@ -124,12 +138,14 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors { return _dbContextOptions; } + //是否触发并发了 var acquired = oneByOne.Start(); if (!acquired) { throw new ShardingCoreException("cant parallel create CreateShareDbContextOptionsBuilder"); } + try { //先创建dbcontext option builder @@ -168,7 +184,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors { oneByOne.Stop(); } - } public static DbContextOptionsBuilder CreateDbContextOptionBuilder() @@ -193,10 +208,12 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors var cacheKey = routeTail.GetRouteTailIdentity(); if (!_dataSourceDbContexts.TryGetValue(cacheKey, out var dbContext)) { - dbContext = _dbContextCreator.CreateDbContext(_shardingShellDbContext,CreateShareDbContextOptionsBuilder(), routeTail); + dbContext = _dbContextCreator.CreateDbContext(_shardingShellDbContext, + CreateShareDbContextOptionsBuilder(), routeTail); _dataSourceDbContexts.Add(cacheKey, dbContext); ShardingDbTransaction(); } + return dbContext; } @@ -208,6 +225,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors JoinCurrentTransaction(); } } + /// /// 加入到当前事务 /// @@ -246,6 +264,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors } } } + /// /// 通知事务自动管理是否要清理还是开启还是加入事务 /// @@ -261,6 +280,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors JoinCurrentTransaction(); } } + /// /// 清理事务 /// @@ -288,15 +308,16 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors return i; } + /// /// 异步提交 /// /// /// /// - public async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken()) + public async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, + CancellationToken cancellationToken = new CancellationToken()) { - int i = 0; foreach (var dataSourceDbContext in _dataSourceDbContexts) { @@ -305,6 +326,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors return i; } + /// /// 获取当前的后缀数据库字典数据 /// @@ -313,6 +335,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors { return _dataSourceDbContexts; } + /// /// 回滚数据 /// @@ -329,6 +352,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors _logger.LogError(e, "rollback error."); } } + /// /// 提交数据 /// @@ -349,7 +373,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors } } #if !EFCORE2 - public async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); @@ -366,7 +389,8 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors } } - public async Task CommitAsync(int dataSourceCount, CancellationToken cancellationToken = new CancellationToken()) + public async Task CommitAsync(int dataSourceCount, CancellationToken cancellationToken = + new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); if (IsDefault) @@ -393,7 +417,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors } } #if !EFCORE2 - public async ValueTask DisposeAsync() { foreach (var dataSourceDbContext in _dataSourceDbContexts) @@ -403,4 +426,4 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors } #endif } -} +} \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs index 6f992e50..9d51358d 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingCompilerExecutor.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ShardingCore.Extensions; +using ShardingCore.Logger; using ShardingCore.Sharding.Parsers.Abstractions; using ShardingCore.Sharding.ShardingExecutors.Abstractions; using ShardingCore.Sharding.Visitors.ShardingExtractParameters; @@ -17,15 +18,14 @@ namespace ShardingCore.Sharding.ShardingExecutors /// public class DefaultShardingCompilerExecutor: IShardingCompilerExecutor { - private readonly ILogger _logger; + private static readonly ILogger _logger=InternalLoggerFactory.CreateLogger(); private readonly IShardingTrackQueryExecutor _shardingTrackQueryExecutor; private readonly IQueryCompilerContextFactory _queryCompilerContextFactory; private readonly IPrepareParser _prepareParser; - public DefaultShardingCompilerExecutor(ILogger logger, + public DefaultShardingCompilerExecutor( IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory,IPrepareParser prepareParser) { - _logger = logger; _shardingTrackQueryExecutor = shardingTrackQueryExecutor; _queryCompilerContextFactory = queryCompilerContextFactory; _prepareParser = prepareParser; diff --git a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs index 465bb60c..c22e5e5a 100644 --- a/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingExecutors/DefaultShardingQueryExecutor.cs @@ -14,6 +14,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using ShardingCore.Core; using ShardingCore.Extensions.InternalExtensions; +using ShardingCore.Logger; #if EFCORE2 using Microsoft.EntityFrameworkCore.Internal; #endif @@ -29,12 +30,8 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors */ public class DefaultShardingQueryExecutor : IShardingQueryExecutor { - private readonly ILogger _logger; + private static readonly ILogger _logger=InternalLoggerFactory.CreateLogger(); - public DefaultShardingQueryExecutor(ILogger logger) - { - _logger = logger; - } public TResult Execute(IMergeQueryCompilerContext mergeQueryCompilerContext) { diff --git a/src/ShardingCore/Sharding/Visitors/QueryableExtraDiscoverVisitor.cs b/src/ShardingCore/Sharding/Visitors/QueryableExtraDiscoverVisitor.cs index e2637edd..8ee0261c 100644 --- a/src/ShardingCore/Sharding/Visitors/QueryableExtraDiscoverVisitor.cs +++ b/src/ShardingCore/Sharding/Visitors/QueryableExtraDiscoverVisitor.cs @@ -62,7 +62,19 @@ namespace ShardingCore.Core.Internal.Visitors { if (typeof(IOrderedQueryable).IsAssignableFrom(node.Type)) { - var expression = (((node.Arguments[1] as UnaryExpression).Operand as LambdaExpression).Body as MemberExpression); + MemberExpression expression =null; + var orderbody = ((node.Arguments[1] as UnaryExpression).Operand as LambdaExpression).Body; + if(orderbody is MemberExpression orderMemberExpression) + { + expression = orderMemberExpression; + } + else if (orderbody.NodeType == ExpressionType.Convert&&orderbody is UnaryExpression orderUnaryExpression) + { + if(orderUnaryExpression.Operand is MemberExpression orderMemberConvertExpression) + { + expression = orderMemberConvertExpression; + } + } if (expression == null) throw new NotSupportedException("sharding order not support "); List properties = new List();