移除补偿cron表达式

This commit is contained in:
xuejiaming 2022-07-25 21:48:47 +08:00
parent dfc3984ce2
commit ace2d0cb6c
10 changed files with 49 additions and 34 deletions

View File

@ -10,6 +10,7 @@ using ShardingCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using ShardingCore.Helpers;
using ShardingCore.Sharding.ReadWriteConfigurations; using ShardingCore.Sharding.ReadWriteConfigurations;
namespace Sample.SqlServer namespace Sample.SqlServer

View File

@ -91,18 +91,11 @@ namespace ShardingCore.VirtualRoutes.Abstractions
/// </summary> /// </summary>
public virtual int IncrementMinutes => 10; public virtual int IncrementMinutes => 10;
public string[] GetJobCronExpressions() public virtual string[] GetJobCronExpressions()
{ {
var cronExpressions = GetCronExpressions(); return GetCronExpressions();
var compensateCronExpressions = GetCompensateCronExpressions();
return cronExpressions.Concat(compensateCronExpressions).Distinct().ToArray();
} }
/// <summary> /// <summary>
/// 补偿cron防止提前创建后没有添加tail到内存中从而无法识别
/// </summary>
/// <returns></returns>
public abstract string[] GetCompensateCronExpressions();
/// <summary>
/// 重写改方法后请一起重写IncrementMinutes值比如你按月分表但是你设置cron表达式为月中的时候建表 /// 重写改方法后请一起重写IncrementMinutes值比如你按月分表但是你设置cron表达式为月中的时候建表
/// 那么会在月中的时候 <code>DateTime.Now.AddMinutes(IncrementMinutes);</code>来获取tail会导致还是当月的所以不会建表 /// 那么会在月中的时候 <code>DateTime.Now.AddMinutes(IncrementMinutes);</code>来获取tail会导致还是当月的所以不会建表
/// </summary> /// </summary>

View File

