添加部分日志移除接口分片约束

This commit is contained in:
xuejmnet 2022-02-21 22:51:53 +08:00
parent 8ad08bc33f
commit 776b221b3d
21 changed files with 121 additions and 71 deletions

View File

@ -10,7 +10,7 @@ namespace Sample.BulkConsole.Entities
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public class Order:IShardingTable
public class Order
{
public string Id { get; set; }
public string OrderNo { get; set; }

View File

@ -6,7 +6,7 @@ using ShardingCore.Core;
namespace Sample.MySql.Domain.Entities
{
public class SysUserLogByMonth : IShardingTable
public class SysUserLogByMonth
{
public string Id { get; set; }
[ShardingTableKey]

View File

@ -8,7 +8,7 @@ namespace Sample.MySql.Domain.Entities
* @Date: Tuesday, 26 January 2021 12:25:39
* @Email: 326308290@qq.com
*/
public class SysUserMod:IShardingTable
public class SysUserMod
{
/// <summary>
/// 用户Id用于分表

View File

@ -12,7 +12,7 @@ namespace Sample.SqlServer.Domain.Entities
/// <summary>
/// 用户表
/// </summary>
public class SysUserMod : IShardingTable
public class SysUserMod
{
/// <summary>
/// 用户Id用于分表

View File

@ -8,7 +8,7 @@ namespace Sample.SqlServer.Domain.Entities
* @Date: Monday, 01 February 2021 15:43:22
* @Email: 326308290@qq.com
*/
public class SysUserSalary:IShardingTable
public class SysUserSalary
{
public string Id { get; set; }
public string UserId { get; set; }

View File

@ -8,7 +8,7 @@ namespace Sample.SqlServer3x.Domain.Entities
* @Date: Thursday, 14 January 2021 15:36:43
* @Email: 326308290@qq.com
*/
public class SysUserMod:IShardingTable
public class SysUserMod
{
/// <summary>
/// 用户Id用于分表

View File

@ -6,7 +6,7 @@ using ShardingCore.Core;
namespace Samples.AutoByDate.SqlServer.Domain.Entities
{
public class TestLogByWeek:IShardingTable
public class TestLogByWeek
{
public string Id { get; set; }
[ShardingTableKey]

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore.Extensions;
using ShardingCore.Jobs;
using ShardingCore.Jobs.Abstaractions;
@ -17,11 +18,13 @@ namespace ShardingCore.Bootstrapers
*/
public class ShardingBootstrapper : IShardingBootstrapper
{
private readonly ILogger<ShardingBootstrapper> _logger;
private readonly IEnumerable<IDbContextTypeCollector> _dbContextTypeCollectors;
private readonly DoOnlyOnce _doOnlyOnce = new DoOnlyOnce();
public ShardingBootstrapper(IServiceProvider serviceProvider,IEnumerable<IDbContextTypeCollector> dbContextTypeCollectors)
public ShardingBootstrapper(IServiceProvider serviceProvider,ILogger<ShardingBootstrapper> logger,IEnumerable<IDbContextTypeCollector> dbContextTypeCollectors)
{
_logger = logger;
_dbContextTypeCollectors = dbContextTypeCollectors;
ShardingContainer.SetServices(serviceProvider);
}
@ -32,10 +35,13 @@ namespace ShardingCore.Bootstrapers
{
if (!_doOnlyOnce.IsUnDo())
return;
_logger.LogDebug("sharding core starting......");
foreach (var dbContextTypeCollector in _dbContextTypeCollectors)
{
var instance = (IShardingDbContextBootstrapper)ShardingContainer.CreateInstance(typeof(ShardingDbContextBootstrapper<>).GetGenericType0(dbContextTypeCollector.ShardingDbContextType));
_logger.LogDebug($"{dbContextTypeCollector.ShardingDbContextType} start init......");
instance.Init();
_logger.LogDebug($"{dbContextTypeCollector.ShardingDbContextType} complete init");
}
var jobManager = ShardingContainer.GetService<IJobManager>();

View File

@ -0,0 +1,15 @@
using System;
namespace ShardingCore.Core
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 21 February 2022 22:33:17
* @Email: 326308290@qq.com
*/
public interface IPrint
{
string GetPrintInfo();
}
}

View File

@ -10,6 +10,7 @@ namespace ShardingCore.Core
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
[Obsolete("will remove")]
public interface IShardingDataSource
{
}

View File

@ -11,6 +11,7 @@ namespace ShardingCore.Core
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
[Obsolete("will remove")]
public interface IShardingTable
{
}

View File

@ -12,7 +12,7 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
public class DataSourceRouteResult
public class DataSourceRouteResult:IPrint
{
public DataSourceRouteResult(ISet<string> intersectDataSources)
{
@ -22,5 +22,10 @@ namespace ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine
/// 交集
/// </summary>
public ISet<string> IntersectDataSources { get; }
public string GetPrintInfo()
{
return $"data source route result:{string.Join(",", IntersectDataSources)}";
}
}
}

View File

@ -13,7 +13,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
* @Date: Thursday, 28 January 2021 10:18:09
* @Email: 326308290@qq.com
*/
public class TableRouteResult
public class TableRouteResult:IPrint
{
public TableRouteResult(IEnumerable<IPhysicTable> replaceTables, Type shardingDbContextType)
{
@ -39,6 +39,10 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
if (obj.GetType() != this.GetType()) return false;
return Equals((TableRouteResult)obj);
}
public string GetPrintInfo()
{
return $"table route result:has different tail:{HasDifferentTail},current table:[{string.Join(",", ReplaceTables.Select(o => o.FullName))}]";
}
#if !EFCORE2
@ -46,6 +50,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine
{
return HashCode.Combine(ReplaceTables, ShardingDbContextType);
}
#endif
#if EFCORE2

View File

@ -10,79 +10,73 @@ namespace ShardingCore.Helpers
{
private EntityMetadataHelper()
{
}
public static void Configure<TEntity>(EntityMetadataTableBuilder<TEntity> builder) where TEntity : class
{
var entityType = typeof(TEntity);
if (typeof(IShardingTable).IsAssignableFrom(entityType))
PropertyInfo[] shardingProperties = entityType.GetProperties();
var shardingTableCount = 0;
foreach (var shardingProperty in shardingProperties)
{
PropertyInfo[] shardingProperties = entityType.GetProperties();
var shardingTableCount = 0;
foreach (var shardingProperty in shardingProperties)
var attributes = shardingProperty.GetCustomAttributes(true);
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingTableKeyAttribute)) is ShardingTableKeyAttribute shardingKey)
{
var attributes = shardingProperty.GetCustomAttributes(true);
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingTableKeyAttribute)) is ShardingTableKeyAttribute shardingKey)
{
if (shardingTableCount > 1)
throw new NotSupportedException($"{entityType} impl {nameof(IShardingTable)} should use single attribute [{nameof(ShardingTableKeyAttribute)}]");
if (shardingTableCount > 1)
throw new NotSupportedException($"{entityType} should use single attribute [{nameof(ShardingTableKeyAttribute)}]");
builder.ShardingProperty(shardingProperty.Name);
var autoCreateTable =
shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.UnKnown
? (bool?) null
: (shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.Create);
builder.AutoCreateTable(autoCreateTable);
builder.TableSeparator(shardingKey.TableSeparator);
shardingTableCount++;
}
builder.ShardingProperty(shardingProperty.Name);
var autoCreateTable =
shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.UnKnown
? (bool?)null
: (shardingKey.AutoCreateTableOnStart == ShardingKeyAutoCreateTableEnum.Create);
builder.AutoCreateTable(autoCreateTable);
builder.TableSeparator(shardingKey.TableSeparator);
shardingTableCount++;
}
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraTableKeyAttribute)) is
ShardingExtraTableKeyAttribute shardingExtraKey)
{
builder.ShardingExtraProperty(shardingProperty.Name);
}
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraTableKeyAttribute)) is
ShardingExtraTableKeyAttribute shardingExtraKey)
{
builder.ShardingExtraProperty(shardingProperty.Name);
}
}
}
public static void Configure<TEntity>(EntityMetadataDataSourceBuilder<TEntity> builder) where TEntity : class
{
var entityType = typeof(TEntity);
if (typeof(IShardingDataSource).IsAssignableFrom(entityType))
PropertyInfo[] shardingProperties = entityType.GetProperties();
var shardingDataSourceCount = 0;
foreach (var shardingProperty in shardingProperties)
{
PropertyInfo[] shardingProperties = entityType.GetProperties();
var shardingDataSourceCount = 0;
foreach (var shardingProperty in shardingProperties)
var attributes = shardingProperty.GetCustomAttributes(true);
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingDataSourceKeyAttribute)) is ShardingDataSourceKeyAttribute shardingDataSourceKey)
{
var attributes = shardingProperty.GetCustomAttributes(true);
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingDataSourceKeyAttribute)) is ShardingDataSourceKeyAttribute shardingDataSourceKey)
{
if (shardingDataSourceCount > 1)
throw new NotSupportedException($"{entityType} impl {nameof(IShardingDataSource)} should use single attribute [{nameof(ShardingDataSourceKeyAttribute)}]");
builder.ShardingProperty(shardingProperty.Name);
if (shardingDataSourceCount > 1)
throw new NotSupportedException($"{entityType} should use single attribute [{nameof(ShardingDataSourceKeyAttribute)}]");
builder.ShardingProperty(shardingProperty.Name);
var autoCreateDataSource = shardingDataSourceKey.AutoCreateDataSourceTableOnStart ==
ShardingKeyAutoCreateTableEnum.UnKnown
? (bool?)null
: (shardingDataSourceKey.AutoCreateDataSourceTableOnStart ==
ShardingKeyAutoCreateTableEnum.Create);
var autoCreateDataSource = shardingDataSourceKey.AutoCreateDataSourceTableOnStart ==
ShardingKeyAutoCreateTableEnum.UnKnown
? (bool?)null
: (shardingDataSourceKey.AutoCreateDataSourceTableOnStart ==
ShardingKeyAutoCreateTableEnum.Create);
builder.AutoCreateDataSource(autoCreateDataSource);
shardingDataSourceCount++;
}
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraDataSourceKeyAttribute)) is
ShardingExtraDataSourceKeyAttribute shardingExtraKey)
{
builder.ShardingExtraProperty(shardingProperty.Name);
}
builder.AutoCreateDataSource(autoCreateDataSource);
shardingDataSourceCount++;
}
if (attributes.FirstOrDefault(x => x.GetType() == typeof(ShardingExtraDataSourceKeyAttribute)) is
ShardingExtraDataSourceKeyAttribute shardingExtraKey)
{
builder.ShardingExtraProperty(shardingProperty.Name);
}
}
}
}
}
}

