refactor(vpc): 统一地区就绪状态投影

go
codex 3 weeks ago
parent b3b1f3fdd5
commit 0923e82ef3

@ -0,0 +1,99 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudHostVpcTransport;
use App\Models\CloudRegion;
use App\Models\CloudVpc;
use App\Models\CloudVpcCidrPool;
/** One projection for regions that can accept a newly allocated VPC. */
final class CloudVpcRegionReadiness
{
/** @return list<int> */
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<array{id:int,code:string,name:string,country_code:string,
* available:int,prefix_lengths:list<int>}>
*/
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;
}
}

@ -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<int> */
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
{

Loading…
Cancel
Save