@ -2,6 +2,7 @@ using ShardingCore.Core.VirtualRoutes;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace ShardingCore.VirtualRoutes.Days namespace ShardingCore.VirtualRoutes.Days
{ {
@ -11,13 +12,16 @@ namespace ShardingCore.VirtualRoutes.Days
* @Date: Wednesday, 27 January 2021 08:41:05 * @Date: Wednesday, 27 January 2021 08:41:05
* @Email: 326308290@qq.com * @Email: 326308290@qq.com
*/ */
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<TEntity>:AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity:class public abstract class
AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<TEntity> :
AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity : class
{ {
/// <summary> /// <summary>
/// begin time use fixed time eg.new DateTime(20xx,xx,xx) /// begin time use fixed time eg.new DateTime(20xx,xx,xx)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public abstract DateTime GetBeginTime(); public abstract DateTime GetBeginTime();
/// <summary> /// <summary>
/// 这个方法会在程序启动的时候被调用,后续整个生命周期将不会被调用,仅用来告诉框架启动的时候有多少张TEntity对象的后缀表, /// 这个方法会在程序启动的时候被调用,后续整个生命周期将不会被调用,仅用来告诉框架启动的时候有多少张TEntity对象的后缀表,
/// 然后会在启动的时候添加到 <see cref="IVirtualTable{TEntity}.AddPhysicTable(IPhysicTable physicTable)"/> /// 然后会在启动的时候添加到 <see cref="IVirtualTable{TEntity}.AddPhysicTable(IPhysicTable physicTable)"/>
@ -26,8 +30,8 @@ namespace ShardingCore.VirtualRoutes.Days
protected override List<string> CalcTailsOnStart() protected override List<string> CalcTailsOnStart()
{ {
var beginTime = GetBeginTime().Date; var beginTime = GetBeginTime().Date;
var tails=new List<string>(); var tails = new List<string>();
//提前创建表 //提前创建表
var nowTimeStamp = DateTime.Now.Date; var nowTimeStamp = DateTime.Now.Date;
if (beginTime > nowTimeStamp) if (beginTime > nowTimeStamp)
@ -39,8 +43,10 @@ namespace ShardingCore.VirtualRoutes.Days
tails.Add(tail); tails.Add(tail);
currentTimeStamp = currentTimeStamp.AddDays(1); currentTimeStamp = currentTimeStamp.AddDays(1);
} }
return tails; return tails;
} }
protected override string TimeFormatToTail(DateTime time) protected override string TimeFormatToTail(DateTime time)
{ {
return $"{time:yyyyMMdd}"; return $"{time:yyyyMMdd}";
@ -53,22 +59,22 @@ namespace ShardingCore.VirtualRoutes.Days
{ {
case ShardingOperatorEnum.GreaterThan: case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual: case ShardingOperatorEnum.GreaterThanOrEqual:
return tail =>String.Compare(tail, t, StringComparison.Ordinal) >= 0; return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan: case ShardingOperatorEnum.LessThan:
{ {
var shardingKeyDate = shardingKey.Date; var shardingKeyDate = shardingKey.Date;
//处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回 //处于临界值 o=>o.time < [2021-01-01 00:00:00] 尾巴20210101不应该被返回
if (shardingKeyDate == shardingKey) if (shardingKeyDate == shardingKey)
return tail =>String.Compare(tail, t, StringComparison.Ordinal) < 0; return tail => String.Compare(tail, t, StringComparison.Ordinal) < 0;
return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
} }
case ShardingOperatorEnum.LessThanOrEqual: case ShardingOperatorEnum.LessThanOrEqual:
return tail =>String.Compare(tail, t, StringComparison.Ordinal) <= 0; return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
case ShardingOperatorEnum.Equal: return tail => tail == t; case ShardingOperatorEnum.Equal: return tail => tail == t;
default: default:
{ {
#if DEBUG #if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail"); Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif #endif
return tail => true; return tail => true;
} }
@ -85,10 +91,10 @@ namespace ShardingCore.VirtualRoutes.Days
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 * * ?" }; var crons = base.GetJobCronExpressions().Concat(new[] { "0 0 0 * * ?" }).Distinct().ToArray();
return crons;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Days namespace ShardingCore.VirtualRoutes.Days
@ -80,9 +81,11 @@ namespace ShardingCore.VirtualRoutes.Days
"0 1 0 * * ?", "0 1 0 * * ?",
}; };
} }
public override string[] GetCompensateCronExpressions()
public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 * * ?" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 * * ?" }).Distinct().ToArray();
return crons;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Months namespace ShardingCore.VirtualRoutes.Months
@ -76,9 +77,10 @@ namespace ShardingCore.VirtualRoutes.Months
"0 1 0 1 * ?", "0 1 0 1 * ?",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 1 * ?" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 * ?"}).Distinct().ToArray();
return crons;
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Months namespace ShardingCore.VirtualRoutes.Months
@ -79,9 +80,10 @@ namespace ShardingCore.VirtualRoutes.Months
"0 1 0 1 * ?", "0 1 0 1 * ?",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 1 * ?" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 * ?"}).Distinct().ToArray();
return crons;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Weeks namespace ShardingCore.VirtualRoutes.Weeks
@ -78,9 +79,10 @@ namespace ShardingCore.VirtualRoutes.Weeks
"0 1 0 ? * 2", "0 1 0 ? * 2",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 ? * 2" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 ? * 2"}).Distinct().ToArray();
return crons;
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace ShardingCore.VirtualRoutes.Weeks namespace ShardingCore.VirtualRoutes.Weeks
{ {
@ -81,9 +82,10 @@ namespace ShardingCore.VirtualRoutes.Weeks
"0 1 0 ? * 2", "0 1 0 ? * 2",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 ? * 2" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 ? * 2"}).Distinct().ToArray();
return crons;
} }
} }
} }

View File

@ -2,6 +2,7 @@ using ShardingCore.Core.VirtualRoutes;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Years namespace ShardingCore.VirtualRoutes.Years
@ -78,9 +79,10 @@ namespace ShardingCore.VirtualRoutes.Years
"0 1 0 1 1 ?", "0 1 0 1 1 ?",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 1 1 ?" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 1 ?"}).Distinct().ToArray();
return crons;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using ShardingCore.Helpers;
using ShardingCore.VirtualRoutes.Abstractions; using ShardingCore.VirtualRoutes.Abstractions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace ShardingCore.VirtualRoutes.Years namespace ShardingCore.VirtualRoutes.Years
@ -102,9 +103,10 @@ namespace ShardingCore.VirtualRoutes.Years
"0 1 0 1 1 ?", "0 1 0 1 1 ?",
}; };
} }
public override string[] GetCompensateCronExpressions() public override string[] GetJobCronExpressions()
{ {
return new[] { "0 0 0 1 1 ?" }; var crons = base.GetJobCronExpressions().Concat(new []{"0 0 0 1 1 ?"}).Distinct().ToArray();
return crons;
} }
} }
} }