using static Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress.MdpRelationalSource; using Microsoft.Extensions.DependencyInjection; using System.Globalization; using System.Security.Cryptography; using System.Text; using System.Text.Json; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors; using Admin.NET.Plugin.AiDOP.Entity.DataPlatform; namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress; public sealed partial class MdpIngestDatasetController { [HttpGet("{id:long}/relational-sync")] public Task ListRelationalSync(long id, long? factoryId, CancellationToken token) => WithScope(factoryId, async scope => { if (!await Can(TrialPermission)) return Forbid(); var dataset = await Datasets(scope).FirstAsync(d => d.Id == id); if (dataset is null) return NotFound(); var profiles = await TrialProfiles(scope, id, token); if (!profiles.Any(p => p.SyncEnabled)) return Ok(Array.Empty()); try { var sync = HttpContext.RequestServices.GetRequiredService(); return Ok(await sync.ListAsync(scope.TenantId, scope.FactoryId, id)); } catch (MySqlConnector.MySqlException) { return StatusCode(503, new { message = "请核对数据库同步存储是否已部署" }); } }); [HttpPost("{id:long}/relational-sync/{profileId}/start")] public Task StartRelationalSync(long id, string profileId, long? factoryId, string? mode, CancellationToken token) => ExecuteRelationalSync(id, profileId, null, factoryId, mode ?? "FULL", token); [HttpPost("{id:long}/relational-sync/{profileId}/{runId:guid}/advance")] public Task AdvanceRelationalSync(long id, string profileId, Guid runId, long? factoryId, CancellationToken token) => ExecuteRelationalSync(id, profileId, runId, factoryId, "RESUME", token); [HttpPost("{id:long}/relational-sync/{profileId}/{runId:guid}/check-archived")] public Task CheckArchivedRelationalSync(long id, string profileId, Guid runId, long? factoryId, CancellationToken token) => WithScope(factoryId, async scope => { if (!await Can(TrialPermission) || !await Can(PublishPermission)) return Forbid(); var dataset = await Datasets(scope).FirstAsync(d => d.Id == id); if (dataset is null) return NotFound(); using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token); deadline.CancelAfter(TimeSpan.FromSeconds(75)); try { var sync = HttpContext.RequestServices.GetRequiredService(); return Ok(await sync.CheckArchivedRunAsync(dataset, profileId, runId, user.UserId, deadline.Token)); } catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { message = RelationalRecoveryMessage(error.Code), code = error.Code }); } catch (OperationCanceledException) when (!token.IsCancellationRequested) { return StatusCode(504, new { message = "旧批次核对超时,可继续核对" }); } catch (Exception error) when (error is System.Data.Common.DbException or SqlSugarException or JsonException or ConnectorContractException or ArgumentException or InvalidOperationException) { return StatusCode(503, new { message = "请核对旧窗口的归档版本、冻结批次和 Raw 回执" }); } }); private static string RelationalRecoveryMessage(string code) => code switch { "RELATIONAL_REINITIALIZATION_REQUIRED" => "来源历史已过期,原水位已保留,请重新全量初始化", "RELATIONAL_PLAN_CHANGED" => "配置已变更,请核对旧批次或恢复原配置后继续", "RELATIONAL_ARCHIVE_UNAVAILABLE" => "无法取得旧窗口的归档映射", "RELATIONAL_ARCHIVE_CHECK_UNAVAILABLE" => "当前窗口没有待核对的旧配置", "RELATIONAL_RUN_BUSY" => "另一实例正在处理该窗口,请稍后再继续", _ => "同步批次尚未取得可推进的回执", }; private Task ExecuteRelationalSync(long id, string profileId, Guid? prior, long? factoryId, string mode, CancellationToken token) => WithScope(factoryId, async scope => { if (!await Can(TrialPermission) || !await Can(PublishPermission)) return Forbid(); var dataset = await Datasets(scope).FirstAsync(d => d.Id == id); if (dataset is null) return NotFound(); using var deadline = CancellationTokenSource.CreateLinkedTokenSource(token); deadline.CancelAfter(TimeSpan.FromSeconds(75)); var work = deadline.Token; try { var profile = (await TrialProfiles(scope, id, work)).SingleOrDefault(p => p.Id == profileId && p.SyncEnabled); if (profile is null) return NotFound(); if (!prior.HasValue && mode is not ("FULL" or "INCR" or "REPLAY")) return BadRequest(new { message = "请选择全量、增量或回看窗口" }); var source = await BoundSource(dataset, profile.SourceId); if (source is null) return Conflict(new { message = "请先启用已审批且绑定来源的接收版本" }); var sync = HttpContext.RequestServices.GetRequiredService(); return Ok(await sync.RunAsync(profile, prior, mode, work)); } catch (OperationCanceledException) when (!token.IsCancellationRequested) { return StatusCode(504, new { message = "同步操作超时。已冻结的批次可从任务记录继续" }); } catch (MdpIngestHttpException error) { return StatusCode(error.StatusCode, new { message = RelationalRecoveryMessage(error.Code), code = error.Code }); } catch (ConnectorContractException error) { return Conflict(new { message = "数据库同步未通过检查", code = error.Code.ToString() }); } catch (MySqlConnector.MySqlException error) when (error.Number == 1062) { return Conflict(new { message = "当前来源已有未结束的同步运行,继续已有运行即可" }); } catch (Exception error) when (error is System.Data.Common.DbException or IOException or JsonException or HttpRequestException or ArgumentException or UnauthorizedAccessException or InvalidOperationException or NotSupportedException) { return StatusCode(503, new { message = "请核对同步存储、只读快照能力和接收配置" }); } }); }