diff --git a/web/app/Services/Cloud/CloudInstanceMigrationService.php b/web/app/Services/Cloud/CloudInstanceMigrationService.php index 177bbc23..9581570b 100644 --- a/web/app/Services/Cloud/CloudInstanceMigrationService.php +++ b/web/app/Services/Cloud/CloudInstanceMigrationService.php @@ -325,19 +325,11 @@ final class CloudInstanceMigrationService 'id', (int) $locked->reservation_id )->lockForUpdate()->first(); - if ($sourceReservation === null - || (string) $sourceReservation->state !== 'committed' - || (int) $sourceReservation->host_id !== (int) $locked->host_id - || (int) $sourceReservation->instance_id !== (int) $locked->id - || (int) $sourceReservation->resource_pool_id - !== (int) $locked->resource_pool_id - || (int) ($sourceReservation->order_id ?? 0) - !== (int) $locked->order_id) { - throw new CloudRecoveryInstanceStateConflict( - 'PeerMigration 源资源预留与实例所属资源池不一致', - 409 - ); - } + self::assertSourcePlacement( + $locked, + $sourceReservation, + $resources + ); CloudPublicNetworkAllocationService::currentClaimSet( (int) $sourceReservation->id, 'committed', @@ -2743,6 +2735,33 @@ final class CloudInstanceMigrationService return $values; } + /** + * Fail closed before a maintenance migration can create a durable job. + * + * The original order offer is historical after a grow-only upgrade. The + * instance and its committed reservation are the current authority for + * both commercial identity and reserved capacity. + */ + private static function assertSourcePlacement( + CloudInstance $instance, + ?CloudResourceReservation $reservation, + array $resources + ): void { + CloudPlacementService::assertCurrentOrderPlacement( + $instance, + $reservation, + ['committed'] + ); + if ((int) $reservation->cpu_cores !== $resources['cpu_cores'] + || (int) $reservation->memory_mib !== $resources['memory_mib'] + || (int) $reservation->disk_gib !== $resources['disk_gib']) { + throw new CloudRecoveryInstanceStateConflict( + 'PeerMigration 源资源预留与实例当前规格不一致', + 409 + ); + } + } + private static function finalizeTargetSpec( array $source, array $body, diff --git a/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php b/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php index 04313734..f788dc4c 100644 --- a/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php +++ b/web/app/Services/Cloud/CloudInstanceMigrationSettlement.php @@ -90,7 +90,8 @@ final class CloudInstanceMigrationSettlement || (string) $source->state !== 'held' || $source->instance_id !== null || (int) $source->order_id !== (int) $order->id - || (int) $source->offer_id !== (int) $order->offer_id + || (int) ($instance->offer_id ?? 0) < 1 + || (int) $source->offer_id !== (int) $instance->offer_id || $source->backup_restore_id !== null || $source->backup_restore_attempt_key !== null || $source->migration_source_debt_id !== null @@ -120,6 +121,7 @@ final class CloudInstanceMigrationSettlement (string) $migration->source_uuid, $frozenSourceClaimSet ); + $currentOfferId = (int) $source->offer_id; $now = time(); $debt = new CloudInstanceMigrationSourceDebt(); $debt->debt_key = bin2hex(random_bytes(16)); @@ -153,7 +155,7 @@ final class CloudInstanceMigrationSettlement $source->save(); $target->backup_restore_id = null; $target->order_id = (int) $order->id; - $target->offer_id = (int) $order->offer_id; + $target->offer_id = $currentOfferId; $target->updated_at = $now; $target->save(); $restore->reservation_id = null; @@ -415,7 +417,7 @@ final class CloudInstanceMigrationSettlement || $source->instance_id !== null || (int) $source->host_id !== (int) $migration->source_host_id || (int) $target->order_id !== (int) $order->id - || (int) $target->offer_id !== (int) $order->offer_id + || (int) $target->offer_id !== (int) $instance->offer_id || $target->backup_restore_id !== null || $target->backup_restore_attempt_key !== null || $target->migration_source_debt_id !== null @@ -543,7 +545,7 @@ final class CloudInstanceMigrationSettlement || (string) $source->release_reason !== 'maintenance_migration_source_removed' || (int) $target->order_id !== (int) $order->id - || (int) $target->offer_id !== (int) $order->offer_id + || (int) $target->offer_id !== (int) $instance->offer_id || $target->backup_restore_id !== null || $target->backup_restore_attempt_key !== null || $target->migration_source_debt_id !== null diff --git a/web/tests/CloudInstanceMigrationWorkflowTest.php b/web/tests/CloudInstanceMigrationWorkflowTest.php index 2027da71..753aed13 100644 --- a/web/tests/CloudInstanceMigrationWorkflowTest.php +++ b/web/tests/CloudInstanceMigrationWorkflowTest.php @@ -2,7 +2,9 @@ require dirname(__DIR__) . '/vendor/autoload.php'; +use App\Models\CloudInstance; use App\Models\CloudInstanceMigration; +use App\Models\CloudResourceReservation; use App\Services\Cloud\CloudInstanceMigrationService; use App\Services\Cloud\CloudInstanceMigrationWorkflow; @@ -99,6 +101,72 @@ foreach ($expectedFailureContracts as $stageKey => $contract) { } $service = new ReflectionClass(CloudInstanceMigrationService::class); +$assertSourcePlacement = $service->getMethod('assertSourcePlacement'); +$sourceInstance = new CloudInstance(); +$sourceInstance->id = 100; +$sourceInstance->user_id = 9; +$sourceInstance->order_id = 200; +$sourceInstance->reservation_id = 300; +$sourceInstance->offer_id = 42; +$sourceInstance->host_id = 1; +$sourceInstance->resource_pool_id = 10; +$sourceReservation = new CloudResourceReservation(); +$sourceReservation->id = 300; +$sourceReservation->order_id = 200; +$sourceReservation->offer_id = 42; +$sourceReservation->user_id = 9; +$sourceReservation->host_id = 1; +$sourceReservation->resource_pool_id = 10; +$sourceReservation->instance_id = 100; +$sourceReservation->backup_restore_id = null; +$sourceReservation->backup_restore_attempt_key = null; +$sourceReservation->migration_source_debt_id = null; +$sourceReservation->operation_id = md5('migration-source-placement'); +$sourceReservation->state = 'committed'; +$sourceReservation->cpu_cores = 4; +$sourceReservation->memory_mib = 4096; +$sourceReservation->disk_gib = 80; +$sourceResources = [ + 'cpu_cores' => 4, + 'memory_mib' => 4096, + 'disk_gib' => 80, + 'bandwidth_mbps' => 1000, +]; +$assertSourcePlacement->invoke( + null, + $sourceInstance, + $sourceReservation, + $sourceResources +); +$staleOfferRejected = false; +$sourceReservation->offer_id = 41; +try { + $assertSourcePlacement->invoke( + null, + $sourceInstance, + $sourceReservation, + $sourceResources + ); +} catch (Throwable) { + $staleOfferRejected = true; +} +$assert($staleOfferRejected, + 'Migration accepted a stale pre-upgrade source offer.'); +$sourceReservation->offer_id = 42; +$sourceReservation->disk_gib = 40; +$staleCapacityRejected = false; +try { + $assertSourcePlacement->invoke( + null, + $sourceInstance, + $sourceReservation, + $sourceResources + ); +} catch (Throwable) { + $staleCapacityRejected = true; +} +$assert($staleCapacityRejected, + 'Migration accepted stale pre-upgrade source capacity.'); $jobKinds = []; $jobFields = []; $recoveryActions = []; diff --git a/web/tests/CloudMaintenanceMigrationFinalizerTest.php b/web/tests/CloudMaintenanceMigrationFinalizerTest.php index 493e83b3..f4aed0e1 100644 --- a/web/tests/CloudMaintenanceMigrationFinalizerTest.php +++ b/web/tests/CloudMaintenanceMigrationFinalizerTest.php @@ -329,7 +329,7 @@ Capsule::table('cloud_public_ip_pools')->insert([ '2404:c140:156:20::1'), ]); -$seed = static function (int $n) use ($now): array { +$seed = static function (int $n, int $currentOfferId = 41) use ($now): array { $instanceId = 100 + $n; $orderId = 200 + $n; $restoreId = 300 + $n; @@ -376,7 +376,7 @@ $seed = static function (int $n) use ($now): array { Capsule::table('cloud_instances')->insert([ 'id' => $instanceId, 'uuid' => $sourceUuid, 'host_id' => 1, 'user_id' => 9, 'order_id' => $orderId, 'plan_id' => 31, - 'offer_id' => 41, 'resource_pool_id' => 10, + 'offer_id' => $currentOfferId, 'resource_pool_id' => 10, 'reservation_id' => $sourceReservationId, 'current_period_id' => 700 + $n, 'expires_at' => $now + 86400, 'renewal_price' => '19.95', 'renewal_days' => 30, @@ -419,7 +419,8 @@ $seed = static function (int $n) use ($now): array { 'operation_id' => $sourceOperation, 'order_id' => $orderId, 'backup_restore_id' => null, 'backup_restore_attempt_key' => null, - 'migration_source_debt_id' => null, 'offer_id' => 41, + 'migration_source_debt_id' => null, + 'offer_id' => $currentOfferId, 'resource_pool_id' => 10, 'host_id' => 1, 'user_id' => 9, 'instance_id' => $instanceId, 'network_attachment_id' => null, 'public_ip_pool_id' => null, 'public_ip_id' => null, @@ -520,7 +521,7 @@ $seed = static function (int $n) use ($now): array { 'instanceId', 'orderId', 'restoreId', 'migrationId', 'sourceReservationId', 'targetReservationId', 'sourceOperation', 'targetOperation', 'sourceUuid', 'targetUuid', - 'sourceSet', 'targetSet' + 'sourceSet', 'targetSet', 'currentOfferId' ); }; @@ -786,7 +787,7 @@ $assert((string) $rolledBack->state === 'rolled_back' // Activation must preserve a powered-off expired service exactly as frozen at // migration start, including the durable guest hostname. -$poweredOffCase = $seed(7); +$poweredOffCase = $seed(7, 42); $poweredOffDebt = $cutover($poweredOffCase); $poweredSpec = json_decode((string) CloudInstance::findOrFail( $poweredOffCase['instanceId'] @@ -838,16 +839,23 @@ $activated = CloudInstanceMigration::findOrFail( $activatedDebt = CloudInstanceMigrationSourceDebt::findOrFail( (int) $poweredOffDebt->id ); +$poweredOffOrder = Capsule::table('cloud_orders') + ->find($poweredOffCase['orderId']); +$poweredOffTarget = Capsule::table('cloud_resource_reservations') + ->find($poweredOffCase['targetReservationId']); $assert((string) $activated->state === 'source_retained' && (string) $activatedDebt->state === 'retained' && (string) $poweredOff->service_status === 'suspended_expired' && (string) $poweredOff->network_status === 'blocked_expired' - && str_contains((string) $poweredOff->spec_json, 'retained.example'), + && str_contains((string) $poweredOff->spec_json, 'retained.example') + && (int) $poweredOff->offer_id === 42 + && (int) $poweredOffTarget->offer_id === 42 + && (int) $poweredOffOrder->offer_id === 41, 'Activation changed powered-off, expired-network, or hostname state.'); // Automatic cleanup: cutover creates prepared debt, then the exact cleanup // operation releases both source families without moving either claim set. -$automatic = $seed(1); +$automatic = $seed(1, 42); $historicalSourceOperation = md5('source-history-automatic'); $activeSourceClaims = Capsule::table('cloud_resource_reservation_ips') ->where('reservation_id', $automatic['sourceReservationId']) @@ -914,6 +922,9 @@ $releasedDebt = Capsule::table('cloud_instance_migration_source_debts') ->find((int) $debt->id); $assert((int) $instance->reservation_id === $automatic['targetReservationId'] && (int) $order->reservation_id === $automatic['targetReservationId'] + && (int) $instance->offer_id === 42 + && (int) $target->offer_id === 42 + && (int) $order->offer_id === 41 && (int) $target->order_id === $automatic['orderId'] && $target->backup_restore_id === null && (string) $target->state === 'committed'