View File

@ -16,7 +16,7 @@ using ShardingCore.Sharding.Visitors.ShardingExtractParameters;
*/
namespace ShardingCore.ShardingExecutors
{
public class CompileParameter:ICompileParameter
public class CompileParameter:ICompileParameter,IPrint
{
private readonly IShardingDbContext _shardingDbContext;
private readonly Expression _nativeQueryExpression;
@ -91,5 +91,10 @@ namespace ShardingCore.ShardingExecutors
{
return _sameWithShardingComparer;
}
public string GetPrintInfo()
{
return $"is not support :{_isNotSupport},max query connections limit:{_maxQueryConnectionsLimit},connection mode:{_connectionMode},readonly:{_readOnly},as route:{_shardingRouteConfigure!=null},is sequence:{_isSequence},same with sharding comparer:{_sameWithShardingComparer}";
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ShardingCore.Extensions;
using ShardingCore.Sharding.ShardingExecutors.Abstractions;
using ShardingCore.Sharding.Visitors.ShardingExtractParameters;
@ -12,17 +13,20 @@ namespace ShardingCore.Sharding.ShardingExecutors
{
public class DefaultShardingComplierExecutor: IShardingComplierExecutor
{
private readonly ILogger<DefaultShardingComplierExecutor> _logger;
private readonly IShardingTrackQueryExecutor _shardingTrackQueryExecutor;
private readonly IQueryCompilerContextFactory _queryCompilerContextFactory;
public DefaultShardingComplierExecutor(IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory)
public DefaultShardingComplierExecutor(ILogger<DefaultShardingComplierExecutor> logger,IShardingTrackQueryExecutor shardingTrackQueryExecutor, IQueryCompilerContextFactory queryCompilerContextFactory)
{
_logger = logger;
_shardingTrackQueryExecutor = shardingTrackQueryExecutor;
_queryCompilerContextFactory = queryCompilerContextFactory;
}
public TResult Execute<TResult>(IShardingDbContext shardingDbContext, Expression query)
{
var compileParameter = new CompileParameter(shardingDbContext,query);
_logger.LogDebug(compileParameter.GetPrintInfo());
using (new CustomerQueryScope(compileParameter))
{
var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter);
@ -38,6 +42,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
CancellationToken cancellationToken = new CancellationToken())
{
var compileParameter = new CompileParameter(shardingDbContext,query);
_logger.LogDebug(compileParameter.GetPrintInfo());
using (new CustomerQueryScope(compileParameter))
{
@ -51,6 +56,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
public IAsyncEnumerable<TResult> ExecuteAsync<TResult>(IShardingDbContext shardingDbContext, Expression query)
{
var compileParameter = new CompileParameter(shardingDbContext,query);
_logger.LogDebug(compileParameter.GetPrintInfo());
using (new CustomerQueryScope(compileParameter))
{
var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter);
@ -62,6 +68,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
CancellationToken cancellationToken)
{
var compileParameter = new CompileParameter(shardingDbContext,query);
_logger.LogDebug(compileParameter.GetPrintInfo());
using (new CustomerQueryScope(compileParameter))
{
var queryCompilerContext = _queryCompilerContextFactory.Create(compileParameter);

View File

@ -5,6 +5,7 @@ using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.Logging;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes.RouteRuleEngine;
using ShardingCore.Core.VirtualRoutes.TableRoutes.RoutingRuleEngine;
using ShardingCore.Exceptions;
@ -18,6 +19,7 @@ namespace ShardingCore.Sharding.ShardingExecutors
{
public class QueryCompilerContextFactory : IQueryCompilerContextFactory
{
private readonly ILogger<QueryCompilerContextFactory> _logger;
private static readonly IQueryableCombine _enumerableQueryableCombine;
private static readonly IQueryableCombine _allQueryableCombine;
private static readonly IQueryableCombine _constantQueryableCombine;
@ -33,23 +35,32 @@ namespace ShardingCore.Sharding.ShardingExecutors
_whereQueryableCombine = new WhereQueryableCombine();
}
public QueryCompilerContextFactory(ILogger<QueryCompilerContextFactory> logger)
{
_logger = logger;
}
public IQueryCompilerContext Create(ICompileParameter compileParameter)
{
var queryCompilerContext =
QueryCompilerContext.Create(compileParameter);
if (queryCompilerContext.GetQueryCompilerExecutor() is not null)
{
_logger.LogDebug($"{queryCompilerContext.GetQueryExpression().ShardingPrint()} is native query");
return queryCompilerContext;
}
var queryableCombine = GetQueryableCombine(queryCompilerContext);
_logger.LogDebug($"queryable combine:{queryableCombine.GetType()}");
var dataSourceRouteRuleEngineFactory = (IDataSourceRouteRuleEngineFactory)ShardingContainer.GetService(typeof(IDataSourceRouteRuleEngineFactory<>).GetGenericType0(queryCompilerContext.GetShardingDbContextType()));
var tableRouteRuleEngineFactory = (ITableRouteRuleEngineFactory)ShardingContainer.GetService(typeof(ITableRouteRuleEngineFactory<>).GetGenericType0(queryCompilerContext.GetShardingDbContextType()));
_logger.LogDebug($"queryable combine before:{queryCompilerContext.GetQueryExpression().ShardingPrint()}");
var queryCombineResult = queryableCombine.Combine(queryCompilerContext);
_logger.LogDebug($"queryable combine after:{queryCombineResult.GetCombineQueryable().ShardingPrint()}");
var dataSourceRouteResult = dataSourceRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable(), compileParameter.GetShardingDbContext());
var tableRouteResults = tableRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable());
var routeResults = tableRouteResults as TableRouteResult[] ?? tableRouteResults.ToArray();
_logger.LogDebug(dataSourceRouteResult.GetPrintInfo());
var routeResults = tableRouteRuleEngineFactory.Route(queryCombineResult.GetCombineQueryable()).ToArray();
_logger.LogDebug($"table route results:{string.Join(",",routeResults.Select(o=>o.GetPrintInfo()))}");
var mergeCombineCompilerContext = MergeQueryCompilerContext.Create(queryCompilerContext, queryCombineResult, dataSourceRouteResult,
routeResults);
return mergeCombineCompilerContext;

View File

@ -3,7 +3,7 @@ using ShardingCore.Core;
namespace ShardingCore.Test.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
public class Order
{
public Guid Id { get; set; }
[ShardingDataSourceKey]

View File

@ -3,7 +3,7 @@ using ShardingCore.Core;
namespace ShardingCore.Test2x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
public class Order
{
public Guid Id { get; set; }
[ShardingDataSourceKey]

View File

@ -3,7 +3,7 @@ using ShardingCore.Core;
namespace ShardingCore.Test3x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
public class Order
{
public Guid Id { get; set; }
[ShardingDataSourceKey]

View File

@ -3,7 +3,7 @@ using ShardingCore.Core;
namespace ShardingCore.Test5x.Domain.Entities
{
public class Order:IShardingDataSource,IShardingTable
public class Order
{
public Guid Id { get; set; }
[ShardingDataSourceKey]