feat: migrate snowflake ids and refresh migrations
This commit is contained in:
@@ -23,15 +23,15 @@ public abstract class AuditableEntityBase : EntityBase, IAuditableEntity
|
||||
/// <summary>
|
||||
/// 创建人用户标识,匿名或系统操作时为 null。
|
||||
/// </summary>
|
||||
public Guid? CreatedBy { get; set; }
|
||||
public long? CreatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最后更新人用户标识,匿名或系统操作时为 null。
|
||||
/// </summary>
|
||||
public Guid? UpdatedBy { get; set; }
|
||||
public long? UpdatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 删除人用户标识(软删除),未删除时为 null。
|
||||
/// </summary>
|
||||
public Guid? DeletedBy { get; set; }
|
||||
public long? DeletedBy { get; set; }
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ public abstract class EntityBase
|
||||
/// <summary>
|
||||
/// 实体唯一标识。
|
||||
/// </summary>
|
||||
public Guid Id { get; set; }
|
||||
public long Id { get; set; }
|
||||
}
|
||||
|
||||
@@ -23,15 +23,15 @@ public interface IAuditableEntity : ISoftDeleteEntity
|
||||
/// <summary>
|
||||
/// 创建人用户标识,匿名或系统操作时为 null。
|
||||
/// </summary>
|
||||
Guid? CreatedBy { get; set; }
|
||||
long? CreatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最后更新人用户标识,匿名或系统操作时为 null。
|
||||
/// </summary>
|
||||
Guid? UpdatedBy { get; set; }
|
||||
long? UpdatedBy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 删除人用户标识(软删除),未删除时为 null。
|
||||
/// </summary>
|
||||
Guid? DeletedBy { get; set; }
|
||||
long? DeletedBy { get; set; }
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ public interface IMultiTenantEntity
|
||||
/// <summary>
|
||||
/// 所属租户 ID。
|
||||
/// </summary>
|
||||
Guid TenantId { get; set; }
|
||||
long TenantId { get; set; }
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ public abstract class MultiTenantEntityBase : AuditableEntityBase, IMultiTenantE
|
||||
/// <summary>
|
||||
/// 所属租户 ID。
|
||||
/// </summary>
|
||||
public Guid TenantId { get; set; }
|
||||
public long TenantId { get; set; }
|
||||
}
|
||||
|
||||
13
src/Core/TakeoutSaaS.Shared.Abstractions/Ids/IIdGenerator.cs
Normal file
13
src/Core/TakeoutSaaS.Shared.Abstractions/Ids/IIdGenerator.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
|
||||
/// <summary>
|
||||
/// 雪花 ID 生成器接口。
|
||||
/// </summary>
|
||||
public interface IIdGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成下一个唯一长整型 ID。
|
||||
/// </summary>
|
||||
/// <returns>雪花 ID。</returns>
|
||||
long NextId();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
|
||||
/// <summary>
|
||||
/// 雪花 ID 生成器配置。
|
||||
/// </summary>
|
||||
public sealed class IdGeneratorOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置节名称。
|
||||
/// </summary>
|
||||
public const string SectionName = "IdGenerator";
|
||||
|
||||
/// <summary>
|
||||
/// 工作节点标识,0-31。
|
||||
/// </summary>
|
||||
[Range(0, 31)]
|
||||
public int WorkerId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 机房标识,0-31。
|
||||
/// </summary>
|
||||
[Range(0, 31)]
|
||||
public int DatacenterId { get; set; }
|
||||
}
|
||||
@@ -99,6 +99,65 @@ public sealed record ApiResponse<T>
|
||||
return TraceContext.TraceId;
|
||||
}
|
||||
|
||||
return Activity.Current?.Id ?? Guid.NewGuid().ToString("N");
|
||||
if (!string.IsNullOrWhiteSpace(TraceContext.TraceId))
|
||||
{
|
||||
return TraceContext.TraceId;
|
||||
}
|
||||
|
||||
if (Activity.Current?.Id is { } id && !string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
return IdFallbackGenerator.Instance.NextId().ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class IdFallbackGenerator
|
||||
{
|
||||
private static readonly Lazy<IdFallbackGenerator> Lazy = new(() => new IdFallbackGenerator());
|
||||
public static IdFallbackGenerator Instance => Lazy.Value;
|
||||
|
||||
private readonly object _sync = new();
|
||||
private long _lastTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
private long _sequence;
|
||||
|
||||
private IdFallbackGenerator()
|
||||
{
|
||||
}
|
||||
|
||||
public long NextId()
|
||||
{
|
||||
lock (_sync)
|
||||
{
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
if (timestamp == _lastTimestamp)
|
||||
{
|
||||
_sequence = (_sequence + 1) & 4095;
|
||||
if (_sequence == 0)
|
||||
{
|
||||
timestamp = WaitNextMillis(_lastTimestamp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_sequence = 0;
|
||||
}
|
||||
|
||||
_lastTimestamp = timestamp;
|
||||
return ((timestamp - 1577836800000L) << 22) | _sequence;
|
||||
}
|
||||
}
|
||||
|
||||
private static long WaitNextMillis(long lastTimestamp)
|
||||
{
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
while (timestamp <= lastTimestamp)
|
||||
{
|
||||
Thread.SpinWait(100);
|
||||
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public interface ICurrentUserAccessor
|
||||
/// <summary>
|
||||
/// 当前用户 ID,未登录时为 Guid.Empty。
|
||||
/// </summary>
|
||||
Guid UserId { get; }
|
||||
long UserId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否已登录。
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Abstractions.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// 将 long 类型的雪花 ID 以字符串形式序列化/反序列化,避免前端精度丢失。
|
||||
/// </summary>
|
||||
public sealed class SnowflakeIdJsonConverter : JsonConverter<long>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.Number => reader.GetInt64(),
|
||||
JsonTokenType.String when long.TryParse(reader.GetString(), out var value) => value,
|
||||
JsonTokenType.Null => 0,
|
||||
_ => throw new JsonException("无法解析雪花 ID")
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value == 0 ? "0" : value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可空雪花 ID 转换器。
|
||||
/// </summary>
|
||||
public sealed class NullableSnowflakeIdJsonConverter : JsonConverter<long?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.TokenType switch
|
||||
{
|
||||
JsonTokenType.Number => reader.GetInt64(),
|
||||
JsonTokenType.String when long.TryParse(reader.GetString(), out var value) => value,
|
||||
JsonTokenType.Null => null,
|
||||
_ => throw new JsonException("无法解析雪花 ID")
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value.HasValue ? value.Value.ToString() : null);
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,5 @@ public interface ITenantProvider
|
||||
/// <summary>
|
||||
/// 获取当前租户 ID,未解析时返回 Guid.Empty。
|
||||
/// </summary>
|
||||
Guid GetCurrentTenantId();
|
||||
long GetCurrentTenantId();
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class TenantContext
|
||||
/// <summary>
|
||||
/// 未解析到租户时的默认上下文。
|
||||
/// </summary>
|
||||
public static TenantContext Empty { get; } = new(Guid.Empty, null, "unresolved");
|
||||
public static TenantContext Empty { get; } = new(0, null, "unresolved");
|
||||
|
||||
/// <summary>
|
||||
/// 初始化租户上下文。
|
||||
@@ -16,7 +16,7 @@ public sealed class TenantContext
|
||||
/// <param name="tenantId">租户 ID</param>
|
||||
/// <param name="tenantCode">租户编码(可选)</param>
|
||||
/// <param name="source">解析来源</param>
|
||||
public TenantContext(Guid tenantId, string? tenantCode, string source)
|
||||
public TenantContext(long tenantId, string? tenantCode, string source)
|
||||
{
|
||||
TenantId = tenantId;
|
||||
TenantCode = tenantCode;
|
||||
@@ -26,7 +26,7 @@ public sealed class TenantContext
|
||||
/// <summary>
|
||||
/// 当前租户 ID,未解析时为 Guid.Empty。
|
||||
/// </summary>
|
||||
public Guid TenantId { get; }
|
||||
public long TenantId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 当前租户编码(例如子域名或业务编码),可为空。
|
||||
@@ -41,5 +41,5 @@ public sealed class TenantContext
|
||||
/// <summary>
|
||||
/// 是否已成功解析到租户。
|
||||
/// </summary>
|
||||
public bool IsResolved => TenantId != Guid.Empty;
|
||||
public bool IsResolved => TenantId != 0;
|
||||
}
|
||||
|
||||
111
src/Core/TakeoutSaaS.Shared.Kernel/Ids/SnowflakeIdGenerator.cs
Normal file
111
src/Core/TakeoutSaaS.Shared.Kernel/Ids/SnowflakeIdGenerator.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System.Diagnostics;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading;
|
||||
using TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Kernel.Ids;
|
||||
|
||||
/// <summary>
|
||||
/// 基于雪花算法的长整型 ID 生成器。
|
||||
/// </summary>
|
||||
public sealed class SnowflakeIdGenerator : IIdGenerator
|
||||
{
|
||||
private const long Twepoch = 1577836800000L; // 2020-01-01 UTC
|
||||
private const int WorkerIdBits = 5;
|
||||
private const int DatacenterIdBits = 5;
|
||||
private const int SequenceBits = 12;
|
||||
|
||||
private const long MaxWorkerId = -1L ^ (-1L << WorkerIdBits);
|
||||
private const long MaxDatacenterId = -1L ^ (-1L << DatacenterIdBits);
|
||||
|
||||
private const int WorkerIdShift = SequenceBits;
|
||||
private const int DatacenterIdShift = SequenceBits + WorkerIdBits;
|
||||
private const int TimestampLeftShift = SequenceBits + WorkerIdBits + DatacenterIdBits;
|
||||
private const long SequenceMask = -1L ^ (-1L << SequenceBits);
|
||||
|
||||
private readonly long _workerId;
|
||||
private readonly long _datacenterId;
|
||||
private long _lastTimestamp = -1L;
|
||||
private long _sequence;
|
||||
private readonly object _syncRoot = new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化生成器。
|
||||
/// </summary>
|
||||
/// <param name="workerId">工作节点 ID。</param>
|
||||
/// <param name="datacenterId">机房 ID。</param>
|
||||
public SnowflakeIdGenerator(long workerId = 0, long datacenterId = 0)
|
||||
{
|
||||
_workerId = Normalize(workerId, MaxWorkerId, nameof(workerId));
|
||||
_datacenterId = Normalize(datacenterId, MaxDatacenterId, nameof(datacenterId));
|
||||
_sequence = RandomNumberGenerator.GetInt32(0, (int)SequenceMask);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public long NextId()
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
var timestamp = CurrentTimeMillis();
|
||||
|
||||
if (timestamp < _lastTimestamp)
|
||||
{
|
||||
// 时钟回拨时等待到下一毫秒。
|
||||
var wait = _lastTimestamp - timestamp;
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(wait));
|
||||
timestamp = CurrentTimeMillis();
|
||||
if (timestamp < _lastTimestamp)
|
||||
{
|
||||
throw new InvalidOperationException($"系统时钟回拨 {_lastTimestamp - timestamp} 毫秒,无法生成 ID。");
|
||||
}
|
||||
}
|
||||
|
||||
if (_lastTimestamp == timestamp)
|
||||
{
|
||||
_sequence = (_sequence + 1) & SequenceMask;
|
||||
if (_sequence == 0)
|
||||
{
|
||||
timestamp = WaitNextMillis(_lastTimestamp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_sequence = 0;
|
||||
}
|
||||
|
||||
_lastTimestamp = timestamp;
|
||||
|
||||
var id = ((timestamp - Twepoch) << TimestampLeftShift)
|
||||
| (_datacenterId << DatacenterIdShift)
|
||||
| (_workerId << WorkerIdShift)
|
||||
| _sequence;
|
||||
|
||||
Debug.Assert(id > 0);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
private static long WaitNextMillis(long lastTimestamp)
|
||||
{
|
||||
var timestamp = CurrentTimeMillis();
|
||||
while (timestamp <= lastTimestamp)
|
||||
{
|
||||
Thread.SpinWait(50);
|
||||
timestamp = CurrentTimeMillis();
|
||||
}
|
||||
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
private static long CurrentTimeMillis() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
|
||||
private static long Normalize(long value, long max, string name)
|
||||
{
|
||||
if (value < 0 || value > max)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(name, value, $"取值范围 0~{max}");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Shared.Abstractions.Diagnostics;
|
||||
using TakeoutSaaS.Shared.Abstractions.Ids;
|
||||
|
||||
namespace TakeoutSaaS.Shared.Web.Middleware;
|
||||
|
||||
@@ -17,11 +18,13 @@ public sealed class CorrelationIdMiddleware
|
||||
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<CorrelationIdMiddleware> _logger;
|
||||
private readonly IIdGenerator _idGenerator;
|
||||
|
||||
public CorrelationIdMiddleware(RequestDelegate next, ILogger<CorrelationIdMiddleware> logger)
|
||||
public CorrelationIdMiddleware(RequestDelegate next, ILogger<CorrelationIdMiddleware> logger, IIdGenerator idGenerator)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
_idGenerator = idGenerator;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
@@ -52,7 +55,7 @@ public sealed class CorrelationIdMiddleware
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveTraceId(HttpContext context)
|
||||
private string ResolveTraceId(HttpContext context)
|
||||
{
|
||||
if (TryGetHeader(context, TraceHeader, out var traceId))
|
||||
{
|
||||
@@ -64,7 +67,7 @@ public sealed class CorrelationIdMiddleware
|
||||
return requestId;
|
||||
}
|
||||
|
||||
return Guid.NewGuid().ToString("N");
|
||||
return _idGenerator.NextId().ToString();
|
||||
}
|
||||
|
||||
private static bool TryGetHeader(HttpContext context, string headerName, out string value)
|
||||
|
||||
@@ -9,20 +9,20 @@ namespace TakeoutSaaS.Shared.Web.Security;
|
||||
public static class ClaimsPrincipalExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取当前用户 Id(不存在时返回 Guid.Empty)
|
||||
/// 获取当前用户 Id(不存在时返回 0)。
|
||||
/// </summary>
|
||||
public static Guid GetUserId(this ClaimsPrincipal? principal)
|
||||
public static long GetUserId(this ClaimsPrincipal? principal)
|
||||
{
|
||||
if (principal == null)
|
||||
{
|
||||
return Guid.Empty;
|
||||
return 0;
|
||||
}
|
||||
|
||||
var identifier = principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? principal.FindFirstValue("sub");
|
||||
|
||||
return Guid.TryParse(identifier, out var userId)
|
||||
return long.TryParse(identifier, out var userId)
|
||||
? userId
|
||||
: Guid.Empty;
|
||||
: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,23 +20,23 @@ public sealed class HttpContextCurrentUserAccessor : ICurrentUserAccessor
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Guid UserId
|
||||
public long UserId
|
||||
{
|
||||
get
|
||||
{
|
||||
var principal = _httpContextAccessor.HttpContext?.User;
|
||||
if (principal == null || !principal.Identity?.IsAuthenticated == true)
|
||||
{
|
||||
return Guid.Empty;
|
||||
return 0;
|
||||
}
|
||||
|
||||
var identifier = principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? principal.FindFirstValue("sub");
|
||||
|
||||
return Guid.TryParse(identifier, out var id) ? id : Guid.Empty;
|
||||
return long.TryParse(identifier, out var id) ? id : 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsAuthenticated => UserId != Guid.Empty;
|
||||
public bool IsAuthenticated => UserId != 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user