浏览代码

fix(data-platform): 修复 Edge 心跳和数据库删除标记兼容性(server 1.0.630)

心跳按 Edge 协议序列化租期并保留无任务 204。
数据库删除标记兼容 sbyte 和 decimal 的精确 0/1,补齐双路径回归。
YY968XX 1 天之前
父节点
当前提交
760246a3a3

+ 3 - 3
server/Admin.NET.Web.Entry/Admin.NET.Web.Entry.csproj

@@ -11,9 +11,9 @@
     <GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>
     <Copyright>Admin.NET</Copyright>
     <Description>Admin.NET 通用权限开发平台</Description>
-    <AssemblyVersion>1.0.629</AssemblyVersion>
-    <FileVersion>1.0.629</FileVersion>
-    <Version>1.0.629</Version>
+    <AssemblyVersion>1.0.630</AssemblyVersion>
+    <FileVersion>1.0.630</FileVersion>
+    <Version>1.0.630</Version>
   </PropertyGroup>
 
   <ItemGroup>

+ 9 - 0
server/Plugins/Admin.NET.Plugin.AiDOP.DataPlatform.Core/Connectors/RelationalFieldValues.cs

@@ -28,6 +28,15 @@ public static partial class RelationalFieldValues
         return new DateTimeOffset(wallTime, sourceZone.GetUtcOffset(wallTime)).ToUniversalTime();
     }
 
