From 0923e82ef34316eb1602b1d8c5f8aebac714f139 Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 31 Oct 2026 06:35:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(vpc):=20=E7=BB=9F=E4=B8=80=E5=9C=B0?= =?UTF-8?q?=E5=8C=BA=E5=B0=B1=E7=BB=AA=E7=8A=B6=E6=80=81=E6=8A=95=E5=BD=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cloud/CloudVpcRegionReadiness.php | 99 +++++++++++++++++++ web/app/Services/Cloud/CloudVpcService.php | 88 ++--------------- 2 files changed, 108 insertions(+), 79 deletions(-) create mode 100644 web/app/Services/Cloud/CloudVpcRegionReadiness.php diff --git a/web/app/Services/Cloud/CloudVpcRegionReadiness.php b/web/app/Services/Cloud/CloudVpcRegionReadiness.php new file mode 100644 index 00000000..e93bf470 --- /dev/null +++ b/web/app/Services/Cloud/CloudVpcRegionReadiness.php @@ -0,0 +1,99 @@ + */ + public static function eligibleRegionIds(): array + { + return CloudHostVpcTransport::query() + ->join('cloud_hosts as vpc_host', 'vpc_host.id', '=', + 'cloud_host_vpc_transports.host_id') + ->join('cloud_host_resource_pool_memberships as membership', + 'membership.host_id', '=', 'vpc_host.id') + ->join('cloud_resource_pools as resource_pool', + 'resource_pool.id', '=', 'membership.resource_pool_id') + ->join('cloud_network_lines as network_line', + 'network_line.id', '=', 'resource_pool.line_id') + ->join('cloud_regions as vpc_region', + 'vpc_region.id', '=', 'network_line.region_id') + ->where('vpc_host.enabled', 1) + ->where('membership.scheduler_state', 'active') + ->where('resource_pool.status', 'active') + ->where('network_line.status', 'active') + ->where('vpc_region.status', 'active') + ->where('cloud_host_vpc_transports.sync_state', 'ready') + ->whereColumn('cloud_host_vpc_transports.observed_revision', + 'cloud_host_vpc_transports.desired_revision') + ->distinct()->orderBy('vpc_region.id') + ->pluck('vpc_region.id')->map( + static fn ($id): int => (int) $id + )->all(); + } + + /** + * Return only host-ready regions with at least one allocatable subnet. + * + * @return list}> + */ + public static function catalog(): array + { + $regionIds = self::eligibleRegionIds(); + if ($regionIds === []) { + return []; + } + $regions = CloudRegion::query()->whereIn('id', $regionIds) + ->orderBy('sort')->orderBy('id')->get([ + 'id', 'code', 'name', 'country_code', 'status', + ])->keyBy('id'); + if ($regions->isEmpty()) { + return []; + } + $usage = CloudVpc::query()->where('active_claim', 1) + ->selectRaw('cidr_pool_id, COUNT(*) AS row_count') + ->groupBy('cidr_pool_id')->pluck('row_count', 'cidr_pool_id'); + $pools = CloudVpcCidrPool::query() + ->whereIn('region_id', $regions->keys()->all()) + ->where('state', 'active')->orderBy('id')->get(); + $summary = []; + foreach ($pools as $pool) { + $regionId = (int) $pool->region_id; + $capacity = 1 << ((int) $pool->allocation_prefix + - (int) $pool->prefix_length); + $used = (int) ($usage[(int) $pool->id] ?? 0); + $summary[$regionId]['available'] = + ($summary[$regionId]['available'] ?? 0) + + max(0, $capacity - $used); + $summary[$regionId]['prefixes'][ + (int) $pool->allocation_prefix + ] = true; + } + $result = []; + foreach ($regions as $region) { + $regionId = (int) $region->id; + $available = (int) ($summary[$regionId]['available'] ?? 0); + if ($available < 1) { + continue; + } + $prefixes = array_keys($summary[$regionId]['prefixes'] ?? []); + sort($prefixes, SORT_NUMERIC); + $result[] = [ + 'id' => $regionId, + 'code' => (string) $region->code, + 'name' => (string) $region->name, + 'country_code' => (string) $region->country_code, + 'available' => $available, + 'prefix_lengths' => $prefixes, + ]; + } + return $result; + } +} diff --git a/web/app/Services/Cloud/CloudVpcService.php b/web/app/Services/Cloud/CloudVpcService.php index 81d07d14..f2d5c37f 100644 --- a/web/app/Services/Cloud/CloudVpcService.php +++ b/web/app/Services/Cloud/CloudVpcService.php @@ -2,7 +2,6 @@ namespace App\Services\Cloud; -use App\Models\CloudHostVpcTransport; use App\Models\CloudRegion; use App\Models\CloudVpc; use App\Models\CloudVpcCidrPool; @@ -20,15 +19,16 @@ final class CloudVpcService $portCounts = CloudVpcPort::query()->where('active_claim', 1) ->selectRaw('vpc_id, COUNT(*) AS row_count')->groupBy('vpc_id') ->pluck('row_count', 'vpc_id'); - $regions = CloudRegion::query()->whereIn('id', self::eligibleRegionIds()) - ->orderBy('sort')->orderBy('id')->get([ - 'id', 'code', 'name', 'country_code', 'status', - ])->keyBy('id'); + $regions = CloudVpcRegionReadiness::catalog(); + $regionNames = []; + foreach ($regions as $region) { + $regionNames[(int) $region['id']] = (string) $region['name']; + } $items = CloudVpc::query()->where('user_id', $userId) ->where('active_claim', 1)->orderByDesc('id')->get() ->map(static fn ($vpc): array => self::dto( $vpc, - (string) ($regions->get((int) $vpc->region_id)?->name + (string) ($regionNames[(int) $vpc->region_id] ?? '地区已不可用'), (int) ($portCounts[(int) $vpc->id] ?? 0) ))->all(); @@ -37,7 +37,7 @@ final class CloudVpcService 'instances' => CloudVpcPortService::managementListing($actor), 'membership_batches' => CloudVpcMembershipBatchService::activeForOwner($userId), - 'regions' => self::regionCatalog($regions), + 'regions' => $regions, 'maximum' => self::MAX_VPCS_PER_USER, 'used' => count($items), ]; @@ -69,7 +69,8 @@ final class CloudVpcService if ($region === null || (string) $region->status !== 'active') { throw new CloudError('所选 VPC 地区当前不可用', 409); } - if (!in_array($regionId, self::eligibleRegionIds(), true)) { + if (!in_array($regionId, + CloudVpcRegionReadiness::eligibleRegionIds(), true)) { throw new CloudError( '所选地区暂无已验证就绪的 VPC 计算节点', 409 ); @@ -191,77 +192,6 @@ final class CloudVpcService )]; } - /** @return list */ - private static function eligibleRegionIds(): array - { - return CloudHostVpcTransport::query() - ->join('cloud_hosts as vpc_host', 'vpc_host.id', '=', - 'cloud_host_vpc_transports.host_id') - ->join('cloud_host_resource_pool_memberships as membership', - 'membership.host_id', '=', 'vpc_host.id') - ->join('cloud_resource_pools as resource_pool', - 'resource_pool.id', '=', 'membership.resource_pool_id') - ->join('cloud_network_lines as network_line', - 'network_line.id', '=', 'resource_pool.line_id') - ->join('cloud_regions as vpc_region', - 'vpc_region.id', '=', 'network_line.region_id') - ->where('vpc_host.enabled', 1) - ->where('membership.scheduler_state', 'active') - ->where('resource_pool.status', 'active') - ->where('network_line.status', 'active') - ->where('vpc_region.status', 'active') - ->where('cloud_host_vpc_transports.sync_state', 'ready') - ->whereColumn('cloud_host_vpc_transports.observed_revision', - 'cloud_host_vpc_transports.desired_revision') - ->distinct()->orderBy('vpc_region.id') - ->pluck('vpc_region.id')->map( - static fn ($id): int => (int) $id - )->all(); - } - - private static function regionCatalog($regions): array - { - if ($regions->isEmpty()) { - return []; - } - $usage = CloudVpc::query()->where('active_claim', 1) - ->selectRaw('cidr_pool_id, COUNT(*) AS row_count') - ->groupBy('cidr_pool_id')->pluck('row_count', 'cidr_pool_id'); - $pools = CloudVpcCidrPool::query() - ->whereIn('region_id', $regions->keys()->all()) - ->where('state', 'active')->orderBy('id')->get(); - $summary = []; - foreach ($pools as $pool) { - $regionId = (int) $pool->region_id; - $capacity = 1 << ((int) $pool->allocation_prefix - - (int) $pool->prefix_length); - $used = (int) ($usage[(int) $pool->id] ?? 0); - $summary[$regionId]['available'] = - ($summary[$regionId]['available'] ?? 0) - + max(0, $capacity - $used); - $summary[$regionId]['prefixes'][(int) $pool->allocation_prefix] = true; - } - $result = []; - foreach ($regions as $region) { - $regionId = (int) $region->id; - $available = (int) ($summary[$regionId]['available'] ?? 0); - if ($available < 1) { - continue; - } - $prefixes = array_keys($summary[$regionId]['prefixes'] ?? []); - sort($prefixes, SORT_NUMERIC); - $result[] = [ - 'id' => $regionId, - 'code' => (string) $region->code, - 'name' => (string) $region->name, - 'country_code' => (string) $region->country_code, - 'available' => $available, - 'prefix_lengths' => $prefixes, - ]; - } - return $result; - } - private static function firstAvailableCidr(CloudVpcCidrPool $pool, array $rows): ?string {