From 61c99a46804fa2d5b8e26edcd482b1703b3b3035 Mon Sep 17 00:00:00 2001 From: codex Date: Tue, 3 Nov 2026 19:26:01 +0800 Subject: [PATCH] feat: route pool replacement through dual-stack transitions --- web/app/Services/Cloud/CloudJobService.php | 221 -- ...oudPublicIpPoolReplacementBatchService.php | 828 ++++++-- .../CloudPublicNetworkTransitionBatchPlan.php | 45 + .../CloudPublicNetworkTransitionCommand.php | 4 +- .../CloudPublicNetworkTransitionWorkflow.php | 289 ++- ...ublicIpPoolReplacementBatchServiceTest.php | 1855 ++++++++++------- ...ublicIpPoolReplacementHttpContractTest.php | 5 +- ...licIpPoolReplacementSchemaContractTest.php | 47 +- ...licNetworkLegacyWriterGateContractTest.php | 28 +- ...cNetworkTransitionWorkflowContractTest.php | 18 +- 10 files changed, 2138 insertions(+), 1202 deletions(-) create mode 100644 web/app/Services/Cloud/CloudPublicNetworkTransitionBatchPlan.php diff --git a/web/app/Services/Cloud/CloudJobService.php b/web/app/Services/Cloud/CloudJobService.php index 89c291b8..c1c49ef0 100644 --- a/web/app/Services/Cloud/CloudJobService.php +++ b/web/app/Services/Cloud/CloudJobService.php @@ -3,13 +3,9 @@ namespace App\Services\Cloud; use App\Models\CloudHost; use App\Models\CloudHostAgent; -use App\Models\CloudHostNetworkAttachment; use App\Models\CloudInstance; use App\Models\CloudJob; -use App\Models\CloudPublicIp; -use App\Models\CloudPublicIpPool; use App\Models\CloudResourcePool; -use App\Models\CloudResourceReservation; use App\Models\CloudTemplate; use App\Models\CloudUserIso; use App\Models\User; @@ -2694,150 +2690,6 @@ final class CloudJobService }, 3); } - /** - * Create one administrator-owned child of a durable public-pool - * replacement batch. The caller owns the instance/host/network ledger - * locks and has already proved that all batch targets fit atomically. - * This method repeats the exact per-instance identity checks, freezes the - * same strict Agent body as an ordinary network_change, and fences the - * target address before the surrounding transaction may commit. - */ - public static function enqueuePoolReplacementNetworkChangeLocked( - CloudInstance $instance, - CloudResourceReservation $reservation, - CloudHostNetworkAttachment $sourceAttachment, - CloudHostNetworkAttachment $targetAttachment, - CloudPublicIpPool $sourcePool, - CloudPublicIpPool $targetPool, - CloudPublicIp $sourceIp, - CloudPublicIp $targetIp, - CloudJob $parent, - int $actorUserId, - string $batchKey - ): CloudJob { - CloudPublicNetworkTransitionGate::assertWriterReady(); - if ($actorUserId < 1 - || preg_match('/^[a-f0-9]{32}$/D', $batchKey) !== 1 - || (string) $parent->kind - !== 'public_ip_pool_replacement_batch' - || (string) $parent->execution_mode !== 'local' - || $parent->parent_job_id !== null - || (int) $parent->user_id !== $actorUserId - || (int) $instance->host_id !== (int) $reservation->host_id - || (int) $reservation->instance_id !== (int) $instance->id - || (int) $reservation->resource_pool_id - !== (int) $instance->resource_pool_id - || (string) $reservation->state !== 'committed' - || (int) $reservation->network_attachment_id - !== (int) $sourceAttachment->id - || (int) $reservation->public_ip_pool_id - !== (int) $sourcePool->id - || (int) $reservation->public_ip_id !== (int) $sourceIp->id - || (int) $sourceAttachment->host_id !== (int) $instance->host_id - || (int) $targetAttachment->host_id !== (int) $instance->host_id - || (int) $sourceAttachment->line_id - !== (int) $targetAttachment->line_id - || (int) $sourcePool->host_id !== (int) $instance->host_id - || (int) $targetPool->host_id !== (int) $instance->host_id - || (int) $sourcePool->network_attachment_id - !== (int) $sourceAttachment->id - || (int) $targetPool->network_attachment_id - !== (int) $targetAttachment->id - || (int) $sourceIp->host_id !== (int) $instance->host_id - || (int) $targetIp->host_id !== (int) $instance->host_id - || (int) $sourceIp->public_ip_pool_id !== (int) $sourcePool->id - || (int) $targetIp->public_ip_pool_id !== (int) $targetPool->id - || (string) $sourceIp->state !== 'assigned' - || (int) $sourceIp->reservation_id !== (int) $reservation->id - || (int) $sourceIp->instance_id !== (int) $instance->id - || !hash_equals( - (string) $reservation->operation_id, - (string) ($sourceIp->operation_id ?? '') - ) - || (string) $targetIp->state !== 'available' - || $targetIp->reservation_id !== null - || $targetIp->instance_id !== null - || $targetIp->operation_id !== null - || !self::publicIpBelongsToPool($targetIp, $targetPool) - || !self::instancePublicIpMatches($instance, $sourceIp) - || !CloudNetworkSelector::isValid( - (string) $targetAttachment->network_selector - ) - || preg_match('/^[a-f0-9]{32}$/D', - (string) $targetPool->pool_key) !== 1) { - throw new CloudError( - '批量更换公网 IP 的实例或网络账本不一致', - 409 - ); - } - if (self::blockingQueryForInstance((int) $instance->id)->exists()) { - throw new CloudError('虚拟机已有任务,请等待完成', 409); - } - - $body = [ - 'network' => (string) $targetAttachment->network_selector, - 'public_pool_key' => (string) $targetPool->pool_key, - 'public_pool_cidr' => (string) $targetPool->cidr, - 'public_ip_cidr' => (string) $targetIp->address . '/' - . (int) $targetIp->prefix_length, - 'gateway' => (string) $targetPool->gateway, - 'dns_servers' => self::poolDnsServers($targetPool), - 'confirm_restart' => true, - ]; - $requestKey = substr( - 'pool-replacement-' . $batchKey . '-' . (int) $instance->id, - 0, - 128 - ); - $fingerprint = hash('sha256', json_encode([ - 'kind' => 'network_change', - 'instance_id' => (int) $instance->id, - 'parent_job_id' => (int) $parent->id, - 'body' => $body, - ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES)); - if (CloudJob::where('user_id', $actorUserId) - ->where('request_idempotency_key', $requestKey)->exists()) { - throw new CloudError('批量更换公网 IP 子任务标识冲突', 409); - } - - $now = time(); - $job = new CloudJob(); - $job->operation_id = bin2hex(random_bytes(16)); - $job->request_idempotency_key = $requestKey; - $job->request_fingerprint = $fingerprint; - $job->parent_job_id = (int) $parent->id; - $job->instance_id = (int) $instance->id; - $job->user_id = $actorUserId; - $job->host_id = (int) $instance->host_id; - $job->kind = 'network_change'; - $job->execution_mode = 'agent'; - $job->sealed_payload = CloudSecrets::encrypt([ - 'method' => 'POST', - 'request_target' => '/v1/vms/' - . rawurlencode((string) $instance->uuid) . '/network', - 'body' => $body, - 'credential' => null, - 'owner_id' => (int) $instance->user_id, - 'can_manage' => true, - 'source_public_ip_id' => (int) $sourceIp->id, - 'target_public_ip_id' => (int) $targetIp->id, - 'source_network_attachment_id' => (int) $sourceAttachment->id, - 'target_network_attachment_id' => (int) $targetAttachment->id, - 'source_public_ip_pool_id' => (int) $sourcePool->id, - 'target_public_ip_pool_id' => (int) $targetPool->id, - 'public_pool_replacement_batch_key' => $batchKey, - ], 'job-payload'); - $job->available_at = $job->created_at = $job->updated_at = $now; - $job->save(); - - $targetIp->state = 'changing'; - $targetIp->instance_id = (int) $instance->id; - $targetIp->operation_id = (string) $job->operation_id; - $targetIp->updated_at = $now; - $targetIp->save(); - return $job; - } - /** * Queue the irreversible second phase of expiry cleanup. * @@ -4249,77 +4101,4 @@ final class CloudJobService ]]; } - private static function publicIpBelongsToPool(CloudPublicIp $ip, - CloudPublicIpPool $pool): bool - { - $parts = explode('/', (string) $pool->cidr, 2); - if (count($parts) !== 2 || !ctype_digit($parts[1])) { - return false; - } - $prefix = (int) $parts[1]; - if ($prefix < 24 || $prefix > 30 - || (int) $ip->prefix_length !== (int) $pool->guest_prefix_length) { - return false; - } - $address = self::ipv4Number((string) $ip->address); - $network = self::ipv4Number($parts[0]); - if ($address === null || $network === null) { - return false; - } - $block = 2 ** (32 - $prefix); - $base = (int) (floor($network / $block) * $block); - return $address > $base && $address < $base + $block - 1; - } - - private static function instancePublicIpMatches(CloudInstance $instance, - CloudPublicIp $ip): bool - { - $spec = json_decode((string) $instance->spec_json, true); - $recorded = is_array($spec) && !array_is_list($spec) - ? ($spec['public_ip_cidr'] ?? null) : null; - return is_string($recorded) && hash_equals( - (string) $ip->address . '/' . (int) $ip->prefix_length, - trim($recorded) - ); - } - - private static function ipv4Number(string $value): ?int - { - if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { - return null; - } - $packed = inet_pton($value); - if ($packed === false || strlen($packed) !== 4) { - return null; - } - $parts = unpack('Nnumber', $packed); - return is_array($parts) && isset($parts['number']) - ? (int) sprintf('%u', $parts['number']) : null; - } - - private static function poolDnsServers(CloudPublicIpPool $pool): array - { - try { - $values = json_decode( - (string) $pool->dns_servers_json, - true, - 16, - JSON_THROW_ON_ERROR - ); - } catch (\Throwable $error) { - throw new CloudError('公网地址池 DNS 配置损坏', 409); - } - if (!is_array($values) || !array_is_list($values) - || $values === [] || count($values) > 4 - || count(array_filter($values, static fn ($value): bool => - is_string($value) && filter_var( - $value, - FILTER_VALIDATE_IP, - FILTER_FLAG_IPV4 - ) !== false - )) !== count($values)) { - throw new CloudError('公网地址池 DNS 配置损坏', 409); - } - return array_values($values); - } } diff --git a/web/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php b/web/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php index 518c97d8..e0aab599 100644 --- a/web/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php +++ b/web/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php @@ -11,10 +11,12 @@ use App\Models\CloudPublicIp; use App\Models\CloudPublicIpPool; use App\Models\CloudPublicIpPoolReplacementBatch; use App\Models\CloudPublicIpPoolReplacementBatchItem; +use App\Models\CloudPublicNetworkTransition; use App\Models\CloudResourcePool; use App\Models\CloudResourceReservation; use App\Models\CloudResourceReservationIp; use App\Models\User; +use Illuminate\Database\Capsule\Manager as DB; /** * Durable same-host replacement of every assigned address in one public pool. @@ -61,6 +63,9 @@ final class CloudPublicIpPoolReplacementBatchService ->where( 'cloud_resource_reservation_ips.public_ip_pool_id', (int) $source->id + )->where( + 'cloud_resource_reservation_ips.address_family', + (int) $source->address_family )->where('cloud_resource_reservation_ips.active_claim', 1) ->where('cloud_resource_reservation_ips.state', 'committed') ->where('reservation.state', 'committed') @@ -87,6 +92,11 @@ final class CloudPublicIpPoolReplacementBatchService $targets = []; $pools = CloudPublicIpPool::where('host_id', $hostId) ->where('id', '<>', (int) $source->id) + ->where('address_family', (int) $source->address_family) + ->where( + 'network_attachment_id', + (int) $source->network_attachment_id + ) ->where('state', 'active')->where('sync_state', 'applied') ->whereColumn('observed_revision', '>=', 'desired_revision') ->orderBy('priority')->orderBy('id')->get(); @@ -98,20 +108,12 @@ final class CloudPublicIpPoolReplacementBatchService || (int) $attachment->host_id !== $hostId || (int) $attachment->line_id !== (int) $sourceAttachment->line_id + || (int) $attachment->id !== (int) $sourceAttachment->id || !CloudPlacementNetworkService:: attachmentReadyForAgent($attachment, $agent)) { continue; } - $available = 0; - foreach (CloudPublicIp::where( - 'public_ip_pool_id', (int) $pool->id - )->where('state', 'available')->whereNull('reservation_id') - ->whereNull('instance_id')->whereNull('operation_id') - ->orderBy('id')->get() as $ip) { - if (self::targetIpIsSafe($ip, $pool, $host)) { - ++$available; - } - } + $available = self::availableCount($pool, $host); $targets[] = [ 'pool_key' => (string) $pool->pool_key, 'name' => (string) $pool->name, @@ -216,7 +218,6 @@ final class CloudPublicIpPoolReplacementBatchService array $actor, array $input): array { CloudAccess::manage($actor); - CloudPublicNetworkTransitionGate::assertWriterReady(); CloudInput::only($input, [ 'target_pool_key', 'expected_source_revision', 'expected_target_revision', 'confirm_replacement', 'request_key', @@ -307,6 +308,9 @@ final class CloudPublicIpPoolReplacementBatchService ->where( 'cloud_resource_reservation_ips.public_ip_pool_id', (int) $sourceRef->id + )->where( + 'cloud_resource_reservation_ips.address_family', + (int) $sourceRef->address_family )->where( 'cloud_resource_reservation_ips.active_claim', 1 @@ -330,6 +334,27 @@ final class CloudPublicIpPoolReplacementBatchService 409 ); } + $reservationIds = $instances->pluck('reservation_id') + ->map(static fn ($id): int => (int) $id) + ->filter()->unique()->sort()->values()->all(); + if (count($reservationIds) !== count($instanceIds)) { + throw new CloudError('源地址池实例缺少资源预留', 409); + } + foreach ($instances as $instance) { + CloudInstanceMigrationService:: + assertInstanceMutationAvailable( + $instance, + 'network_change' + ); + if (CloudJobService::blockingQueryForInstance( + (int) $instance->id + )->exists()) { + throw new CloudError( + '源地址池中有实例正在执行其他任务', + 409 + ); + } + } $host = CloudHost::where('id', $hostId) ->lockForUpdate()->first(); $agent = CloudHostAgent::where('host_id', $hostId) @@ -338,13 +363,67 @@ final class CloudPublicIpPoolReplacementBatchService throw new CloudError('宿主机 Agent 暂不可用于批量更换 IP', 409); } + $activeTransitions = CloudPublicNetworkTransition::whereIn( + 'instance_id', $instanceIds + )->where('active_transition', 1)->orderBy('id') + ->lockForUpdate()->get(['id']); + if ($activeTransitions->isNotEmpty()) { + throw new CloudError( + '源地址池中有实例正在执行公网网络变更', + 409 + ); + } + $resourcePoolIds = $instances->pluck('resource_pool_id') + ->map(static fn ($id): int => (int) $id) + ->unique()->sort()->values()->all(); + $resourcePools = CloudResourcePool::whereIn( + 'id', $resourcePoolIds + )->orderBy('id')->lockForUpdate()->get()->keyBy('id'); + if ($resourcePools->count() !== count($resourcePoolIds)) { + throw new CloudError('源地址池实例资源池缺失', 409); + } + + $claimHints = DB::table('cloud_resource_reservation_ips') + ->whereIn('reservation_id', $reservationIds) + ->where('active_claim', 1) + ->where('state', 'committed') + ->orderBy('reservation_id')->orderBy('address_family') + ->orderBy('id')->get()->all(); + $claimHintCounts = []; $poolIds = [(int) $sourceRef->id, (int) $targetRef->id]; + $attachmentIds = []; + foreach ($claimHints as $hint) { + $reservationId = (int) $hint->reservation_id; + $family = (int) $hint->address_family; + if (!in_array($family, [4, 6], true) + || isset($claimHintCounts[$reservationId][$family])) { + throw new CloudError( + '源地址池实例活动双栈声明不完整', + 409 + ); + } + $claimHintCounts[$reservationId][$family] = true; + $poolIds[] = (int) $hint->public_ip_pool_id; + $attachmentIds[] = (int) $hint->network_attachment_id; + } + foreach ($reservationIds as $reservationId) { + if (array_keys($claimHintCounts[$reservationId] ?? []) + !== [4, 6]) { + throw new CloudError( + '源地址池实例活动双栈声明不完整', + 409 + ); + } + } + $poolIds = array_values(array_unique($poolIds)); sort($poolIds, SORT_NUMERIC); $poolRefs = CloudPublicIpPool::whereIn('id', $poolIds) ->orderBy('id')->get()->keyBy('id'); - $attachmentIds = $poolRefs->pluck('network_attachment_id') - ->map(static fn ($id): int => (int) $id) - ->unique()->sort()->values()->all(); + foreach ($poolRefs as $poolRef) { + $attachmentIds[] = (int) $poolRef->network_attachment_id; + } + $attachmentIds = array_values(array_unique($attachmentIds)); + sort($attachmentIds, SORT_NUMERIC); $attachments = CloudHostNetworkAttachment::whereIn( 'id', $attachmentIds )->orderBy('id')->lockForUpdate()->get()->keyBy('id'); @@ -391,107 +470,95 @@ final class CloudPublicIpPoolReplacementBatchService ) !== (int) $expectedTargetRevision || (int) $sourceAttachment->host_id !== $hostId || (int) $targetAttachment->host_id !== $hostId + || (int) $sourcePool->address_family + !== (int) $targetPool->address_family + || (int) $sourcePool->network_attachment_id + !== (int) $targetPool->network_attachment_id + || (int) $sourceAttachment->id + !== (int) $targetAttachment->id || (int) $sourceAttachment->line_id !== (int) $targetAttachment->line_id || !CloudPlacementNetworkService:: attachmentReadyForAgent($targetAttachment, $agent)) { throw new CloudError( - '源或目标公网地址池不满足同宿主机同线路的迁移条件', + '源或目标公网地址池不满足同地址族同网络接入的迁移条件', 409 ); } self::assertTargetPoolConfiguration($host, $targetPool); - $replacementPoolIds = [ - (int) $sourcePool->id, - (int) $targetPool->id, - ]; - if (CloudPublicIpPoolReplacementBatch::where( - 'host_id', $hostId - )->where('active_claim', 1)->where(function ( - $query - ) use ($replacementPoolIds): void { - $query->whereIn( - 'source_public_ip_pool_id', $replacementPoolIds - )->orWhereIn( - 'target_public_ip_pool_id', $replacementPoolIds - ); - })->lockForUpdate()->exists()) { - throw new CloudError( - '源或目标地址池已参与其他迁移批次', - 409 - ); - } - - $reservationIds = $instances->pluck('reservation_id') - ->map(static fn ($id): int => (int) $id) - ->filter()->unique()->sort()->values()->all(); - if (count($reservationIds) !== count($instanceIds)) { - throw new CloudError('源地址池实例缺少资源预留', 409); - } $reservations = CloudResourceReservation::whereIn( 'id', $reservationIds )->orderBy('id')->lockForUpdate()->get()->keyBy('id'); - $occupiedIds = CloudPublicIp::where( - 'public_ip_pool_id', (int) $sourcePool->id - )->where(function ($query): void { - $query->whereIn('state', self::OCCUPIED_IP_STATES) - ->orWhereNotNull('reservation_id') - ->orWhereNotNull('instance_id') - ->orWhereNotNull('operation_id'); - })->orderBy('id')->pluck('id')->map( + if ($reservations->count() !== count($reservationIds)) { + throw new CloudError('源地址池实例资源预留缺失', 409); + } + $allClaims = DB::table('cloud_resource_reservation_ips') + ->whereIn('reservation_id', $reservationIds) + ->orderBy('id')->lockForUpdate()->get(); + $claimsByReservation = $allClaims->groupBy('reservation_id'); + $activeClaimsByInstance = []; + $claimIpIds = $allClaims->pluck('public_ip_id')->map( static fn ($id): int => (int) $id - )->all(); - $targetCandidateIds = CloudPublicIp::where( - 'public_ip_pool_id', (int) $targetPool->id - )->where('state', 'available')->whereNull('reservation_id') - ->whereNull('instance_id')->whereNull('operation_id') - ->orderBy('id')->pluck('id')->map( - static fn ($id): int => (int) $id - )->all(); - $ipIds = array_values(array_unique([ - ...$occupiedIds, - ...$targetCandidateIds, - ])); - sort($ipIds, SORT_NUMERIC); - $ips = CloudPublicIp::whereIn('id', $ipIds)->orderBy('id') - ->lockForUpdate()->get()->keyBy('id'); - $targetIps = []; - foreach ($targetCandidateIds as $targetId) { - $candidate = $ips->get($targetId); - if ($candidate !== null && self::targetIpIsSafe( - $candidate, - $targetPool, - $host - )) { - $targetIps[] = $candidate; - } - if (count($targetIps) === count($instanceIds)) { - break; + )->unique()->sort()->values()->all(); + // One current locking read owns every claim address plus the + // complete source/target catalogs. A prior non-locking pluck + // would be a repeatable-read snapshot and could miss a sparse + // IPv6 row committed just before the pool lock was obtained. + $ips = CloudPublicIp::where(static function ($query) use ( + $claimIpIds, + $sourcePool, + $targetPool + ): void { + $query->whereIn('id', $claimIpIds)->orWhereIn( + 'public_ip_pool_id', + [(int) $sourcePool->id, (int) $targetPool->id] + ); + })->orderBy('id')->lockForUpdate()->get()->keyBy('id'); + foreach ($claimIpIds as $claimIpId) { + if (!$ips->has($claimIpId)) { + throw new CloudError( + '源地址池实例公网地址账本缺失', + 409 + ); } } - if (count($targetIps) !== count($instanceIds)) { - throw new CloudError('目标公网地址池可用 IP 数量不足', 409); - } - $sourceIpIds = []; foreach ($instanceIds as $instanceId) { $instance = $instances->get($instanceId); $reservation = $instance === null ? null : $reservations->get((int) $instance->reservation_id); $resourcePool = $reservation === null ? null - : CloudResourcePool::find( + : $resourcePools->get( (int) $reservation->resource_pool_id ); - $sourceIp = $reservation === null ? null - : $ips->get((int) $reservation->public_ip_id); + $claims = $reservation === null ? collect() + : ($claimsByReservation->get( + (int) $reservation->id + ) ?? collect()); + $activeClaims = []; + foreach ($claims as $claim) { + if ((int) ($claim->active_claim ?? 0) === 1 + && (string) $claim->state === 'committed') { + $claimFamily = (int) $claim->address_family; + if (isset($activeClaims[$claimFamily])) { + throw new CloudError( + '源地址池实例活动双栈声明重复', + 409 + ); + } + $activeClaims[$claimFamily] = $claim; + } + } + ksort($activeClaims, SORT_NUMERIC); $spec = $instance === null ? null : json_decode( (string) $instance->spec_json, true ); if ($instance === null || $reservation === null - || $resourcePool === null || $sourceIp === null + || $resourcePool === null || (string) $instance->status !== 'active' + || !$instance->serviceAvailable() || (int) $instance->host_id !== $hostId || !is_array($spec) || array_is_list($spec) || ($spec['managed'] ?? false) !== true @@ -503,43 +570,63 @@ final class CloudPublicIpPoolReplacementBatchService !== (int) $instance->resource_pool_id || (int) $resourcePool->line_id !== (int) $sourceAttachment->line_id - || (int) $reservation->network_attachment_id + || array_keys($activeClaims) !== [4, 6] + || (int) $activeClaims[4]->network_attachment_id + !== (int) $sourceAttachment->id + || (int) $activeClaims[6]->network_attachment_id !== (int) $sourceAttachment->id - || (int) $reservation->public_ip_pool_id - !== (int) $sourcePool->id - || (int) $sourceIp->host_id !== $hostId - || (int) $sourceIp->public_ip_pool_id - !== (int) $sourcePool->id - || (string) $sourceIp->state !== 'assigned' - || (int) $sourceIp->reservation_id - !== (int) $reservation->id - || (int) $sourceIp->instance_id !== $instanceId - || !hash_equals( - (string) $reservation->operation_id, - (string) ($sourceIp->operation_id ?? '') - )) { + || (int) $activeClaims[ + (int) $sourcePool->address_family + ]->public_ip_pool_id !== (int) $sourcePool->id) { throw new CloudError( '源地址池实例或资源预留账本不一致', 409 ); } - CloudInstanceMigrationService:: - assertInstanceMutationAvailable( - $instance, - 'network_change' - ); - if (CloudJobService::blockingQueryForInstance( - $instanceId - )->exists()) { - throw new CloudError( - '源地址池中有实例正在执行其他任务', - 409 - ); + $sourceIps = []; + foreach ([4, 6] as $family) { + $claim = $activeClaims[$family]; + $sourceIp = $ips->get((int) $claim->public_ip_id); + if (!$sourceIp instanceof CloudPublicIp + || (int) $claim->reservation_id + !== (int) $reservation->id + || (int) $claim->address_family !== $family + || (int) $sourceIp->host_id !== $hostId + || (int) $sourceIp->address_family !== $family + || (int) $sourceIp->public_ip_pool_id + !== (int) $claim->public_ip_pool_id + || (string) $sourceIp->state !== 'assigned' + || (int) $sourceIp->reservation_id + !== (int) $reservation->id + || (int) $sourceIp->reservation_ip_claim_id + !== (int) $claim->id + || (int) $sourceIp->instance_id !== $instanceId + || !hash_equals( + (string) $claim->operation_id, + (string) ($sourceIp->operation_id ?? '') + )) { + throw new CloudError( + '源地址池实例活动公网声明不一致', + 409 + ); + } + $sourceIps[$family] = $sourceIp; } - $sourceIpIds[] = (int) $sourceIp->id; + $changedFamily = (int) $sourcePool->address_family; + $sourceIpIds[] = (int) $sourceIps[$changedFamily]->id; + $activeClaimsByInstance[$instanceId] = [ + 'all_claims' => $claims->values()->all(), + 'active_claims' => $activeClaims, + 'source_ips' => $sourceIps, + 'resource_pool' => $resourcePool, + ]; } sort($sourceIpIds, SORT_NUMERIC); - $occupiedSorted = array_values(array_unique($occupiedIds)); + $occupiedSorted = $ips->filter(static fn ($ip): bool => + (int) $ip->public_ip_pool_id === (int) $sourcePool->id + && self::ipOccupied($ip) + )->pluck('id')->map(static fn ($id): int => (int) $id) + ->unique()->sort()->values()->all(); sort($occupiedSorted, SORT_NUMERIC); if ($sourceIpIds !== $occupiedSorted) { throw new CloudError( @@ -547,6 +634,33 @@ final class CloudPublicIpPoolReplacementBatchService 409 ); } + [$targetIps, $ips] = self::freezeExactTargetsLocked( + $targetPool, + $host, + $ips, + count($instanceIds) + ); + $replacementPoolIds = [ + (int) $sourcePool->id, + (int) $targetPool->id, + ]; + $activeBatches = CloudPublicIpPoolReplacementBatch::where( + 'host_id', $hostId + )->where('active_claim', 1)->where(function ( + $query + ) use ($replacementPoolIds): void { + $query->whereIn( + 'source_public_ip_pool_id', $replacementPoolIds + )->orWhereIn( + 'target_public_ip_pool_id', $replacementPoolIds + ); + })->orderBy('id')->lockForUpdate()->get(['id']); + if ($activeBatches->isNotEmpty()) { + throw new CloudError( + '源或目标地址池已参与其他迁移批次', + 409 + ); + } $parent = self::newParentJob( $actorUserId, @@ -579,27 +693,14 @@ final class CloudPublicIpPoolReplacementBatchService $reservation = $reservations->get( (int) $instance->reservation_id ); - $sourceIp = $ips->get( - (int) $reservation->public_ip_id - ); + $ledger = $activeClaimsByInstance[$instanceId]; + $family = (int) $sourcePool->address_family; + $sourceIp = $ledger['source_ips'][$family]; $targetIp = $targetIps[$index]; - $child = CloudJobService:: - enqueuePoolReplacementNetworkChangeLocked( - $instance, - $reservation, - $sourceAttachment, - $targetAttachment, - $sourcePool, - $targetPool, - $sourceIp, - $targetIp, - $parent, - $actorUserId, - $batchKey - ); $item = new CloudPublicIpPoolReplacementBatchItem(); $item->batch_id = (int) $batch->id; $item->instance_id = $instanceId; + $item->address_family = $family; $item->source_reservation_id = (int) $reservation->id; $item->source_public_ip_id = (int) $sourceIp->id; $item->target_public_ip_id = (int) $targetIp->id; @@ -607,11 +708,53 @@ final class CloudPublicIpPoolReplacementBatchService (int) $sourceAttachment->id; $item->target_network_attachment_id = (int) $targetAttachment->id; - $item->child_job_id = (int) $child->id; - $item->state = 'queued'; + $item->child_job_id = null; + $item->state = 'preparing'; $item->last_error_code = ''; $item->created_at = $item->updated_at = $now; $item->save(); + + $childRequestKey = substr( + 'pool-replacement-' . $batchKey . '-' + . $instanceId . '-' . $family, + 0, + 128 + ); + $command = CloudPublicNetworkTransitionCommand:: + poolReplacement( + $instanceId, + $actorUserId, + $childRequestKey, + (int) $parent->id, + (int) $item->id, + [$family => [ + 'pool_key' => (string) $targetPool->pool_key, + 'address_cidr' => (string) $targetIp->address + . '/' . (int) $targetIp->prefix_length, + ]] + ); + CloudPublicNetworkTransitionWorkflow:: + beginPoolReplacementBatch( + $command, + new CloudPublicNetworkTransitionBatchPlan( + $actorUser, + $parent, + $batch, + $item, + $instance, + $host, + $agent, + $ledger['resource_pool'], + $sourceAttachment, + $reservation, + $pools, + $ips, + $ledger['all_claims'], + $ledger['active_claims'], + $ledger['source_ips'], + $targetIp + ) + ); } return [ 'job' => $parent, @@ -635,6 +778,9 @@ final class CloudPublicIpPoolReplacementBatchService if ($parent === null || (string) $parent->kind !== self::PARENT_KIND) { return; } + if ($job->public_network_transition_id === null) { + throw new CloudError('公网地址池迁移子任务缺少网络变更账本', 409); + } // Hints discover immutable IDs only. The mutable catalog is locked // before batch/item rows so convergence never takes pool after batch. $itemHint = CloudPublicIpPoolReplacementBatchItem::where( @@ -677,6 +823,7 @@ final class CloudPublicIpPoolReplacementBatchService || (int) $item->instance_id !== (int) $job->instance_id) { throw new CloudError('公网地址池迁移批次账本不一致', 409); } + self::assertItemTransitionTopology($item, $batch, $parent, $job); $nextState = match ($catalogStatus) { 'succeeded' => 'succeeded', 'failed' => 'failed', @@ -841,6 +988,7 @@ final class CloudPublicIpPoolReplacementBatchService )->where(function ($query): void { $query->whereIn('state', self::OCCUPIED_IP_STATES) ->orWhereNotNull('reservation_id') + ->orWhereNotNull('reservation_ip_claim_id') ->orWhereNotNull('instance_id') ->orWhereNotNull('operation_id'); })->count(); @@ -916,6 +1064,72 @@ final class CloudPublicIpPoolReplacementBatchService if ($batch === null) { throw new CloudError('公网地址池迁移幂等账本不完整', 409); } + $items = CloudPublicIpPoolReplacementBatchItem::where( + 'batch_id', (int) $batch->id + )->orderBy('id')->get(); + $childIds = $items->pluck('child_job_id')->map( + static fn ($id): int => (int) $id + )->filter()->unique()->sort()->values()->all(); + $itemIds = $items->pluck('id')->map( + static fn ($id): int => (int) $id + )->all(); + $ipIds = $items->flatMap(static fn ($item): array => [ + (int) $item->source_public_ip_id, + (int) $item->target_public_ip_id, + ])->unique()->sort()->values()->all(); + $children = CloudJob::whereIn('id', $childIds) + ->orderBy('id')->get()->keyBy('id'); + $transitions = CloudPublicNetworkTransition::whereIn( + 'pool_replacement_batch_item_id', $itemIds + )->orderBy('id')->get()->keyBy('pool_replacement_batch_item_id'); + $ips = CloudPublicIp::whereIn('id', $ipIds) + ->orderBy('id')->get()->keyBy('id'); + if ($items->isEmpty() || count($childIds) !== $items->count() + || $children->count() !== $items->count() + || $transitions->count() !== $items->count() + || $ips->count() !== count($ipIds)) { + throw new CloudError('公网地址池迁移幂等拓扑不完整', 409); + } + foreach ($items as $item) { + $child = $children->get((int) $item->child_job_id); + $transition = $transitions->get((int) $item->id); + $sourceIp = $ips->get((int) $item->source_public_ip_id); + $targetIp = $ips->get((int) $item->target_public_ip_id); + if (!$child instanceof CloudJob + || !$transition instanceof CloudPublicNetworkTransition + || !$sourceIp instanceof CloudPublicIp + || !$targetIp instanceof CloudPublicIp + || !in_array((int) $item->address_family, [4, 6], true) + || (string) $item->state === 'preparing' + || (int) $item->source_network_attachment_id + !== (int) $item->target_network_attachment_id + || (int) $sourceIp->address_family + !== (int) $item->address_family + || (int) $targetIp->address_family + !== (int) $item->address_family + || (int) $sourceIp->public_ip_pool_id + !== (int) $batch->source_public_ip_pool_id + || (int) $targetIp->public_ip_pool_id + !== (int) $batch->target_public_ip_pool_id + || (int) $transition->pool_replacement_batch_item_id + !== (int) $item->id + || (string) $transition->kind !== 'pool_replacement' + || (int) $transition->instance_id !== (int) $item->instance_id + || (int) $transition->host_id !== (int) $batch->host_id + || (int) $transition->requested_by_user_id + !== (int) $job->user_id + || (int) $child->id !== (int) $item->child_job_id + || (int) ($child->parent_job_id ?? 0) !== (int) $job->id + || (int) ($child->public_network_transition_id ?? 0) + !== (int) $transition->id + || (int) $child->instance_id !== (int) $item->instance_id + || (int) $child->user_id !== (int) $job->user_id + || (int) $child->host_id !== (int) $batch->host_id + || (string) $child->kind !== 'network_change') { + throw new CloudError('公网地址池迁移幂等拓扑不一致', 409); + } + self::assertItemTransitionTopology($item, $batch, $job, $child); + } return [ 'job' => $job, 'batch' => self::display($batch), @@ -923,6 +1137,136 @@ final class CloudPublicIpPoolReplacementBatchService ]; } + private static function assertItemTransitionTopology( + CloudPublicIpPoolReplacementBatchItem $item, + CloudPublicIpPoolReplacementBatch $batch, + CloudJob $parent, + CloudJob $child + ): CloudPublicNetworkTransition { + $transition = $child->public_network_transition_id === null ? null + : CloudPublicNetworkTransition::find( + (int) $child->public_network_transition_id + ); + if ((string) $parent->kind !== self::PARENT_KIND + || (string) $parent->execution_mode !== 'local' + || $parent->parent_job_id !== null + || $parent->host_id !== null + || $parent->public_network_transition_id !== null + || (int) $batch->parent_job_id !== (int) $parent->id + || (int) $batch->requested_by_user_id !== (int) $parent->user_id + || (int) $item->batch_id !== (int) $batch->id + || !$transition instanceof CloudPublicNetworkTransition + || (string) $transition->kind !== 'pool_replacement' + || (int) $transition->pool_replacement_batch_item_id + !== (int) $item->id + || (int) $transition->instance_id !== (int) $item->instance_id + || (int) $transition->host_id !== (int) $batch->host_id + || (int) $transition->reservation_id + !== (int) $item->source_reservation_id + || (int) $transition->requested_by_user_id !== (int) $parent->user_id + || (int) $transition->source_network_attachment_id + !== (int) $item->source_network_attachment_id + || (int) $transition->target_network_attachment_id + !== (int) $item->target_network_attachment_id + || (int) $item->source_network_attachment_id + !== (int) $item->target_network_attachment_id + || (int) $child->id !== (int) $item->child_job_id + || (int) ($child->parent_job_id ?? 0) !== (int) $parent->id + || (int) ($child->public_network_transition_id ?? 0) + !== (int) $transition->id + || (int) $child->instance_id !== (int) $item->instance_id + || (int) $child->host_id !== (int) $batch->host_id + || (int) $child->user_id !== (int) $parent->user_id + || (string) $child->kind !== 'network_change' + || (string) $child->execution_mode !== 'agent' + || !hash_equals( + (string) $transition->operation_id, + (string) $child->operation_id + )) { + throw new CloudError('公网地址池迁移子任务拓扑不一致', 409); + } + $legs = DB::table('cloud_public_network_transition_ips') + ->where('transition_id', (int) $transition->id) + ->orderBy('address_family')->orderBy('id')->get()->all(); + if (count($legs) !== 2) { + throw new CloudError('公网地址池迁移双栈明细不完整', 409); + } + $changed = null; + $unchanged = null; + foreach ($legs as $leg) { + if ((bool) $leg->changed) { + if ($changed !== null || $leg->target_claim_id === null) { + throw new CloudError('公网地址池迁移变更明细损坏', 409); + } + $changed = $leg; + } else { + if ($unchanged !== null || $leg->target_claim_id !== null) { + throw new CloudError('公网地址池迁移保留明细损坏', 409); + } + $unchanged = $leg; + } + } + if ($changed === null || $unchanged === null + || (int) $changed->address_family !== (int) $item->address_family + || !in_array((int) $unchanged->address_family, [4, 6], true) + || (int) $unchanged->address_family === (int) $item->address_family) { + throw new CloudError('公网地址池迁移地址族明细不一致', 409); + } + $claimIds = [ + (int) $changed->source_claim_id, + (int) $changed->target_claim_id, + (int) $unchanged->source_claim_id, + ]; + $claimIds = array_values(array_unique($claimIds)); + sort($claimIds, SORT_NUMERIC); + $claims = DB::table('cloud_resource_reservation_ips') + ->whereIn('id', $claimIds)->orderBy('id')->get()->keyBy('id'); + $source = $claims->get((int) $changed->source_claim_id); + $target = $claims->get((int) $changed->target_claim_id); + $companion = $claims->get((int) $unchanged->source_claim_id); + // A terminal child no longer owns the instance mutation fence even + // while siblings keep the aggregate batch running. Historical replay + // therefore validates only its frozen companion identity; queued or + // unknown children with an active transition still require the live + // companion claim that will be sent to the Agent. + $requireActiveCompanion = in_array( + (string) $item->state, + ['queued', 'unknown'], + true + ) && (int) ($transition->active_transition ?? 0) === 1; + if ($claims->count() !== 3 + || !is_object($source) || !is_object($target) + || !is_object($companion) + || (int) $source->public_ip_id !== (int) $item->source_public_ip_id + || (int) $target->public_ip_id !== (int) $item->target_public_ip_id + || (int) $source->reservation_id + !== (int) $item->source_reservation_id + || (int) $target->reservation_id + !== (int) $item->source_reservation_id + || (int) $companion->reservation_id + !== (int) $item->source_reservation_id + || (int) $source->address_family !== (int) $item->address_family + || (int) $target->address_family !== (int) $item->address_family + || (int) $companion->address_family + !== (int) $unchanged->address_family + || (int) $source->public_ip_pool_id + !== (int) $batch->source_public_ip_pool_id + || (int) $target->public_ip_pool_id + !== (int) $batch->target_public_ip_pool_id + || (int) $source->network_attachment_id + !== (int) $item->source_network_attachment_id + || (int) $target->network_attachment_id + !== (int) $item->target_network_attachment_id + || (int) $companion->network_attachment_id + !== (int) $item->source_network_attachment_id + || ($requireActiveCompanion + && ((int) ($companion->active_claim ?? 0) !== 1 + || (string) ($companion->state ?? '') !== 'committed'))) { + throw new CloudError('公网地址池迁移声明明细不一致', 409); + } + return $transition; + } + private static function newParentJob(int $actorUserId, string $requestKey, string $fingerprint, int $hostId, CloudPublicIpPool $source, CloudPublicIpPool $target, @@ -976,16 +1320,9 @@ final class CloudPublicIpPoolReplacementBatchService private static function assertTargetPoolConfiguration(CloudHost $host, CloudPublicIpPool $pool): void { - $dns = self::decodeIpv4List((string) $pool->dns_servers_json, 4); - $reserved = self::decodeIpv4List( - (string) $pool->reserved_ips_json, - 254, - true - ); - if ($dns === null || $dns === [] || $reserved === null - || filter_var((string) $pool->gateway, FILTER_VALIDATE_IP, - FILTER_FLAG_IPV4) === false - || !self::addressInPool((string) $pool->gateway, $pool)) { + try { + CloudPublicIpPoolContract::fromRow($pool); + } catch (\Throwable) { throw new CloudError('目标公网地址池配置损坏', 409); } $management = CloudPanelUsageService::managementAddress($host); @@ -999,85 +1336,174 @@ final class CloudPublicIpPoolReplacementBatchService { if ((int) $ip->host_id !== (int) $pool->host_id || (int) $ip->public_ip_pool_id !== (int) $pool->id + || (int) $ip->address_family !== (int) $pool->address_family || (string) $ip->state !== 'available' - || $ip->reservation_id !== null || $ip->instance_id !== null + || $ip->reservation_id !== null + || $ip->reservation_ip_claim_id !== null + || $ip->instance_id !== null || $ip->operation_id !== null || (int) $ip->prefix_length - !== (int) $pool->guest_prefix_length - || !self::addressInPool((string) $ip->address, $pool)) { + !== (int) $pool->guest_prefix_length) { return false; } - $dns = self::decodeIpv4List((string) $pool->dns_servers_json, 4); - $reserved = self::decodeIpv4List( - (string) $pool->reserved_ips_json, - 254, - true - ); - if ($dns === null || $reserved === null) { + try { + $contract = CloudPublicIpPoolContract::fromRow($pool); + $address = CloudIpAddress::parse((string) $ip->address); + if (!is_string($ip->address_binary) + || !hash_equals($address->binary(), $ip->address_binary) + || !$contract->addressAllowed($address)) { + return false; + } + $management = CloudPanelUsageService::managementAddress($host); + return !is_string($management) || $management === '' + || !$address->equals(CloudIpAddress::parse($management)); + } catch (\Throwable) { return false; } - $blocked = array_values(array_unique([ - (string) $pool->gateway, - ...$dns, - ...$reserved, - ...array_filter([CloudPanelUsageService::managementAddress($host)]), - ])); - return !in_array((string) $ip->address, $blocked, true); } - private static function addressInPool(string $address, - CloudPublicIpPool $pool): bool + private static function availableCount(CloudPublicIpPool $pool, + CloudHost $host): int { - $parts = explode('/', (string) $pool->cidr, 2); - if (count($parts) !== 2 || !ctype_digit($parts[1])) { - return false; + try { + $contract = CloudPublicIpPoolContract::fromRow($pool); + } catch (\Throwable) { + return 0; } - $prefix = (int) $parts[1]; - $number = self::ipv4Number($address); - $network = self::ipv4Number($parts[0]); - if ($prefix < 24 || $prefix > 30 - || $number === null || $network === null) { - return false; + $rows = CloudPublicIp::where( + 'public_ip_pool_id', (int) $pool->id + )->orderBy('id')->get(); + if ($contract->family() === 4) { + return $rows->filter(static fn ($ip): bool => + self::targetIpIsSafe($ip, $pool, $host) + )->count(); } - $block = 2 ** (32 - $prefix); - $base = (int) (floor($network / $block) * $block); - return $number > $base && $number < $base + $block - 1; + $occupied = $rows->filter(static fn ($ip): bool => + self::ipOccupied($ip) + )->count(); + return max(0, $contract->maxAllocations() - $occupied); } - private static function ipv4Number(string $value): ?int + private static function ipOccupied(CloudPublicIp $ip): bool { - if (filter_var($value, FILTER_VALIDATE_IP, - FILTER_FLAG_IPV4) === false) { - return null; - } - $packed = inet_pton($value); - $parts = $packed === false ? false : unpack('Nnumber', $packed); - return is_array($parts) && isset($parts['number']) - ? (int) sprintf('%u', $parts['number']) : null; + return in_array((string) $ip->state, + self::OCCUPIED_IP_STATES, true) + || $ip->reservation_id !== null + || $ip->reservation_ip_claim_id !== null + || $ip->instance_id !== null + || $ip->operation_id !== null; } - /** @return list|null */ - private static function decodeIpv4List(string $json, int $maximum, - bool $allowEmpty = false): ?array - { + /** + * @return array{0:list,1:\Illuminate\Support\Collection} + */ + private static function freezeExactTargetsLocked( + CloudPublicIpPool $pool, + CloudHost $host, + $lockedIps, + int $needed + ): array { + if ($needed < 1) { + throw new CloudError('目标公网地址池迁移数量无效', 409); + } + try { + $contract = CloudPublicIpPoolContract::fromRow($pool); + } catch (\Throwable) { + throw new CloudError('目标公网地址池配置损坏', 409); + } + $poolRows = $lockedIps->filter(static fn ($ip): bool => + (int) $ip->public_ip_pool_id === (int) $pool->id + )->sortBy('id')->values(); + $selected = $poolRows->filter(static fn ($ip): bool => + self::targetIpIsSafe($ip, $pool, $host) + )->take($needed)->values()->all(); + if ($contract->family() === 4) { + if (count($selected) !== $needed) { + throw new CloudError('目标公网 IPv4 地址池可用地址不足', 409); + } + return [$selected, $lockedIps]; + } + + $occupied = $poolRows->filter(static fn ($ip): bool => + self::ipOccupied($ip) + )->count(); + if ($contract->maxAllocations() - $occupied < $needed) { + throw new CloudError('目标公网 IPv6 稀疏容量不足', 409); + } + if (count($selected) === $needed) { + return [$selected, $lockedIps]; + } + $start = $contract->allocationStart(); + $end = $contract->allocationEnd(); + $candidate = $contract->allocationCursor() ?? $start; + if (!$start instanceof CloudIpAddress + || !$end instanceof CloudIpAddress + || !$candidate instanceof CloudIpAddress) { + throw new CloudError('目标公网 IPv6 稀疏分配合同损坏', 409); + } + $seen = []; + foreach ($lockedIps as $row) { + if (is_string($row->address_binary)) { + $seen[bin2hex($row->address_binary)] = true; + } + } + $management = null; try { - $values = json_decode($json, true, 16, JSON_THROW_ON_ERROR); - } catch (\Throwable $error) { - return null; + $rawManagement = CloudPanelUsageService::managementAddress($host); + $management = is_string($rawManagement) && $rawManagement !== '' + ? CloudIpAddress::parse($rawManagement) : null; + } catch (\Throwable) { + throw new CloudError('宿主机管理地址配置损坏', 409); } - if (!is_array($values) || !array_is_list($values) - || count($values) > $maximum - || (!$allowEmpty && $values === []) - || count(array_filter($values, static fn ($value): bool => - is_string($value) && filter_var( - $value, - FILTER_VALIDATE_IP, - FILTER_FLAG_IPV4 - ) !== false - )) !== count($values)) { - return null; + $attempts = max(1, $poolRows->count() + + $contract->maxAllocations() + + count($contract->reservedAddresses()) + + count($contract->dnsServers()) + 2); + $now = time(); + for ($attempt = 0; $attempt < $attempts + && count($selected) < $needed; $attempt++) { + $current = $candidate; + $candidate = $candidate->compare($end) === 0 + ? $start : $candidate->next(); + $binaryKey = bin2hex($current->binary()); + if (isset($seen[$binaryKey]) + || !$contract->addressAllowed($current) + || ($management instanceof CloudIpAddress + && $current->equals($management))) { + continue; + } + $ip = new CloudPublicIp(); + $ip->host_id = (int) $pool->host_id; + $ip->public_ip_pool_id = (int) $pool->id; + $ip->address_family = 6; + $ip->address = $current->text(); + $ip->address_binary = $current->binary(); + $ip->prefix_length = $contract->guestPrefixLength(); + $ip->state = 'available'; + $ip->reservation_id = null; + $ip->reservation_ip_claim_id = null; + $ip->instance_id = null; + $ip->operation_id = null; + $ip->created_at = $ip->updated_at = $now; + try { + $ip->save(); + } catch (\Illuminate\Database\QueryException) { + throw new CloudError( + '目标公网 IPv6 地址已被其他地址池占用', + 409 + ); + } + $seen[$binaryKey] = true; + $lockedIps->put((int) $ip->id, $ip); + $selected[] = $ip; } - return array_values(array_unique($values)); + if (count($selected) !== $needed) { + throw new CloudError('目标公网 IPv6 稀疏范围可用地址不足', 409); + } + $pool->allocation_cursor = $candidate->text(); + $pool->updated_at = $now; + $pool->save(); + return [$selected, $lockedIps]; } private static function assertAdminRead(array $actor): void diff --git a/web/app/Services/Cloud/CloudPublicNetworkTransitionBatchPlan.php b/web/app/Services/Cloud/CloudPublicNetworkTransitionBatchPlan.php new file mode 100644 index 00000000..9df4c747 --- /dev/null +++ b/web/app/Services/Cloud/CloudPublicNetworkTransitionBatchPlan.php @@ -0,0 +1,45 @@ + $allClaimsSorted + * @param array $activeClaimsByFamily + * @param array $sourceIpsByFamily + */ + public function __construct( + public readonly User $actor, + public readonly CloudJob $parent, + public readonly CloudPublicIpPoolReplacementBatch $batch, + public readonly CloudPublicIpPoolReplacementBatchItem $item, + public readonly CloudInstance $instance, + public readonly CloudHost $host, + public readonly CloudHostAgent $agent, + public readonly CloudResourcePool $resourcePool, + public readonly CloudHostNetworkAttachment $attachment, + public readonly CloudResourceReservation $reservation, + public readonly Collection $poolsById, + public readonly Collection $ipsById, + public readonly array $allClaimsSorted, + public readonly array $activeClaimsByFamily, + public readonly array $sourceIpsByFamily, + public readonly CloudPublicIp $exactTargetIp + ) { + } +} diff --git a/web/app/Services/Cloud/CloudPublicNetworkTransitionCommand.php b/web/app/Services/Cloud/CloudPublicNetworkTransitionCommand.php index a9d5b178..41e47759 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkTransitionCommand.php +++ b/web/app/Services/Cloud/CloudPublicNetworkTransitionCommand.php @@ -53,8 +53,8 @@ final class CloudPublicNetworkTransitionCommand } /** - * Stage 6 will own batch aggregation; this command freezes only the exact - * per-instance target and immutable batch-item association. + * The batch coordinator owns aggregation; this command freezes only one + * exact per-instance target and its immutable batch-item association. * * @param array $targets */ diff --git a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php index 232a6c29..d9b6d37d 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php +++ b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php @@ -57,6 +57,157 @@ final class CloudPublicNetworkTransitionWorkflow }, 3); } + /** + * Freeze one child of a pool-replacement batch inside the caller's outer + * transaction. The batch coordinator already owns the canonical actor -> + * parent -> instances -> host/agent -> transitions -> resource pools -> + * attachments/pools -> reservations -> claims/IPs lock chain. This seam + * therefore performs no lock acquisition and never opens a transaction. + */ + public static function beginPoolReplacementBatch( + CloudPublicNetworkTransitionCommand $command, + CloudPublicNetworkTransitionBatchPlan $plan + ): CloudPublicNetworkTransitionDispatch { + $actorUser = $plan->actor; + $parent = $plan->parent; + $item = $plan->item; + $batch = $plan->batch; + $instance = $plan->instance; + $host = $plan->host; + $agent = $plan->agent; + $resourcePool = $plan->resourcePool; + $reservation = $plan->reservation; + $sourceAttachment = $plan->attachment; + $allClaims = $plan->allClaimsSorted; + $activeClaims = $plan->activeClaimsByFamily; + $sourceIps = $plan->sourceIpsByFamily; + $pools = $plan->poolsById; + $ips = $plan->ipsById; + $targetIp = $plan->exactTargetIp; + $family = (int) $item->address_family; + $targets = $command->targets(); + if ($command->kind() !== 'pool_replacement' + || DB::connection()->transactionLevel() < 1 + || (int) $actorUser->id !== $command->actorUserId() + || (int) $parent->id !== (int) $command->parentJobId() + || (string) $parent->kind + !== 'public_ip_pool_replacement_batch' + || (string) $parent->execution_mode !== 'local' + || $parent->parent_job_id !== null + || $parent->host_id !== null + || $parent->public_network_transition_id !== null + || (int) $parent->user_id !== $command->actorUserId() + || (int) $item->id !== (int) $command->batchItemId() + || (int) $item->batch_id !== (int) $batch->id + || (int) $item->instance_id !== $command->instanceId() + || (string) $item->state !== 'preparing' + || $item->child_job_id !== null + || (int) $batch->parent_job_id !== (int) $parent->id + || (int) $batch->requested_by_user_id + !== $command->actorUserId() + || (int) $batch->host_id !== (int) $host->id + || (string) $batch->state !== 'running' + || (int) ($batch->active_claim ?? 0) !== 1 + || (int) $instance->id !== $command->instanceId() + || (int) $instance->host_id !== (int) $host->id + || (int) $agent->host_id !== (int) $host->id + || (int) $resourcePool->id !== (int) $instance->resource_pool_id + || (int) $reservation->id !== (int) $instance->reservation_id + || (int) $reservation->host_id !== (int) $host->id + || (int) $reservation->instance_id !== (int) $instance->id + || (int) $reservation->resource_pool_id + !== (int) $resourcePool->id + || (string) $reservation->state !== 'committed' + || (int) $item->source_reservation_id !== (int) $reservation->id + || !in_array($family, self::FAMILIES, true) + || array_keys($targets) !== [$family] + || count($activeClaims) !== 2 + || !isset($activeClaims[4], $activeClaims[6]) + || !$sourceIps[4] instanceof CloudPublicIp + || !$sourceIps[6] instanceof CloudPublicIp + || (int) $activeClaims[$family]->public_ip_id + !== (int) $item->source_public_ip_id + || (int) $targetIp->id !== (int) $item->target_public_ip_id) { + throw new CloudError('公网地址池迁移子任务身份无效', 409); + } + if (!$host instanceof CloudHost || !(bool) $host->enabled + || !$agent instanceof CloudHostAgent || !$agent->isReady() + || !$resourcePool instanceof CloudResourcePool + || !$reservation instanceof CloudResourceReservation + || (string) $reservation->state !== 'committed' + || (int) $reservation->instance_id !== (int) $instance->id + || (int) $reservation->host_id !== (int) $instance->host_id + || (int) $reservation->resource_pool_id + !== (int) $instance->resource_pool_id + || (int) $item->source_reservation_id !== (int) $reservation->id) { + throw new CloudError('公网地址池迁移资源账本不一致', 409); + } + $sourcePool = $pools->get( + (int) $activeClaims[$family]->public_ip_pool_id + ); + $targetPool = $pools->get((int) $targetIp->public_ip_pool_id); + $attachmentId = (int) $activeClaims[4]->network_attachment_id; + $target = $targets[$family]; + if (!$sourcePool instanceof CloudPublicIpPool + || !$targetPool instanceof CloudPublicIpPool + || !$sourceAttachment instanceof CloudHostNetworkAttachment + || (int) $activeClaims[6]->network_attachment_id !== $attachmentId + || (int) $item->source_network_attachment_id !== $attachmentId + || (int) $item->target_network_attachment_id !== $attachmentId + || (int) $sourcePool->id + !== (int) $batch->source_public_ip_pool_id + || (int) $targetPool->id + !== (int) $batch->target_public_ip_pool_id + || (int) $sourcePool->address_family !== $family + || (int) $targetPool->address_family !== $family + || (int) $sourcePool->host_id !== (int) $instance->host_id + || (int) $targetPool->host_id !== (int) $instance->host_id + || (int) $sourcePool->network_attachment_id !== $attachmentId + || (int) $targetPool->network_attachment_id !== $attachmentId + || (int) $sourceAttachment->line_id + !== (int) $resourcePool->line_id + || !hash_equals((string) $targetPool->pool_key, + (string) $target['pool_key']) + || !hash_equals( + (string) $targetIp->address . '/' + . (int) $targetIp->prefix_length, + (string) $target['address_cidr'] + )) { + throw new CloudError('公网地址池迁移冻结拓扑不一致', 409); + } + + foreach (self::FAMILIES as $sourceFamily) { + self::assertCommittedSourceLeg( + $activeClaims[$sourceFamily], + $sourceIps[$sourceFamily], + (int) $reservation->id, + (int) $instance->id + ); + } + $dispatch = self::freezeTransitionLocked( + $command, + $actorUser, + $instance, + $agent, + $resourcePool, + $reservation, + $allClaims, + $activeClaims, + $sourceIps, + $sourceAttachment, + collect([(int) $sourceAttachment->id => $sourceAttachment]), + $pools, + $ips, + $targetIp, + $host + ); + $item->child_job_id = $dispatch->jobId(); + $item->state = 'queued'; + $item->updated_at = time(); + $item->save(); + return $dispatch; + } + /** * Reserve a transition while the caller owns the actor row and the outer * transaction. This internal seam must never create a nested transaction: @@ -317,7 +468,41 @@ final class CloudPublicNetworkTransitionWorkflow $sourceIps[$family] = $sourceIp; } - $sourceAttachment = $attachments->get($sourceAttachmentId); + return self::freezeTransitionLocked( + $command, + $actorUser, + $instance, + $agent, + $resourcePool, + $reservation, + $allClaims, + $activeClaims, + $sourceIps, + $attachments->get($sourceAttachmentId), + $attachments, + $pools, + $ips + ); + } + + private static function freezeTransitionLocked( + CloudPublicNetworkTransitionCommand $command, + User $actorUser, + CloudInstance $instance, + CloudHostAgent $agent, + CloudResourcePool $resourcePool, + CloudResourceReservation $reservation, + array $allClaims, + array $activeClaims, + array $sourceIps, + CloudHostNetworkAttachment $sourceAttachment, + $attachments, + $pools, + $ips, + ?CloudPublicIp $exactTargetIp = null, + ?CloudHost $lockedHost = null + ): CloudPublicNetworkTransitionDispatch { + $reservationId = (int) $reservation->id; $sourceSnapshot = self::snapshot( $instance, $agent, @@ -339,19 +524,57 @@ final class CloudPublicNetworkTransitionWorkflow ); self::assertInstanceSnapshot($instance, $sourceSnapshot); - [$targetAttachment, $targetClaims, $targetIps, $changed] = - self::selectTargetLocked( - $command, - $instance, - $agent, - $sourceAttachment, - $activeClaims, - $sourceIps, - $attachments, - $pools, - $ips, - (int) $resourcePool->line_id + if ($exactTargetIp instanceof CloudPublicIp) { + $targetFamilies = array_keys($command->targets()); + $family = count($targetFamilies) === 1 + ? (int) $targetFamilies[0] : 0; + $targetPool = $pools->get( + (int) $exactTargetIp->public_ip_pool_id ); + if (!in_array($family, self::FAMILIES, true) + || !$targetPool instanceof CloudPublicIpPool + || (int) $targetPool->address_family !== $family + || (int) $targetPool->network_attachment_id + !== (int) $sourceAttachment->id + || !self::poolUsable( + $targetPool, + $instance, + $agent, + $attachments, + (int) $resourcePool->line_id + ) + || !self::ipAvailableForPool( + $exactTargetIp, + $targetPool, + $lockedHost + ) + || (int) $exactTargetIp->id === (int) $sourceIps[$family]->id) { + throw new CloudError('公网地址池迁移精确目标已不可用', 409); + } + $targetAttachment = $sourceAttachment; + $targetClaims = $activeClaims; + $targetIps = $sourceIps; + $targetClaims[$family] = (object) [ + 'network_attachment_id' => (int) $sourceAttachment->id, + 'public_ip_pool_id' => (int) $targetPool->id, + ]; + $targetIps[$family] = $exactTargetIp; + $changed = [$family => true]; + } else { + [$targetAttachment, $targetClaims, $targetIps, $changed] = + self::selectTargetLocked( + $command, + $instance, + $agent, + $sourceAttachment, + $activeClaims, + $sourceIps, + $attachments, + $pools, + $ips, + (int) $resourcePool->line_id + ); + } $targetSnapshot = self::snapshot( $instance, $agent, @@ -465,10 +688,30 @@ final class CloudPublicNetworkTransitionWorkflow if ($updated !== 1) { throw new CloudError('目标公网地址声明绑定失败', 409); } - $targetClaim = DB::table('cloud_resource_reservation_ips') - ->where('id', $claimId)->first(); - $boundIp = CloudPublicIp::where('id', (int) $targetIp->id) - ->first(); + $targetClaim = (object) [ + 'id' => $claimId, + 'reservation_id' => $reservationId, + 'address_family' => $family, + 'claim_generation' => $generation, + 'active_claim' => null, + 'network_attachment_id' => + (int) $targetPool->network_attachment_id, + 'public_ip_pool_id' => (int) $targetPool->id, + 'public_ip_id' => (int) $targetIp->id, + 'operation_id' => $operationId, + 'state' => 'reserved', + 'released_at' => null, + 'release_reason' => '', + 'created_at' => $now, + 'updated_at' => $now, + ]; + $targetIp->state = 'reserved'; + $targetIp->reservation_id = $reservationId; + $targetIp->reservation_ip_claim_id = $claimId; + $targetIp->instance_id = null; + $targetIp->operation_id = $operationId; + $targetIp->updated_at = $now; + $boundIp = $targetIp; $effectiveClaims[$family] = $targetClaim; $effectiveIps[$family] = $boundIp; self::insertTransitionLeg( @@ -496,7 +739,7 @@ final class CloudPublicNetworkTransitionWorkflow $job->operation_id = $operationId; $job->request_idempotency_key = $command->requestKey(); $job->request_fingerprint = $command->fingerprint(); - $job->parent_job_id = null; + $job->parent_job_id = $command->parentJobId(); $job->invoice_id = $invoice?->id; $job->instance_id = (int) $instance->id; $job->public_network_transition_id = (int) $transition->id; @@ -2568,7 +2811,7 @@ final class CloudPublicNetworkTransitionWorkflow } private static function ipAvailableForPool(CloudPublicIp $ip, - CloudPublicIpPool $pool): bool + CloudPublicIpPool $pool, ?CloudHost $lockedHost = null): bool { if ((string) $ip->state !== 'available' || $ip->reservation_id !== null @@ -2577,7 +2820,7 @@ final class CloudPublicNetworkTransitionWorkflow return false; } try { - self::assertAddressInPool($ip, $pool); + self::assertAddressInPool($ip, $pool, $lockedHost); return true; } catch (\Throwable) { return false; @@ -2661,7 +2904,7 @@ final class CloudPublicNetworkTransitionWorkflow } private static function assertAddressInPool(CloudPublicIp $ip, - CloudPublicIpPool $pool): void + CloudPublicIpPool $pool, ?CloudHost $lockedHost = null): void { try { $contract = CloudPublicIpPoolContract::fromRow($pool); @@ -2678,7 +2921,9 @@ final class CloudPublicNetworkTransitionWorkflow throw new \InvalidArgumentException(); } $management = CloudPanelUsageService::managementAddress( - CloudHost::find((int) $pool->host_id) + $lockedHost instanceof CloudHost + ? $lockedHost + : CloudHost::find((int) $pool->host_id) ); if (is_string($management) && $management !== '' && $address->equals(CloudIpAddress::parse($management))) { diff --git a/web/tests/CloudPublicIpPoolReplacementBatchServiceTest.php b/web/tests/CloudPublicIpPoolReplacementBatchServiceTest.php index 558c75b6..f2081290 100644 --- a/web/tests/CloudPublicIpPoolReplacementBatchServiceTest.php +++ b/web/tests/CloudPublicIpPoolReplacementBatchServiceTest.php @@ -18,6 +18,14 @@ namespace App\Services\Cloud { } } + final class CloudPanelUsageService + { + public static function managementAddress($host): ?string + { + return null; + } + } + final class CloudInstanceMigrationService { public static function assertInstanceMutationAvailable( @@ -31,14 +39,12 @@ namespace App\Services\Cloud { namespace { require dirname(__DIR__) . '/vendor/autoload.php'; - use App\Models\CloudInstance; use App\Models\CloudJob; use App\Models\CloudPublicIpPoolReplacementBatch; use App\Services\Cloud\CloudError; - use App\Services\Cloud\CloudJobService; - use App\Services\Cloud\CloudPlacementService; + use App\Services\Cloud\CloudIpAddress; use App\Services\Cloud\CloudPublicIpPoolReplacementBatchService; - use Illuminate\Database\Capsule\Manager as Capsule; + use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Schema\Blueprint; $assert = static function (bool $condition, string $message): void { @@ -46,22 +52,40 @@ namespace { throw new RuntimeException($message); } }; - $expectCloudError = static function ( - callable $callback, - int $status, - string $message - ) use ($assert): void { + $rejects = static function (callable $callback, string $message) use ( + $assert + ): void { try { $callback(); } catch (CloudError $error) { - $assert($error->httpStatus === $status, $message - . ' (unexpected HTTP status)'); + $assert(in_array($error->httpStatus, [404, 409], true), + $message . ' (unexpected HTTP status)'); return; } - throw new RuntimeException($message . ' (no CloudError)'); + throw new RuntimeException($message . ' (request was accepted)'); + }; + $snapshot = static function (): array { + $tables = [ + 'cloud_jobs', + 'cloud_public_ip_pool_replacement_batches', + 'cloud_public_ip_pool_replacement_batch_items', + 'cloud_public_network_transitions', + 'cloud_public_network_transition_ips', + 'cloud_resource_reservation_ips', + 'cloud_public_ips', + ]; + $result = []; + foreach ($tables as $table) { + $result[$table] = DB::table($table)->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all(); + } + $result['pools'] = DB::table('cloud_public_ip_pools') + ->orderBy('id')->get()->map(static fn ($row): array => (array) $row) + ->all(); + return $result; }; - $capsule = new Capsule(); + $capsule = new DB(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', @@ -70,387 +94,619 @@ namespace { $capsule->bootEloquent(); $schema = $capsule->schema('default'); - $schema->create('cloud_hosts', static function (Blueprint $table): void { - $table->increments('id'); - $table->string('name'); - $table->boolean('enabled')->default(true); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $schema->create('user', static function (Blueprint $t): void { + $t->increments('id'); + $t->string('money')->default('0.00'); + $t->boolean('enable')->default(true); }); - $schema->create('cloud_host_agents', static function ( - Blueprint $table - ): void { - $table->integer('host_id')->primary(); - $table->text('sealed_api_secret')->default('sealed'); - $table->boolean('enabled'); - $table->integer('revision'); - $table->integer('tested_revision'); - $table->string('last_test_status'); - $table->string('last_error_code')->default(''); - $table->text('network_selectors_json'); - $table->integer('last_tested_at')->default(0); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $schema->create('cloud_hosts', static function (Blueprint $t): void { + $t->increments('id'); + $t->string('name'); + $t->boolean('enabled'); + $t->integer('created_at')->default(0); + $t->integer('updated_at')->default(0); + }); + $schema->create('cloud_host_agents', static function (Blueprint $t): void { + $t->integer('host_id')->primary(); + $t->text('sealed_api_secret'); + $t->boolean('enabled'); + $t->integer('revision'); + $t->integer('tested_revision'); + $t->string('last_test_status'); + $t->string('last_error_code'); + $t->string('last_agent_version'); + $t->text('network_selectors_json'); + $t->text('public_network_capabilities_json'); + $t->integer('last_tested_at'); + $t->integer('created_at'); + $t->integer('updated_at'); + }); + $schema->create('cloud_resource_pools', static function (Blueprint $t): void { + $t->increments('id'); + $t->integer('line_id'); + $t->string('code'); + $t->string('name'); + $t->string('status'); }); $schema->create('cloud_host_network_attachments', static function ( - Blueprint $table + Blueprint $t ): void { - $table->increments('id'); - $table->integer('host_id'); - $table->integer('line_id'); - $table->string('network_selector'); - $table->string('state'); - $table->integer('priority')->default(0); - $table->string('verification_state'); - $table->integer('verified_agent_revision'); - $table->integer('last_verified_at'); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $t->increments('id'); + $t->string('attachment_key'); + $t->integer('host_id'); + $t->integer('line_id'); + $t->string('name'); + $t->string('network_selector'); + $t->string('state'); + $t->integer('priority'); + $t->integer('verified_agent_revision'); + $t->string('verification_state'); + $t->integer('last_verified_at'); + $t->integer('created_at')->default(0); + $t->integer('updated_at')->default(0); }); $schema->create('cloud_public_ip_pools', static function ( - Blueprint $table + Blueprint $t ): void { - $table->increments('id'); - $table->string('pool_key', 32)->unique(); - $table->integer('host_id'); - $table->integer('network_attachment_id'); - $table->string('name'); - $table->string('cidr'); - $table->string('gateway'); - $table->integer('guest_prefix_length'); - $table->text('dns_servers_json'); - $table->text('reserved_ips_json'); - $table->string('state'); - $table->integer('priority')->default(0); - $table->integer('desired_revision'); - $table->integer('observed_revision'); - $table->string('sync_state'); - $table->string('last_error_code')->default(''); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $t->increments('id'); + $t->string('pool_key', 32)->unique(); + $t->integer('host_id'); + $t->integer('network_attachment_id'); + $t->integer('address_family'); + $t->string('name'); + $t->string('cidr'); + $t->string('gateway'); + $t->boolean('gateway_on_link'); + $t->integer('guest_prefix_length'); + $t->text('dns_servers_json'); + $t->text('reserved_ips_json'); + $t->string('allocation_start')->nullable(); + $t->string('allocation_end')->nullable(); + $t->string('allocation_cursor')->nullable(); + $t->integer('max_allocations'); + $t->string('state'); + $t->integer('priority'); + $t->integer('desired_revision'); + $t->integer('observed_revision'); + $t->string('sync_state'); + $t->string('last_error_code')->default(''); + $t->integer('created_at')->default(0); + $t->integer('updated_at')->default(0); }); - $schema->create('cloud_resource_pools', static function ( - Blueprint $table - ): void { - $table->increments('id'); - $table->integer('line_id'); + $schema->create('cloud_public_ips', static function (Blueprint $t): void { + $t->increments('id'); + $t->integer('host_id'); + $t->integer('public_ip_pool_id'); + $t->integer('address_family'); + $t->string('address'); + $t->binary('address_binary'); + $t->integer('prefix_length'); + $t->string('state'); + $t->integer('reservation_id')->nullable(); + $t->integer('reservation_ip_claim_id')->nullable()->unique(); + $t->integer('instance_id')->nullable(); + $t->string('operation_id')->nullable(); + $t->integer('created_at'); + $t->integer('updated_at'); + $t->unique(['address_family', 'address_binary']); }); $schema->create('cloud_resource_reservations', static function ( - Blueprint $table + Blueprint $t ): void { - $table->increments('id'); - $table->string('operation_id', 32)->unique(); - $table->integer('resource_pool_id'); - $table->integer('host_id'); - $table->integer('user_id'); - $table->integer('instance_id')->nullable(); - $table->integer('network_attachment_id'); - $table->integer('public_ip_pool_id'); - $table->integer('public_ip_id'); - $table->string('state'); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $t->increments('id'); + $t->string('reservation_key'); + $t->string('operation_id')->unique(); + $t->integer('order_id')->nullable(); + $t->integer('backup_restore_id')->nullable(); + $t->integer('offer_id')->nullable(); + $t->integer('resource_pool_id'); + $t->integer('host_id'); + $t->integer('user_id'); + $t->integer('instance_id')->nullable(); + $t->integer('network_attachment_id')->nullable(); + $t->integer('public_ip_pool_id')->nullable(); + $t->integer('public_ip_id')->nullable(); + $t->integer('cpu_cores'); + $t->integer('memory_mib'); + $t->integer('disk_gib'); + $t->string('state'); + $t->integer('expires_at')->nullable(); + $t->integer('released_at')->nullable(); + $t->string('release_reason'); + $t->integer('created_at'); + $t->integer('updated_at'); }); $schema->create('cloud_resource_reservation_ips', static function ( - Blueprint $table + Blueprint $t ): void { - $table->increments('id'); - $table->integer('reservation_id'); - $table->integer('public_ip_pool_id'); - $table->integer('active_claim')->nullable(); - $table->string('state'); + $t->increments('id'); + $t->integer('reservation_id'); + $t->integer('address_family'); + $t->integer('claim_generation'); + $t->integer('active_claim')->nullable(); + $t->integer('network_attachment_id'); + $t->integer('public_ip_pool_id'); + $t->integer('public_ip_id'); + $t->string('operation_id'); + $t->string('state'); + $t->integer('released_at')->nullable(); + $t->string('release_reason'); + $t->integer('created_at'); + $t->integer('updated_at'); + $t->unique(['reservation_id', 'address_family', 'active_claim']); + $t->unique(['reservation_id', 'claim_generation', 'address_family']); + $t->unique(['operation_id', 'address_family']); }); - $schema->create('cloud_instances', static function ( - Blueprint $table + $schema->create('cloud_instances', static function (Blueprint $t): void { + $t->increments('id'); + $t->integer('host_id'); + $t->integer('user_id'); + $t->integer('order_id')->nullable(); + $t->integer('plan_id')->nullable(); + $t->integer('offer_id')->nullable(); + $t->integer('resource_pool_id'); + $t->integer('reservation_id'); + $t->string('uuid'); + $t->string('name'); + $t->string('status'); + $t->string('service_status'); + $t->string('network_status'); + $t->integer('expires_at')->nullable(); + $t->text('spec_json'); + $t->integer('created_at'); + $t->integer('updated_at'); + }); + $schema->create('cloud_public_network_transitions', static function ( + Blueprint $t ): void { - $table->increments('id'); - $table->string('uuid'); - $table->string('name'); - $table->integer('user_id'); - $table->integer('host_id'); - $table->integer('resource_pool_id'); - $table->integer('reservation_id'); - $table->string('status'); - $table->text('spec_json'); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $t->increments('id'); + $t->string('transition_key')->unique(); + $t->string('operation_id')->unique(); + $t->string('kind'); + $t->integer('instance_id'); + $t->integer('host_id'); + $t->integer('reservation_id'); + $t->integer('invoice_id')->nullable()->unique(); + $t->integer('pool_replacement_batch_item_id')->nullable()->unique(); + $t->integer('requested_by_user_id'); + $t->integer('source_network_attachment_id'); + $t->integer('target_network_attachment_id'); + $t->string('source_claim_set_sha256'); + $t->string('target_claim_set_sha256'); + $t->text('source_public_network_json'); + $t->string('source_public_network_sha256'); + $t->text('target_public_network_json'); + $t->string('target_public_network_sha256'); + $t->string('state'); + $t->string('effect_resolution'); + $t->integer('active_transition')->nullable(); + $t->string('last_error_code'); + $t->integer('effect_resolved_at')->nullable(); + $t->integer('terminal_at')->nullable(); + $t->integer('created_at'); + $t->integer('updated_at'); + $t->unique(['instance_id', 'active_transition']); + $t->unique(['reservation_id', 'active_transition']); }); - $schema->create('cloud_public_ips', static function ( - Blueprint $table + $schema->create('cloud_public_network_transition_ips', static function ( + Blueprint $t ): void { - $table->increments('id'); - $table->integer('host_id'); - $table->integer('public_ip_pool_id'); - $table->string('address'); - $table->integer('prefix_length'); - $table->string('state'); - $table->integer('reservation_id')->nullable(); - $table->integer('instance_id')->nullable(); - $table->string('operation_id', 32)->nullable(); - $table->integer('created_at')->default(0); - $table->integer('updated_at')->default(0); + $t->increments('id'); + $t->integer('transition_id'); + $t->integer('address_family'); + $t->integer('source_claim_id'); + $t->integer('target_claim_id')->nullable()->unique(); + $t->boolean('changed'); + $t->integer('created_at'); + $t->integer('updated_at'); + $t->unique(['transition_id', 'address_family']); }); - $schema->create('cloud_jobs', static function (Blueprint $table): void { - $table->increments('id'); - $table->string('operation_id', 32)->unique(); - $table->string('request_idempotency_key', 128); - $table->string('request_fingerprint', 64); - $table->integer('parent_job_id')->nullable(); - $table->integer('supersedes_job_id')->nullable(); - $table->integer('vpc_repair_root_job_id')->nullable(); - $table->integer('order_id')->nullable(); - $table->integer('invoice_id')->nullable(); - $table->integer('instance_id')->nullable(); - $table->integer('snapshot_id')->nullable(); - $table->integer('backup_id')->nullable(); - $table->integer('backup_restore_id')->nullable(); - $table->integer('backup_recovery_id')->nullable(); - $table->integer('vpc_id')->nullable(); - $table->integer('vpc_port_id')->nullable(); - $table->integer('vpc_batch_id')->nullable(); - $table->integer('user_id'); - $table->integer('host_id')->nullable(); - $table->string('kind'); - $table->string('execution_mode'); - $table->string('phase'); - $table->string('outcome')->nullable(); - $table->string('status'); - $table->text('sealed_payload'); - $table->integer('attempts'); - $table->integer('dispatch_attempts'); - $table->integer('dispatch_deadline_at'); - $table->integer('dispatch_started_at')->nullable(); - $table->integer('agent_accepted_at')->nullable(); - $table->integer('receipt_deadline_at')->nullable(); - $table->integer('terminal_at')->nullable(); - $table->boolean('effect_may_have_applied')->nullable(); - $table->boolean('safe_to_resubmit')->nullable(); - $table->string('effect_resolution'); - $table->integer('effect_resolved_at')->nullable(); - $table->integer('effect_resolved_by')->nullable(); - $table->text('effect_resolution_reason')->nullable(); - $table->boolean('finalization_incident')->default(false); - $table->string('finalization_error_code')->nullable(); - $table->string('completion_event_id')->nullable(); - $table->integer('read_at')->nullable(); - $table->integer('dispatched_at')->nullable(); - $table->string('lease_token')->nullable(); - $table->integer('lease_until')->default(0); - $table->integer('available_at'); - $table->string('error_code')->nullable(); - $table->text('result_json')->nullable(); - $table->integer('created_at'); - $table->integer('updated_at'); - $table->unique(['user_id', 'request_idempotency_key']); + $schema->create('cloud_jobs', static function (Blueprint $t): void { + $t->increments('id'); + $t->string('operation_id', 32)->unique(); + $t->string('request_idempotency_key', 128); + $t->string('request_fingerprint', 64); + foreach (['parent_job_id', 'supersedes_job_id', + 'vpc_repair_root_job_id', 'order_id', 'invoice_id', 'instance_id', + 'snapshot_id', 'backup_id', 'backup_restore_id', + 'backup_recovery_id', 'public_network_transition_id', 'vpc_id', + 'vpc_port_id', 'vpc_batch_id'] as $column) { + $t->integer($column)->nullable(); + } + $t->integer('user_id'); + $t->integer('host_id')->nullable(); + $t->string('kind'); + $t->string('execution_mode'); + $t->string('phase'); + $t->string('outcome')->nullable(); + $t->string('status'); + $t->text('sealed_payload'); + $t->integer('attempts'); + $t->integer('dispatch_attempts'); + $t->integer('dispatch_deadline_at'); + $t->integer('dispatch_started_at')->nullable(); + $t->integer('agent_accepted_at')->nullable(); + $t->integer('receipt_deadline_at')->nullable(); + $t->integer('terminal_at')->nullable(); + $t->boolean('effect_may_have_applied')->nullable(); + $t->boolean('safe_to_resubmit')->nullable(); + $t->string('effect_resolution'); + $t->integer('effect_resolved_at')->nullable(); + $t->integer('effect_resolved_by')->nullable(); + $t->text('effect_resolution_reason')->nullable(); + $t->boolean('finalization_incident'); + $t->string('finalization_error_code')->nullable(); + $t->string('completion_event_id')->nullable(); + $t->integer('read_at')->nullable(); + $t->integer('dispatched_at')->nullable(); + $t->string('lease_token')->nullable(); + $t->integer('lease_until')->default(0); + $t->integer('available_at'); + $t->string('error_code')->nullable(); + $t->text('result_json')->nullable(); + $t->integer('created_at'); + $t->integer('updated_at'); + $t->unique(['user_id', 'request_idempotency_key']); }); $schema->create('cloud_public_ip_pool_replacement_batches', - static function (Blueprint $table): void { - $table->increments('id'); - $table->string('batch_key', 32)->unique(); - $table->integer('host_id'); - $table->integer('source_public_ip_pool_id'); - $table->integer('target_public_ip_pool_id'); - $table->string('source_initial_state'); - $table->integer('parent_job_id')->unique(); - $table->integer('requested_by_user_id'); - $table->string('state'); - $table->integer('active_claim')->nullable(); - $table->string('last_error_code')->default(''); - $table->integer('terminal_at')->nullable(); - $table->integer('created_at'); - $table->integer('updated_at'); - }); - $schema->create('cloud_public_ip_pool_replacement_batch_items', - static function (Blueprint $table): void { - $table->increments('id'); - $table->integer('batch_id'); - $table->integer('instance_id'); - $table->integer('source_reservation_id'); - $table->integer('source_public_ip_id'); - $table->integer('target_public_ip_id'); - $table->integer('source_network_attachment_id'); - $table->integer('target_network_attachment_id'); - $table->integer('child_job_id')->unique(); - $table->string('state'); - $table->string('last_error_code')->default(''); - $table->integer('created_at'); - $table->integer('updated_at'); - $table->unique(['batch_id', 'target_public_ip_id']); + static function (Blueprint $t): void { + $t->increments('id'); + $t->string('batch_key', 32)->unique(); + $t->integer('host_id'); + $t->integer('source_public_ip_pool_id'); + $t->integer('target_public_ip_pool_id'); + $t->string('source_initial_state'); + $t->integer('parent_job_id')->unique(); + $t->integer('requested_by_user_id'); + $t->string('state'); + $t->integer('active_claim')->nullable(); + $t->string('last_error_code')->default(''); + $t->integer('terminal_at')->nullable(); + $t->integer('created_at'); + $t->integer('updated_at'); }); + DB::statement(<<<'SQL' +CREATE TABLE cloud_public_ip_pool_replacement_batch_items ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + batch_id INTEGER NOT NULL, + instance_id INTEGER NOT NULL, + address_family INTEGER NOT NULL, + source_reservation_id INTEGER NOT NULL, + source_public_ip_id INTEGER NOT NULL, + target_public_ip_id INTEGER NOT NULL, + source_network_attachment_id INTEGER NOT NULL, + target_network_attachment_id INTEGER NOT NULL, + child_job_id INTEGER NULL UNIQUE, + state TEXT NOT NULL, + last_error_code TEXT NOT NULL DEFAULT '', + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + UNIQUE (batch_id, instance_id), + UNIQUE (batch_id, target_public_ip_id), + CHECK (source_public_ip_id <> target_public_ip_id), + CHECK (address_family IN (4,6)), + CHECK ( + (state = 'preparing' AND child_job_id IS NULL) + OR (state IN ('queued','succeeded','failed','unknown') + AND child_job_id IS NOT NULL) + ) +) +SQL); $now = time(); - Capsule::table('cloud_hosts')->insert([ - 'id' => 1, - 'name' => 'HKG-A', - 'enabled' => 1, - 'created_at' => $now, - 'updated_at' => $now, - ]); - Capsule::table('cloud_host_agents')->insert([ - 'host_id' => 1, - 'enabled' => 1, - 'revision' => 7, - 'tested_revision' => 7, - 'last_test_status' => 'healthy', - 'network_selectors_json' => json_encode([ - 'bridge:cross-line', 'bridge:other-source', 'bridge:source', - 'bridge:target', - ], JSON_THROW_ON_ERROR), - 'last_tested_at' => $now, - 'created_at' => $now, - 'updated_at' => $now, + DB::table('user')->insert([ + ['id' => 99, 'money' => '0.00', 'enable' => 1], + ['id' => 501, 'money' => '0.00', 'enable' => 1], + ['id' => 502, 'money' => '0.00', 'enable' => 1], ]); - Capsule::table('cloud_host_network_attachments')->insert([ - ['id' => 11, 'host_id' => 1, 'line_id' => 10, - 'network_selector' => 'bridge:source', 'state' => 'active', - 'priority' => 10, 'verification_state' => 'verified', - 'verified_agent_revision' => 7, 'last_verified_at' => $now], - ['id' => 12, 'host_id' => 1, 'line_id' => 10, - 'network_selector' => 'bridge:target', 'state' => 'active', - 'priority' => 20, 'verification_state' => 'verified', - 'verified_agent_revision' => 7, 'last_verified_at' => $now], - ['id' => 13, 'host_id' => 1, 'line_id' => 10, - 'network_selector' => 'bridge:other-source', 'state' => 'active', - 'priority' => 30, 'verification_state' => 'verified', - 'verified_agent_revision' => 7, 'last_verified_at' => $now], - ['id' => 14, 'host_id' => 1, 'line_id' => 20, - 'network_selector' => 'bridge:cross-line', 'state' => 'active', - 'priority' => 40, 'verification_state' => 'verified', - 'verified_agent_revision' => 7, 'last_verified_at' => $now], + DB::table('cloud_hosts')->insert([ + ['id' => 1, 'name' => 'HKG-A', 'enabled' => 1, + 'created_at' => $now, 'updated_at' => $now], + ['id' => 2, 'name' => 'HKG-B', 'enabled' => 1, + 'created_at' => $now, 'updated_at' => $now], ]); - Capsule::table('cloud_resource_pools')->insert([ - ['id' => 21, 'line_id' => 10], + $capabilities = json_encode([ + 'contract' => 1, + 'dual_stack_required' => true, + 'project_owned_filter' => true, + 'ndp_guard' => true, + ], JSON_THROW_ON_ERROR); + foreach ([1, 2] as $hostId) { + DB::table('cloud_host_agents')->insert([ + 'host_id' => $hostId, + 'sealed_api_secret' => 'unused', + 'enabled' => 1, + 'revision' => 3, + 'tested_revision' => 3, + 'last_test_status' => 'healthy', + 'last_error_code' => '', + 'last_agent_version' => 'test', + 'network_selectors_json' => + '["bridge:br0","bridge:br1","bridge:br2"]', + 'public_network_capabilities_json' => $capabilities, + 'last_tested_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ]); + } + DB::table('cloud_resource_pools')->insert([ + ['id' => 1, 'line_id' => 10, 'code' => 'line-a', + 'name' => 'Line A', 'status' => 'active'], + ['id' => 2, 'line_id' => 20, 'code' => 'line-b', + 'name' => 'Line B', 'status' => 'active'], ]); - $pool = static function ( - int $id, - int $attachmentId, - string $keyDigit, - string $name, - string $cidr, - string $gateway, - int $revision - ): array { + foreach ([ + [11, 'a', 1, 10, 'bridge:br0'], + [12, 'b', 1, 10, 'bridge:br1'], + [13, 'c', 1, 20, 'bridge:br2'], + [14, 'd', 2, 10, 'bridge:br0'], + ] as [$id, $key, $hostId, $lineId, $selector]) { + DB::table('cloud_host_network_attachments')->insert([ + 'id' => $id, + 'attachment_key' => str_repeat($key, 32), + 'host_id' => $hostId, + 'line_id' => $lineId, + 'name' => 'Attachment ' . $id, + 'network_selector' => $selector, + 'state' => 'active', + 'priority' => $id, + 'verified_agent_revision' => 3, + 'verification_state' => 'verified', + 'last_verified_at' => $now, + 'created_at' => $now, + 'updated_at' => $now, + ]); + } + + $poolKey = static fn (string $character): string => str_repeat( + $character, + 32 + ); + $pool = static function (int $id, string $key, int $hostId, + int $attachmentId, int $family, string $cidr, string $gateway, + int $revision = 1, ?string $start = null, ?string $end = null, + ?string $cursor = null, int $maximum = 0 + ) use ($now): array { return [ 'id' => $id, - 'pool_key' => str_repeat($keyDigit, 32), - 'host_id' => 1, + 'pool_key' => str_repeat($key, 32), + 'host_id' => $hostId, 'network_attachment_id' => $attachmentId, - 'name' => $name, + 'address_family' => $family, + 'name' => 'Pool ' . $id, 'cidr' => $cidr, 'gateway' => $gateway, - 'guest_prefix_length' => 29, - 'dns_servers_json' => json_encode(['1.1.1.1']), + 'gateway_on_link' => 0, + 'guest_prefix_length' => $family === 4 ? 29 : 64, + 'dns_servers_json' => '["1.1.1.1"]', 'reserved_ips_json' => '[]', + 'allocation_start' => $start, + 'allocation_end' => $end, + 'allocation_cursor' => $cursor, + 'max_allocations' => $maximum, 'state' => 'active', 'priority' => $id, 'desired_revision' => $revision, 'observed_revision' => $revision, 'sync_state' => 'applied', 'last_error_code' => '', + 'created_at' => $now, + 'updated_at' => $now, ]; }; - Capsule::table('cloud_public_ip_pools')->insert([ - $pool(31, 11, 'a', 'Source A', '198.51.100.0/29', - '198.51.100.1', 7), - $pool(32, 12, 'b', 'Target B', '198.51.100.8/29', - '198.51.100.9', 9), - $pool(33, 13, 'c', 'Source D', '198.51.100.16/29', - '198.51.100.17', 4), - $pool(34, 14, 'd', 'Cross Line', '198.51.100.24/29', - '198.51.100.25', 3), + DB::table('cloud_public_ip_pools')->insert([ + $pool(11, 'a', 1, 11, 4, '8.8.8.0/29', '8.8.8.1', 11), + $pool(12, 'b', 1, 11, 6, '2404:c140:156:3a::/64', + '2404:c140:156:3a::1', 12, '2404:c140:156:3a::1000', + '2404:c140:156:3a::10ff', '2404:c140:156:3a::1000', 100), + $pool(13, 'c', 1, 11, 4, '8.8.8.8/29', '8.8.8.9', 13), + $pool(14, 'd', 1, 11, 6, '2404:c140:156:3b::/64', + '2404:c140:156:3b::1', 14, '2404:c140:156:3b::1000', + '2404:c140:156:3b::10ff', '2404:c140:156:3b::1000', 100), + $pool(15, 'e', 1, 12, 4, '8.8.8.16/29', '8.8.8.17', 15), + $pool(16, 'f', 1, 13, 4, '8.8.8.24/29', '8.8.8.25', 16), + $pool(17, '1', 2, 14, 4, '9.9.9.0/29', '9.9.9.1', 17), + $pool(21, '2', 1, 11, 4, '9.9.9.8/29', '9.9.9.9', 21), + $pool(22, '3', 1, 11, 6, '2404:c140:156:3c::/64', + '2404:c140:156:3c::1', 22, '2404:c140:156:3c::1000', + '2404:c140:156:3c::10ff', '2404:c140:156:3c::1000', 100), + $pool(23, '4', 1, 11, 6, '2404:c140:156:3d::/64', + '2404:c140:156:3d::1', 23, '2404:c140:156:3d::1000', + '2404:c140:156:3d::1003', '2404:c140:156:3d::1000', 4), ]); - $reservationOperation = static fn (int $id): string => str_pad( + + $operation = static fn (int $id): string => str_pad( dechex($id), 32, '0', STR_PAD_LEFT ); - Capsule::table('cloud_resource_reservations')->insert([ - ['id' => 41, 'operation_id' => $reservationOperation(41), - 'resource_pool_id' => 21, 'host_id' => 1, 'user_id' => 501, - 'instance_id' => 51, 'network_attachment_id' => 11, - 'public_ip_pool_id' => 31, 'public_ip_id' => 61, - 'state' => 'committed'], - ['id' => 42, 'operation_id' => $reservationOperation(42), - 'resource_pool_id' => 21, 'host_id' => 1, 'user_id' => 502, - 'instance_id' => 52, 'network_attachment_id' => 11, - 'public_ip_pool_id' => 31, 'public_ip_id' => 62, - 'state' => 'committed'], - ['id' => 43, 'operation_id' => $reservationOperation(43), - 'resource_pool_id' => 21, 'host_id' => 1, 'user_id' => 503, - 'instance_id' => 53, 'network_attachment_id' => 13, - 'public_ip_pool_id' => 33, 'public_ip_id' => 63, - 'state' => 'committed'], + $ipRow = static function (int $id, int $poolId, int $family, + string $address, string $state, ?int $reservationId, + ?int $claimId, ?int $instanceId, ?string $operationId + ) use ($now): array { + return [ + 'id' => $id, + 'host_id' => 1, + 'public_ip_pool_id' => $poolId, + 'address_family' => $family, + 'address' => $address, + 'address_binary' => CloudIpAddress::parse($address)->binary(), + 'prefix_length' => $family === 4 ? 29 : 64, + 'state' => $state, + 'reservation_id' => $reservationId, + 'reservation_ip_claim_id' => $claimId, + 'instance_id' => $instanceId, + 'operation_id' => $operationId, + 'created_at' => $now, + 'updated_at' => $now, + ]; + }; + DB::table('cloud_public_ips')->insert([ + $ipRow(1101, 11, 4, '8.8.8.2', 'assigned', 1001, 10011, + 101, $operation(1001)), + $ipRow(1102, 11, 4, '8.8.8.3', 'assigned', 1002, 10021, + 102, $operation(1002)), + $ipRow(1201, 12, 6, '2404:c140:156:3a::1000', 'assigned', 1001, + 10012, 101, $operation(1001)), + $ipRow(1202, 12, 6, '2404:c140:156:3a::1001', 'assigned', 1002, + 10022, 102, $operation(1002)), + $ipRow(1301, 13, 4, '8.8.8.10', 'available', null, null, + null, null), + $ipRow(1302, 13, 4, '8.8.8.11', 'available', null, null, + null, null), + $ipRow(1501, 15, 4, '8.8.8.18', 'available', null, null, + null, null), + $ipRow(1502, 15, 4, '8.8.8.19', 'available', null, null, + null, null), + $ipRow(1601, 16, 4, '8.8.8.26', 'available', null, null, + null, null), + $ipRow(1602, 16, 4, '8.8.8.27', 'available', null, null, + null, null), + $ipRow(2101, 21, 4, '9.9.9.10', 'assigned', 2001, 20011, + 201, $operation(2001)), + $ipRow(2102, 21, 4, '9.9.9.11', 'assigned', 2002, 20021, + 202, $operation(2002)), + $ipRow(2201, 22, 6, '2404:c140:156:3c::1000', 'assigned', 2001, + 20012, 201, $operation(2001)), + $ipRow(2202, 22, 6, '2404:c140:156:3c::1001', 'assigned', 2002, + 20022, 202, $operation(2002)), ]); - Capsule::table('cloud_resource_reservation_ips')->insert([ - ['id' => 101, 'reservation_id' => 41, 'public_ip_pool_id' => 31, - 'active_claim' => 1, 'state' => 'committed'], - ['id' => 102, 'reservation_id' => 42, 'public_ip_pool_id' => 31, - 'active_claim' => 1, 'state' => 'committed'], - ['id' => 103, 'reservation_id' => 43, 'public_ip_pool_id' => 33, - 'active_claim' => 1, 'state' => 'committed'], + DB::table('cloud_resource_reservations')->insert(array_map( + static function (array $row) use ($operation, $now): array { + [$id, $userId, $instanceId, $resourcePool] = $row; + return [ + 'id' => $id, + 'reservation_key' => str_pad((string) $id, 32, '0'), + 'operation_id' => $operation($id), + 'order_id' => $id, + 'backup_restore_id' => null, + 'offer_id' => 1, + 'resource_pool_id' => $resourcePool, + 'host_id' => 1, + 'user_id' => $userId, + 'instance_id' => $instanceId, + 'network_attachment_id' => 999, + 'public_ip_pool_id' => 999, + 'public_ip_id' => 999, + 'cpu_cores' => 1, + 'memory_mib' => 1024, + 'disk_gib' => 20, + 'state' => 'committed', + 'expires_at' => null, + 'released_at' => null, + 'release_reason' => '', + 'created_at' => $now, + 'updated_at' => $now, + ]; + }, + [ + [1001, 501, 101, 1], + [1002, 502, 102, 1], + [2001, 501, 201, 1], + [2002, 502, 202, 1], + ] + )); + $claim = static function (int $id, int $reservationId, int $family, + int $poolId, int $ipId + ) use ($operation, $now): array { + return [ + 'id' => $id, + 'reservation_id' => $reservationId, + 'address_family' => $family, + 'claim_generation' => 1, + 'active_claim' => 1, + 'network_attachment_id' => 11, + 'public_ip_pool_id' => $poolId, + 'public_ip_id' => $ipId, + 'operation_id' => $operation($reservationId), + 'state' => 'committed', + 'released_at' => null, + 'release_reason' => '', + 'created_at' => $now, + 'updated_at' => $now, + ]; + }; + DB::table('cloud_resource_reservation_ips')->insert([ + $claim(10011, 1001, 4, 11, 1101), + $claim(10012, 1001, 6, 12, 1201), + $claim(10021, 1002, 4, 11, 1102), + $claim(10022, 1002, 6, 12, 1202), + $claim(20011, 2001, 4, 21, 2101), + $claim(20012, 2001, 6, 22, 2201), + $claim(20021, 2002, 4, 21, 2102), + $claim(20022, 2002, 6, 22, 2202), ]); - $instanceSpec = static function (string $publicIp): string { - return json_encode([ - 'managed' => true, - 'os_source' => 'cloud_template', - 'public_ip_cidr' => $publicIp . '/29', - ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); + $publicNetwork = static function (int $instanceId): array { + $v4 = $instanceId < 200 + ? ['pool' => str_repeat('a', 32), 'cidr' => '8.8.8.0/29', + 'address' => $instanceId === 101 + ? '8.8.8.2/29' : '8.8.8.3/29', + 'gateway' => '8.8.8.1'] + : ['pool' => str_repeat('2', 32), 'cidr' => '9.9.9.8/29', + 'address' => $instanceId === 201 + ? '9.9.9.10/29' : '9.9.9.11/29', + 'gateway' => '9.9.9.9']; + $v6 = $instanceId < 200 + ? ['pool' => str_repeat('b', 32), + 'cidr' => '2404:c140:156:3a::/64', + 'address' => $instanceId === 101 + ? '2404:c140:156:3a::1000/64' + : '2404:c140:156:3a::1001/64', + 'gateway' => '2404:c140:156:3a::1'] + : ['pool' => str_repeat('3', 32), + 'cidr' => '2404:c140:156:3c::/64', + 'address' => $instanceId === 201 + ? '2404:c140:156:3c::1000/64' + : '2404:c140:156:3c::1001/64', + 'gateway' => '2404:c140:156:3c::1']; + return [ + 'contract' => 1, + 'network_selector' => 'bridge:br0', + 'ipv4' => [ + 'pool_key' => $v4['pool'], + 'pool_cidr' => $v4['cidr'], + 'address_cidr' => $v4['address'], + 'gateway' => $v4['gateway'], + 'gateway_on_link' => false, + ], + 'ipv6' => [ + 'pool_key' => $v6['pool'], + 'pool_cidr' => $v6['cidr'], + 'address_cidr' => $v6['address'], + 'gateway' => $v6['gateway'], + 'gateway_on_link' => false, + ], + 'dns_servers' => ['1.1.1.1'], + ]; }; - Capsule::table('cloud_instances')->insert([ - ['id' => 51, 'uuid' => '11111111-1111-4111-8111-111111111111', - 'name' => 'vm-a1', 'user_id' => 501, 'host_id' => 1, - 'resource_pool_id' => 21, 'reservation_id' => 41, - 'status' => 'active', - 'spec_json' => $instanceSpec('198.51.100.2')], - ['id' => 52, 'uuid' => '22222222-2222-4222-8222-222222222222', - 'name' => 'vm-a2', 'user_id' => 502, 'host_id' => 1, - 'resource_pool_id' => 21, 'reservation_id' => 42, - 'status' => 'active', - 'spec_json' => $instanceSpec('198.51.100.3')], - ['id' => 53, 'uuid' => '33333333-3333-4333-8333-333333333333', - 'name' => 'vm-d1', 'user_id' => 503, 'host_id' => 1, - 'resource_pool_id' => 21, 'reservation_id' => 43, + foreach ([101, 102, 201, 202] as $instanceId) { + $reservationId = $instanceId < 200 + ? 900 + $instanceId : 1800 + $instanceId; + DB::table('cloud_instances')->insert([ + 'id' => $instanceId, + 'host_id' => 1, + 'user_id' => $instanceId % 2 === 1 ? 501 : 502, + 'order_id' => $reservationId, + 'plan_id' => 1, + 'offer_id' => 1, + 'resource_pool_id' => 1, + 'reservation_id' => $reservationId, + 'uuid' => sprintf('00000000-0000-4000-8000-%012d', $instanceId), + 'name' => 'vm-' . $instanceId, 'status' => 'active', - 'spec_json' => $instanceSpec('198.51.100.18')], - ]); - Capsule::table('cloud_public_ips')->insert([ - ['id' => 61, 'host_id' => 1, 'public_ip_pool_id' => 31, - 'address' => '198.51.100.2', 'prefix_length' => 29, - 'state' => 'assigned', 'reservation_id' => 41, - 'instance_id' => 51, - 'operation_id' => $reservationOperation(41)], - ['id' => 62, 'host_id' => 1, 'public_ip_pool_id' => 31, - 'address' => '198.51.100.3', 'prefix_length' => 29, - 'state' => 'assigned', 'reservation_id' => 42, - 'instance_id' => 52, - 'operation_id' => $reservationOperation(42)], - ['id' => 63, 'host_id' => 1, 'public_ip_pool_id' => 33, - 'address' => '198.51.100.18', 'prefix_length' => 29, - 'state' => 'assigned', 'reservation_id' => 43, - 'instance_id' => 53, - 'operation_id' => $reservationOperation(43)], - ['id' => 71, 'host_id' => 1, 'public_ip_pool_id' => 32, - 'address' => '198.51.100.10', 'prefix_length' => 29, - 'state' => 'available', 'reservation_id' => null, - 'instance_id' => null, 'operation_id' => null], - ['id' => 72, 'host_id' => 1, 'public_ip_pool_id' => 32, - 'address' => '198.51.100.11', 'prefix_length' => 29, - 'state' => 'available', 'reservation_id' => null, - 'instance_id' => null, 'operation_id' => null], - ['id' => 73, 'host_id' => 1, 'public_ip_pool_id' => 32, - 'address' => '198.51.100.12', 'prefix_length' => 29, - 'state' => 'available', 'reservation_id' => null, - 'instance_id' => null, 'operation_id' => null], - ['id' => 74, 'host_id' => 1, 'public_ip_pool_id' => 32, - 'address' => '198.51.100.13', 'prefix_length' => 29, - 'state' => 'available', 'reservation_id' => null, - 'instance_id' => null, 'operation_id' => null], - ['id' => 75, 'host_id' => 1, 'public_ip_pool_id' => 34, - 'address' => '198.51.100.26', 'prefix_length' => 29, - 'state' => 'available', 'reservation_id' => null, - 'instance_id' => null, 'operation_id' => null], - ]); + 'service_status' => 'active', + 'network_status' => 'active', + 'expires_at' => $now + 86400, + 'spec_json' => json_encode([ + 'managed' => true, + 'os_source' => 'cloud_template', + 'public_network' => $publicNetwork($instanceId), + ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES), + 'created_at' => $now, + 'updated_at' => $now, + ]); + } $admin = [ 'user_id' => 99, @@ -459,441 +715,578 @@ namespace { 'can_manage' => true, ]; $readAdmin = [ - 'user_id' => 98, + 'user_id' => 99, 'is_admin' => true, - 'is_super_admin' => false, + 'is_super_admin' => true, 'can_manage' => false, ]; - $sourceKey = str_repeat('a', 32); - $targetKey = str_repeat('b', 32); - $otherSourceKey = str_repeat('c', 32); + $input = static function (string $targetKey, int $sourceRevision, + int $targetRevision, string $requestKey): array { + return [ + 'target_pool_key' => $targetKey, + 'expected_source_revision' => $sourceRevision, + 'expected_target_revision' => $targetRevision, + 'confirm_replacement' => true, + 'request_key' => $requestKey, + ]; + }; - $overview = CloudPublicIpPoolReplacementBatchService::overview( + $v4Overview = CloudPublicIpPoolReplacementBatchService::overview( 1, - $sourceKey, + $poolKey('a'), $readAdmin ); - $assert($overview['source_pool']['expected_revision'] === 7 - && $overview['source_pool']['assigned_count'] === 2 - && $overview['source_pool']['replacement_locked'] === false - && $overview['can_start'] === true, - 'Read-only administrator overview lost source revision or occupancy.'); - $targetOptions = array_column( - $overview['target_options'], - null, - 'pool_key' - ); - $assert(isset($targetOptions[$targetKey]) - && $targetOptions[$targetKey]['expected_revision'] === 9 - && $targetOptions[$targetKey]['available_count'] === 4 - && $targetOptions[$targetKey]['eligible'] === true - && !isset($targetOptions[str_repeat('d', 32)]), - 'Overview did not enforce same-line verified target selection.'); - $expectCloudError( - static fn () => CloudPublicIpPoolReplacementBatchService::overview( - 1, - $sourceKey, - ['user_id' => 501, 'is_admin' => false] - ), - 403, - 'Tenant must not read administrator replacement batches.' + $v4Targets = array_column($v4Overview['target_options'], null, 'pool_key'); + $assert($v4Overview['source_pool']['assigned_count'] === 2 + && $v4Targets[$poolKey('c')]['available_count'] === 2 + && $v4Targets[$poolKey('c')]['eligible'] === true + && !isset($v4Targets[$poolKey('d')]) + && !isset($v4Targets[$poolKey('e')]) + && !isset($v4Targets[$poolKey('f')]), + 'Overview accepted a cross-family/attachment/line target: ' + . json_encode($v4Overview, JSON_UNESCAPED_SLASHES)); + $v6Overview = CloudPublicIpPoolReplacementBatchService::overview( + 1, + $poolKey('3'), + $readAdmin ); + $v6Targets = array_column($v6Overview['target_options'], null, 'pool_key'); + $assert($v6Targets[$poolKey('4')]['available_count'] === 4 + && $v6Targets[$poolKey('4')]['eligible'] === true, + 'Sparse IPv6 overview used materialized row count instead of capacity.'); - // C10 keeps the read-only occupancy projection on active claims while the - // scalar replacement writer is fail-closed until C11 owns a claim pair. - $gatedInput = [ - 'target_pool_key' => $targetKey, - 'expected_source_revision' => 7, - 'expected_target_revision' => 9, - 'confirm_replacement' => true, - 'request_key' => 'replacement-c10-gated-0001', - ]; - $expectCloudError( - static fn () => CloudPublicIpPoolReplacementBatchService::start( - 1, - $sourceKey, - $admin, - $gatedInput - ), - 409, - 'Pool replacement must remain gated before its dual-stack writer.' - ); - $assert(CloudJob::count() === 0 - && CloudPublicIpPoolReplacementBatch::count() === 0 - && Capsule::table('cloud_public_ip_pools')->where('id', 31) - ->value('state') === 'active' - && Capsule::table('cloud_public_ips')->whereIn('id', [71, 72, 73, 74]) - ->where('state', 'available')->count() === 4, - 'Gated pool replacement created durable or address-side effects.'); + foreach ([ + [$poolKey('d'), 14, 'cross-family'], + [$poolKey('e'), 15, 'cross-attachment'], + [$poolKey('f'), 16, 'cross-line'], + [$poolKey('1'), 17, 'cross-host'], + ] as [$targetKey, $revision, $label]) { + $before = $snapshot(); + $rejects(static fn () => + CloudPublicIpPoolReplacementBatchService::start( + 1, + $poolKey('a'), + $admin, + $input($targetKey, 11, $revision, + 'replacement-reject-' . $label) + ), 'Replacement accepted ' . $label . ' target.'); + $assert($snapshot() === $before, + 'Rejected ' . $label . ' target wrote durable state.'); + } - // Lock-order convergence must use the same locked item collection it - // updates. One unknown projection followed by one resolved projection is - // sufficient to release the blocked batch; no second hook may be needed. - $job = static function (int $id, string $operationId, string $requestKey, - string $kind, ?int $parentId, ?int $instanceId - ) use ($now): array { - return [ - 'id' => $id, - 'operation_id' => $operationId, - 'request_idempotency_key' => $requestKey, - 'request_fingerprint' => hash('sha256', $requestKey), - 'parent_job_id' => $parentId, - 'instance_id' => $instanceId, - 'user_id' => 99, - 'host_id' => $parentId === null ? null : 1, - 'kind' => $kind, - 'execution_mode' => $parentId === null ? 'local' : 'agent', - 'phase' => 'accepted', - 'outcome' => null, - 'status' => 'queued', - 'sealed_payload' => '{}', - 'attempts' => 0, - 'dispatch_attempts' => 0, - 'dispatch_deadline_at' => $now + 300, - 'effect_resolution' => 'not_applicable', - 'finalization_incident' => 0, - 'lease_until' => 0, - 'available_at' => $now, - 'created_at' => $now, - 'updated_at' => $now, - ]; - }; - Capsule::table('cloud_jobs')->insert([ - $job(900, str_repeat('9', 32), 'batch-lock-parent-0001', - 'public_ip_pool_replacement_batch', null, null), - $job(901, str_repeat('8', 32), 'batch-lock-child-0001', - 'network_change', 900, 51), - ]); - Capsule::table('cloud_public_ip_pool_replacement_batches')->insert([ - 'id' => 900, - 'batch_key' => str_repeat('7', 32), - 'host_id' => 1, - 'source_public_ip_pool_id' => 31, - 'target_public_ip_pool_id' => 32, - 'source_initial_state' => 'active', - 'parent_job_id' => 900, - 'requested_by_user_id' => 99, - 'state' => 'running', - 'active_claim' => 1, - 'last_error_code' => '', - 'terminal_at' => null, - 'created_at' => $now, - 'updated_at' => $now, + DB::table('cloud_public_ips')->where('id', 1302)->update([ + 'state' => 'reserved', + 'operation_id' => str_repeat('9', 32), ]); - Capsule::table('cloud_public_ip_pool_replacement_batch_items')->insert([ - 'id' => 900, - 'batch_id' => 900, - 'instance_id' => 51, - 'source_reservation_id' => 41, - 'source_public_ip_id' => 61, - 'target_public_ip_id' => 71, - 'source_network_attachment_id' => 11, - 'target_network_attachment_id' => 12, - 'child_job_id' => 901, - 'state' => 'queued', - 'last_error_code' => '', - 'created_at' => $now, - 'updated_at' => $now, + $beforeShortage = $snapshot(); + $rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start( + 1, + $poolKey('a'), + $admin, + $input($poolKey('c'), 11, 13, 'replacement-v4-shortage-0001') + ), 'IPv4 target shortage was accepted.'); + $assert($snapshot() === $beforeShortage, + 'IPv4 target shortage left partial batch writes.'); + DB::table('cloud_public_ips')->where('id', 1302)->update([ + 'state' => 'available', + 'operation_id' => null, ]); - $lockedChild = CloudJob::find(901); - CloudPublicIpPoolReplacementBatchService::finishChildLocked( - $lockedChild, - 'uncertain' - ); - $assert(CloudPublicIpPoolReplacementBatch::find(900)->state === 'blocked' - && Capsule::table('cloud_public_ip_pool_replacement_batch_items') - ->where('id', 900)->value('state') === 'unknown', - 'One unknown child projection did not block the batch immediately.'); - $lockedChild->phase = 'terminal'; - $lockedChild->outcome = 'failed'; - $lockedChild->status = 'failed'; - $lockedChild->effect_may_have_applied = 1; - $lockedChild->safe_to_resubmit = 0; - $lockedChild->effect_resolution = 'applied'; - $lockedChild->save(); - CloudPublicIpPoolReplacementBatchService::finishResolvedChildLocked( - $lockedChild->fresh() - ); - $resolvedBatch = CloudPublicIpPoolReplacementBatch::find(900); - $assert((string) $resolvedBatch->state === 'failed' - && $resolvedBatch->active_claim === null - && Capsule::table('cloud_public_ip_pool_replacement_batch_items') - ->where('id', 900)->value('state') === 'succeeded' - && (string) CloudJob::find(900)->phase === 'terminal', - 'One resolved child projection left the batch permanently blocked.'); - echo "Cloud public IP pool replacement C10 gate tests passed\n"; - return; - $staleInput = [ - 'target_pool_key' => $targetKey, - 'expected_source_revision' => 6, - 'expected_target_revision' => 9, - 'confirm_replacement' => true, - 'request_key' => 'replacement-stale-0001', - ]; - $expectCloudError( - static fn () => CloudPublicIpPoolReplacementBatchService::start( + DB::statement(<<<'SQL' +CREATE TRIGGER fail_second_pool_replacement_child +BEFORE INSERT ON cloud_jobs +WHEN NEW.kind = 'network_change' AND NEW.instance_id = 102 +BEGIN + SELECT RAISE(ABORT, 'injected second child failure'); +END +SQL); + $beforeFault = $snapshot(); + try { + CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, + $poolKey('a'), $admin, - $staleInput - ), - 409, - 'A stale source revision must fail after the locked recheck.' - ); - $assert(CloudJob::count() === 0, - 'Stale revision rejection created a durable operation.'); + $input($poolKey('c'), 11, 13, + 'replacement-v4-second-fault') + ); + throw new RuntimeException('Second-child fault was not raised.'); + } catch (Throwable $error) { + $assert(str_contains($error->getMessage(), 'second child failure'), + 'Unexpected error replaced the injected second-child fault: ' + . $error->getMessage()); + } + $assert($snapshot() === $beforeFault, + 'Second-child fault did not roll back the entire batch transaction.'); + DB::statement('DROP TRIGGER fail_second_pool_replacement_child'); - $input = [ - 'target_pool_key' => $targetKey, - 'expected_source_revision' => 7, - 'expected_target_revision' => 9, - 'confirm_replacement' => true, - 'request_key' => 'replacement-valid-0001', - ]; + $companionBefore = DB::table('cloud_resource_reservation_ips') + ->whereIn('id', [10012, 10022])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all(); + $companionIpsBefore = DB::table('cloud_public_ips') + ->whereIn('id', [1201, 1202])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all(); + $v4Input = $input( + $poolKey('c'), + 11, + 13, + 'replacement-v4-success-0001' + ); $started = CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, + $poolKey('a'), $admin, - $input + $v4Input ); - $assert($started['unchanged'] === false - && $started['batch']['state'] === 'running' - && $started['batch']['source_initial_state'] === 'active' - && $started['batch']['counts']['total'] === 2, - 'Valid replacement did not create one running durable batch.'); - $parent = CloudJob::find((int) $started['job']->id); - $children = CloudJob::where('parent_job_id', (int) $parent->id) - ->orderBy('instance_id')->get(); - $assert($children->count() === 2 - && $children->every(static fn ($job): bool => - (string) $job->kind === 'network_change' - && (string) $job->execution_mode === 'agent') - && Capsule::table('cloud_public_ips') - ->whereIn('id', [71, 72])->where('state', 'changing') - ->count() === 2 - && Capsule::table('cloud_public_ip_pools')->where('id', 31) - ->value('state') === 'draining', - 'Batch did not atomically fence every target IP and drain the source.'); - $runningChild = $children->first(); - $runningChild->phase = 'dispatching'; - $runningChild->status = 'running'; - $runningChild->save(); - $runningDetail = CloudPublicIpPoolReplacementBatchService::detail( + $parentId = (int) $started['job']->id; + $batchId = (int) CloudPublicIpPoolReplacementBatch::where( + 'parent_job_id', + $parentId + )->value('id'); + $items = DB::table('cloud_public_ip_pool_replacement_batch_items') + ->where('batch_id', $batchId)->orderBy('instance_id')->get(); + $assert($started['unchanged'] === false && $items->count() === 2 + && $items->every(static fn ($item): bool => + (int) $item->address_family === 4 + && (string) $item->state === 'queued' + && $item->child_job_id !== null), + 'IPv4 replacement did not queue two production-shaped items.'); + foreach ($items as $item) { + $child = DB::table('cloud_jobs')->where( + 'id', + (int) $item->child_job_id + )->first(); + $transition = DB::table('cloud_public_network_transitions')->where( + 'id', + (int) $child->public_network_transition_id + )->first(); + $legs = DB::table('cloud_public_network_transition_ips')->where( + 'transition_id', + (int) $transition->id + )->orderBy('address_family')->get(); + $assert((int) $child->parent_job_id === $parentId + && (int) $child->instance_id === (int) $item->instance_id + && (int) $transition->pool_replacement_batch_item_id + === (int) $item->id + && (int) $transition->instance_id === (int) $item->instance_id + && $legs->count() === 2 + && (int) $legs[0]->address_family === 4 + && (int) $legs[0]->changed === 1 + && $legs[0]->target_claim_id !== null + && (int) $legs[1]->address_family === 6 + && (int) $legs[1]->changed === 0 + && $legs[1]->target_claim_id === null, + 'Item/transition/child dual-stack topology is incomplete.'); + } + $assert(DB::table('cloud_resource_reservation_ips') + ->whereIn('id', [10012, 10022])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all() + === $companionBefore + && DB::table('cloud_public_ips')->whereIn('id', [1201, 1202]) + ->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all() + === $companionIpsBefore, + 'IPv4 replacement mutated its IPv6 companion family.'); + + $activeSnapshot = $snapshot(); + $replay = CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, - $started['batch']['batch_key'], - $readAdmin + $poolKey('a'), + $admin, + $v4Input ); - $assert($runningDetail['counts']['running'] === 1 - && $runningDetail['counts']['queued'] === 1 - && $runningDetail['items'][0]['state'] === 'running', - 'Batch DTO did not project an executing child as running.'); + $assert($replay['unchanged'] === true + && (int) $replay['job']->id === $parentId + && $snapshot() === $activeSnapshot, + 'Exact active replay duplicated or changed durable topology.'); - $idempotent = CloudPublicIpPoolReplacementBatchService::start( + $firstChildId = (int) $items->first()->child_job_id; + DB::table('cloud_jobs')->where('id', $firstChildId)->update([ + 'parent_job_id' => null, + ]); + $rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, + $poolKey('a'), $admin, - $input - ); - $assert($idempotent['unchanged'] === true - && (int) $idempotent['job']->id === (int) $parent->id - && CloudJob::where('parent_job_id', (int) $parent->id)->count() === 2, - 'Idempotent retry duplicated child mutations.'); + $v4Input + ), 'Active replay accepted a corrupted child-parent topology.'); + DB::table('cloud_jobs')->where('id', $firstChildId)->update([ + 'parent_job_id' => $parentId, + ]); - $activeOverview = CloudPublicIpPoolReplacementBatchService::overview( + DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([ + 'state' => 'uncertain', + ]); + $rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, - $readAdmin - ); - $activeTargets = array_column( - $activeOverview['target_options'], - null, - 'pool_key' - ); - $assert($activeOverview['source_pool']['replacement_locked'] === true - && $activeOverview['can_start'] === false - && $activeOverview['active_batch']['batch_key'] - === $started['batch']['batch_key'] - && $activeTargets[$targetKey]['replacement_locked'] === true - && $activeTargets[$targetKey]['eligible'] === false, - 'Overview did not publish source/target participation locks.'); + $poolKey('a'), + $admin, + $v4Input + ), 'Active replay accepted a drifted companion claim.'); + DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([ + 'state' => 'committed', + ]); - $overlapInput = [ - 'target_pool_key' => $targetKey, - 'expected_source_revision' => 4, - 'expected_target_revision' => 9, - 'confirm_replacement' => true, - 'request_key' => 'replacement-overlap-001', - ]; - $expectCloudError( - static fn () => CloudPublicIpPoolReplacementBatchService::start( - 1, - $otherSourceKey, - $admin, - $overlapInput - ), - 409, - 'An active target pool must not join a second concurrent batch.' - ); - $expectCloudError( - static fn () => CloudPublicIpPoolReplacementBatchService::detail( - 1, - $otherSourceKey, - $started['batch']['batch_key'], - $readAdmin - ), - 404, - 'Batch detail must bind the batch to its exact source pool.' + // Finishing refuses a child whose frozen topology has drifted. The + // poisoned scalar reservation mirrors remain irrelevant throughout. + DB::table('cloud_jobs')->where('id', $firstChildId)->update([ + 'host_id' => 2, + ]); + $rejects(static fn () => + CloudPublicIpPoolReplacementBatchService::finishChildLocked( + CloudJob::find($firstChildId), + 'succeeded' + ), 'Finish accepted a child with a drifted frozen host.'); + DB::table('cloud_jobs')->where('id', $firstChildId)->update([ + 'host_id' => 1, + ]); + + $firstChild = CloudJob::find($firstChildId); + CloudPublicIpPoolReplacementBatchService::finishChildLocked( + $firstChild, + 'uncertain' ); + $assert((string) CloudPublicIpPoolReplacementBatch::find($batchId)->state + === 'blocked' + && (int) CloudPublicIpPoolReplacementBatch::find($batchId)->active_claim + === 1 + && DB::table('cloud_public_ip_pool_replacement_batch_items') + ->where('child_job_id', $firstChildId)->value('state') === 'unknown' + && (string) CloudJob::find($parentId)->phase !== 'terminal', + 'Unknown child did not retain the batch and parent fences.'); - $settleChild = static function ( - CloudJob $child, - string $outcome, - bool $publish = true - ): void { - $item = Capsule::table( - 'cloud_public_ip_pool_replacement_batch_items' - )->where('child_job_id', (int) $child->id)->first(); - $instance = CloudInstance::find((int) $child->instance_id); - CloudPlacementService::finishNetworkChange( - $instance, - (string) $child->operation_id, - (int) $item->source_public_ip_id, - (int) $item->target_public_ip_id, - (int) $item->source_network_attachment_id, - (int) $item->target_network_attachment_id, - (int) Capsule::table('cloud_public_ips') - ->where('id', (int) $item->source_public_ip_id) - ->value('public_ip_pool_id'), - 32, - $outcome + $applyChild = static function (int $childId): CloudJob { + $child = CloudJob::find($childId); + $transition = DB::table('cloud_public_network_transitions')->where( + 'id', + (int) $child->public_network_transition_id + )->first(); + $changed = DB::table('cloud_public_network_transition_ips') + ->where('transition_id', (int) $transition->id) + ->where('changed', 1)->first(); + $sourceClaim = DB::table('cloud_resource_reservation_ips')->where( + 'id', + (int) $changed->source_claim_id + )->first(); + $targetClaim = DB::table('cloud_resource_reservation_ips')->where( + 'id', + (int) $changed->target_claim_id + )->first(); + $sourceIpId = (int) $sourceClaim->public_ip_id; + $targetIpId = (int) $targetClaim->public_ip_id; + $finishedAt = time(); + DB::table('cloud_resource_reservation_ips')->where( + 'id', + (int) $sourceClaim->id + )->update([ + 'active_claim' => null, + 'state' => 'released', + 'released_at' => $finishedAt, + 'release_reason' => 'network_change_succeeded', + 'updated_at' => $finishedAt, + ]); + DB::table('cloud_public_ips')->where('id', $sourceIpId)->update([ + 'state' => 'available', + 'reservation_id' => null, + 'reservation_ip_claim_id' => null, + 'instance_id' => null, + 'operation_id' => null, + 'updated_at' => $finishedAt, + ]); + DB::table('cloud_resource_reservation_ips')->where( + 'id', + (int) $targetClaim->id + )->update([ + 'active_claim' => 1, + 'state' => 'committed', + 'updated_at' => $finishedAt, + ]); + DB::table('cloud_public_ips')->where('id', $targetIpId)->update([ + 'state' => 'assigned', + 'reservation_id' => (int) $targetClaim->reservation_id, + 'reservation_ip_claim_id' => (int) $targetClaim->id, + 'instance_id' => (int) $child->instance_id, + 'operation_id' => (string) $targetClaim->operation_id, + 'updated_at' => $finishedAt, + ]); + DB::table('cloud_public_network_transitions')->where( + 'id', + (int) $transition->id + )->update([ + 'state' => 'succeeded', + 'effect_resolution' => 'applied', + 'active_transition' => null, + 'effect_resolved_at' => $finishedAt, + 'terminal_at' => $finishedAt, + 'updated_at' => $finishedAt, + ]); + $targetNetwork = json_decode( + (string) $transition->target_public_network_json, + true, + 64, + JSON_THROW_ON_ERROR + ); + $spec = json_decode( + (string) DB::table('cloud_instances')->where( + 'id', + (int) $child->instance_id + )->value('spec_json'), + true, + 64, + JSON_THROW_ON_ERROR ); - if ($outcome === 'succeeded') { - $target = Capsule::table('cloud_public_ips') - ->where('id', (int) $item->target_public_ip_id)->first(); - $spec = json_decode((string) $instance->spec_json, true); - $spec['public_ip_cidr'] = (string) $target->address . '/29'; - $instance->spec_json = json_encode( + $spec['public_network'] = $targetNetwork; + DB::table('cloud_instances')->where( + 'id', + (int) $child->instance_id + )->update([ + 'spec_json' => json_encode( $spec, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES - ); - $instance->save(); - } - if ($publish) { - CloudPublicIpPoolReplacementBatchService::finishChildLocked( - $child, - $outcome - ); - } + ), + 'updated_at' => $finishedAt, + ]); + DB::table('cloud_jobs')->where('id', $childId)->update([ + 'phase' => 'terminal', + 'outcome' => 'succeeded', + 'status' => 'succeeded', + 'terminal_at' => $finishedAt, + 'effect_may_have_applied' => 0, + 'safe_to_resubmit' => 0, + 'effect_resolution' => 'applied', + 'effect_resolved_at' => $finishedAt, + 'error_code' => null, + 'updated_at' => $finishedAt, + ]); + return CloudJob::find($childId); }; - $first = $children->first(); - $settleChild($first, 'uncertain'); - $blocked = CloudPublicIpPoolReplacementBatch::where( - 'batch_key', $started['batch']['batch_key'] - )->first(); - $assert((string) $blocked->state === 'blocked' - && (int) $blocked->active_claim === 1 - && (string) $parent->fresh()->phase !== 'terminal', - 'Unknown child effect did not retain the active claim and parent fence.'); + $resolvedFirst = $applyChild($firstChildId); + CloudPublicIpPoolReplacementBatchService::finishResolvedChildLocked( + $resolvedFirst + ); + $assert(DB::table('cloud_public_ip_pool_replacement_batch_items') + ->where('child_job_id', $firstChildId) + ->value('state') === 'succeeded' + && (string) CloudPublicIpPoolReplacementBatch::find($batchId)->state + === 'running' + && (string) CloudJob::find($parentId)->phase !== 'terminal', + 'One resolved child did not release blocked state in one hook.'); - $settleChild($first->fresh(), 'succeeded', false); - $resolvedFirst = $first->fresh(); - $resolvedFirst->phase = 'terminal'; - $resolvedFirst->outcome = 'failed'; - $resolvedFirst->status = 'failed'; - $resolvedFirst->effect_may_have_applied = 1; - $resolvedFirst->safe_to_resubmit = 0; - $resolvedFirst->effect_resolution = 'applied'; - $resolvedFirst->save(); - CloudJobService::reconcileAggregateParentEffectLocked( - $resolvedFirst, - 99 + $secondChildId = (int) $items->last()->child_job_id; + $resolvedSecond = $applyChild($secondChildId); + CloudPublicIpPoolReplacementBatchService::finishChildLocked( + $resolvedSecond, + 'succeeded' ); - $settleChild($children->last()->fresh(), 'succeeded'); - $completed = $blocked->fresh(); - $parent = $parent->fresh(); - $assert((string) $completed->state === 'succeeded' - && $completed->active_claim === null - && (string) $parent->phase === 'terminal' - && (string) $parent->outcome === 'succeeded' - && Capsule::table('cloud_public_ip_pools')->where('id', 31) + $terminalBatch = CloudPublicIpPoolReplacementBatch::find($batchId); + $terminalParent = CloudJob::find($parentId); + $assert((string) $terminalBatch->state === 'succeeded' + && $terminalBatch->active_claim === null + && (string) $terminalParent->phase === 'terminal' + && (string) $terminalParent->outcome === 'succeeded' + && DB::table('cloud_public_ip_pools')->where('id', 11) ->value('state') === 'disabled' - && Capsule::table('cloud_public_ips')->where('public_ip_pool_id', 31) - ->where('state', '<>', 'available')->count() === 0, - 'Evidence-backed recovery did not converge the batch and disable the empty source.'); + && DB::table('cloud_public_ips')->where('public_ip_pool_id', 11) + ->where(function ($query): void { + $query->where('state', '<>', 'available') + ->orWhereNotNull('reservation_id') + ->orWhereNotNull('reservation_ip_claim_id') + ->orWhereNotNull('instance_id') + ->orWhereNotNull('operation_id'); + })->count() === 0, + 'All-success children did not converge parent/batch/source in one pass.'); - $completedOverview = CloudPublicIpPoolReplacementBatchService::overview( + // A strict terminal topology remains replayable after the instance has + // legitimately acquired a later companion-family generation. + $terminalAt = time() + 10; + DB::table('cloud_resource_reservation_ips')->where('id', 10012)->update([ + 'active_claim' => null, + 'state' => 'released', + 'released_at' => $terminalAt, + 'release_reason' => 'later_manual_change', + 'updated_at' => $terminalAt, + ]); + DB::table('cloud_public_ips')->where('id', 1201)->update([ + 'state' => 'available', + 'reservation_id' => null, + 'reservation_ip_claim_id' => null, + 'instance_id' => null, + 'operation_id' => null, + 'updated_at' => $terminalAt, + ]); + $newCompanionOperation = str_repeat('8', 32); + DB::table('cloud_public_ips')->insert([ + 'id' => 1203, + 'host_id' => 1, + 'public_ip_pool_id' => 12, + 'address_family' => 6, + 'address' => '2404:c140:156:3a::1002', + 'address_binary' => CloudIpAddress::parse( + '2404:c140:156:3a::1002' + )->binary(), + 'prefix_length' => 64, + 'state' => 'assigned', + 'reservation_id' => 1001, + 'reservation_ip_claim_id' => 10013, + 'instance_id' => 101, + 'operation_id' => $newCompanionOperation, + 'created_at' => $terminalAt, + 'updated_at' => $terminalAt, + ]); + DB::table('cloud_resource_reservation_ips')->insert([ + 'id' => 10013, + 'reservation_id' => 1001, + 'address_family' => 6, + 'claim_generation' => 2, + 'active_claim' => 1, + 'network_attachment_id' => 11, + 'public_ip_pool_id' => 12, + 'public_ip_id' => 1203, + 'operation_id' => $newCompanionOperation, + 'state' => 'committed', + 'released_at' => null, + 'release_reason' => '', + 'created_at' => $terminalAt, + 'updated_at' => $terminalAt, + ]); + $instanceSpec = json_decode( + (string) DB::table('cloud_instances')->where('id', 101) + ->value('spec_json'), + true, + 64, + JSON_THROW_ON_ERROR + ); + $instanceSpec['public_network']['ipv6']['address_cidr'] = + '2404:c140:156:3a::1002/64'; + DB::table('cloud_instances')->where('id', 101)->update([ + 'spec_json' => json_encode( + $instanceSpec, + JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES + ), + 'updated_at' => $terminalAt, + ]); + $terminalSnapshot = $snapshot(); + $terminalReplay = CloudPublicIpPoolReplacementBatchService::start( 1, - $sourceKey, - $readAdmin + $poolKey('a'), + $admin, + $v4Input ); - $assert($completedOverview['replacement_history_count'] === 1 - && $completedOverview['has_replacement_history'] === true - && $completedOverview['source_pool']['replacement_history_count'] === 1, - 'Read-only overview did not expose terminal replacement history.'); + $assert($terminalReplay['unchanged'] === true + && (int) $terminalReplay['job']->id === $parentId + && $snapshot() === $terminalSnapshot, + 'Strict terminal replay rejected a later companion generation.'); - Capsule::table('cloud_public_ip_pools')->where('id', 33)->update([ - 'state' => 'draining', - 'desired_revision' => 5, - 'observed_revision' => 5, + // Sparse IPv6 shortage is a zero-write rejection even though the target + // catalog has no pre-materialized rows. + DB::table('cloud_public_ip_pools')->where('id', 23)->update([ + 'max_allocations' => 1, + ]); + $beforeV6Shortage = $snapshot(); + $rejects(static fn () => CloudPublicIpPoolReplacementBatchService::start( + 1, + $poolKey('3'), + $admin, + $input($poolKey('4'), 22, 23, 'replacement-v6-shortage-0001') + ), 'Sparse IPv6 capacity shortage was accepted.'); + $assert($snapshot() === $beforeV6Shortage, + 'Sparse IPv6 shortage materialized rows or wrote batch state.'); + DB::table('cloud_public_ip_pools')->where('id', 23)->update([ + 'max_allocations' => 4, ]); - $failureInput = [ - 'target_pool_key' => $targetKey, - 'expected_source_revision' => 5, - 'expected_target_revision' => 9, - 'confirm_replacement' => true, - 'request_key' => 'replacement-failure-001', - ]; - $failure = CloudPublicIpPoolReplacementBatchService::start( + $v4CompanionBefore = DB::table('cloud_resource_reservation_ips') + ->whereIn('id', [20011, 20021])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all(); + $v4CompanionIpsBefore = DB::table('cloud_public_ips') + ->whereIn('id', [2101, 2102])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all(); + $v6Input = $input( + $poolKey('4'), + 22, + 23, + 'replacement-v6-success-0001' + ); + $v6Started = CloudPublicIpPoolReplacementBatchService::start( 1, - $otherSourceKey, + $poolKey('3'), $admin, - $failureInput + $v6Input + ); + $v6ParentId = (int) $v6Started['job']->id; + $v6BatchId = (int) CloudPublicIpPoolReplacementBatch::where( + 'parent_job_id', + $v6ParentId + )->value('id'); + $v6Items = DB::table('cloud_public_ip_pool_replacement_batch_items') + ->where('batch_id', $v6BatchId)->orderBy('instance_id')->get(); + $targetV6Ids = $v6Items->pluck('target_public_ip_id')->all(); + $targetV6 = DB::table('cloud_public_ips')->whereIn('id', $targetV6Ids) + ->orderBy('id')->get(); + $assert($v6Items->count() === 2 + && $v6Items->every(static fn ($item): bool => + (int) $item->address_family === 6 + && (string) $item->state === 'queued' + && $item->child_job_id !== null) + && count(array_unique($targetV6Ids)) === 2 + && $targetV6->pluck('address')->all() === [ + '2404:c140:156:3d::1000', + '2404:c140:156:3d::1001', + ] + && DB::table('cloud_public_ip_pools')->where('id', 23) + ->value('allocation_cursor') === '2404:c140:156:3d::1002', + 'Sparse IPv6 replacement did not materialize bounded unique targets.'); + $assert(DB::table('cloud_resource_reservation_ips') + ->whereIn('id', [20011, 20021])->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all() + === $v4CompanionBefore + && DB::table('cloud_public_ips')->whereIn('id', [2101, 2102]) + ->orderBy('id')->get() + ->map(static fn ($row): array => (array) $row)->all() + === $v4CompanionIpsBefore, + 'IPv6 replacement mutated its IPv4 companion family.'); + foreach ($v6Items as $item) { + $child = DB::table('cloud_jobs')->where( + 'id', + (int) $item->child_job_id + )->first(); + $transition = DB::table('cloud_public_network_transitions')->where( + 'id', + (int) $child->public_network_transition_id + )->first(); + $legs = DB::table('cloud_public_network_transition_ips')->where( + 'transition_id', + (int) $transition->id + )->orderBy('address_family')->get(); + $assert((int) $child->parent_job_id === $v6ParentId + && (int) $transition->pool_replacement_batch_item_id + === (int) $item->id + && $legs->count() === 2 + && (int) $legs[0]->address_family === 4 + && (int) $legs[0]->changed === 0 + && $legs[0]->target_claim_id === null + && (int) $legs[1]->address_family === 6 + && (int) $legs[1]->changed === 1 + && $legs[1]->target_claim_id !== null, + 'Sparse IPv6 child did not freeze the companion IPv4 leg.'); + } + + $jobService = file_get_contents( + dirname(__DIR__) . '/app/Services/Cloud/CloudJobService.php' ); - $failureParent = CloudJob::find((int) $failure['job']->id); - $failureChild = CloudJob::where( - 'parent_job_id', (int) $failureParent->id - )->first(); - $settleChild($failureChild, 'failed'); - $resolvedFailureChild = $failureChild->fresh(); - $resolvedFailureChild->phase = 'terminal'; - $resolvedFailureChild->outcome = 'failed'; - $resolvedFailureChild->status = 'failed'; - $resolvedFailureChild->effect_may_have_applied = 0; - $resolvedFailureChild->safe_to_resubmit = 1; - $resolvedFailureChild->effect_resolution = 'not_applied'; - $resolvedFailureChild->error_code = 'agent_network_not_applied'; - $resolvedFailureChild->save(); - CloudJobService::reconcileAggregateParentEffectLocked( - $resolvedFailureChild, - 99 + $batchService = file_get_contents( + dirname(__DIR__) + . '/app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php' ); - $failedBatch = CloudPublicIpPoolReplacementBatch::where( - 'batch_key', $failure['batch']['batch_key'] - )->first(); - $failureParent = $failureParent->fresh(); - $assert((string) $failedBatch->state === 'failed' - && (string) $failedBatch->source_initial_state === 'draining' - && $failedBatch->active_claim === null - && (string) $failureParent->outcome === 'failed' - && (int) $failureParent->effect_may_have_applied === 0 - && (int) $failureParent->safe_to_resubmit === 1 - && Capsule::table('cloud_public_ip_pools')->where('id', 33) - ->value('state') === 'draining' - && Capsule::table('cloud_public_ips') - ->where('id', (int) Capsule::table( - 'cloud_public_ip_pool_replacement_batch_items' - )->where('batch_id', (int) $failedBatch->id) - ->value('target_public_ip_id')) - ->value('state') === 'available' - && Capsule::table( - 'cloud_public_ip_pool_replacement_batch_items' - )->where('batch_id', (int) $failedBatch->id) - ->value('last_error_code') === 'agent_network_not_applied', - 'All-not-applied failure did not restore the exact initial source state.'); + $assert(is_string($jobService) && is_string($batchService) + && !str_contains( + $jobService, + 'enqueuePoolReplacementNetworkChangeLocked' + ) + && !str_contains( + $batchService, + 'enqueuePoolReplacementNetworkChangeLocked' + ), 'Legacy scalar pool-replacement child enqueuer remains reachable.'); - echo "Cloud public IP pool replacement batch service tests passed\n"; + echo "Cloud public IP pool replacement dual-stack behavior tests passed\n"; } diff --git a/web/tests/CloudPublicIpPoolReplacementHttpContractTest.php b/web/tests/CloudPublicIpPoolReplacementHttpContractTest.php index 4c94144e..3835d9b1 100644 --- a/web/tests/CloudPublicIpPoolReplacementHttpContractTest.php +++ b/web/tests/CloudPublicIpPoolReplacementHttpContractTest.php @@ -38,7 +38,10 @@ $assert(is_string($service) 'The browser boundary can select instances or target IPs directly.'); $assert(str_contains( $service, - 'enqueuePoolReplacementNetworkChangeLocked' + 'beginPoolReplacementBatch(' +) && str_contains( + $service, + 'new CloudPublicNetworkTransitionBatchPlan(' ) && str_contains($service, "'active_claim', 1") && str_contains($service, "'network_change_effect_unknown'"), 'Replacement service lost durable child jobs or unknown-effect fencing.'); diff --git a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php index ba74408e..918c77bc 100644 --- a/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php +++ b/web/tests/CloudPublicIpPoolReplacementSchemaContractTest.php @@ -10,6 +10,12 @@ $itemModel = file_get_contents( $root . '/app/Models/CloudPublicIpPoolReplacementBatchItem.php' ); $jobs = file_get_contents($root . '/app/Services/Cloud/CloudJobService.php'); +$workflow = file_get_contents( + $root . '/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php' +); +$batchPlan = file_get_contents( + $root . '/app/Services/Cloud/CloudPublicNetworkTransitionBatchPlan.php' +); $worker = file_get_contents($root . '/app/Services/Cloud/CloudWorkerService.php'); $assert = static function (bool $condition, string $message): void { @@ -88,13 +94,17 @@ $assert(is_string($service) && str_contains($service, 'self::assertAdminRead($actor)'), 'Replacement service lost revision fencing, overlap locking, or unknown-effect convergence.'); $assert(is_string($jobs) - && str_contains($jobs, - 'enqueuePoolReplacementNetworkChangeLocked') - && str_contains($jobs, - "'public_pool_replacement_batch_key' => \$batchKey") - && str_contains($jobs, "'confirm_restart' => true") - && str_contains($jobs, "\$targetIp->state = 'changing'"), - 'Replacement children no longer freeze ordinary network-change identity.'); + && !str_contains($jobs, 'enqueuePoolReplacementNetworkChangeLocked') + && is_string($workflow) + && str_contains($workflow, 'beginPoolReplacementBatch(') + && str_contains($workflow, 'CloudPublicNetworkTransitionBatchPlan $plan') + && str_contains($workflow, + '$transition->pool_replacement_batch_item_id =') + && str_contains($workflow, + '$job->parent_job_id = $command->parentJobId()') + && is_string($batchPlan) + && str_contains($batchPlan, 'final class CloudPublicNetworkTransitionBatchPlan'), + 'Replacement children do not use the canonical dual-stack transition seam.'); $assert(is_string($worker) && str_contains($worker, 'CloudPublicIpPoolReplacementBatchService::finishChildLocked') @@ -130,10 +140,11 @@ $startPositions = array_map( '$instances = CloudInstance::whereIn(', '$host = CloudHost::where(', '$agent = CloudHostAgent::where(', + '$resourcePools = CloudResourcePool::whereIn(', '$attachments = CloudHostNetworkAttachment::whereIn(', '$pools = CloudPublicIpPool::whereIn(', '$reservations = CloudResourceReservation::whereIn(', - '$ips = CloudPublicIp::whereIn(', + '$ips = CloudPublicIp::where(', ] ); $sortedStartPositions = $startPositions; @@ -142,6 +153,22 @@ $assert(!in_array(false, $startPositions, true) && $startPositions === $sortedStartPositions && count(array_unique($startPositions)) === count($startPositions), 'Replacement start reversed actor/job/instance/host/catalog locks.'); +$ipCatalogStart = strpos($lockedStart, '$allClaims = DB::table('); +$ipCatalogEnd = strpos($lockedStart, '$sourceIpIds = [];', + $ipCatalogStart === false ? 0 : $ipCatalogStart); +$ipCatalogLock = $ipCatalogStart === false || $ipCatalogEnd === false + ? '' : substr( + $lockedStart, + $ipCatalogStart, + $ipCatalogEnd - $ipCatalogStart + ); +$assert($ipCatalogLock !== '' + && !str_contains($ipCatalogLock, '$catalogIpIds') + && str_contains($ipCatalogLock, "->orWhereIn(\n" + . " 'public_ip_pool_id'") + && str_contains($ipCatalogLock, + "->orderBy('id')->lockForUpdate()->get()->keyBy('id')"), + 'Replacement start can discover IP catalog rows outside its current locking read.'); $finishResolved = $method( $service, 'public static function finishResolvedChildLocked(', @@ -167,6 +194,10 @@ sort($sortedPositions, SORT_NUMERIC); $assert(!in_array(false, $positions, true) && $positions === $sortedPositions && count(array_unique($positions)) === count($positions) + && str_contains($finishChild, 'assertItemTransitionTopology(') + && str_contains($service, "(string) \$transition->kind !== 'pool_replacement'") + && str_contains($service, + "(string) \$child->execution_mode !== 'agent'") && !str_contains($finishResolved, 'CloudHost::where(') && !str_contains($converge, 'lockForUpdate()'), 'Replacement convergence reversed parent/pool/batch locks.'); diff --git a/web/tests/CloudPublicNetworkLegacyWriterGateContractTest.php b/web/tests/CloudPublicNetworkLegacyWriterGateContractTest.php index eadbc455..90e48ec6 100644 --- a/web/tests/CloudPublicNetworkLegacyWriterGateContractTest.php +++ b/web/tests/CloudPublicNetworkLegacyWriterGateContractTest.php @@ -36,10 +36,16 @@ $placementSource = $read('app/Services/Cloud/CloudPlacementService.php'); $migrationSource = $read( 'app/Services/Cloud/CloudInstanceMigrationService.php' ); +$batchSource = $read( + 'app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php' +); $batch = $method( - $read('app/Services/Cloud/CloudPublicIpPoolReplacementBatchService.php'), + $batchSource, 'public static function start(' ); +$workflow = $read( + 'app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php' +); $backupRestore = $method( $read('app/Services/Cloud/CloudBackupService.php'), 'public static function restore(' @@ -68,10 +74,6 @@ $migrationCleanup = $method( $migrationSource, 'public static function cleanupRetained(' ); -$poolChild = $method( - $jobs, - 'public static function enqueuePoolReplacementNetworkChangeLocked(' -); $networkFinalizer = $method( $placementSource, 'public static function finishNetworkChange(' @@ -125,10 +127,11 @@ $assert($before($worker, "'phase' => 'dispatching'") && $before($worker, "'instance_migration_'", "'phase' => 'dispatching'"), 'Worker defense runs after an old operation may be dispatched.'); -$assert($before($batch, - 'CloudPublicNetworkTransitionGate::assertWriterReady()', - 'new CloudPublicIpPoolReplacementBatch()'), - 'Pool-replacement gate runs after a batch may be created.'); +$assert(!str_contains($jobs, 'enqueuePoolReplacementNetworkChangeLocked') + && !str_contains($batch, 'CloudPublicNetworkTransitionGate::assertWriterReady()') + && str_contains($batchSource, 'beginPoolReplacementBatch(') + && str_contains($workflow, 'CloudPublicNetworkTransitionBatchPlan $plan'), + 'Pool replacement retained the retired scalar writer or legacy gate.'); $assert($before($backupRestore, 'CloudPublicNetworkTransitionGate::assertPortableRestoreReady()', 'headObject(') @@ -157,13 +160,10 @@ $assert($before($migrationEnsureCleanup, 'CloudPublicNetworkTransitionGate::assertMigrationReady()', 'getConnection()->transaction'), 'A migration recovery entry can create retry intent before its gate.'); -$assert($before($poolChild, - 'CloudPublicNetworkTransitionGate::assertWriterReady()', - 'new CloudJob()') - && $before($networkFinalizer, +$assert($before($networkFinalizer, 'CloudPublicNetworkTransitionGate::assertWriterReady()', 'lockForUpdate()'), - 'An old network-change child/finalizer can mutate before its gate.'); + 'An old network-change finalizer can mutate before its gate.'); $assert(str_contains($deletedInstance, 'CloudPublicNetworkTransitionGate::assertPortableRestoreReady()') && !str_contains($placementSource, 'releaseLockedReservation(') diff --git a/web/tests/CloudPublicNetworkTransitionWorkflowContractTest.php b/web/tests/CloudPublicNetworkTransitionWorkflowContractTest.php index 11f5e529..7792ba16 100644 --- a/web/tests/CloudPublicNetworkTransitionWorkflowContractTest.php +++ b/web/tests/CloudPublicNetworkTransitionWorkflowContractTest.php @@ -58,6 +58,7 @@ $public = array_values(array_map( )); $assert($workflow->isFinal() && $public === [ 'begin', + 'beginPoolReplacementBatch', 'markDispatched', 'markJobUncertain', 'recordEvidence', @@ -76,12 +77,17 @@ foreach (['userPaidAutomatic', 'adminExact', 'poolReplacement'] as $factory) { $begin = $method( $source, 'public static function begin(', + 'public static function beginPoolReplacementBatch(' +); +$batchBegin = $method( + $source, + 'public static function beginPoolReplacementBatch(', 'private static function reserveTransitionLocked(' ); $reserve = $method( $source, 'private static function reserveTransitionLocked(', - 'public static function markDispatched(' + 'private static function freezeTransitionLocked(' ); $ordered = static function (string $body, array $needles) use ($assert): void { $cursor = -1; @@ -97,8 +103,16 @@ $ordered($begin, [ 'self::reserveTransitionLocked($command, $actorUser)', ]); $assert(substr_count($begin, 'transaction(') === 1 - && !str_contains($reserve, 'transaction('), + && !str_contains($reserve, 'transaction(') + && !str_contains($batchBegin, 'transaction(') + && !str_contains($batchBegin, 'lockForUpdate(') + && !preg_match('/::(?:find|where|whereIn)\(/', $batchBegin), 'Reservation seam introduced a nested transaction.'); +$assert(str_contains($batchBegin, + 'CloudPublicNetworkTransitionBatchPlan $plan') + && str_contains($batchBegin, '$item->state = \'queued\'') + && str_contains($batchBegin, '$item->child_job_id = $dispatch->jobId()'), + 'Batch transition seam no longer consumes only a caller-locked plan.'); $ordered($reserve, [ '$jobHint = CloudJob::where(', '$existingParent = CloudJob::where(',