| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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<IActionResult> 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<object>());
- try
- {
- var sync = HttpContext.RequestServices.GetRequiredService<MdpRelationalSyncService>();
- 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<IActionResult> 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<IActionResult> 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<IActionResult> 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<MdpRelationalSyncService>();
- 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<IActionResult> 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<MdpRelationalSyncService>();
- 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 = "请核对同步存储、只读快照能力和接收配置" });
- }
- });
- }
|