feat(admin): project compute host region summaries

go
codex 3 weeks ago
parent 9c859071db
commit 2fc38f3c5d

@ -56,9 +56,9 @@ final class CloudAdminController
$write = $request->getMethod() !== 'GET';
$this->actor($request, $write);
if (!$write && !isset($args['id'])) {
return $response->withJson([
'hosts' => CloudAdminHostProjectionService::all(),
])->withHeader('Cache-Control', 'no-store');
return $response->withJson(
CloudAdminHostProjectionService::listing()
)->withHeader('Cache-Control', 'no-store');
}
if (!$write) {
return $response->withJson(CloudAdminHostProjectionService::one(

@ -3,31 +3,154 @@
namespace App\Services\Cloud;
use App\Models\CloudHost;
use App\Models\CloudHostAgent;
use App\Models\CloudHostCapacityPolicy;
use App\Models\CloudHostResourcePoolMembership;
use App\Models\CloudHostStorageConfiguration;
use App\Models\CloudNetworkLine;
use App\Models\CloudPublicIp;
use App\Models\CloudRegion;
use App\Models\CloudResourcePool;
/** Administrator host DTOs backed by the authoritative pool membership. */
/**
* One read model for the administrator compute-node page.
*
* Region placement, readiness, capacity and public-IP summaries are resolved
* here so browser callers do not need to reconstruct scheduling authority or
* issue one request per host.
*/
final class CloudAdminHostProjectionService
{
private const SCHEDULER_STATES = ['active', 'draining', 'disabled'];
public static function all(): array
/** @return array{summary:array,regions:list<array>,hosts:list<array>} */
public static function listing(): array
{
$hosts = CloudHost::orderBy('id')->get();
if ($hosts->isEmpty()) {
return [];
return [
'summary' => self::emptySummary(),
'regions' => [],
'hosts' => [],
];
}
$hostIds = $hosts->pluck('id')->map(
static fn ($id): int => (int) $id
)->all();
$memberships = CloudHostResourcePoolMembership::whereIn(
'host_id', $hosts->pluck('id')->all()
'host_id', $hostIds
)->orderBy('host_id')->orderBy('resource_pool_id')->orderBy('id')->get();
$byHost = [];
$membershipsByHost = [];
foreach ($memberships as $membership) {
$byHost[(int) $membership->host_id][] = $membership;
$membershipsByHost[(int) $membership->host_id][] = $membership;
}
$selectedByHost = [];
$poolIds = [];
foreach ($hosts as $host) {
$selected = CloudHostPoolMembershipService::selected(
$membershipsByHost[(int) $host->id] ?? []
);
$selectedByHost[(int) $host->id] = $selected;
if ($selected !== null) {
$poolIds[] = (int) $selected->resource_pool_id;
}
}
$pools = self::keyById($poolIds === [] ? []
: CloudResourcePool::whereIn('id', array_values(array_unique(
$poolIds
)))->orderBy('sort')->orderBy('id')->get());
$lineIds = [];
foreach ($pools as $pool) {
$lineIds[] = (int) $pool->line_id;
}
$lines = self::keyById($lineIds === [] ? []
: CloudNetworkLine::whereIn('id', array_values(array_unique(
$lineIds
)))->orderBy('sort')->orderBy('id')->get());
$regionIds = [];
foreach ($lines as $line) {
$regionIds[] = (int) $line->region_id;
}
$regions = self::keyById($regionIds === [] ? []
: CloudRegion::whereIn('id', array_values(array_unique(
$regionIds
)))->orderBy('sort')->orderBy('id')->get());
$policies = self::keyByHost(CloudHostCapacityPolicy::whereIn(
'host_id', $hostIds
)->get());
$agents = self::keyByHost(CloudHostAgent::whereIn(
'host_id', $hostIds
)->get());
$storage = self::keyByHost(CloudHostStorageConfiguration::whereIn(
'host_id', $hostIds
)->get());
$usage = CloudPanelUsageService::forHosts($hostIds);
$ipStats = self::ipStats($hostIds);
return $hosts->map(static fn (CloudHost $host): array => self::dto(
$host, $byHost[(int) $host->id] ?? []
))->values()->all();
$flat = [];
$groups = [];
foreach ($hosts as $host) {
$hostId = (int) $host->id;
$membership = $selectedByHost[$hostId] ?? null;
$pool = $membership === null ? null
: ($pools[(int) $membership->resource_pool_id] ?? null);
$line = $pool === null ? null
: ($lines[(int) $pool->line_id] ?? null);
$region = $line === null ? null
: ($regions[(int) $line->region_id] ?? null);
$lineIp = $line === null ? self::emptyIpStats()
: ($ipStats[$hostId][(int) $line->id]
?? self::emptyIpStats());
$row = self::hostDto(
$host,
$membership,
$pool,
$line,
$region,
$policies[$hostId] ?? null,
$agents[$hostId] ?? null,
$storage[$hostId] ?? null,
$usage[$hostId] ?? CloudPanelUsageService::emptyUsage(),
$lineIp
);
$flat[] = $row;
$key = $region === null ? 'unclassified' : 'region:' . $region->id;
if (!isset($groups[$key])) {
$groups[$key] = self::regionDto($region, $host);
}
$groups[$key]['hosts'][] = $row;
self::addHostToGroup($groups[$key], $row);
}
$groupRows = array_values($groups);
usort($groupRows, static function (array $left, array $right): int {
if ($left['id'] === null) {
return $right['id'] === null ? 0 : 1;
}
if ($right['id'] === null) {
return -1;
}
return [$left['sort'], $left['id']] <=> [$right['sort'], $right['id']];
});
foreach ($groupRows as &$group) {
usort($group['hosts'], static fn (array $left, array $right): int =>
[$left['name'], $left['id']] <=> [$right['name'], $right['id']]
);
}
unset($group);
return [
'summary' => self::summary($flat, $groupRows),
'regions' => $groupRows,
'hosts' => $flat,
];
}
public static function all(): array
{
return self::listing()['hosts'];
}
public static function one(CloudHost $host): array
@ -35,10 +158,11 @@ final class CloudAdminHostProjectionService
$memberships = CloudHostResourcePoolMembership::where(
'host_id', (int) $host->id
)->orderBy('resource_pool_id')->orderBy('id')->get();
return self::dto($host, $memberships);
return self::basicDto($host, $memberships);
}
private static function dto(CloudHost $host, iterable $memberships): array
private static function basicDto(CloudHost $host,
iterable $memberships): array
{
$selected = CloudHostPoolMembershipService::selected($memberships);
$state = (string) ($selected?->scheduler_state ?? 'disabled');
@ -49,4 +173,400 @@ final class CloudAdminHostProjectionService
? $state : 'disabled';
return $dto;
}
private static function hostDto(CloudHost $host, $membership, $pool,
$line, $region, $policy, ?CloudHostAgent $agent,
?CloudHostStorageConfiguration $storage, array $usage,
array $ip): array
{
$base = self::basicDto($host, $membership === null
? [] : [$membership]);
$scheduler = (string) $base['scheduler_state'];
$selectorReady = CloudNetworkSelector::isValid(
(string) $host->default_network
);
$storageReady = $storage !== null && $storage->isApplied();
$agentReady = $agent !== null && $agent->isReady();
$capacity = self::capacity($policy, $usage, $ip);
$capacityConfigured = $policy !== null
&& $capacity['cpu']['configured'] > 0
&& $capacity['memory']['configured'] > 0
&& $capacity['disk']['configured'] > 0
&& $capacity['cpu']['safety'] < $capacity['cpu']['configured']
&& $capacity['memory']['safety'] < $capacity['memory']['configured']
&& $capacity['disk']['safety'] < $capacity['disk']['configured'];
$maxInstances = (int) $capacity['instances']['max'];
$instanceCount = (int) $capacity['instances']['allocated']
+ (int) $capacity['instances']['reserved'];
$poolPathReady = $pool !== null && (string) $pool->status === 'active'
&& $line !== null && (string) $line->status === 'active'
&& $region !== null && (string) $region->status === 'active';
$placementReady = (bool) $host->enabled
&& $scheduler === 'active' && $poolPathReady && $selectorReady
&& $agentReady && $storageReady && $capacityConfigured
&& (int) $capacity['unknown_instances'] === 0
&& ($maxInstances <= 0 || $instanceCount < $maxInstances)
&& $capacity['cpu']['free'] > 0
&& $capacity['memory']['free'] > 0
&& $capacity['disk']['free'] > 0
&& $capacity['ip']['available'] > 0;
$attention = self::attention(
$host,
$membership,
$pool,
$line,
$region,
$scheduler,
$selectorReady,
$agentReady,
$storageReady,
$capacityConfigured,
$capacity
);
$country = CloudCountryCatalog::option((string) ($region?->country_code
?? $host->country_code));
return $base + [
'location' => [
'physical_label' => (string) $host->region,
'country_code' => $country['code'],
'country_name' => $country['name'],
'country_flag' => $country['flag'],
],
'placement' => [
'region_id' => $region === null ? null : (int) $region->id,
'region_code' => $region === null ? '' : (string) $region->code,
'region_name' => $region === null ? '未分类节点'
: (string) $region->name,
'region_status' => $region === null ? 'missing'
: (string) $region->status,
'line_id' => $line === null ? null : (int) $line->id,
'line_name' => $line === null ? '' : (string) $line->name,
'line_status' => $line === null ? 'missing'
: (string) $line->status,
'pool_id' => $pool === null ? null : (int) $pool->id,
'pool_name' => $pool === null ? '' : (string) $pool->name,
'pool_status' => $pool === null ? 'missing'
: (string) $pool->status,
],
'agent' => [
'state' => self::agentState($agent),
'ready' => $agentReady,
'enabled' => $agent !== null && (bool) $agent->enabled,
'last_test_status' => $agent === null ? 'never'
: (string) $agent->last_test_status,
'last_error_code' => $agent === null ? ''
: (string) $agent->last_error_code,
'version' => $agent === null ? ''
: (string) $agent->last_agent_version,
'last_tested_at' => $agent === null ? 0
: (int) $agent->last_tested_at,
],
'readiness' => [
'network_selector_ready' => $selectorReady,
'storage_ready' => $storageReady,
'capacity_configured' => $capacityConfigured,
'placement_ready' => $placementReady,
],
'capacity' => $capacity,
'attention' => $attention,
'needs_attention' => $attention !== [],
];
}
private static function agentState(?CloudHostAgent $agent): string
{
if ($agent === null) {
return 'unconfigured';
}
if (!(bool) $agent->enabled) {
return 'disabled';
}
if ($agent->isReady()) {
return 'ready';
}
if ((string) $agent->last_test_status === 'failed') {
return 'failed';
}
return 'pending';
}
private static function capacity($policy, array $usage, array $ip): array
{
$result = [];
foreach ([
'cpu' => ['configured_cpu_cores', 'safety_cpu_cores'],
'memory' => ['configured_memory_mib', 'safety_memory_mib'],
'disk' => ['configured_disk_gib', 'safety_disk_gib'],
] as $key => [$configuredField, $safetyField]) {
$configured = max(0, (int) ($policy?->{$configuredField} ?? 0));
$safety = max(0, (int) ($policy?->{$safetyField} ?? 0));
$allocated = max(0, (int) ($usage[$key]['allocated'] ?? 0));
$reserved = max(0, (int) ($usage[$key]['reserved'] ?? 0));
$allocatable = max(0, $configured - $safety);
$free = max(0, $allocatable - $allocated - $reserved);
$result[$key] = compact(
'configured', 'safety', 'allocatable', 'allocated',
'reserved', 'free'
);
}
$result['instances'] = [
'allocated' => max(0,
(int) ($usage['instances']['allocated'] ?? 0)),
'reserved' => max(0,
(int) ($usage['instances']['reserved'] ?? 0)),
'max' => max(0, (int) ($policy?->max_instances ?? 0)),
];
$result['ip'] = $ip;
$result['unknown_instances'] = max(0, (int) ($usage['unknown'] ?? 0));
return $result;
}
private static function attention(CloudHost $host, $membership, $pool,
$line, $region, string $scheduler, bool $selectorReady,
bool $agentReady, bool $storageReady, bool $capacityConfigured,
array $capacity): array
{
$attention = [];
if ($membership === null || $pool === null || $line === null
|| $region === null) {
$attention[] = 'placement_unclassified';
}
if (!$selectorReady) {
$attention[] = 'network_selector_invalid';
}
if (!$agentReady) {
$attention[] = 'agent_not_ready';
}
if (!$storageReady) {
$attention[] = 'storage_not_ready';
}
if ((int) $capacity['unknown_instances'] > 0) {
$attention[] = 'instance_capacity_unknown';
}
if ($scheduler === 'active') {
if (!(bool) $host->enabled) {
$attention[] = 'host_service_disabled';
}
if ($pool !== null && (string) $pool->status !== 'active') {
$attention[] = 'pool_inactive';
}
if ($line !== null && (string) $line->status !== 'active') {
$attention[] = 'line_inactive';
}
if ($region !== null && (string) $region->status !== 'active') {
$attention[] = 'region_inactive';
}
if (!$capacityConfigured) {
$attention[] = 'capacity_not_configured';
}
if ((int) $capacity['ip']['available'] <= 0) {
$attention[] = 'public_ip_unavailable';
}
}
return array_values(array_unique($attention));
}
private static function ipStats(array $hostIds): array
{
$result = [];
if ($hostIds === []) {
return $result;
}
$rows = CloudPublicIp::query()
->join('cloud_public_ip_pools as public_pool',
'public_pool.id', '=', 'cloud_public_ips.public_ip_pool_id')
->join('cloud_host_network_attachments as network_attachment',
'network_attachment.id', '=', 'public_pool.network_attachment_id')
->join('cloud_hosts as selected_host',
'selected_host.id', '=', 'cloud_public_ips.host_id')
->whereIn('cloud_public_ips.host_id', $hostIds)
->whereColumn('public_pool.host_id', 'cloud_public_ips.host_id')
->whereColumn('network_attachment.host_id',
'cloud_public_ips.host_id')
->whereColumn('network_attachment.network_selector',
'selected_host.default_network')
->groupBy('cloud_public_ips.host_id', 'network_attachment.line_id')
->selectRaw(
'cloud_public_ips.host_id, network_attachment.line_id, '
. 'COUNT(*) AS total_count, '
. "SUM(CASE WHEN cloud_public_ips.state = 'assigned' "
. 'THEN 1 ELSE 0 END) AS allocated_count, '
. "SUM(CASE WHEN cloud_public_ips.state IN "
. "('reserved','changing','uncertain') THEN 1 ELSE 0 END) "
. 'AS reserved_count, '
. "SUM(CASE WHEN cloud_public_ips.state = 'disabled' "
. 'THEN 1 ELSE 0 END) AS disabled_count, '
. "SUM(CASE WHEN cloud_public_ips.state = 'available' "
. 'AND cloud_public_ips.reservation_id IS NULL '
. 'AND cloud_public_ips.instance_id IS NULL '
. 'AND cloud_public_ips.operation_id IS NULL '
. "AND network_attachment.state = 'active' "
. "AND network_attachment.sync_state = 'applied' "
. 'AND network_attachment.observed_revision >= '
. 'network_attachment.desired_revision '
. "AND public_pool.state = 'active' "
. "AND public_pool.sync_state = 'applied' "
. 'AND public_pool.observed_revision >= public_pool.desired_revision '
. 'THEN 1 ELSE 0 END) AS available_count'
)->get();
foreach ($rows as $row) {
$result[(int) $row->host_id][(int) $row->line_id] = [
'total' => (int) $row->total_count,
'allocated' => (int) $row->allocated_count,
'reserved' => (int) $row->reserved_count,
'available' => (int) $row->available_count,
'disabled' => (int) $row->disabled_count,
];
}
return $result;
}
private static function emptyIpStats(): array
{
return [
'total' => 0,
'allocated' => 0,
'reserved' => 0,
'available' => 0,
'disabled' => 0,
];
}
private static function regionDto($region, CloudHost $host): array
{
$country = CloudCountryCatalog::option((string) ($region?->country_code
?? $host->country_code));
return [
'id' => $region === null ? null : (int) $region->id,
'code' => $region === null ? 'unclassified' : (string) $region->code,
'name' => $region === null ? '未分类节点' : (string) $region->name,
'status' => $region === null ? 'missing' : (string) $region->status,
'sort' => $region === null ? PHP_INT_MAX : (int) $region->sort,
'country_code' => $country['code'],
'country_name' => $country['name'],
'country_flag' => $country['flag'],
'summary' => self::emptyGroupSummary(),
'capacity' => self::emptyAggregateCapacity(),
'hosts' => [],
];
}
private static function addHostToGroup(array &$group, array $host): void
{
$summary = &$group['summary'];
$summary['hosts_total']++;
$summary['enabled_hosts'] += $host['enabled'] ? 1 : 0;
$scheduler = (string) $host['scheduler_state'];
$summary['scheduler_' . $scheduler]++;
$summary['agent_ready_hosts'] += $host['agent']['ready'] ? 1 : 0;
$summary['placement_ready_hosts'] +=
$host['readiness']['placement_ready'] ? 1 : 0;
$summary['attention_hosts'] += $host['needs_attention'] ? 1 : 0;
self::addCapacity($group['capacity'], $host['capacity']);
}
private static function summary(array $hosts, array $regions): array
{
$summary = self::emptySummary();
$summary['regions_total'] = count(array_filter(
$regions, static fn (array $region): bool => $region['id'] !== null
));
foreach ($hosts as $host) {
$summary['hosts_total']++;
$summary['enabled_hosts'] += $host['enabled'] ? 1 : 0;
$summary['scheduler_active'] +=
$host['scheduler_state'] === 'active' ? 1 : 0;
$summary['scheduler_draining'] +=
$host['scheduler_state'] === 'draining' ? 1 : 0;
$summary['scheduler_disabled'] +=
$host['scheduler_state'] === 'disabled' ? 1 : 0;
$summary['agent_ready_hosts'] += $host['agent']['ready'] ? 1 : 0;
$summary['placement_ready_hosts'] +=
$host['readiness']['placement_ready'] ? 1 : 0;
$summary['attention_hosts'] += $host['needs_attention'] ? 1 : 0;
self::addCapacity($summary['capacity'], $host['capacity']);
}
return $summary;
}
private static function emptySummary(): array
{
return self::emptyGroupSummary() + [
'regions_total' => 0,
'capacity' => self::emptyAggregateCapacity(),
];
}
private static function emptyGroupSummary(): array
{
return [
'hosts_total' => 0,
'enabled_hosts' => 0,
'scheduler_active' => 0,
'scheduler_draining' => 0,
'scheduler_disabled' => 0,
'agent_ready_hosts' => 0,
'placement_ready_hosts' => 0,
'attention_hosts' => 0,
];
}
private static function emptyAggregateCapacity(): array
{
return [
'cpu' => self::emptyResourceCapacity(),
'memory' => self::emptyResourceCapacity(),
'disk' => self::emptyResourceCapacity(),
'instances' => ['allocated' => 0, 'reserved' => 0, 'max' => 0],
'ip' => self::emptyIpStats(),
];
}
private static function emptyResourceCapacity(): array
{
return [
'configured' => 0,
'safety' => 0,
'allocatable' => 0,
'allocated' => 0,
'reserved' => 0,
'free' => 0,
];
}
private static function addCapacity(array &$target, array $source): void
{
foreach (['cpu', 'memory', 'disk'] as $resource) {
foreach (array_keys(self::emptyResourceCapacity()) as $field) {
$target[$resource][$field] +=
(int) ($source[$resource][$field] ?? 0);
}
}
foreach (['allocated', 'reserved', 'max'] as $field) {
$target['instances'][$field] +=
(int) ($source['instances'][$field] ?? 0);
}
foreach (array_keys(self::emptyIpStats()) as $field) {
$target['ip'][$field] += (int) ($source['ip'][$field] ?? 0);
}
}
private static function keyById(iterable $rows): array
{
$result = [];
foreach ($rows as $row) {
$result[(int) $row->id] = $row;
}
return $result;
}
private static function keyByHost(iterable $rows): array
{
$result = [];
foreach ($rows as $row) {
$result[(int) $row->host_id] = $row;
}
return $result;
}
}

@ -92,14 +92,7 @@ final class CloudPanelUsageService
$usage['memory']['allocated'] += $spec['memory'];
$usage['disk']['allocated'] += $spec['disk'];
}
$usage['disk']['allocated'] +=
(int) ceil($snapshotBytes / self::GIB);
$usage['cpu_sum'] = $usage['cpu']['allocated'] + $usage['cpu']['reserved'];
$usage['memory_sum'] = $usage['memory']['allocated'] + $usage['memory']['reserved'];
$usage['disk_sum'] = $usage['disk']['allocated'] + $usage['disk']['reserved'];
$usage['instance_count'] = $usage['instances']['allocated']
+ $usage['instances']['reserved'];
return $usage;
return self::finalizeUsage($usage, $snapshotBytes);
}
public static function forHosts(array $hostIds): array
@ -107,9 +100,84 @@ final class CloudPanelUsageService
$hostIds = array_values(array_unique(array_filter(
array_map('intval', $hostIds), static fn (int $id): bool => $id > 0
)));
sort($hostIds, SORT_NUMERIC);
$result = [];
foreach ($hostIds as $hostId) {
$result[$hostId] = self::forHost($hostId);
$result[$hostId] = self::emptyUsage();
}
if ($hostIds === []) {
return $result;
}
$coveredInstances = [];
foreach (CloudResourceReservation::whereIn('host_id', $hostIds)
->whereIn('state', ['held', 'uncertain', 'committed'])
->orderBy('host_id')->orderBy('id')
->get(['host_id', 'state', 'instance_id', 'cpu_cores',
'memory_mib', 'disk_gib']) as $row) {
$hostId = (int) $row->host_id;
$bucket = (string) $row->state === 'committed'
? 'allocated' : 'reserved';
$result[$hostId]['instances'][$bucket]++;
$result[$hostId]['cpu'][$bucket] += max(0, (int) $row->cpu_cores);
$result[$hostId]['memory'][$bucket] += max(0, (int) $row->memory_mib);
$result[$hostId]['disk'][$bucket] += max(0, (int) $row->disk_gib);
if ($row->instance_id !== null) {
$coveredInstances[$hostId][(int) $row->instance_id] = true;
}
}
$snapshotBytes = array_fill_keys($hostIds, 0);
$instances = CloudInstance::whereIn('cloud_instances.host_id', $hostIds)
->where('cloud_instances.status', '<>', 'deleted')
->leftJoin(
'cloud_instance_snapshots as capacity_snapshots',
static function ($join): void {
$join->on(
'capacity_snapshots.instance_id',
'=',
'cloud_instances.id'
)->whereColumn(
'capacity_snapshots.host_id',
'cloud_instances.host_id'
)->whereIn('capacity_snapshots.state', [
'creating', 'ready', 'restoring',
'deleting', 'uncertain',
])->whereNotNull('capacity_snapshots.size_bytes');
}
)->groupBy('cloud_instances.host_id', 'cloud_instances.id',
'cloud_instances.spec_json')
->select([
'cloud_instances.id', 'cloud_instances.host_id',
'cloud_instances.spec_json',
])->selectRaw(
'COALESCE(SUM(capacity_snapshots.size_bytes), 0)'
. ' AS snapshot_bytes'
)->orderBy('cloud_instances.host_id')->orderBy('cloud_instances.id')
->get();
foreach ($instances as $instance) {
$hostId = (int) $instance->host_id;
$snapshotBytes[$hostId] += max(
0, (int) ($instance->snapshot_bytes ?? 0)
);
if (isset($coveredInstances[$hostId][(int) $instance->id])) {
continue;
}
$spec = self::spec((string) $instance->spec_json);
if ($spec === null) {
$result[$hostId]['unknown']++;
continue;
}
$result[$hostId]['instances']['allocated']++;
$result[$hostId]['cpu']['allocated'] += $spec['cpu'];
$result[$hostId]['memory']['allocated'] += $spec['memory'];
$result[$hostId]['disk']['allocated'] += $spec['disk'];
}
foreach ($hostIds as $hostId) {
$result[$hostId] = self::finalizeUsage(
$result[$hostId], $snapshotBytes[$hostId]
);
}
return $result;
}
@ -125,6 +193,22 @@ final class CloudPanelUsageService
];
}
private static function finalizeUsage(array $usage,
int $snapshotBytes): array
{
$usage['disk']['allocated'] +=
(int) ceil(max(0, $snapshotBytes) / self::GIB);
$usage['cpu_sum'] = $usage['cpu']['allocated']
+ $usage['cpu']['reserved'];
$usage['memory_sum'] = $usage['memory']['allocated']
+ $usage['memory']['reserved'];
$usage['disk_sum'] = $usage['disk']['allocated']
+ $usage['disk']['reserved'];
$usage['instance_count'] = $usage['instances']['allocated']
+ $usage['instances']['reserved'];
return $usage;
}
private static function spec(string $encoded): ?array
{
try {

@ -42,7 +42,7 @@ $assert(str_contains($controller, "'resource_pool_id', 'scheduler_state'")
strpos($controller, 'public function userIsoSettings')
- strpos($controller, 'public function hostScheduling')), 'public_ip'),
'The scheduling allowlist must not accept manual public IP fields.');
$assert(str_contains($controller, 'CloudAdminHostProjectionService::all()')
$assert(str_contains($controller, 'CloudAdminHostProjectionService::listing()')
&& str_contains($controller, 'CloudAdminHostProjectionService::one(')
&& str_contains($projectionService,
'CloudHostPoolMembershipService::selected($memberships)')

@ -42,6 +42,133 @@ $schema->create('cloud_host_resource_pool_memberships',
$table->integer('updated_at');
$table->unique(['host_id', 'resource_pool_id']);
});
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('country_code', 2);
$table->string('status');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_network_lines', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('region_id');
$table->string('code');
$table->string('name');
$table->boolean('is_default');
$table->string('status');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('line_id');
$table->string('code');
$table->string('name');
$table->string('status');
$table->string('scheduling_policy');
$table->integer('sort');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_capacity_policies',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('configured_cpu_cores');
$table->integer('configured_memory_mib');
$table->integer('configured_disk_gib');
$table->integer('safety_cpu_cores');
$table->integer('safety_memory_mib');
$table->integer('safety_disk_gib');
$table->integer('max_instances');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_agents', static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->text('sealed_api_secret');
$table->boolean('enabled');
$table->integer('revision');
$table->integer('tested_revision');
$table->string('last_test_status');
$table->string('last_error_code');
$table->string('last_agent_version');
$table->integer('last_tested_at');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_host_storage_configurations',
static function (Blueprint $table): void {
$table->integer('host_id')->primary();
$table->integer('revision');
$table->text('managed_directory');
$table->text('template_directory');
$table->text('seed_directory');
$table->text('user_iso_directory');
$table->text('rescue_directory');
$table->string('state');
$table->string('operation_id')->nullable();
$table->integer('job_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_resource_reservations',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('state');
$table->integer('instance_id')->nullable();
$table->integer('cpu_cores');
$table->integer('memory_mib');
$table->integer('disk_gib');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_instance_snapshots',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('host_id');
$table->string('state');
$table->integer('size_bytes')->nullable();
});
$schema->create('cloud_host_network_attachments',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('line_id');
$table->string('network_selector');
$table->string('state');
$table->string('sync_state');
$table->integer('observed_revision');
$table->integer('desired_revision');
});
$schema->create('cloud_public_ip_pools',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('network_attachment_id');
$table->string('state');
$table->string('sync_state');
$table->integer('observed_revision');
$table->integer('desired_revision');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('public_ip_pool_id');
$table->string('state');
$table->integer('reservation_id')->nullable();
$table->integer('instance_id')->nullable();
$table->string('operation_id')->nullable();
});
$assert(!$schema->hasColumn('cloud_hosts', 'resource_pool_id')
&& !$schema->hasColumn('cloud_hosts', 'scheduler_state'),
@ -66,6 +193,75 @@ Capsule::table('cloud_host_resource_pool_memberships')->insert([
'scheduler_state' => 'draining', 'priority' => 0, 'weight' => 100,
'created_at' => 1, 'updated_at' => 400],
]);
Capsule::table('cloud_regions')->insert([
'id' => 1, 'code' => 'hongkong', 'name' => 'HongKong',
'country_code' => 'HK', 'status' => 'active', 'sort' => 10,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_network_lines')->insert([
'id' => 1, 'region_id' => 1, 'code' => 'default', 'name' => '默认线路',
'is_default' => 1, 'status' => 'active', 'sort' => 10,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_resource_pools')->insert([
'id' => 11, 'line_id' => 1, 'code' => 'hkg', 'name' => 'HKG',
'status' => 'active', 'scheduling_policy' => 'balanced', 'sort' => 10,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_host_capacity_policies')->insert([
'host_id' => 1, 'configured_cpu_cores' => 32,
'configured_memory_mib' => 65536, 'configured_disk_gib' => 1000,
'safety_cpu_cores' => 2, 'safety_memory_mib' => 2048,
'safety_disk_gib' => 50, 'max_instances' => 100,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_host_agents')->insert([
'host_id' => 1, 'sealed_api_secret' => 'sealed', 'enabled' => 1,
'revision' => 2, 'tested_revision' => 2,
'last_test_status' => 'healthy', 'last_error_code' => '',
'last_agent_version' => 'v3', 'last_tested_at' => 100,
'created_at' => 1, 'updated_at' => 100,
]);
Capsule::table('cloud_host_storage_configurations')->insert([
'host_id' => 1, 'revision' => 1,
'managed_directory' => '/managed', 'template_directory' => '/templates',
'seed_directory' => '/seeds', 'user_iso_directory' => '/user-isos',
'rescue_directory' => '/rescue', 'state' => 'applied',
'operation_id' => null, 'job_id' => null,
'created_at' => 1, 'updated_at' => 1,
]);
Capsule::table('cloud_resource_reservations')->insert([
'id' => 1, 'host_id' => 1, 'state' => 'committed', 'instance_id' => 1,
'cpu_cores' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
]);
Capsule::table('cloud_instances')->insert([
'id' => 1, 'host_id' => 1, 'status' => 'active',
'spec_json' => json_encode([
'vcpu' => 2, 'memory_mib' => 2048, 'disk_gib' => 20,
], JSON_THROW_ON_ERROR),
]);
Capsule::table('cloud_host_network_attachments')->insert([
'id' => 1, 'host_id' => 1, 'line_id' => 1,
'network_selector' => 'bridge:br0', 'state' => 'active',
'sync_state' => 'applied', 'observed_revision' => 1,
'desired_revision' => 1,
]);
Capsule::table('cloud_public_ip_pools')->insert([
'id' => 1, 'host_id' => 1, 'network_attachment_id' => 1,
'state' => 'active', 'sync_state' => 'applied',
'observed_revision' => 1, 'desired_revision' => 1,
]);
Capsule::table('cloud_public_ips')->insert([
['id' => 1, 'host_id' => 1, 'public_ip_pool_id' => 1,
'state' => 'assigned', 'reservation_id' => 1, 'instance_id' => 1,
'operation_id' => null],
['id' => 2, 'host_id' => 1, 'public_ip_pool_id' => 1,
'state' => 'available', 'reservation_id' => null, 'instance_id' => null,
'operation_id' => null],
['id' => 3, 'host_id' => 1, 'public_ip_pool_id' => 1,
'state' => 'disabled', 'reservation_id' => null, 'instance_id' => null,
'operation_id' => null],
]);
$request = Request::createFromEnvironment(Environment::mock([
'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts',
@ -86,14 +282,46 @@ $assert($response->getStatusCode() === 200
[$first, $second] = $payload['hosts'];
$assert($first['id'] === 1 && $first['name'] === 'Host A'
&& $first['enabled'] === true && $first['resource_pool_id'] === 11
&& $first['scheduler_state'] === 'active',
&& $first['scheduler_state'] === 'active'
&& $first['placement']['region_name'] === 'HongKong'
&& $first['placement']['line_name'] === '默认线路'
&& $first['placement']['pool_name'] === 'HKG'
&& $first['agent']['state'] === 'ready'
&& $first['readiness']['placement_ready'] === true
&& $first['capacity']['instances']['allocated'] === 1
&& $first['capacity']['ip']['available'] === 1,
'Host list did not project the selected active membership authority.');
$assert($second['id'] === 2 && $second['name'] === 'Host B'
&& $second['enabled'] === false && $second['resource_pool_id'] === null
&& $second['scheduler_state'] === 'disabled',
&& $second['scheduler_state'] === 'disabled'
&& $second['placement']['region_id'] === null
&& $second['needs_attention'] === true,
'Host without a membership did not receive an explicit disabled projection.');
$assert($queries === 2,
'Host list introduced an N+1 query pattern: ' . $queries . ' queries.');
$assert($payload['summary']['regions_total'] === 1
&& $payload['summary']['hosts_total'] === 2
&& $payload['summary']['agent_ready_hosts'] === 1
&& count($payload['regions']) === 2
&& $payload['regions'][0]['name'] === 'HongKong'
&& $payload['regions'][0]['summary']['hosts_total'] === 1
&& $payload['regions'][1]['id'] === null,
'Host list did not provide stable region groups and page summary data.');
Capsule::table('cloud_hosts')->insert([
'id' => 3, 'name' => 'Host C', 'region' => 'Singapore',
'country_code' => 'SG', 'default_network' => 'bridge:br0', 'enabled' => 0,
'created_at' => 30, 'updated_at' => 30,
]);
Capsule::connection('default')->flushQueryLog();
$secondListResponse = (new CloudAdminController())->hosts(
$request, new Response()
);
$expandedQueries = count(Capsule::connection('default')->getQueryLog());
$expandedPayload = json_decode(
(string) $secondListResponse->getBody(), true, 512, JSON_THROW_ON_ERROR
);
$assert(count($expandedPayload['hosts']) === 3 && $queries === $expandedQueries,
'Host list introduced an N+1 query pattern: '
. $queries . ' versus ' . $expandedQueries . ' queries.');
$oneRequest = Request::createFromEnvironment(Environment::mock([
'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/api/v1/admin/hosts/1',

Loading…
Cancel
Save