refactor: centralize migration workflow state
parent
83ca5abb53
commit
8d32cdfbd6
@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudInstance;
|
||||
use App\Models\CloudInstanceMigration;
|
||||
use App\Models\CloudOrder;
|
||||
use App\Models\CloudPublicIp;
|
||||
use App\Models\CloudResourceReservation;
|
||||
|
||||
/**
|
||||
* Resource-ledger settlement for maintenance migration.
|
||||
*
|
||||
* Stage execution decides when a result is proven. This module alone moves
|
||||
* order ownership, reservations and public IP assignments after that proof.
|
||||
*/
|
||||
final class CloudInstanceMigrationSettlement
|
||||
{
|
||||
/**
|
||||
* Make the order-linked reservation authoritative on the target while a
|
||||
* separate restore reservation accounts for the stopped source duplicate.
|
||||
*/
|
||||
public static function retainSourceLocked(
|
||||
CloudInstanceMigration $migration
|
||||
): void {
|
||||
if (self::isSettled($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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,612 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudInstanceMigration;
|
||||
|
||||
/**
|
||||
* Executable definition of every maintenance-migration stage.
|
||||
*
|
||||
* Job routing, uncertain-result reconciliation, manual recovery and the
|
||||
* administrator progress projection all read this one registry. Adding a
|
||||
* stage without its durable job field or recovery contract therefore fails
|
||||
* the registry integrity check instead of silently creating a dead end.
|
||||
*/
|
||||
final class CloudInstanceMigrationWorkflow
|
||||
{
|
||||
private const STAGES = [
|
||||
'prepare' => [
|
||||
'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<string,array<string,mixed>> */
|
||||
public static function stages(): array
|
||||
{
|
||||
self::assertIntegrity();
|
||||
return self::STAGES;
|
||||
}
|
||||
|
||||
/** @return array<string,mixed>|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<string> */
|
||||
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<array{action:string,label:string,destructive:bool}>
|
||||
*/
|
||||
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<string> */
|
||||
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<string,mixed>
|
||||
*/
|
||||
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<string,mixed>|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'],
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -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'}
|
||||
|
||||
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudInstanceMigration;
|
||||
use App\Services\Cloud\CloudInstanceMigrationService;
|
||||
use App\Services\Cloud\CloudInstanceMigrationWorkflow;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$migration = static function (
|
||||
string $state,
|
||||
string $recoveryAction = ''
|
||||
): CloudInstanceMigration {
|
||||
$value = new CloudInstanceMigration();
|
||||
$value->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";
|
||||
Loading…
Reference in New Issue