对分片操作符注释进行修改和完善,针对定时分片时间的补偿,防止提前创建表导致重启后内存中可能不会存在分片信息

This commit is contained in:
xuejiaming 2022-07-21 09:24:00 +08:00
parent 7b8f163893
commit 9b1af63ada
14 changed files with 66 additions and 11 deletions

View File

@ -26,6 +26,5 @@ namespace Sample.SqlServer.Shardings
{
return true;
}
}
}

View File

@ -16,7 +16,7 @@ namespace ShardingCore.Core.Collections
{
}
public IReadOnlyList<T> Data => _list;
public ImmutableList<T> Data => _list;
public void Append(T value)
{

View File

@ -7,7 +7,7 @@ namespace ShardingCore.Jobs.Abstaractions
public interface IJob
{
string JobName { get; }
string[] GetCronExpressions();
string[] GetJobCronExpressions();
Task ExecuteAsync();
bool AppendJob();
}

View File

@ -15,6 +15,12 @@ namespace ShardingCore.Jobs.Impls
*/
public sealed class JobEntry
{
public JobEntry(IJob job)
{
JobInstance = job;
JobName = job.JobName;
JobCronExpressions = job.GetJobCronExpressions();
}
/// <summary>
/// 保证多线程只有一个清理操作
/// </summary>
@ -32,7 +38,7 @@ namespace ShardingCore.Jobs.Impls
/// <summary>
/// job实例
/// </summary>
public IJob JobInstance { get; set; }
public IJob JobInstance { get; }
/// <summary>
/// 是否跳过如果正在运行
@ -43,6 +49,10 @@ namespace ShardingCore.Jobs.Impls
/// 下次运行时间
/// </summary>
public DateTime? NextUtcTime { get; set; }
/// <summary>
/// 任务的cron表达式
/// </summary>
public string[] JobCronExpressions { get; }
/// <summary>
/// 是否正在运行
@ -69,7 +79,7 @@ namespace ShardingCore.Jobs.Impls
/// </summary>
public void CalcNextUtcTime()
{
this.NextUtcTime= JobInstance.GetCronExpressions().Select(cron => new CronExpression(cron).GetTimeAfter(DateTime.UtcNow)).Min();
this.NextUtcTime= JobCronExpressions.Select(cron => new CronExpression(cron).GetTimeAfter(DateTime.UtcNow)).Min();
}
}
}

View File

@ -17,11 +17,7 @@ namespace ShardingCore.Jobs
public static JobEntry Create(IJob job)
{
var jobEntry = new JobEntry()
{
JobInstance = job,
JobName = job.JobName,
};
var jobEntry = new JobEntry(job);
jobEntry.CalcNextUtcTime();
return jobEntry;
}

View File

@ -22,12 +22,17 @@ using ShardingCore.TableCreator;
namespace ShardingCore.VirtualRoutes.Abstractions
{
/// <summary>
/// 分片字段追加
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
[ExcludeFromCodeCoverage]
public abstract class
AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey> :
AbstractShardingOperatorVirtualTableRoute<TEntity, TKey>, ITailAppendable, IJob where TEntity : class
{
private static readonly object APPEND_LOCK = new object();
private readonly object APPEND_LOCK = new object();
private readonly SafeReadAppendList<string> _tails = new SafeReadAppendList<string>();
@ -85,6 +90,17 @@ namespace ShardingCore.VirtualRoutes.Abstractions
/// </summary>
public virtual int IncrementMinutes => 10;
public string[] GetJobCronExpressions()
{
var cronExpressions = GetCronExpressions();
var compensateCronExpressions = GetCompensateCronExpressions();
return cronExpressions.Concat(compensateCronExpressions).Distinct().ToArray();
}
/// <summary>
/// 补偿cron防止提前创建后没有添加tail到内存中从而无法识别
/// </summary>
/// <returns></returns>
public abstract string[] GetCompensateCronExpressions();
/// <summary>
/// 重写改方法后请一起重写IncrementMinutes值比如你按月分表但是你设置cron表达式为月中的时候建表
/// 那么会在月中的时候 <code>DateTime.Now.AddMinutes(IncrementMinutes);</code>来获取tail会导致还是当月的所以不会建表
@ -99,6 +115,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions
/// <returns></returns>
protected abstract string ConvertNowToTail(DateTime now);
public virtual Task ExecuteAsync()
{
var logger=RouteShardingProvider

View File

@ -85,5 +85,10 @@ namespace ShardingCore.VirtualRoutes.Days
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 * * ?" };
}
}
}

View File

@ -80,5 +80,9 @@ namespace ShardingCore.VirtualRoutes.Days
"0 1 0 * * ?",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 * * ?" };
}
}
}

View File

@ -76,6 +76,10 @@ namespace ShardingCore.VirtualRoutes.Months
"0 1 0 1 * ?",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 1 * ?" };
}
}
}

View File

@ -79,5 +79,9 @@ namespace ShardingCore.VirtualRoutes.Months
"0 1 0 1 * ?",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 1 * ?" };
}
}
}

View File

@ -78,6 +78,10 @@ namespace ShardingCore.VirtualRoutes.Weeks
"0 1 0 ? * 2",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 ? * 2" };
}
}
}

View File

@ -81,5 +81,9 @@ namespace ShardingCore.VirtualRoutes.Weeks
"0 1 0 ? * 2",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 ? * 2" };
}
}
}

View File

@ -78,5 +78,9 @@ namespace ShardingCore.VirtualRoutes.Years
"0 1 0 1 1 ?",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 1 1 ?" };
}
}
}

View File

@ -102,5 +102,9 @@ namespace ShardingCore.VirtualRoutes.Years
"0 1 0 1 1 ?",
};
}
public override string[] GetCompensateCronExpressions()
{
return new[] { "0 0 0 1 1 ?" };
}
}
}