From 8d32cdfbd6cc904f7033f4cfba9619ed2364734f Mon Sep 17 00:00:00 2001 From: codex Date: Wed, 28 Oct 2026 06:52:04 +0800 Subject: [PATCH] refactor: centralize migration workflow state --- docs/PORTABLE_VM_BACKUPS.md | 2 + docs/VALIDATION.md | 1 + web/app/Services/Cloud/CloudBackupService.php | 16 +- .../Cloud/CloudInstanceMigrationService.php | 535 +++++---------- .../CloudInstanceMigrationSettlement.php | 248 +++++++ .../Cloud/CloudInstanceMigrationWorkflow.php | 612 ++++++++++++++++++ web/public/cloud/admin-instances.js | 86 ++- web/resources/views/venix/layout_assets.tpl | 2 +- .../CloudInstanceMigrationWorkflowTest.php | 185 ++++++ ...CloudMaintenanceMigrationFinalizerTest.php | 22 +- ...CloudPortableBackupAdminUiContractTest.php | 31 +- web/tests/LegalPagesContractTest.php | 2 +- web/tests/PublicPageLocaleContractTest.php | 2 +- web/tests/SiteBrandContractTest.php | 2 +- 14 files changed, 1310 insertions(+), 436 deletions(-) create mode 100644 web/app/Services/Cloud/CloudInstanceMigrationSettlement.php create mode 100644 web/app/Services/Cloud/CloudInstanceMigrationWorkflow.php create mode 100644 web/tests/CloudInstanceMigrationWorkflowTest.php diff --git a/docs/PORTABLE_VM_BACKUPS.md b/docs/PORTABLE_VM_BACKUPS.md index 1041e679..402ec89c 100644 --- a/docs/PORTABLE_VM_BACKUPS.md +++ b/docs/PORTABLE_VM_BACKUPS.md @@ -62,6 +62,8 @@ 任何派发后的超时、断连或回执丢失都进入原 operation 的只读核对,系统不会重放源停机、目标创建或源删除,也不会提前释放资源。传输明确失败且目标创建被证明没有发生时,管理员可以“重试传输”并从已有分块继续,或“中止传输”恢复源机原电源/autostart 状态并释放目标预留。防火墙、目标激活和源回滚的确定失败分别提供同阶段重试;源清理失败回到 `source_retained`,允许再次显式清理。只有不确定效果仍由任务安全核对栅栏阻止重放,因此业务状态不会依靠一个无出口的永久软件锁。 +迁移控制面以一份可执行阶段注册表定义源准备、传输、账本切换、防火墙、激活、源清理和回滚。任务字段、完成处理、不确定结果的核对动作、人工恢复动作和管理员页面进度都从该注册表生成;恢复接口在数据库锁内重新使用同一判定,不能只信任页面按钮。资源预留、订单归属和公网 IP 的最终结算由独立模块执行,只有阶段结果得到明确证明后才能调用。后台显示持久化的阶段步骤及完成、跳过、待处理状态;当前 PeerMigration V3 没有持久化字节遥测,因此传输阶段不显示伪造的百分比。 + PeerMigration 不创建 `vm-migrations/` 对象,也不持有 S3 凭据。目标成功导入后立即删除本地分块暂存;中断或主机崩溃留下的暂存目录按票据最长寿命进行有界过期回收。普通云备份继续使用 `vm-backups/` 且不携带快照,二者的目录、保留和删除规则互不复用。 ## 失败与不确定效果 diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index d655f656..aa24f8e8 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -133,6 +133,7 @@ jq empty deploy/*.json.example - `PeerMigration V3` 必须验证当前全部快照都进入同一不可变清单、任一快照缺失或回执漂移即拒绝切换、目标恢复后快照目录整体改绑。源 Agent 自动生成持久 TLS 1.3 身份并发布本机地址目录,目标必须精确锁定源证书 SHA-256 并携带一次性 Bearer 票据;公开清单、任务结果和浏览器响应均不得出现宿主机路径、私钥或票据。目标必须优先尝试同网段地址并可回退公网地址,按固定 8 MiB Range 分块续传、复用合法 `.partial` 并在导入前核对每个完整 SHA-256。 - 目标分块暂存必须由 Agent 派生到第一个 `storage_path_roots` 下的 `.peer-migration-staging`,不能落在 Journal 系统盘。故障注入分块断连后应从已有偏移继续;成功导入删除暂存,过期中断会话只能由有界本地清理回收,不能删除不匹配目录。 - 目标在恢复和用户防火墙收敛期间必须保持关机;源机的运行/关机、autostart、断网状态与修改后主机名必须在独立激活步骤后保持一致。传输确定失败须同时提供续传和安全回滚,防火墙、激活、回滚、源清理的确定失败均有显式恢复出口。若源准备已存在精确成功回执但 PHP 最终化失败,只能将原准备效果收敛为 `applied` 后创建新的补偿回滚任务;Agent 明确确认源机恢复前不得释放目标预留或 IP。未知效果只能核对原 operation,不能重放。普通云备份仍只能产生单系统盘 S3 对象,PeerMigration 不得创建 S3 迁移对象。 +- 维护迁移阶段注册表必须通过完整性测试:每个 Agent 任务种类只能对应一个账本字段、完成处理和不确定结果核对动作,每个人工恢复动作只能对应一个执行入口。页面必须直接使用后端返回的恢复选项和阶段进度;接口在加锁后使用同一判定重新授权。资源结算模块只能在源删除或目标接管得到明确证明后移动订单预留和公网 IP。没有持久化字节遥测时只能显示阶段进度,不能合成传输百分比。 - 独立恢复可选择原宿主机或其他合格宿主机;无论目标是否为原宿主机,都必须单独校验并预留容量与全新公网 IP。目标使用 `bridge:br0`;校验下载对象字节数与 SHA-256,新实例 UUID、MAC、seed、NVRAM、网络过滤器和流量状态均不得复用源实例,且不得继承订单、账单、服务期、到期时间或流量周期。 - 对备份/恢复断连、超时和回执丢失执行故障注入:同一 Agent operation ID 只能查询收敛,不能再次派发。恢复失败只有在全部托管产物明确不存在时才能释放容量与 IP。对象删除响应未知时不得重放原 DELETE;必须用两次间隔 HEAD 证明同一大小、ETag 和 `version_id` 仍存在,才允许创建新的删除代次。验证每轮最多三次、超限转人工、手动重试重新建立稳定证据、精确版本删除、清理债务不占保留份数,并确认大量异常债务不会饿死新的保留清理。 diff --git a/web/app/Services/Cloud/CloudBackupService.php b/web/app/Services/Cloud/CloudBackupService.php index d7faf3d7..f5b8eb9d 100644 --- a/web/app/Services/Cloud/CloudBackupService.php +++ b/web/app/Services/Cloud/CloudBackupService.php @@ -268,13 +268,10 @@ final class CloudBackupService ) ) ) : []; - $migrationBlocks = $migration !== null && in_array( - (string) $migration->state, - ['preparing_source', 'transferring', 'cutover', - 'firewall_running', 'activation_running', 'rollback_running', - 'cleanup_running', 'attention_required'], - true - ); + $migrationBlocks = $migration !== null + && CloudInstanceMigrationWorkflow::blocksInstance( + (string) $migration->state + ); $canOperate = $canManage && $active === null && !$migrationBlocks @@ -283,9 +280,10 @@ final class CloudBackupService && (bool) ($storage['ready'] ?? false); $canMigrate = $canManage && $active === null - && !$migrationBlocks && !($migration !== null - && (string) $migration->state === 'source_retained') + && CloudInstanceMigrationWorkflow::blocksNewMigration( + (string) $migration->state + )) && (string) $instance->status === 'active' && $supported && $migrationTargets !== []; diff --git a/web/app/Services/Cloud/CloudInstanceMigrationService.php b/web/app/Services/Cloud/CloudInstanceMigrationService.php index 2cd6e4ea..3ef54d3d 100644 --- a/web/app/Services/Cloud/CloudInstanceMigrationService.php +++ b/web/app/Services/Cloud/CloudInstanceMigrationService.php @@ -8,7 +8,6 @@ use App\Models\CloudInstance; use App\Models\CloudInstanceFirewall; use App\Models\CloudInstanceMigration; use App\Models\CloudJob; -use App\Models\CloudOrder; use App\Models\CloudPublicIp; use App\Models\CloudResourceReservation; @@ -19,12 +18,6 @@ final class CloudInstanceMigrationService private const PROTOCOL_VERSION = 3; private const CHUNK_SIZE = 8388608; private const TICKET_LIFETIME_SECONDS = 604800; - private const BLOCKING_STATES = [ - 'preparing_source', 'transferring', 'cutover', 'firewall_running', - 'activation_running', 'rollback_running', 'cleanup_running', - 'attention_required', - ]; - /** * Queue one plan-bound cleanup for residue left by an interrupted target * receive. The original receive remains fenced and is never replayed; its @@ -422,9 +415,10 @@ final class CloudInstanceMigrationService } $query = CloudInstanceMigration::where( 'instance_id', (int) $instance->id - )->whereIn('state', array_merge(self::BLOCKING_STATES, [ - 'source_retained', - ])); + )->whereIn('state', array_merge( + CloudInstanceMigrationWorkflow::blockingStates(), + ['source_retained'] + )); if ($excludeMigrationId !== null) { $query->where('id', '<>', $excludeMigrationId); } @@ -453,31 +447,30 @@ final class CloudInstanceMigrationService { $state = (string) $migration->state; $action = (string) $migration->recovery_action; - $recoveryActions = self::recoveryActions($migration); - return [ + $hasAbortEvidence = self::hasAbortPrepareEvidence($migration); + $recoveryOptions = CloudInstanceMigrationWorkflow::recoveryOptions( + $migration, + $hasAbortEvidence + ); + $display = [ 'id' => (string) $migration->id, 'state' => $state, 'cleanup_mode' => (string) $migration->cleanup_mode, 'source_host_id' => (string) $migration->source_host_id, 'target_host_id' => (string) $migration->target_host_id, - 'prepare_task_id' => $migration->source_prepare_job_id === null - ? null : (string) $migration->source_prepare_job_id, - 'transfer_task_id' => $migration->target_receive_job_id === null - ? null : (string) $migration->target_receive_job_id, - 'cleanup_task_id' => $migration->cleanup_job_id === null - ? null : (string) $migration->cleanup_job_id, - 'firewall_task_id' => $migration->firewall_job_id === null - ? null : (string) $migration->firewall_job_id, - 'activation_task_id' => $migration->activate_job_id === null - ? null : (string) $migration->activate_job_id, - 'rollback_task_id' => $migration->rollback_job_id === null - ? null : (string) $migration->rollback_job_id, 'source_retained' => $state === 'source_retained', 'can_cleanup' => $state === 'source_retained' && $action !== 'reconcile_cleanup', 'recovery_action' => $action === '' ? null : $action, - 'can_recover' => $recoveryActions !== [], - 'recovery_actions' => $recoveryActions, + 'can_recover' => $recoveryOptions !== [], + 'recovery_actions' => array_column( + $recoveryOptions, + 'action' + ), + 'recovery_options' => $recoveryOptions, + 'progress' => CloudInstanceMigrationWorkflow::progress( + $migration + ), 'error_code' => (string) $migration->error_code, 'cutover_at' => $migration->cutover_at === null ? null : (int) $migration->cutover_at, @@ -486,27 +479,16 @@ final class CloudInstanceMigrationService 'created_at' => (int) $migration->created_at, 'updated_at' => (int) $migration->updated_at, ]; - } - - /** @return list */ - private static function recoveryActions( - CloudInstanceMigration $migration - ): array { - if ((string) $migration->state !== 'attention_required') { - return []; - } - $action = (string) $migration->recovery_action; - if ($action === 'retry_transfer') { - return ['retry_transfer', 'abort_transfer']; - } - if (str_starts_with($action, 'retry_')) { - return [$action]; - } - if (in_array($action, ['reconcile_prepare', 'abort_prepare'], true) - && self::hasAbortPrepareEvidence($migration)) { - return ['abort_prepare']; + foreach (CloudInstanceMigrationWorkflow::stages() as $stage) { + if ($stage['task_key'] === null) { + continue; + } + $field = (string) $stage['job_field']; + $display[(string) $stage['task_key']] = + $migration->{$field} === null + ? null : (string) $migration->{$field}; } - return []; + return $display; } private static function hasAbortPrepareEvidence( @@ -620,7 +602,10 @@ final class CloudInstanceMigrationService return $instanceId > 0 && self::catalogAvailable() && CloudInstanceMigration::where('instance_id', $instanceId) - ->whereIn('state', self::BLOCKING_STATES)->exists(); + ->whereIn( + 'state', + CloudInstanceMigrationWorkflow::blockingStates() + )->exists(); } public static function assertInstanceMutationAvailable( @@ -654,43 +639,18 @@ final class CloudInstanceMigrationService if (!self::catalogAvailable()) { return; } - $field = match ((string) $job->kind) { - 'instance_migration_prepare' => 'source_prepare_job_id', - 'instance_migration_transfer' => 'target_receive_job_id', - 'firewall_apply' => 'firewall_job_id', - 'instance_migration_activate' => 'activate_job_id', - 'instance_migration_rollback' => 'rollback_job_id', - 'instance_migration_cleanup' => 'cleanup_job_id', - default => null, - }; - if ($field === null) { + $stage = CloudInstanceMigrationWorkflow::job((string) $job->kind); + if ($stage === null) { return; } + $field = (string) $stage['job_field']; $migration = CloudInstanceMigration::where($field, (int) $job->id) ->lockForUpdate()->first(); if ($migration === null) { return; } - match ((string) $job->kind) { - 'instance_migration_prepare' => self::finishPrepareLocked( - $migration, $job, $status, $result, $payload - ), - 'instance_migration_transfer' => self::finishTransferLocked( - $migration, $job, $status, $result, $payload - ), - 'firewall_apply' => self::finishFirewallLocked( - $migration, $job, $status - ), - 'instance_migration_activate' => self::finishActivationLocked( - $migration, $job, $status, $result - ), - 'instance_migration_rollback' => self::finishRollbackLocked( - $migration, $job, $status, $result - ), - 'instance_migration_cleanup' => self::finishCleanupLocked( - $migration, $job, $status, $result - ), - }; + $handler = (string) $stage['finish_handler']; + self::{$handler}($migration, $job, $status, $result, $payload); } public static function markJobUncertain(CloudJob $job): void @@ -698,37 +658,23 @@ final class CloudInstanceMigrationService if (!self::catalogAvailable()) { return; } - $field = match ((string) $job->kind) { - 'instance_migration_prepare' => 'source_prepare_job_id', - 'instance_migration_transfer' => 'target_receive_job_id', - 'firewall_apply' => 'firewall_job_id', - 'instance_migration_activate' => 'activate_job_id', - 'instance_migration_rollback' => 'rollback_job_id', - 'instance_migration_cleanup' => 'cleanup_job_id', - default => null, - }; - if ($field === null) { + $stage = CloudInstanceMigrationWorkflow::job((string) $job->kind); + if ($stage === null) { return; } + $field = (string) $stage['job_field']; $migration = CloudInstanceMigration::where($field, (int) $job->id) ->lockForUpdate()->first(); if ($migration === null) { return; } - $action = match ((string) $job->kind) { - 'instance_migration_prepare' => 'reconcile_prepare', - 'instance_migration_transfer' => 'reconcile_transfer', - 'firewall_apply' => 'reconcile_firewall', - 'instance_migration_activate' => 'reconcile_activation', - 'instance_migration_rollback' => 'reconcile_rollback', - 'instance_migration_cleanup' => 'reconcile_cleanup', - }; + $action = (string) $stage['reconcile_action']; if ((string) $job->kind === 'instance_migration_prepare' && (string) $migration->recovery_action === 'abort_prepare') { $action = 'abort_prepare'; } - if ((string) $job->kind === 'instance_migration_cleanup') { - self::settleRetainedPlacementLocked($migration); + if ((string) $stage['key'] === 'cleanup') { + CloudInstanceMigrationSettlement::retainSourceLocked($migration); self::setState($migration, 'source_retained', 'finalization_incident', $action); return; @@ -746,15 +692,12 @@ final class CloudInstanceMigrationService CloudInput::only($input, ['action', 'confirm_recovery']); $action = $input['action'] ?? null; if (!is_string($action) - || !in_array($action, [ - 'abort_prepare', 'retry_transfer', 'abort_transfer', - 'retry_firewall', 'retry_activation', 'retry_rollback', - ], true) + || !CloudInstanceMigrationWorkflow::isRecoveryAction($action) || ($input['confirm_recovery'] ?? false) !== true) { throw new CloudError('请选择并确认可用的迁移恢复操作'); } if ($action === 'abort_prepare') { - return self::abortPrepare($instance); + return self::recoverAbortPrepare($instance); } return $instance->getConnection()->transaction(function () use ( $instance, @@ -767,41 +710,39 @@ final class CloudInstanceMigrationService 'instance_id', (int) $locked->id )->where('state', 'attention_required')->orderByDesc('id') ->lockForUpdate()->first(); - $recorded = $migration === null - ? '' : (string) $migration->recovery_action; if ($migration === null - || ($action === 'abort_transfer' - ? $recorded !== 'retry_transfer' - : !hash_equals($recorded, $action))) { + || !in_array( + $action, + CloudInstanceMigrationWorkflow::recoveryActions( + $migration, + false + ), + true + )) { throw new CloudError('当前迁移没有该恢复出口', 409); } + $handler = CloudInstanceMigrationWorkflow::recoveryHandler( + $action + ); + if ($handler === null || !method_exists(self::class, $handler)) { + throw new \LogicException( + '迁移恢复动作缺少可执行处理方法' + ); + } $migration->retry_generation = (int) $migration->retry_generation + 1; $migration->updated_at = time(); $migration->save(); - return match ($action) { - 'retry_transfer' => self::retryTransferLocked( - $migration, $locked - ), - 'abort_transfer' => self::queueRollbackLocked( - $migration, - $locked, - (int) $actor['user_id'], - 'migration_transfer_aborted' - ), - 'retry_firewall' => self::retryFirewallLocked( - $migration, $locked, (int) $actor['user_id'] - ), - 'retry_activation' => self::retryActivationLocked( - $migration, $locked, (int) $actor['user_id'] - ), - 'retry_rollback' => self::retryRollbackLocked( - $migration, $locked, (int) $actor['user_id'] - ), - }; + return self::{$handler}( + $migration, + $locked, + (int) $actor['user_id'] + ); }, 3); } - private static function abortPrepare(CloudInstance $instance): CloudJob + private static function recoverAbortPrepare( + CloudInstance $instance + ): CloudJob { $candidate = CloudInstanceMigration::where( 'instance_id', (int) $instance->id @@ -822,6 +763,16 @@ final class CloudInstanceMigrationService throw new CloudError('当前没有可中止的迁移源准备任务', 409); } $evidence = self::abortPrepareEvidence($candidate); + if (!in_array( + 'abort_prepare', + CloudInstanceMigrationWorkflow::recoveryActions( + $candidate, + true + ), + true + )) { + throw new CloudError('当前迁移没有该恢复出口', 409); + } $migrationId = (int) $candidate->id; $prepareJobId = (int) $evidence['job']->id; $eventId = (string) $evidence['event']->event_id; @@ -1500,6 +1451,28 @@ final class CloudInstanceMigrationService return $job; } + private static function recoverRetryTransferLocked( + CloudInstanceMigration $migration, + CloudInstance $instance, + int $actorUserId + ): CloudJob { + unset($actorUserId); + return self::retryTransferLocked($migration, $instance); + } + + private static function recoverAbortTransferLocked( + CloudInstanceMigration $migration, + CloudInstance $instance, + int $actorUserId + ): CloudJob { + return self::queueRollbackLocked( + $migration, + $instance, + $actorUserId, + 'migration_transfer_aborted' + ); + } + /** * A confirmed not-applied transfer may be retried only under a fresh * Agent operation ID. Rotate the restore, reservation and IP ledgers as @@ -1629,12 +1602,26 @@ final class CloudInstanceMigrationService ); } + private static function recoverRetryRollbackLocked( + CloudInstanceMigration $migration, + CloudInstance $instance, + int $actorUserId + ): CloudJob { + return self::retryRollbackLocked( + $migration, + $instance, + $actorUserId + ); + } + private static function finishRollbackLocked( CloudInstanceMigration $migration, CloudJob $job, string $status, - $result + $result, + array $payload ): void { + unset($payload); $instance = CloudInstance::where( 'id', (int) $migration->instance_id )->lockForUpdate()->first(); @@ -1686,8 +1673,11 @@ final class CloudInstanceMigrationService private static function finishFirewallLocked( CloudInstanceMigration $migration, CloudJob $job, - string $status + string $status, + $result, + array $payload ): void { + unset($result, $payload); $instance = CloudInstance::where( 'id', (int) $migration->instance_id )->lockForUpdate()->first(); @@ -1765,6 +1755,18 @@ final class CloudInstanceMigrationService return $job; } + private static function recoverRetryFirewallLocked( + CloudInstanceMigration $migration, + CloudInstance $instance, + int $actorUserId + ): CloudJob { + return self::retryFirewallLocked( + $migration, + $instance, + $actorUserId + ); + } + private static function advanceAfterTargetReadyLocked( CloudInstanceMigration $migration, CloudInstance $instance, @@ -1841,12 +1843,26 @@ final class CloudInstanceMigrationService ); } + private static function recoverRetryActivationLocked( + CloudInstanceMigration $migration, + CloudInstance $instance, + int $actorUserId + ): CloudJob { + return self::retryActivationLocked( + $migration, + $instance, + $actorUserId + ); + } + private static function finishActivationLocked( CloudInstanceMigration $migration, CloudJob $job, string $status, - $result + $result, + array $payload ): void { + unset($payload); $instance = CloudInstance::where( 'id', (int) $migration->instance_id )->lockForUpdate()->first(); @@ -1905,7 +1921,7 @@ final class CloudInstanceMigrationService ]); return; } - self::settleRetainedPlacementLocked($migration); + CloudInstanceMigrationSettlement::retainSourceLocked($migration); self::setState($migration, 'source_retained', ''); } @@ -1962,8 +1978,10 @@ final class CloudInstanceMigrationService CloudInstanceMigration $migration, CloudJob $job, string $status, - $result + $result, + array $payload ): void { + unset($payload); if ((int) $migration->cleanup_job_id !== (int) $job->id || (int) $job->instance_id !== (int) $migration->instance_id || (int) $job->host_id !== (int) $migration->source_host_id) { @@ -1975,13 +1993,13 @@ final class CloudInstanceMigrationService return; } if ($status === 'uncertain') { - self::settleRetainedPlacementLocked($migration); + CloudInstanceMigrationSettlement::retainSourceLocked($migration); self::setState($migration, 'source_retained', 'operation_effect_unknown', 'reconcile_cleanup'); return; } if ($status === 'failed') { - self::settleRetainedPlacementLocked($migration); + CloudInstanceMigrationSettlement::retainSourceLocked($migration); self::setState($migration, 'source_retained', 'migration_cleanup_failed'); return; @@ -1993,254 +2011,29 @@ final class CloudInstanceMigrationService '源 Agent 未确认实例和托管磁盘均已删除', 502 ); } - if (self::placementAlreadySettled($migration)) { - self::releaseRetainedDebtLocked($migration); + if (CloudInstanceMigrationSettlement::isSettled($migration)) { + CloudInstanceMigrationSettlement::releaseRetainedSourceLocked( + $migration + ); } else { - self::finalizePlacementLocked($migration); + CloudInstanceMigrationSettlement::finalizeSourceDeletionLocked( + $migration + ); } self::setState($migration, 'completed', ''); $migration->completed_at = time(); $migration->save(); } - - /** - * Make the order-linked reservation authoritative on the target while a - * separate restore reservation accounts for the stopped source duplicate. - * This lets the target VM operate normally without releasing source - * capacity or its old address before deletion is proven. - */ - private static function settleRetainedPlacementLocked( - CloudInstanceMigration $migration - ): void { - if (self::placementAlreadySettled($migration)) { - return; - } - $instance = CloudInstance::where( - 'id', (int) $migration->instance_id - )->lockForUpdate()->first(); - $source = CloudResourceReservation::where( - 'id', (int) $migration->source_reservation_id - )->lockForUpdate()->first(); - $target = CloudResourceReservation::where( - 'id', (int) $migration->target_reservation_id - )->lockForUpdate()->first(); - $order = $instance === null || $instance->order_id === null ? null - : CloudOrder::where('id', (int) $instance->order_id) - ->lockForUpdate()->first(); - $sourceIp = $source === null ? null : CloudPublicIp::where( - 'id', (int) $source->public_ip_id - )->lockForUpdate()->first(); - $targetIp = $target === null ? null : CloudPublicIp::where( - 'id', (int) $target->public_ip_id - )->lockForUpdate()->first(); - if ($instance === null || $source === null || $target === null - || $order === null || $sourceIp === null || $targetIp === null - || (int) $instance->host_id !== (int) $migration->target_host_id - || !hash_equals(strtolower((string) $instance->uuid), - strtolower((string) $migration->target_uuid)) - || (int) $instance->reservation_id !== (int) $target->id - || (int) $source->order_id !== (int) $order->id - || $source->backup_restore_id !== null - || (int) $target->backup_restore_id !== (int) $migration->restore_id - || $target->order_id !== null - || (string) $source->state !== 'committed' - || (string) $target->state !== 'committed' - || (int) $sourceIp->reservation_id !== (int) $source->id - || (int) $targetIp->reservation_id !== (int) $target->id) { - throw new CloudRecoveryInstanceStateConflict( - 'PeerMigration 保留源副本时资源账本不一致', 409 - ); - } - $now = time(); - $sourceFields = []; - $targetFields = []; - foreach (['resource_pool_id', 'host_id', 'network_attachment_id', - 'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib', - 'disk_gib'] as $field) { - $sourceFields[$field] = $source->{$field}; - $targetFields[$field] = $target->{$field}; - } - foreach ($targetFields as $field => $value) { - $source->{$field} = $value; - } - $source->instance_id = (int) $instance->id; - $source->updated_at = $now; - $source->save(); - foreach ($sourceFields as $field => $value) { - $target->{$field} = $value; - } - $target->instance_id = null; - $target->updated_at = $now; - $target->save(); - - $targetIp->reservation_id = (int) $source->id; - $targetIp->instance_id = (int) $instance->id; - $targetIp->operation_id = (string) $source->operation_id; - $targetIp->state = 'assigned'; - $targetIp->updated_at = $now; - $targetIp->save(); - $sourceIp->reservation_id = (int) $target->id; - $sourceIp->instance_id = null; - $sourceIp->operation_id = (string) $target->operation_id; - $sourceIp->state = 'assigned'; - $sourceIp->updated_at = $now; - $sourceIp->save(); - - $instance->reservation_id = (int) $source->id; - $instance->resource_pool_id = (int) $source->resource_pool_id; - $instance->updated_at = $now; - $instance->save(); - $order->host_id = (int) $source->host_id; - $order->resource_pool_id = (int) $source->resource_pool_id; - $order->reservation_id = (int) $source->id; - $order->updated_at = $now; - $order->save(); - $oldSourceId = (int) $migration->source_reservation_id; - $migration->source_reservation_id = (int) $target->id; - $migration->target_reservation_id = $oldSourceId; - $migration->updated_at = $now; - $migration->save(); - } - - private static function placementAlreadySettled( - CloudInstanceMigration $migration - ): bool { - $instance = CloudInstance::find((int) $migration->instance_id); - $target = CloudResourceReservation::find( - (int) $migration->target_reservation_id - ); - return $instance !== null && $target !== null - && (int) $instance->reservation_id === (int) $target->id - && $target->order_id !== null - && $target->backup_restore_id === null - && (int) $target->host_id === (int) $migration->target_host_id; - } - - /** Immediate-success path: delete source, then transfer its order row. */ - private static function finalizePlacementLocked( - CloudInstanceMigration $migration - ): void { - $instance = CloudInstance::where( - 'id', (int) $migration->instance_id - )->lockForUpdate()->first(); - $source = CloudResourceReservation::where( - 'id', (int) $migration->source_reservation_id - )->lockForUpdate()->first(); - $target = CloudResourceReservation::where( - 'id', (int) $migration->target_reservation_id - )->lockForUpdate()->first(); - $sourceIp = $source === null ? null : CloudPublicIp::where( - 'id', (int) $source->public_ip_id - )->lockForUpdate()->first(); - $targetIp = $target === null ? null : CloudPublicIp::where( - 'id', (int) $target->public_ip_id - )->lockForUpdate()->first(); - $order = $instance === null || $instance->order_id === null ? null - : CloudOrder::where('id', (int) $instance->order_id) - ->lockForUpdate()->first(); - if ($instance === null || $source === null || $target === null - || $sourceIp === null || $targetIp === null || $order === null - || (int) $instance->host_id !== (int) $migration->target_host_id - || !hash_equals(strtolower((string) $instance->uuid), - strtolower((string) $migration->target_uuid)) - || (int) $instance->reservation_id !== (int) $target->id - || (int) $source->order_id !== (int) $order->id - || (string) $source->state !== 'committed' - || (int) $target->backup_restore_id !== (int) $migration->restore_id - || (string) $target->state !== 'committed' - || (int) $sourceIp->reservation_id !== (int) $source->id - || (int) $targetIp->reservation_id !== (int) $target->id) { - throw new CloudRecoveryInstanceStateConflict( - 'PeerMigration 清理后的资源预留账本不一致', 409 - ); - } - $now = time(); - $sourceIp->state = 'available'; - $sourceIp->reservation_id = null; - $sourceIp->instance_id = null; - $sourceIp->operation_id = null; - $sourceIp->updated_at = $now; - $sourceIp->save(); - foreach (['resource_pool_id', 'host_id', 'network_attachment_id', - 'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib', - 'disk_gib'] as $field) { - $source->{$field} = $target->{$field}; - } - $source->instance_id = (int) $instance->id; - $source->updated_at = $now; - $source->save(); - $targetIp->reservation_id = (int) $source->id; - $targetIp->instance_id = (int) $instance->id; - $targetIp->operation_id = (string) $source->operation_id; - $targetIp->state = 'assigned'; - $targetIp->updated_at = $now; - $targetIp->save(); - $target->instance_id = null; - $target->network_attachment_id = null; - $target->public_ip_pool_id = null; - $target->public_ip_id = null; - $target->state = 'released'; - $target->released_at = $now; - $target->release_reason = 'maintenance_migration_transferred'; - $target->updated_at = $now; - $target->save(); - $instance->reservation_id = (int) $source->id; - $instance->resource_pool_id = (int) $source->resource_pool_id; - $instance->updated_at = $now; - $instance->save(); - $order->host_id = (int) $source->host_id; - $order->resource_pool_id = (int) $source->resource_pool_id; - $order->reservation_id = (int) $source->id; - $order->updated_at = $now; - $order->save(); - $migration->source_reservation_id = (int) $target->id; - $migration->target_reservation_id = (int) $source->id; - $migration->updated_at = $now; - $migration->save(); - } - - private static function releaseRetainedDebtLocked( - CloudInstanceMigration $migration - ): void { - $debt = CloudResourceReservation::where( - 'id', (int) $migration->source_reservation_id - )->lockForUpdate()->first(); - $ip = $debt === null ? null : CloudPublicIp::where( - 'id', (int) $debt->public_ip_id - )->lockForUpdate()->first(); - if ($debt === null || $ip === null - || (int) $debt->host_id !== (int) $migration->source_host_id - || (int) $debt->backup_restore_id !== (int) $migration->restore_id - || (string) $debt->state !== 'committed' - || (int) $ip->reservation_id !== (int) $debt->id - || !hash_equals((string) $debt->operation_id, - (string) $ip->operation_id)) { - throw new CloudRecoveryInstanceStateConflict( - 'PeerMigration 源副本债务账本不一致', 409 - ); - } - $now = time(); - $ip->state = 'available'; - $ip->reservation_id = null; - $ip->instance_id = null; - $ip->operation_id = null; - $ip->updated_at = $now; - $ip->save(); - $debt->state = 'released'; - $debt->released_at = $now; - $debt->release_reason = 'maintenance_migration_source_removed'; - $debt->expires_at = null; - $debt->instance_id = null; - $debt->updated_at = $now; - $debt->save(); - } - private static function setState( CloudInstanceMigration $migration, string $state, string $errorCode, string $recoveryAction = '' ): void { + CloudInstanceMigrationWorkflow::assertState( + $state, + $recoveryAction + ); $migration->state = $state; $migration->error_code = $errorCode; $migration->recovery_action = $recoveryAction; diff --git a/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php b/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php new file mode 100644 index 00000000..4914c10e --- /dev/null +++ b/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php @@ -0,0 +1,248 @@ +instance_id + )->lockForUpdate()->first(); + $source = CloudResourceReservation::where( + 'id', (int) $migration->source_reservation_id + )->lockForUpdate()->first(); + $target = CloudResourceReservation::where( + 'id', (int) $migration->target_reservation_id + )->lockForUpdate()->first(); + $order = $instance === null || $instance->order_id === null ? null + : CloudOrder::where('id', (int) $instance->order_id) + ->lockForUpdate()->first(); + $sourceIp = $source === null ? null : CloudPublicIp::where( + 'id', (int) $source->public_ip_id + )->lockForUpdate()->first(); + $targetIp = $target === null ? null : CloudPublicIp::where( + 'id', (int) $target->public_ip_id + )->lockForUpdate()->first(); + if ($instance === null || $source === null || $target === null + || $order === null || $sourceIp === null || $targetIp === null + || (int) $instance->host_id !== (int) $migration->target_host_id + || !hash_equals(strtolower((string) $instance->uuid), + strtolower((string) $migration->target_uuid)) + || (int) $instance->reservation_id !== (int) $target->id + || (int) $source->order_id !== (int) $order->id + || $source->backup_restore_id !== null + || (int) $target->backup_restore_id !== (int) $migration->restore_id + || $target->order_id !== null + || (string) $source->state !== 'committed' + || (string) $target->state !== 'committed' + || (int) $sourceIp->reservation_id !== (int) $source->id + || (int) $targetIp->reservation_id !== (int) $target->id) { + throw new CloudRecoveryInstanceStateConflict( + 'PeerMigration 保留源副本时资源账本不一致', 409 + ); + } + $now = time(); + $sourceFields = []; + $targetFields = []; + foreach (['resource_pool_id', 'host_id', 'network_attachment_id', + 'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib', + 'disk_gib'] as $field) { + $sourceFields[$field] = $source->{$field}; + $targetFields[$field] = $target->{$field}; + } + foreach ($targetFields as $field => $value) { + $source->{$field} = $value; + } + $source->instance_id = (int) $instance->id; + $source->updated_at = $now; + $source->save(); + foreach ($sourceFields as $field => $value) { + $target->{$field} = $value; + } + $target->instance_id = null; + $target->updated_at = $now; + $target->save(); + + $targetIp->reservation_id = (int) $source->id; + $targetIp->instance_id = (int) $instance->id; + $targetIp->operation_id = (string) $source->operation_id; + $targetIp->state = 'assigned'; + $targetIp->updated_at = $now; + $targetIp->save(); + $sourceIp->reservation_id = (int) $target->id; + $sourceIp->instance_id = null; + $sourceIp->operation_id = (string) $target->operation_id; + $sourceIp->state = 'assigned'; + $sourceIp->updated_at = $now; + $sourceIp->save(); + + $instance->reservation_id = (int) $source->id; + $instance->resource_pool_id = (int) $source->resource_pool_id; + $instance->updated_at = $now; + $instance->save(); + $order->host_id = (int) $source->host_id; + $order->resource_pool_id = (int) $source->resource_pool_id; + $order->reservation_id = (int) $source->id; + $order->updated_at = $now; + $order->save(); + $oldSourceId = (int) $migration->source_reservation_id; + $migration->source_reservation_id = (int) $target->id; + $migration->target_reservation_id = $oldSourceId; + $migration->updated_at = $now; + $migration->save(); + } + + public static function isSettled( + CloudInstanceMigration $migration + ): bool { + $instance = CloudInstance::find((int) $migration->instance_id); + $target = CloudResourceReservation::find( + (int) $migration->target_reservation_id + ); + return $instance !== null && $target !== null + && (int) $instance->reservation_id === (int) $target->id + && $target->order_id !== null + && $target->backup_restore_id === null + && (int) $target->host_id === (int) $migration->target_host_id; + } + + /** Source deletion is proven; transfer its order-linked reservation. */ + public static function finalizeSourceDeletionLocked( + CloudInstanceMigration $migration + ): void { + $instance = CloudInstance::where( + 'id', (int) $migration->instance_id + )->lockForUpdate()->first(); + $source = CloudResourceReservation::where( + 'id', (int) $migration->source_reservation_id + )->lockForUpdate()->first(); + $target = CloudResourceReservation::where( + 'id', (int) $migration->target_reservation_id + )->lockForUpdate()->first(); + $sourceIp = $source === null ? null : CloudPublicIp::where( + 'id', (int) $source->public_ip_id + )->lockForUpdate()->first(); + $targetIp = $target === null ? null : CloudPublicIp::where( + 'id', (int) $target->public_ip_id + )->lockForUpdate()->first(); + $order = $instance === null || $instance->order_id === null ? null + : CloudOrder::where('id', (int) $instance->order_id) + ->lockForUpdate()->first(); + if ($instance === null || $source === null || $target === null + || $sourceIp === null || $targetIp === null || $order === null + || (int) $instance->host_id !== (int) $migration->target_host_id + || !hash_equals(strtolower((string) $instance->uuid), + strtolower((string) $migration->target_uuid)) + || (int) $instance->reservation_id !== (int) $target->id + || (int) $source->order_id !== (int) $order->id + || (string) $source->state !== 'committed' + || (int) $target->backup_restore_id !== (int) $migration->restore_id + || (string) $target->state !== 'committed' + || (int) $sourceIp->reservation_id !== (int) $source->id + || (int) $targetIp->reservation_id !== (int) $target->id) { + throw new CloudRecoveryInstanceStateConflict( + 'PeerMigration 清理后的资源预留账本不一致', 409 + ); + } + $now = time(); + $sourceIp->state = 'available'; + $sourceIp->reservation_id = null; + $sourceIp->instance_id = null; + $sourceIp->operation_id = null; + $sourceIp->updated_at = $now; + $sourceIp->save(); + foreach (['resource_pool_id', 'host_id', 'network_attachment_id', + 'public_ip_pool_id', 'public_ip_id', 'cpu_cores', 'memory_mib', + 'disk_gib'] as $field) { + $source->{$field} = $target->{$field}; + } + $source->instance_id = (int) $instance->id; + $source->updated_at = $now; + $source->save(); + $targetIp->reservation_id = (int) $source->id; + $targetIp->instance_id = (int) $instance->id; + $targetIp->operation_id = (string) $source->operation_id; + $targetIp->state = 'assigned'; + $targetIp->updated_at = $now; + $targetIp->save(); + $target->instance_id = null; + $target->network_attachment_id = null; + $target->public_ip_pool_id = null; + $target->public_ip_id = null; + $target->state = 'released'; + $target->released_at = $now; + $target->release_reason = 'maintenance_migration_transferred'; + $target->updated_at = $now; + $target->save(); + $instance->reservation_id = (int) $source->id; + $instance->resource_pool_id = (int) $source->resource_pool_id; + $instance->updated_at = $now; + $instance->save(); + $order->host_id = (int) $source->host_id; + $order->resource_pool_id = (int) $source->resource_pool_id; + $order->reservation_id = (int) $source->id; + $order->updated_at = $now; + $order->save(); + $migration->source_reservation_id = (int) $target->id; + $migration->target_reservation_id = (int) $source->id; + $migration->updated_at = $now; + $migration->save(); + } + + public static function releaseRetainedSourceLocked( + CloudInstanceMigration $migration + ): void { + $debt = CloudResourceReservation::where( + 'id', (int) $migration->source_reservation_id + )->lockForUpdate()->first(); + $ip = $debt === null ? null : CloudPublicIp::where( + 'id', (int) $debt->public_ip_id + )->lockForUpdate()->first(); + if ($debt === null || $ip === null + || (int) $debt->host_id !== (int) $migration->source_host_id + || (int) $debt->backup_restore_id !== (int) $migration->restore_id + || (string) $debt->state !== 'committed' + || (int) $ip->reservation_id !== (int) $debt->id + || !hash_equals((string) $debt->operation_id, + (string) $ip->operation_id)) { + throw new CloudRecoveryInstanceStateConflict( + 'PeerMigration 源副本债务账本不一致', 409 + ); + } + $now = time(); + $ip->state = 'available'; + $ip->reservation_id = null; + $ip->instance_id = null; + $ip->operation_id = null; + $ip->updated_at = $now; + $ip->save(); + $debt->state = 'released'; + $debt->released_at = $now; + $debt->release_reason = 'maintenance_migration_source_removed'; + $debt->expires_at = null; + $debt->instance_id = null; + $debt->updated_at = $now; + $debt->save(); + } +} diff --git a/web/app/Services/Cloud/CloudInstanceMigrationWorkflow.php b/web/app/Services/Cloud/CloudInstanceMigrationWorkflow.php new file mode 100644 index 00000000..b2ece235 --- /dev/null +++ b/web/app/Services/Cloud/CloudInstanceMigrationWorkflow.php @@ -0,0 +1,612 @@ + [ + 'position' => 1, + 'label' => '准备源实例', + 'running_state' => 'preparing_source', + 'job_kind' => 'instance_migration_prepare', + 'job_field' => 'source_prepare_job_id', + 'task_key' => 'prepare_task_id', + 'finish_handler' => 'finishPrepareLocked', + 'reconcile_action' => 'reconcile_prepare', + 'recovery_actions' => [ + 'abort_prepare' => [ + 'label' => '中止迁移并恢复源实例', + 'handler' => 'recoverAbortPrepare', + 'destructive' => true, + 'recorded_actions' => [ + 'reconcile_prepare', 'abort_prepare', + ], + 'requires_abort_evidence' => true, + ], + ], + ], + 'transfer' => [ + 'position' => 2, + 'label' => '传输磁盘与快照', + 'running_state' => 'transferring', + 'job_kind' => 'instance_migration_transfer', + 'job_field' => 'target_receive_job_id', + 'task_key' => 'transfer_task_id', + 'finish_handler' => 'finishTransferLocked', + 'reconcile_action' => 'reconcile_transfer', + 'recovery_actions' => [ + 'retry_transfer' => [ + 'label' => '继续分块传输', + 'handler' => 'recoverRetryTransferLocked', + 'destructive' => false, + 'recorded_actions' => ['retry_transfer'], + 'requires_abort_evidence' => false, + ], + 'abort_transfer' => [ + 'label' => '中止传输并恢复源实例', + 'handler' => 'recoverAbortTransferLocked', + 'destructive' => true, + 'recorded_actions' => ['retry_transfer'], + 'requires_abort_evidence' => false, + ], + ], + ], + 'cutover' => [ + 'position' => 3, + 'label' => '切换实例账本', + 'running_state' => 'cutover', + 'job_kind' => null, + 'job_field' => null, + 'task_key' => null, + 'finish_handler' => null, + 'reconcile_action' => null, + 'recovery_actions' => [], + ], + 'firewall' => [ + 'position' => 4, + 'label' => '恢复目标防火墙', + 'running_state' => 'firewall_running', + 'job_kind' => 'firewall_apply', + 'job_field' => 'firewall_job_id', + 'task_key' => 'firewall_task_id', + 'finish_handler' => 'finishFirewallLocked', + 'reconcile_action' => 'reconcile_firewall', + 'recovery_actions' => [ + 'retry_firewall' => [ + 'label' => '重试目标防火墙', + 'handler' => 'recoverRetryFirewallLocked', + 'destructive' => false, + 'recorded_actions' => ['retry_firewall'], + 'requires_abort_evidence' => false, + ], + ], + ], + 'activation' => [ + 'position' => 5, + 'label' => '恢复目标电源状态', + 'running_state' => 'activation_running', + 'job_kind' => 'instance_migration_activate', + 'job_field' => 'activate_job_id', + 'task_key' => 'activation_task_id', + 'finish_handler' => 'finishActivationLocked', + 'reconcile_action' => 'reconcile_activation', + 'recovery_actions' => [ + 'retry_activation' => [ + 'label' => '重试目标状态恢复', + 'handler' => 'recoverRetryActivationLocked', + 'destructive' => false, + 'recorded_actions' => ['retry_activation'], + 'requires_abort_evidence' => false, + ], + ], + ], + 'cleanup' => [ + 'position' => 6, + 'label' => '清理源副本', + 'running_state' => 'cleanup_running', + 'job_kind' => 'instance_migration_cleanup', + 'job_field' => 'cleanup_job_id', + 'task_key' => 'cleanup_task_id', + 'finish_handler' => 'finishCleanupLocked', + 'reconcile_action' => 'reconcile_cleanup', + 'recovery_actions' => [], + ], + 'rollback' => [ + 'position' => null, + 'label' => '恢复源实例', + 'running_state' => 'rollback_running', + 'job_kind' => 'instance_migration_rollback', + 'job_field' => 'rollback_job_id', + 'task_key' => 'rollback_task_id', + 'finish_handler' => 'finishRollbackLocked', + 'reconcile_action' => 'reconcile_rollback', + 'recovery_actions' => [ + 'retry_rollback' => [ + 'label' => '重试恢复源实例', + 'handler' => 'recoverRetryRollbackLocked', + 'destructive' => false, + 'recorded_actions' => ['retry_rollback'], + 'requires_abort_evidence' => false, + ], + ], + ], + ]; + + private const PASSIVE_STATES = [ + 'attention_required', 'source_retained', 'completed', 'rolled_back', + 'failed', + ]; + + /** @return array> */ + public static function stages(): array + { + self::assertIntegrity(); + return self::STAGES; + } + + /** @return array|null */ + public static function job(string $kind): ?array + { + self::assertIntegrity(); + foreach (self::STAGES as $key => $stage) { + if ($stage['job_kind'] === $kind) { + return ['key' => $key] + $stage; + } + } + return null; + } + + /** @return list */ + public static function blockingStates(): array + { + self::assertIntegrity(); + $states = ['attention_required']; + foreach (self::STAGES as $stage) { + $states[] = $stage['running_state']; + } + return array_values(array_unique($states)); + } + + public static function blocksInstance(string $state): bool + { + return in_array($state, self::blockingStates(), true); + } + + public static function blocksNewMigration(string $state): bool + { + return self::blocksInstance($state) || $state === 'source_retained'; + } + + public static function isRecoveryAction(string $action): bool + { + return self::recoveryDefinition($action) !== null; + } + + public static function recoveryHandler(string $action): ?string + { + $definition = self::recoveryDefinition($action); + return $definition === null ? null : (string) $definition['handler']; + } + + /** + * @return list + */ + public static function recoveryOptions( + CloudInstanceMigration $migration, + bool $hasAbortPrepareEvidence + ): array { + self::assertIntegrity(); + if ((string) $migration->state !== 'attention_required') { + return []; + } + $recorded = (string) $migration->recovery_action; + $options = []; + foreach (self::STAGES as $stage) { + foreach ($stage['recovery_actions'] as $action => $definition) { + if (!in_array($recorded, + $definition['recorded_actions'], true)) { + continue; + } + if ($definition['requires_abort_evidence'] + && !$hasAbortPrepareEvidence) { + continue; + } + $options[] = [ + 'action' => $action, + 'label' => (string) $definition['label'], + 'destructive' => (bool) $definition['destructive'], + ]; + } + } + return $options; + } + + /** @return list */ + public static function recoveryActions( + CloudInstanceMigration $migration, + bool $hasAbortPrepareEvidence + ): array { + return array_map( + static fn (array $option): string => $option['action'], + self::recoveryOptions($migration, $hasAbortPrepareEvidence) + ); + } + + public static function assertState( + string $state, + string $recoveryAction + ): void { + self::assertIntegrity(); + $knownStates = self::PASSIVE_STATES; + foreach (self::STAGES as $stage) { + $knownStates[] = $stage['running_state']; + } + if (!in_array($state, $knownStates, true)) { + throw new \LogicException( + 'Unknown maintenance-migration state: ' . $state + ); + } + if ($state === 'attention_required') { + if (!self::isRecordedRecoveryAction($recoveryAction)) { + throw new \LogicException( + 'Attention state requires a registered recovery action' + ); + } + return; + } + if ($state === 'source_retained') { + if (!in_array($recoveryAction, + ['', 'reconcile_cleanup'], true)) { + throw new \LogicException( + 'Source-retained state has an invalid recovery action' + ); + } + return; + } + if ($recoveryAction !== '') { + throw new \LogicException( + 'Migration state does not accept a recovery action: ' . $state + ); + } + } + + /** + * A stable, byte-progress-free projection. PeerMigration V3 currently + * returns no durable transfer telemetry, so the panel reports truthful + * stage progress instead of inventing a percentage. + * + * @return array + */ + public static function progress( + CloudInstanceMigration $migration + ): array { + self::assertIntegrity(); + $state = (string) $migration->state; + $currentKey = self::stageForStateOrRecovery( + $state, + (string) $migration->recovery_action + ); + $mainStages = array_filter( + self::STAGES, + static fn (array $stage): bool => is_int($stage['position']) + ); + uasort($mainStages, static fn (array $left, array $right): int => + $left['position'] <=> $right['position']); + $currentPosition = $currentKey !== null + && is_int(self::STAGES[$currentKey]['position']) + ? self::STAGES[$currentKey]['position'] : null; + $terminalCompleted = $state === 'completed'; + $onRollbackPath = $currentKey === 'rollback' + || $state === 'rolled_back'; + $steps = []; + foreach ($mainStages as $key => $stage) { + $stepStatus = 'pending'; + if ($onRollbackPath) { + if ($key === 'prepare') { + $stepStatus = (string) $migration->error_code + === 'migration_transfer_aborted' + ? 'completed' : 'interrupted'; + } elseif ($key === 'transfer') { + $stepStatus = $migration->target_receive_job_id === null + ? 'skipped' : 'interrupted'; + } + } elseif ($terminalCompleted) { + $stepStatus = self::stageWasSkipped($migration, $key) + ? 'skipped' : 'completed'; + } elseif ($state === 'source_retained') { + if ($key === 'cleanup') { + $stepStatus = (string) $migration->recovery_action + === 'reconcile_cleanup' + ? 'attention_required' : 'available'; + } else { + $stepStatus = self::stageWasSkipped($migration, $key) + ? 'skipped' : 'completed'; + } + } elseif ($currentPosition !== null + && $stage['position'] < $currentPosition) { + $stepStatus = self::stageWasSkipped($migration, $key) + ? 'skipped' : 'completed'; + } elseif ($key === $currentKey) { + $stepStatus = $state === 'attention_required' + ? 'attention_required' : 'running'; + } + $steps[] = [ + 'key' => $key, + 'label' => (string) $stage['label'], + 'status' => $stepStatus, + 'status_label' => self::stepStatusLabel($stepStatus), + ]; + } + if ($currentKey === 'rollback' || $state === 'rolled_back') { + $steps[] = [ + 'key' => 'rollback', + 'label' => (string) self::STAGES['rollback']['label'], + 'status' => $state === 'rolled_back' ? 'completed' + : ($state === 'attention_required' + ? 'attention_required' : 'running'), + 'status_label' => $state === 'rolled_back' + ? self::stepStatusLabel('completed') + : self::stepStatusLabel( + $state === 'attention_required' + ? 'attention_required' : 'running' + ), + ]; + } + $status = match ($state) { + 'completed' => 'completed', + 'rolled_back' => 'rolled_back', + 'source_retained' => (string) $migration->recovery_action + === 'reconcile_cleanup' + ? 'attention_required' : 'source_retained', + 'attention_required' => 'attention_required', + 'failed' => 'failed', + default => 'running', + }; + $currentStage = $currentKey === null ? null : [ + 'key' => $currentKey, + 'label' => (string) self::STAGES[$currentKey]['label'], + 'index' => self::STAGES[$currentKey]['position'], + ]; + return [ + 'status' => $status, + 'label' => self::progressLabel($status, $currentStage), + 'blocks_instance' => self::blocksInstance($state), + 'blocks_new_migration' => self::blocksNewMigration($state), + 'current_stage' => $currentStage, + 'stage_count' => count($mainStages), + 'steps' => $steps, + 'byte_progress_available' => false, + 'updated_at' => (int) $migration->updated_at, + ]; + } + + public static function assertIntegrity(): void + { + static $checked = false; + if ($checked) { + return; + } + $required = [ + 'position', 'label', 'running_state', 'job_kind', 'job_field', + 'task_key', 'finish_handler', 'reconcile_action', + 'recovery_actions', + ]; + $positions = []; + $states = []; + $jobKinds = []; + $jobFields = []; + $taskKeys = []; + $reconcileActions = []; + $actions = []; + foreach (self::STAGES as $key => $stage) { + if (array_diff($required, array_keys($stage)) !== [] + || !is_string($stage['label']) || $stage['label'] === '' + || !is_string($stage['running_state']) + || $stage['running_state'] === '' + || !is_array($stage['recovery_actions'])) { + throw new \LogicException( + 'Incomplete maintenance-migration stage: ' . $key + ); + } + if (isset($states[$stage['running_state']])) { + throw new \LogicException( + 'Duplicate maintenance-migration running state' + ); + } + $states[$stage['running_state']] = true; + if (is_int($stage['position'])) { + if (isset($positions[$stage['position']])) { + throw new \LogicException( + 'Duplicate maintenance-migration stage position' + ); + } + $positions[$stage['position']] = true; + } + if ($stage['job_kind'] !== null) { + foreach (['job_kind', 'job_field', 'task_key', + 'finish_handler', 'reconcile_action'] as $field) { + if (!is_string($stage[$field]) + || $stage[$field] === '') { + throw new \LogicException( + 'Executable migration stage is incomplete: ' . $key + ); + } + } + if (isset($jobKinds[$stage['job_kind']]) + || isset($jobFields[$stage['job_field']]) + || isset($taskKeys[$stage['task_key']]) + || isset($reconcileActions[$stage['reconcile_action']])) { + throw new \LogicException( + 'Duplicate maintenance-migration job definition' + ); + } + $jobKinds[$stage['job_kind']] = true; + $jobFields[$stage['job_field']] = true; + $taskKeys[$stage['task_key']] = true; + $reconcileActions[$stage['reconcile_action']] = true; + } elseif ($stage['job_field'] !== null + || $stage['task_key'] !== null + || $stage['finish_handler'] !== null + || $stage['reconcile_action'] !== null) { + throw new \LogicException( + 'Non-job migration stage contains partial job metadata' + ); + } + foreach ($stage['recovery_actions'] as $action => $definition) { + if (isset($actions[$action]) || !is_string($action) + || $action === '' || !is_array($definition) + || !is_string($definition['label'] ?? null) + || ($definition['label'] ?? '') === '' + || !is_string($definition['handler'] ?? null) + || ($definition['handler'] ?? '') === '' + || !is_bool($definition['destructive'] ?? null) + || !is_array($definition['recorded_actions'] ?? null) + || $definition['recorded_actions'] === [] + || !array_is_list($definition['recorded_actions']) + || array_filter( + $definition['recorded_actions'], + static fn ($recorded): bool => + !is_string($recorded) || $recorded === '' + ) !== [] + || !is_bool( + $definition['requires_abort_evidence'] ?? null + )) { + throw new \LogicException( + 'Invalid maintenance-migration recovery definition' + ); + } + $actions[$action] = true; + } + } + ksort($positions); + if (array_keys($positions) !== range(1, count($positions))) { + throw new \LogicException( + 'Maintenance-migration stage positions are not contiguous' + ); + } + foreach (self::STAGES as $stage) { + foreach ($stage['recovery_actions'] as $definition) { + foreach ($definition['recorded_actions'] as $recorded) { + if (!isset($actions[$recorded]) + && !isset($reconcileActions[$recorded])) { + throw new \LogicException( + 'Recovery button references an unknown ledger action' + ); + } + } + } + } + $checked = true; + } + + /** @return array|null */ + private static function recoveryDefinition(string $action): ?array + { + self::assertIntegrity(); + foreach (self::STAGES as $stageKey => $stage) { + if (isset($stage['recovery_actions'][$action])) { + return ['stage_key' => $stageKey] + + $stage['recovery_actions'][$action]; + } + } + return null; + } + + private static function isRecordedRecoveryAction(string $action): bool + { + if ($action === '') { + return false; + } + foreach (self::STAGES as $stage) { + if ($stage['reconcile_action'] === $action) { + return true; + } + foreach ($stage['recovery_actions'] as $definition) { + if (in_array($action, + $definition['recorded_actions'], true)) { + return true; + } + } + } + return false; + } + + private static function stageForStateOrRecovery( + string $state, + string $recoveryAction + ): ?string { + foreach (self::STAGES as $key => $stage) { + if ($stage['running_state'] === $state) { + return $key; + } + if ($state === 'attention_required') { + if ($stage['reconcile_action'] === $recoveryAction) { + return $key; + } + foreach ($stage['recovery_actions'] as $definition) { + if (in_array($recoveryAction, + $definition['recorded_actions'], true)) { + return $key; + } + } + } + } + if ($state === 'source_retained' || $state === 'completed') { + return 'cleanup'; + } + if ($state === 'rolled_back') { + return 'rollback'; + } + return null; + } + + private static function stageWasSkipped( + CloudInstanceMigration $migration, + string $stageKey + ): bool { + if ($stageKey === 'firewall') { + return $migration->firewall_job_id === null; + } + return false; + } + + private static function stepStatusLabel(string $status): string + { + return match ($status) { + 'completed' => '已完成', + 'running' => '执行中', + 'skipped' => '已跳过', + 'attention_required' => '需要处理', + 'available' => '可手动清理', + 'interrupted' => '已中止', + default => '等待中', + }; + } + + /** @param array{key:string,label:string,index:int|null}|null $stage */ + private static function progressLabel( + string $status, + ?array $stage + ): string { + return match ($status) { + 'completed' => '迁移已完成', + 'rolled_back' => '迁移已中止,源实例已恢复', + 'source_retained' => '目标已切换,源副本已保留', + 'attention_required' => '迁移需要核对或人工处理', + 'failed' => '迁移失败', + default => $stage === null + ? '迁移进行中' : '正在' . (string) $stage['label'], + }; + } +} diff --git a/web/public/cloud/admin-instances.js b/web/public/cloud/admin-instances.js index e9996cf6..7d5e402f 100644 --- a/web/public/cloud/admin-instances.js +++ b/web/public/cloud/admin-instances.js @@ -979,19 +979,9 @@ function maintenanceMigrationSection(payload, vmId) { body.append(el('h3', 'h6 fw-semibold', '维护迁移')); const migration = payload?.maintenance_migration; if (migration && typeof migration === 'object') { - const stateLabel = ({ - preparing_source: '正在停止源实例并冻结迁移目录', - transferring: '目标 Agent 正在分块拉取迁移数据', - cutover: '正在切换实例账本', - firewall_running: '正在恢复目标防火墙', - activation_running: '正在恢复目标电源与自启状态', - rollback_running: '正在中止迁移并恢复源实例', - rolled_back: '迁移已中止,源实例已恢复', - cleanup_running: '正在清理源副本', - source_retained: '目标已切换,源副本已保留', - attention_required: '迁移需要核对或人工选择恢复出口', - completed: '迁移已完成', failed: '迁移失败', - })[String(migration.state || '')] || '状态未知'; + const progress = migration?.progress && typeof migration.progress === 'object' + ? migration.progress : {}; + const stateLabel = text(progress.label, '状态未知'); body.append(infoList([ ['状态', stateLabel], ['迁移方向', `宿主机 #${text(migration.source_host_id)} → #${text(migration.target_host_id)}`], @@ -999,6 +989,40 @@ function maintenanceMigrationSection(payload, vmId) { ['切换时间', date(migration.cutover_at)], ['完成时间', date(migration.completed_at)], ])); + const progressSteps = Array.isArray(progress.steps) ? progress.steps : []; + if (progressSteps.length) { + const progressBox = el('div', 'mb-3'); + const currentStage = progress?.current_stage; + const currentIndex = Number(currentStage?.index); + const stageCount = Number(progress?.stage_count); + const heading = currentStage && Number.isInteger(currentIndex) + && currentIndex > 0 && Number.isInteger(stageCount) && stageCount > 0 + ? `当前阶段 ${currentIndex}/${stageCount} · ${text(currentStage.label)}` + : text(currentStage?.label, stateLabel); + progressBox.append(el('p', 'fw-semibold fs-13 mb-2', heading)); + const stepList = el('div', 'd-flex flex-wrap gap-2'); + const tones = { + completed: 'text-success', running: 'text-primary', + attention_required: 'text-danger', available: 'text-warning', + interrupted: 'text-danger', skipped: 'text-muted', pending: 'text-muted', + }; + progressSteps.forEach(step => { + if (!step || typeof step !== 'object') return; + const status = String(step.status || 'pending'); + const item = el('span', + `d-inline-flex align-items-center gap-1 border rounded-pill px-2 py-1 fs-13 ${tones[status] || 'text-muted'}`); + item.append(el('span', 'fw-semibold', text(step.label, '未知阶段')), + el('span', '', text(step.status_label, '等待中'))); + stepList.append(item); + }); + progressBox.append(stepList); + if (currentStage?.key === 'transfer' + && progress.byte_progress_available !== true) { + progressBox.append(el('p', 'text-muted fs-13 mb-0 mt-2', + '当前协议提供可靠的阶段进度;尚未提供可持久化的字节传输百分比。')); + } + body.append(progressBox); + } if (migration.state === 'attention_required') { body.append(el('p', 'alert alert-danger fs-13', '系统不会自动重放不确定操作。请等待原操作编号收敛;只有确认可重试时,下方才会显示恢复按钮。')); @@ -1009,37 +1033,34 @@ function maintenanceMigrationSection(payload, vmId) { ? '源副本删除效果正在按原操作编号核对,资源保持占用且不会盲目重放删除。' : '目标实例已经接管原账单、到期时间和流量周期;源宿主机副本及原公网 IP 仍被保留并占用资源。')); } - const recoveryLabels = { - abort_prepare: '中止迁移并恢复源实例', - retry_transfer: '继续分块传输', - abort_transfer: '中止传输并恢复源实例', - retry_firewall: '重试目标防火墙', - retry_activation: '重试目标状态恢复', - retry_rollback: '重试恢复源实例', - }; - const recoveryActions = Array.isArray(migration.recovery_actions) - ? migration.recovery_actions.filter(action => recoveryLabels[action]) : []; - if (recoveryActions.length) { + const recoveryOptions = Array.isArray(migration.recovery_options) + ? migration.recovery_options.filter(option => option + && typeof option === 'object' + && /^[a-z_]+$/.test(String(option.action || '')) + && String(option.label || '').trim()) : []; + if (recoveryOptions.length) { const recoveryBox = el('div', 'd-flex flex-wrap justify-content-end gap-2 mb-2'); - recoveryActions.forEach(action => { - const aborting = action === 'abort_prepare' || action === 'abort_transfer'; + recoveryOptions.forEach(option => { + const action = String(option.action); + const label = String(option.label).trim(); + const aborting = option.destructive === true; const button = el('button', aborting - ? 'btn btn-outline-danger btn-sm' : 'btn btn-outline-primary btn-sm', recoveryLabels[action]); + ? 'btn btn-outline-danger btn-sm' : 'btn btn-outline-primary btn-sm', label); button.type = 'button'; button.addEventListener('click', async () => { const warning = aborting ? '确定中止本次迁移并将源实例恢复到迁移前电源与自启状态吗?' - : `确定${recoveryLabels[action]}吗?`; + : `确定${label}吗?`; if (state.backupMutationRequest || !window.confirm(warning)) return; const controller = new AbortController(); state.backupMutationRequest = controller; for (const item of recoveryBox.querySelectorAll('button')) item.disabled = true; - detailNotice(`正在创建“${recoveryLabels[action]}”任务,请勿重复提交。`); + detailNotice(`正在创建“${label}”任务,请勿重复提交。`); try { const receipt = await mutate( `/api/v1/admin/instances/${encodeURIComponent(vmId)}/maintenance-migrations/recover`, { action, confirm_recovery: true }, controller, 'POST'); - watchBackupTask(vmId, receipt, recoveryLabels[action]); + watchBackupTask(vmId, receipt, label); void loadBackups(vmId, true); } catch (error) { if (!(controller.signal.aborted && controller.signal.reason !== 'timeout')) { @@ -1110,10 +1131,9 @@ function maintenanceMigrationSection(payload, vmId) { const feedback = el('p', 'text-muted fs-13 mb-0 mt-2'); feedback.dataset.migrationFeedback = ''; feedback.hidden = true; const blocked = payload?.can_migrate !== true; + const migrationBlocksNew = migration?.progress?.blocks_new_migration === true; for (const control of [host, mode, submit, confirm]) control.disabled = blocked; - if (blocked && (!migration || !['preparing_source', 'transferring', 'cutover', - 'firewall_running', 'activation_running', 'rollback_running', - 'cleanup_running', 'source_retained', 'attention_required'].includes(migration.state))) { + if (blocked && (!migration || !migrationBlocksNew)) { feedback.hidden = false; feedback.className = 'text-warning fs-13 mb-0 mt-2'; feedback.textContent = targets.length ? text(payload?.create_block_reason, '当前实例不能开始维护迁移。') diff --git a/web/resources/views/venix/layout_assets.tpl b/web/resources/views/venix/layout_assets.tpl index e871cf4b..4a3e466b 100644 --- a/web/resources/views/venix/layout_assets.tpl +++ b/web/resources/views/venix/layout_assets.tpl @@ -1,2 +1,2 @@ {* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *} -{assign var=venix_asset_version value='2026102803' scope='global'} +{assign var=venix_asset_version value='2026102804' scope='global'} diff --git a/web/tests/CloudInstanceMigrationWorkflowTest.php b/web/tests/CloudInstanceMigrationWorkflowTest.php new file mode 100644 index 00000000..d5ad0ad3 --- /dev/null +++ b/web/tests/CloudInstanceMigrationWorkflowTest.php @@ -0,0 +1,185 @@ +state = $state; + $value->recovery_action = $recoveryAction; + $value->cleanup_mode = 'delete'; + $value->source_prepare_job_id = 1; + $value->target_receive_job_id = null; + $value->firewall_job_id = null; + $value->activate_job_id = null; + $value->rollback_job_id = null; + $value->cleanup_job_id = null; + $value->updated_at = 1_800_000_000; + return $value; +}; +$step = static function (array $projection, string $key): ?array { + foreach ($projection['steps'] ?? [] as $item) { + if (($item['key'] ?? null) === $key) { + return $item; + } + } + return null; +}; + +CloudInstanceMigrationWorkflow::assertIntegrity(); +$stages = CloudInstanceMigrationWorkflow::stages(); +$assert(array_keys($stages) === [ + 'prepare', 'transfer', 'cutover', 'firewall', 'activation', 'cleanup', + 'rollback', +], 'The executable migration-stage registry is incomplete or reordered.'); + +$service = new ReflectionClass(CloudInstanceMigrationService::class); +$jobKinds = []; +$jobFields = []; +$recoveryActions = []; +foreach ($stages as $key => $definition) { + if ($definition['job_kind'] !== null) { + $jobKinds[] = $definition['job_kind']; + $jobFields[] = $definition['job_field']; + $assert($service->hasMethod($definition['finish_handler']), + "Stage {$key} has no executable completion handler."); + } + foreach ($definition['recovery_actions'] as $action => $recovery) { + $recoveryActions[] = $action; + $assert(CloudInstanceMigrationWorkflow::recoveryHandler($action) + === $recovery['handler'] + && $service->hasMethod($recovery['handler']), + "Recovery action {$action} has no executable handler."); + } +} +$assert(count($jobKinds) === count(array_unique($jobKinds)) + && count($jobFields) === count(array_unique($jobFields)) + && count($recoveryActions) === count(array_unique($recoveryActions)), + 'Migration stages contain duplicate job or recovery definitions.'); + +$transferFailure = $migration('attention_required', 'retry_transfer'); +$assert(CloudInstanceMigrationWorkflow::recoveryActions( + $transferFailure, + false + ) === ['retry_transfer', 'abort_transfer'], + 'Transfer recovery buttons and execution policy diverged.'); +$firewallFailure = $migration('attention_required', 'retry_firewall'); +$assert(CloudInstanceMigrationWorkflow::recoveryActions( + $firewallFailure, + false + ) === ['retry_firewall'], + 'Firewall recovery policy is incomplete.'); +$prepareUnknown = $migration('attention_required', 'reconcile_prepare'); +$assert(CloudInstanceMigrationWorkflow::recoveryActions( + $prepareUnknown, + false + ) === [] + && CloudInstanceMigrationWorkflow::recoveryActions( + $prepareUnknown, + true + ) === ['abort_prepare'], + 'Prepare abort was exposed without evidence or hidden after proof.'); + +$invalidStateRejected = false; +try { + CloudInstanceMigrationWorkflow::assertState( + 'attention_required', + 'missing_recovery_exit' + ); +} catch (LogicException $error) { + $invalidStateRejected = true; +} +$assert($invalidStateRejected, + 'An attention state accepted an unregistered recovery exit.'); + +$transferring = $migration('transferring'); +$transferring->target_receive_job_id = 2; +$projection = CloudInstanceMigrationWorkflow::progress($transferring); +$assert(($projection['current_stage']['key'] ?? null) === 'transfer' + && ($projection['current_stage']['index'] ?? null) === 2 + && ($projection['stage_count'] ?? null) === 6 + && ($projection['byte_progress_available'] ?? null) === false + && ($step($projection, 'prepare')['status'] ?? null) === 'completed' + && ($step($projection, 'transfer')['status'] ?? null) === 'running', + 'Transfer progress projection is not deterministic.'); + +$firewallAttention = CloudInstanceMigrationWorkflow::progress( + $firewallFailure +); +$assert(($firewallAttention['status'] ?? null) === 'attention_required' + && ($firewallAttention['current_stage']['key'] ?? null) === 'firewall' + && ($step($firewallAttention, 'firewall')['status'] ?? null) + === 'attention_required', + 'Recovery attention was projected onto the wrong stage.'); + +$retained = $migration('source_retained'); +$retainedProjection = CloudInstanceMigrationWorkflow::progress($retained); +$assert(($retainedProjection['status'] ?? null) === 'source_retained' + && ($retainedProjection['blocks_instance'] ?? null) === false + && ($retainedProjection['blocks_new_migration'] ?? null) === true + && ($step($retainedProjection, 'cleanup')['status'] ?? null) + === 'available', + 'A retained source copy has an incorrect progress or locking projection.'); +$retained->recovery_action = 'reconcile_cleanup'; +$cleanupUnknown = CloudInstanceMigrationWorkflow::progress($retained); +$assert(($cleanupUnknown['status'] ?? null) === 'attention_required' + && ($step($cleanupUnknown, 'cleanup')['status'] ?? null) + === 'attention_required', + 'Unknown source cleanup was presented as an ordinary retained copy.'); + +$rolledBack = $migration('rolled_back'); +$rolledBack->error_code = 'migration_transfer_aborted'; +$rolledBack->target_receive_job_id = 2; +$rollbackProjection = CloudInstanceMigrationWorkflow::progress($rolledBack); +$assert(($rollbackProjection['status'] ?? null) === 'rolled_back' + && ($step($rollbackProjection, 'prepare')['status'] ?? null) + === 'completed' + && ($step($rollbackProjection, 'transfer')['status'] ?? null) + === 'interrupted' + && ($step($rollbackProjection, 'rollback')['status'] ?? null) + === 'completed', + 'Rollback progress hid the interrupted migration stage.'); + +$completed = $migration('completed'); +$completed->target_receive_job_id = 2; +$completed->activate_job_id = 3; +$completed->cleanup_job_id = 4; +$completedProjection = CloudInstanceMigrationWorkflow::progress($completed); +$assert(($completedProjection['status'] ?? null) === 'completed' + && ($step($completedProjection, 'firewall')['status'] ?? null) + === 'skipped' + && ($step($completedProjection, 'cleanup')['status'] ?? null) + === 'completed', + 'Completed migration stages were not projected truthfully.'); + +$serviceSource = (string) file_get_contents( + dirname(__DIR__) . '/app/Services/Cloud/CloudInstanceMigrationService.php' +); +$settlementSource = (string) file_get_contents( + dirname(__DIR__) + . '/app/Services/Cloud/CloudInstanceMigrationSettlement.php' +); +$assert(substr_count($serviceSource, + 'CloudInstanceMigrationWorkflow::job(') === 2 + && !str_contains($serviceSource, + '$field = match ((string) $job->kind)') + && str_contains($serviceSource, + 'CloudInstanceMigrationWorkflow::recoveryActions(') + && !str_contains($serviceSource, + "'maintenance_migration_transferred'") + && str_contains($settlementSource, + "'maintenance_migration_transferred'"), + 'Migration routing or resource settlement escaped its single module.'); + +echo "Maintenance migration workflow integrity and progress tests passed.\n"; diff --git a/web/tests/CloudMaintenanceMigrationFinalizerTest.php b/web/tests/CloudMaintenanceMigrationFinalizerTest.php index aebaef76..b68e8e36 100644 --- a/web/tests/CloudMaintenanceMigrationFinalizerTest.php +++ b/web/tests/CloudMaintenanceMigrationFinalizerTest.php @@ -5,6 +5,7 @@ require dirname(__DIR__) . '/vendor/autoload.php'; use App\Models\CloudInstance; use App\Models\CloudInstanceMigration; use App\Models\CloudJob; +use App\Services\Cloud\CloudInstanceMigrationSettlement; use App\Services\Cloud\CloudInstanceMigrationService; use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Schema\Blueprint; @@ -256,11 +257,9 @@ $before = CloudInstance::findOrFail(11)->only([ 'renewal_traffic_bytes', 'traffic_quota_bytes', 'lifetime_download_bytes', 'lifetime_upload_bytes', 'created_at', ]); -$finalize = new ReflectionMethod( - CloudInstanceMigrationService::class, - 'finalizePlacementLocked' +CloudInstanceMigrationSettlement::finalizeSourceDeletionLocked( + CloudInstanceMigration::findOrFail(1) ); -$finalize->invoke(null, CloudInstanceMigration::findOrFail(1)); $instance = CloudInstance::findOrFail(11); $after = $instance->only(array_keys($before)); @@ -356,7 +355,8 @@ $finishRollback->invoke( 'state' => 'running', 'running' => true, 'autostart_enabled' => true, - ] + ], + [] ); $rolledBack = CloudInstanceMigration::findOrFail(2); $assert((string) $rolledBack->state === 'rolled_back' @@ -463,7 +463,8 @@ $finishActivation->invoke( 'state' => 'shutoff', 'running' => false, 'autostart_enabled' => true, - ] + ], + [] ); $poweredOff = CloudInstance::findOrFail(13); $activated = CloudInstanceMigration::findOrFail(3); @@ -599,7 +600,8 @@ $finishRollback->invoke( CloudInstanceMigration::findOrFail(4), $prepareAbortRollbackJob, 'uncertain', - null + null, + [] ); $uncertainAbort = CloudInstanceMigration::findOrFail(4); $assert((string) $uncertainAbort->state === 'attention_required' @@ -619,7 +621,8 @@ $finishRollback->invoke( CloudInstanceMigration::findOrFail(4), $prepareAbortRollbackJob, 'failed', - null + null, + [] ); $failedAbort = CloudInstanceMigration::findOrFail(4); $assert((string) $failedAbort->state === 'attention_required' @@ -643,7 +646,8 @@ $finishRollback->invoke( 'uuid' => $prepareAbortUuid, 'running' => true, 'autostart_enabled' => true, - ] + ], + [] ); $completedAbort = CloudInstanceMigration::findOrFail(4); $releasedAbortRestore = $capsule->table('cloud_backup_restores')->find(55); diff --git a/web/tests/CloudPortableBackupAdminUiContractTest.php b/web/tests/CloudPortableBackupAdminUiContractTest.php index 27c76250..22df05ae 100644 --- a/web/tests/CloudPortableBackupAdminUiContractTest.php +++ b/web/tests/CloudPortableBackupAdminUiContractTest.php @@ -51,6 +51,12 @@ $assert(str_contains($routes, 'Administrator portable backup routes must use the durable backup service and strict task receipt.'); $migration = $read('app/Services/Cloud/CloudInstanceMigrationService.php'); +$migrationWorkflow = $read( + 'app/Services/Cloud/CloudInstanceMigrationWorkflow.php' +); +$migrationSettlement = $read( + 'app/Services/Cloud/CloudInstanceMigrationSettlement.php' +); $peerArtifacts = $read( 'app/Services/Cloud/CloudPeerMigrationArtifactService.php' ); @@ -58,7 +64,10 @@ $assert(str_contains($runtime, 'function maintenanceMigrationSection(') && str_contains($runtime, 'confirm_maintenance_migration: true') && str_contains($runtime, 'confirm_source_cleanup: true') && str_contains($runtime, '原账单、到期时间和流量周期') - && str_contains($migration, "'maintenance_migration_transferred'") + && str_contains( + $migrationSettlement, + "'maintenance_migration_transferred'" + ) && str_contains($migration, "'traffic_counter_generation'") && str_contains($migration, "'reconcile_cleanup'") && str_contains($migration, "'managed_artifacts_absent'") @@ -86,20 +95,22 @@ $assert(str_contains($peerArtifacts, && str_contains($peerArtifacts, 'finishReceiveLocked(') && str_contains($migration, "'network_suspended' =>") && str_contains($migration, "'chunk_size_bytes'") - && str_contains($migration, "'retry_transfer', 'abort_transfer'") - && str_contains($migration, "'abort_prepare'") + && str_contains($migrationWorkflow, "'retry_transfer'") + && str_contains($migrationWorkflow, "'abort_transfer'") + && str_contains($migrationWorkflow, "'abort_prepare'") && str_contains($migration, 'queueMaintenanceMigrationReapplyLocked(') && str_contains($migration, 'queueActivationLocked(') && strpos($migration, 'queueMaintenanceMigrationReapplyLocked(') < strpos($migration, 'queueActivationLocked(') && str_contains($migration, "\$spec['guest_hostname'] ?? \$instance->name") - && str_contains($runtime, - "activation_running: '正在恢复目标电源与自启状态'") - && str_contains($runtime, "retry_activation: '重试目标状态恢复'") - && str_contains($runtime, - "abort_prepare: '中止迁移并恢复源实例'") - && str_contains($runtime, - "abort_transfer: '中止传输并恢复源实例'"), + && str_contains($migrationWorkflow, "'label' => '恢复目标电源状态'") + && str_contains($migrationWorkflow, "'label' => '重试目标状态恢复'") + && str_contains($migrationWorkflow, + "'label' => '中止迁移并恢复源实例'") + && str_contains($migrationWorkflow, + "'label' => '中止传输并恢复源实例'") + && str_contains($runtime, 'migration.recovery_options') + && str_contains($runtime, 'progress.steps'), 'PeerMigration V3 must transfer every snapshot offline, converge firewall before activation, and expose bounded recovery exits.'); $assert(str_contains($template, 'id="instances-backup-library"') diff --git a/web/tests/LegalPagesContractTest.php b/web/tests/LegalPagesContractTest.php index 6b313949..b76f8af4 100644 --- a/web/tests/LegalPagesContractTest.php +++ b/web/tests/LegalPagesContractTest.php @@ -73,7 +73,7 @@ $assert(str_contains($auth, "Config::get('tos_reg') === 'true'") 'Legal consent is only client-side and can be bypassed.'); $assets = $read('web/resources/views/venix/layout_assets.tpl'); -$assert(str_contains($assets, "value='2026102803'") +$assert(str_contains($assets, "value='2026102804'") && str_contains($admin, '?v={$venix_asset_version}') && str_contains($read('web/resources/views/venix/home/layout_header.tpl'), '?v={$venix_asset_version}'), 'Shared browser asset version was not coupled to the new assets.'); diff --git a/web/tests/PublicPageLocaleContractTest.php b/web/tests/PublicPageLocaleContractTest.php index 1f9448cc..20c730cd 100644 --- a/web/tests/PublicPageLocaleContractTest.php +++ b/web/tests/PublicPageLocaleContractTest.php @@ -80,7 +80,7 @@ $assert(preg_match('/\.landing-site-header\s*\{[^}]*position:\s*fixed;/s', $css) && str_contains($accountCss, 'padding-top: 88px;') && str_contains($accountCss, 'padding-top: 76px;'), 'Persistent desktop or mobile public-navigation styling is incomplete.'); -$assert(str_contains($read('web/resources/views/venix/layout_assets.tpl'), "value='2026102803'"), +$assert(str_contains($read('web/resources/views/venix/layout_assets.tpl'), "value='2026102804'"), 'The shared browser asset version was not advanced with the navigation styles.'); echo "Public page locale and shared-layout contract tests passed.\n"; diff --git a/web/tests/SiteBrandContractTest.php b/web/tests/SiteBrandContractTest.php index 7da3a274..349cad2a 100644 --- a/web/tests/SiteBrandContractTest.php +++ b/web/tests/SiteBrandContractTest.php @@ -69,7 +69,7 @@ foreach ($templates as $template) { } $assets = $read('web/resources/views/venix/layout_assets.tpl'); -$assert(str_contains($assets, "value='2026102803'"), +$assert(str_contains($assets, "value='2026102804'"), 'The shared asset version was not updated for the canonical logo.'); echo "Shared site-brand contract passed across public, auth, user, cloud and admin layouts.\n";