+    public static bool ReadDeleteMarker(object? value) => value switch
+    {
+        bool flag => flag,
+        sbyte or byte or short or int or long when Convert.ToInt64(value, CultureInfo.InvariantCulture) is 0 or 1
+            => Convert.ToInt64(value, CultureInfo.InvariantCulture) == 1,
+        decimal number when number is 0m or 1m => number == 1m,
+        _ => throw new ConnectorContractException(ConnectorError.InvalidBatch),
+    };
+
     public static decimal DecimalValue(string value, int precision, int scale)
     {
         if (precision is < 1 or > 28 || scale < 0 || scale > precision)

+ 1 - 10
server/Plugins/Admin.NET.Plugin.AiDOP.DataPlatform.Relational/NativeReadSession.cs

@@ -215,16 +215,7 @@ internal sealed partial class NativeReadSession(DbConnection connection, Relatio
                     throw new ConnectorContractException(ConnectorError.InvalidBatch);
                 var isDeleted = false;
                 if (mapping.DeleteColumn is { } deletedColumn)
-                {
-                    var value = reader.GetValue(ColumnPosition(deletedColumn));
-                    isDeleted = value switch
-                    {
-                        bool flag => flag,
-                        byte or short or int or long when Convert.ToInt64(value, CultureInfo.InvariantCulture) is 0 or 1
-                            => Convert.ToInt64(value, CultureInfo.InvariantCulture) == 1,
-                        _ => throw new ConnectorContractException(ConnectorError.InvalidBatch),
-                    };
-                }
+                    isDeleted = RelationalFieldValues.ReadDeleteMarker(reader.GetValue(ColumnPosition(deletedColumn)));
                 json.WriteNumber("_mdpChangeVersion", version);
                 json.WriteBoolean("_mdpDeleted", isDeleted);
             }

+ 47 - 0
server/Plugins/Admin.NET.Plugin.AiDOP.Tests/DataPlatform/MdpEdgeReceiptTests.cs

@@ -1,11 +1,58 @@
+using System.Text.Json;
 using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
 using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors;
+using Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Abstractions;
+using Microsoft.AspNetCore.Routing;
+using Microsoft.Extensions.DependencyInjection;
 using Xunit;
 
 namespace Admin.NET.Plugin.AiDOP.Tests.DataPlatform;
 
 public sealed class MdpEdgeReceiptTests
 {
+    [Fact]
+    public async Task HeartbeatTaskCanBeDeserializedByTheEdgeClient()
+    {
+        var lease = new DateTimeOffset(2026, 12, 2, 7, 25, 54, TimeSpan.Zero);
+        var task = new EdgeTask("run-A", EdgeProtocol.ApiPull, 123456789012345678L, 2, 3, 4,
+            "S", "test", "schema", "config", "page-2", "READY", LeaseUntil: lease);
+
+        var result = await RenderHeartbeatAsync(task);
+
+        Assert.Equal(StatusCodes.Status200OK, result.StatusCode);
+        Assert.StartsWith("application/json", result.ContentType);
+        var received = JsonSerializer.Deserialize<EdgeTask>(result.Body, EdgeProtocol.Json);
+        Assert.Equal(task, received);
+        Assert.Equal(lease.Offset, received.LeaseUntil.Value.Offset);
+    }
+
+    [Fact]
+    public async Task HeartbeatWithoutATaskRemainsNoContent()
+    {
+        var result = await RenderHeartbeatAsync(null);
+
+        Assert.Equal(StatusCodes.Status204NoContent, result.StatusCode);
+        Assert.Empty(result.Body);
+    }
+
+    private static async Task<(int StatusCode, string ContentType, byte[] Body)> RenderHeartbeatAsync(EdgeTask task)
+    {
+        var builder = WebApplication.CreateBuilder();
+        builder.Services.AddControllers().AddNewtonsoftJson(options =>
+            options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss");
+        await using var app = builder.Build();
+        await using var body = new MemoryStream();
+        var http = new DefaultHttpContext { RequestServices = app.Services };
+        http.Response.Body = body;
+        var action = new ActionContext(http, new RouteData(), new ActionDescriptor());
+        await MdpEdgeDeviceController.HeartbeatResult(task).ExecuteResultAsync(action);
+        return (http.Response.StatusCode, http.Response.ContentType, body.ToArray());
+    }
+
     [Fact]
     public void OnlyMatchingCloudReceiptCanCommitPosition()
     {

+ 30 - 0
server/Plugins/Admin.NET.Plugin.AiDOP.Tests/DataPlatform/MdpRelationalValueTests.cs

@@ -6,6 +6,36 @@ namespace Admin.NET.Plugin.AiDOP.Tests.DataPlatform;
 
 public sealed class MdpRelationalValueTests
 {
+    [Fact]
+    public void MySqlTinyIntDeleteMarkerUsesSignedBytes()
+    {
+        Assert.False(RelationalFieldValues.ReadDeleteMarker((sbyte)0));
+        Assert.True(RelationalFieldValues.ReadDeleteMarker((sbyte)1));
+    }
+
+    [Fact]
+    public void OracleNumberDeleteMarkerUsesDecimals()
+    {
+        Assert.False(RelationalFieldValues.ReadDeleteMarker(0m));
+        Assert.True(RelationalFieldValues.ReadDeleteMarker(1m));
+    }
+
+    [Fact]
+    public void DeleteMarkerKeepsBinarySemanticsAcrossProviders()
+    {
+        Assert.False(RelationalFieldValues.ReadDeleteMarker(false));
+        Assert.True(RelationalFieldValues.ReadDeleteMarker(true));
+        Assert.False(RelationalFieldValues.ReadDeleteMarker((byte)0));
+        Assert.True(RelationalFieldValues.ReadDeleteMarker((short)1));
+        Assert.False(RelationalFieldValues.ReadDeleteMarker(0));
+        Assert.True(RelationalFieldValues.ReadDeleteMarker(1L));
+        foreach (var invalid in new object[] { DBNull.Value, "1", (sbyte)-1, 2, 0.5m, 1.001m, 2m })
+        {
+            var error = Assert.Throws<ConnectorContractException>(() => RelationalFieldValues.ReadDeleteMarker(invalid));
+            Assert.Equal(ConnectorError.InvalidBatch, error.Code);
+        }
+    }
+
     [Fact]
     public void RejectsInvalidTextAndAmbiguousOrMissingLocalTime()
     {

+ 15 - 1
server/Plugins/Admin.NET.Plugin.AiDOP/DataPlatform/UnifiedIngress/MdpEdgeDeviceController.cs

@@ -1,3 +1,4 @@
+using System.Text.Json;
 using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
 using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
 using Microsoft.AspNetCore.Authorization;
@@ -26,7 +27,7 @@ public sealed class MdpEdgeDeviceController(MdpEdgeDeviceService devices) : Cont
 
     [HttpPost("heartbeat"), RequestSizeLimit(4096)]
     public Task<IActionResult> Poll(EdgeReport report, CancellationToken token) =>
-        Execute(async () => Ok(await devices.PollAsync(await DeviceCertificate(token), report)));
+        Execute(async () => HeartbeatResult(await devices.PollAsync(await DeviceCertificate(token), report)));
 
     [HttpGet("batches/{id:guid}")]
     public Task<IActionResult> Status(Guid id, CancellationToken token) => Execute(async () =>
@@ -39,6 +40,19 @@ public sealed class MdpEdgeDeviceController(MdpEdgeDeviceService devices) : Cont
     public Task<IActionResult> Upload(EdgeBatch batch, CancellationToken token) =>
         Execute(async () => Ok(await devices.ReceiveAsync(await DeviceCertificate(token), batch, token)));
 
+    internal static IActionResult HeartbeatResult(EdgeTask? task)
+    {
+        if (task is null)
+            return new NoContentResult();
+
+        return new ContentResult
+        {
+            Content = JsonSerializer.Serialize(task, EdgeProtocol.Json),
+            ContentType = "application/json",
+            StatusCode = StatusCodes.Status200OK,
+        };
+    }
+
     private async Task<System.Security.Cryptography.X509Certificates.X509Certificate2> DeviceCertificate(CancellationToken token) =>
         await HttpContext.Connection.GetClientCertificateAsync(token) ?? throw new EdgeContractException("EDGE_DEVICE_FORBIDDEN");