diff --git a/web/app/Services/Cloud/CloudVpcPortService.php b/web/app/Services/Cloud/CloudVpcPortService.php index 8b034f59..5fad956a 100644 --- a/web/app/Services/Cloud/CloudVpcPortService.php +++ b/web/app/Services/Cloud/CloudVpcPortService.php @@ -42,6 +42,8 @@ final class CloudVpcPortService $blocking = CloudJobService::blockingQueryForInstance( (int) $instance->id )->orderBy('id')->first(); + $activeVpcJob = $port === null ? null + : self::activePortJob((int) $instance->id, (int) $port->id); return [ 'port' => $port === null ? null : self::portDto($port), 'vpcs' => $vpcs->map(static fn (CloudVpc $vpc): array => [ @@ -59,6 +61,11 @@ final class CloudVpcPortService && (string) $port->state === 'attached' && (string) $instance->status === 'active' && $instance->serviceAvailable(), + 'active_task' => $activeVpcJob === null ? null : [ + 'id' => (string) $activeVpcJob->id, + 'status_url' => '/api/v1/tasks/' . (int) $activeVpcJob->id, + 'kind' => (string) $activeVpcJob->kind, + ], 'requires_cold_restart' => true, ]; } @@ -84,13 +91,10 @@ final class CloudVpcPortService $locked = CloudInstance::query()->where('id', (int) $instance->id) ->lockForUpdate()->first(); self::assertOwnerAndInstance($locked, $actor, true); - self::assertNoInstanceJobLocked($locked); $vpc = CloudVpc::query()->where('vpc_key', $vpcKey) ->lockForUpdate()->first(); if ($vpc === null || (int) $vpc->user_id !== (int) $locked->user_id - || $vpc->active_claim === null - || !in_array((string) $vpc->state, - ['active', 'degraded'], true)) { + || $vpc->active_claim === null) { throw new CloudError('VPC 不存在或当前不可用', 404); } if ((int) $vpc->region_id !== self::instanceRegionId($locked)) { @@ -109,20 +113,38 @@ final class CloudVpcPortService return ['port' => self::portDto($port), 'job' => null, 'unchanged' => true]; } - if ((string) $port->state !== 'preparing') { + $expectedPortKey = self::portKey( + (int) $locked->user_id, (int) $locked->id, + $vpcKey, $requestKey + ); + if ((string) $port->state !== 'preparing' + || !hash_equals((string) $port->port_key, + $expectedPortKey)) { throw new CloudError('VPC 端口正在执行其他操作', 409); } + $active = self::activePortJobLocked($locked, $port); + if ($active !== null) { + return ['port' => self::portDto($port), 'job' => $active, + 'unchanged' => false]; + } + self::assertNoInstanceJobLocked($locked); $job = self::retryPreparingLocked($locked, $vpc, $port); return ['port' => self::portDto($port->fresh()), 'job' => $job, 'unchanged' => false]; } + if (!in_array((string) $vpc->state, + ['active', 'degraded'], true)) { + throw new CloudError('VPC 当前正在执行其他操作', 409); + } + self::assertNoInstanceJobLocked($locked); self::assertVpcSettledLocked($vpc); $now = time(); $port = new CloudVpcPort(); - $port->port_key = substr(hash('sha256', "vpc-port\0" - . (int) $locked->user_id . "\0" . (int) $locked->id - . "\0" . $vpcKey . "\0" . $requestKey), 0, 32); + $port->port_key = self::portKey( + (int) $locked->user_id, (int) $locked->id, + $vpcKey, $requestKey + ); $existing = CloudVpcPort::query() ->where('port_key', (string) $port->port_key) ->lockForUpdate()->first(); @@ -177,7 +199,6 @@ final class CloudVpcPortService $locked = CloudInstance::query()->where('id', (int) $instance->id) ->lockForUpdate()->first(); self::assertOwnerAndInstance($locked, $actor, true); - self::assertNoInstanceJobLocked($locked); $port = CloudVpcPort::query() ->where('instance_id', (int) $locked->id) ->where('active_claim', 1)->lockForUpdate()->first(); @@ -189,7 +210,21 @@ final class CloudVpcPortService if ($vpc === null || (int) $vpc->user_id !== (int) $locked->user_id) { throw new CloudError('VPC 端口账本与所属人不一致', 409); } + if ((string) $port->state === 'detaching') { + $active = self::activePortJobLocked($locked, $port); + if ($active === null) { + throw new CloudError('VPC 断开任务账本不完整', 409); + } + return ['port' => self::portDto($port), 'job' => $active, + 'unchanged' => false]; + } if ((string) $port->state === 'release_pending') { + $active = self::activePortJobLocked($locked, $port); + if ($active !== null) { + return ['port' => self::portDto($port), 'job' => $active, + 'unchanged' => false]; + } + self::assertNoInstanceJobLocked($locked); $job = self::retryReleaseLocked($locked, $vpc, $port); return ['port' => $port->active_claim === null ? null : self::portDto($port->fresh()), @@ -198,6 +233,7 @@ final class CloudVpcPortService if ((string) $port->state !== 'attached') { throw new CloudError('VPC 端口正在执行其他操作', 409); } + self::assertNoInstanceJobLocked($locked); self::assertVpcSettledLocked($vpc); $hostState = CloudVpcHostState::query() ->where('vpc_id', (int) $vpc->id) @@ -961,6 +997,24 @@ final class CloudVpcPortService } } + private static function activePortJob(int $instanceId, + int $portId): ?CloudJob + { + return CloudJobService::blockingQueryForInstance($instanceId) + ->where('vpc_port_id', $portId) + ->whereIn('kind', self::JOB_KINDS) + ->orderByDesc('id')->first(); + } + + private static function activePortJobLocked(CloudInstance $instance, + CloudVpcPort $port): ?CloudJob + { + return CloudJobService::blockingQueryForInstance((int) $instance->id) + ->where('vpc_port_id', (int) $port->id) + ->whereIn('kind', self::JOB_KINDS) + ->orderByDesc('id')->lockForUpdate()->first(); + } + private static function assertOwnerAndInstance(?CloudInstance $instance, array $actor, bool $mutation): void { @@ -1078,6 +1132,13 @@ final class CloudVpcPortService return $key; } + private static function portKey(int $userId, int $instanceId, + string $vpcKey, string $requestKey): string + { + return substr(hash('sha256', "vpc-port\0" . $userId . "\0" + . $instanceId . "\0" . $vpcKey . "\0" . $requestKey), 0, 32); + } + private static function hostReady(?CloudVpcHostState $state): bool { return $state !== null && (string) $state->sync_state === 'ready' diff --git a/web/tests/CloudVpcPortWorkflowTest.php b/web/tests/CloudVpcPortWorkflowTest.php index 5c53c101..58d2baa1 100644 --- a/web/tests/CloudVpcPortWorkflowTest.php +++ b/web/tests/CloudVpcPortWorkflowTest.php @@ -258,11 +258,25 @@ namespace { $assert($queued['job'] !== null && CloudJob::where('kind', 'vpc_host_apply')->count() === 2, 'Attach did not queue a complete two-host projection.'); + $replayedAttach = CloudVpcPortService::attach($target, $actor, [ + 'vpc_key' => (string) $vpc->vpc_key, 'confirm_restart' => true, + ]); + $activeHostJobIds = CloudJob::where('kind', 'vpc_host_apply') + ->pluck('id')->map(static fn ($id): int => (int) $id)->all(); + $assert($replayedAttach['job'] !== null + && in_array((int) $replayedAttach['job']->id, $activeHostJobIds, true) + && CloudJob::where('kind', 'vpc_host_apply')->count() === 2, + 'An accepted attach retry did not return its existing workflow.'); $port = CloudVpcPort::where('instance_id', 1) ->where('active_claim', 1)->first(); $assert($port !== null && (string) $port->state === 'preparing' && (string) $port->private_ip === '10.60.0.2', 'Attach did not reserve the first usable free address.'); + $listing = CloudVpcPortService::listing($target, ['user_id' => 7]); + $assert(($listing['active_task']['id'] ?? null) + === (string) $replayedAttach['job']->id + && ($listing['can_attach'] ?? true) === false, + 'The read model did not expose the tenant VPC task after reconnect.'); foreach (CloudJob::where('kind', 'vpc_host_apply')->orderBy('id')->get() as $hostJob) { @@ -314,6 +328,12 @@ namespace { 'confirm_restart' => true, ]); $detachJob = $detaching['job']; + $replayedDetach = CloudVpcPortService::detach($target, $detachActor, [ + 'confirm_restart' => true, + ]); + $assert((int) ($replayedDetach['job']->id ?? 0) === (int) $detachJob->id + && CloudJob::where('kind', 'vpc_port_detach')->count() === 1, + 'An accepted detach retry did not return its existing workflow.'); $detachPayload = CloudSecrets::decrypt( (string) $detachJob->sealed_payload, 'job-payload' );