You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudAdminHostProjectionSer...

694 lines
28 KiB
PHP

<?php
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\CloudRegion;
use App\Models\CloudResourcePool;
/**
* 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'];
/** @return array{summary:array,regions:list<array>,hosts:list<array>} */
public static function listing(): array
{
$hosts = CloudHost::orderBy('id')->get();
if ($hosts->isEmpty()) {
return [
'summary' => self::emptySummary(),
'regions' => [],
'hosts' => [],
];
}
$hostIds = $hosts->pluck('id')->map(
static fn ($id): int => (int) $id
)->all();
$memberships = CloudHostResourcePoolMembership::whereIn(
'host_id', $hostIds
)->orderBy('host_id')->orderBy('resource_pool_id')->orderBy('id')->get();
$membershipsByHost = [];
foreach ($memberships as $membership) {
$membershipsByHost[(int) $membership->host_id][] = $membership;
}
$poolIds = [];
foreach ($memberships as $membership) {
$poolIds[] = (int) $membership->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);
$ipCapacity = CloudAdminPublicNetworkCapacityProjection::forHosts(
$hostIds
);
$flat = [];
$groups = [];
foreach ($hosts as $host) {
$hostId = (int) $host->id;
$hostMemberships = $membershipsByHost[$hostId] ?? [];
$primary = CloudHostPoolMembershipService::primary(
$hostMemberships
);
$primaryPool = $primary === null ? null
: ($pools[(int) $primary->resource_pool_id] ?? null);
$primaryLine = $primaryPool === null ? null
: ($lines[(int) $primaryPool->line_id] ?? null);
$primaryRegion = $primaryLine === null ? null
: ($regions[(int) $primaryLine->region_id] ?? null);
$row = self::hostDto(
$host,
$hostMemberships,
$pools,
$lines,
$regions,
$policies[$hostId] ?? null,
$agents[$hostId] ?? null,
$storage[$hostId] ?? null,
$usage[$hostId] ?? CloudPanelUsageService::emptyUsage(),
$ipCapacity[$hostId]['lines'] ?? []
);
$flat[] = $row;
$key = $primaryRegion === null
? 'unclassified' : 'region:' . $primaryRegion->id;
if (!isset($groups[$key])) {
$groups[$key] = self::regionDto($primaryRegion, $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
{
foreach (self::listing()['hosts'] as $row) {
if ((int) $row['id'] === (int) $host->id) {
return $row;
}
}
return self::basicDto($host, []);
}
private static function basicDto(CloudHost $host,
iterable $memberships): array
{
$rows = [];
foreach ($memberships as $membership) {
$rows[] = $membership;
}
$primary = CloudHostPoolMembershipService::primary($rows);
$state = (string) ($primary?->scheduler_state ?? 'disabled');
$dto = $host->toArray();
// Compact compatibility fields are a deterministic summary only.
// Every scheduling decision must use one exact memberships[] row.
$dto['resource_pool_id'] = $primary === null
? null : (int) $primary->resource_pool_id;
$dto['scheduler_state'] = in_array($state, self::SCHEDULER_STATES, true)
? $state : 'disabled';
$dto['active_memberships'] = count(array_filter(
$rows,
static fn ($row): bool =>
(string) $row->scheduler_state === 'active'
));
$dto['draining_memberships'] = count(array_filter(
$rows,
static fn ($row): bool =>
(string) $row->scheduler_state === 'draining'
));
return $dto;
}
private static function hostDto(CloudHost $host, iterable $memberships,
array $pools, array $lines, array $regions, $policy,
?CloudHostAgent $agent,
?CloudHostStorageConfiguration $storage, array $usage,
array $ipByLine): array
{
$membershipModels = [];
foreach ($memberships as $membership) {
$membershipModels[] = $membership;
}
$base = self::basicDto($host, $membershipModels);
$primary = CloudHostPoolMembershipService::primary($membershipModels);
$storageReady = $storage !== null && $storage->isApplied();
$agentReady = $agent !== null && $agent->isReady();
$aggregateIpParts = [];
$countedLines = [];
$membershipRows = [];
$lineRows = [];
foreach ($membershipModels as $membership) {
$pool = $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);
$lineId = $line === null ? null : (int) $line->id;
$lineIp = $lineId === null
? CloudAdminPublicNetworkCapacityProjection::empty()
: ($ipByLine[$lineId]
?? CloudAdminPublicNetworkCapacityProjection::empty());
if ($lineId !== null && !isset($countedLines[$lineId])) {
$aggregateIpParts[] = $lineIp;
$countedLines[$lineId] = true;
}
$membershipRows[] = self::membershipDto(
$membership, $pool, $line, $region, $lineIp
);
if ($lineId !== null && !isset($lineRows[$lineId])) {
$lineRows[$lineId] = [
'id' => $lineId,
'code' => (string) $line->code,
'name' => (string) $line->name,
'status' => (string) $line->status,
'region_id' => $region === null ? null : (int) $region->id,
'region_code' => $region === null
? '' : (string) $region->code,
'region_name' => $region === null
? '地区已缺失' : (string) $region->name,
'network_ready' =>
(int) $lineIp['bundles']['available'] > 0,
'ip' => $lineIp,
'membership_count' => 0,
'active_memberships' => 0,
'draining_memberships' => 0,
'placement_ready_memberships' => 0,
];
}
if ($lineId !== null) {
$lineRows[$lineId]['membership_count']++;
$state = (string) $membership->scheduler_state;
if ($state === 'active') {
$lineRows[$lineId]['active_memberships']++;
} elseif ($state === 'draining') {
$lineRows[$lineId]['draining_memberships']++;
}
}
}
$aggregateIp = CloudAdminPublicNetworkCapacityProjection::sum(
$aggregateIpParts
);
$capacity = self::capacity($policy, $usage, $aggregateIp);
$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'];
$hostCapacityReady = $capacityConfigured
&& (int) $capacity['unknown_instances'] === 0
&& ($maxInstances <= 0 || $instanceCount < $maxInstances)
&& $capacity['cpu']['free'] > 0
&& $capacity['memory']['free'] > 0
&& $capacity['disk']['free'] > 0;
$placementReady = false;
$networkReady = false;
$primaryPlacement = null;
foreach ($membershipRows as &$membershipRow) {
$membershipRow['placement_ready'] = (bool) $host->enabled
&& $membershipRow['scheduler_state'] === 'active'
&& $membershipRow['path_ready']
&& $membershipRow['network_ready']
&& $agentReady && $storageReady && $hostCapacityReady;
if ($membershipRow['scheduler_state'] === 'active'
&& $membershipRow['network_ready']) {
$networkReady = true;
}
if ($membershipRow['placement_ready']) {
$placementReady = true;
$lineId = $membershipRow['line_id'];
if ($lineId !== null && isset($lineRows[$lineId])) {
$lineRows[$lineId]['placement_ready_memberships']++;
}
}
if ($primary !== null
&& (int) $membershipRow['id'] === (int) $primary->id) {
$primaryPlacement = $membershipRow;
}
}
unset($membershipRow);
$lineRows = array_values($lineRows);
usort($lineRows, static fn (array $left, array $right): int =>
[$left['name'], $left['id']] <=> [$right['name'], $right['id']]
);
$placement = self::compactPlacement($primaryPlacement);
$attention = self::attention(
$host,
$membershipRows,
$agentReady,
$storageReady,
$capacityConfigured,
$capacity,
$networkReady
);
$primaryRegionId = $primaryPlacement['region_id'] ?? null;
$primaryRegion = $primaryRegionId === null
? null : ($regions[(int) $primaryRegionId] ?? null);
$country = CloudCountryCatalog::option((string) (
$primaryRegion?->country_code ?? $host->country_code
));
return $base + [
'memberships' => $membershipRows,
'lines' => $lineRows,
'location' => [
'physical_label' => (string) $host->region,
'country_code' => $country['code'],
'country_name' => $country['name'],
'country_flag' => $country['flag'],
],
'placement' => $placement,
'agent' => [
'state' => self::agentState($agent),
'ready' => $agentReady,
'enabled' => $agent !== null && (bool) $agent->enabled,
'secret_configured' => $agent !== null
&& trim((string) $agent->sealed_api_secret) !== '',
'revision' => $agent === null ? 0 : (int) $agent->revision,
'tested_revision' => $agent === null
? 0 : (int) $agent->tested_revision,
'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,
'updated_at' => $agent === null ? 0
: (int) $agent->updated_at,
],
'readiness' => [
'network_ready' => $networkReady,
// Compatibility alias: this now reflects the deterministic
// primary membership's verified line, never default_network.
'network_selector_ready' => (bool) (
$primaryPlacement['network_ready'] ?? false
),
'storage_ready' => $storageReady,
'capacity_configured' => $capacityConfigured,
'placement_ready' => $placementReady,
],
'capacity' => $capacity,
'attention' => $attention,
'needs_attention' => $attention !== [],
];
}
private static function membershipDto($membership, $pool, $line,
$region, array $ip): array
{
$state = (string) $membership->scheduler_state;
if (!in_array($state, self::SCHEDULER_STATES, true)) {
$state = 'disabled';
}
$pathReady = $pool !== null && (string) $pool->status === 'active'
&& $line !== null && (string) $line->status === 'active'
&& $region !== null && (string) $region->status === 'active';
return [
'id' => (int) $membership->id,
'resource_pool_id' => (int) $membership->resource_pool_id,
'scheduler_state' => $state,
'priority' => (int) $membership->priority,
'weight' => (int) $membership->weight,
'revision' => max(1, (int) $membership->revision),
'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_code' => $line === null ? '' : (string) $line->code,
'line_name' => $line === null ? '' : (string) $line->name,
'line_status' => $line === null
? 'missing' : (string) $line->status,
'pool_id' => $pool === null ? null : (int) $pool->id,
'pool_code' => $pool === null ? '' : (string) $pool->code,
'pool_name' => $pool === null ? '' : (string) $pool->name,
'pool_status' => $pool === null
? 'missing' : (string) $pool->status,
'path_ready' => $pathReady,
'network_ready' => (int) $ip['bundles']['available'] > 0,
'ip' => $ip,
'placement_ready' => false,
];
}
private static function compactPlacement(?array $membership): array
{
if ($membership === null) {
return [
'membership_id' => null,
'scheduler_state' => 'disabled',
'region_id' => null,
'region_code' => '',
'region_name' => '未分类节点',
'region_status' => 'missing',
'line_id' => null,
'line_name' => '',
'line_status' => 'missing',
'pool_id' => null,
'pool_name' => '',
'pool_status' => 'missing',
'network_ready' => false,
'placement_ready' => false,
];
}
return [
'membership_id' => (int) $membership['id'],
'scheduler_state' => (string) $membership['scheduler_state'],
'region_id' => $membership['region_id'],
'region_code' => (string) $membership['region_code'],
'region_name' => (string) $membership['region_name'],
'region_status' => (string) $membership['region_status'],
'line_id' => $membership['line_id'],
'line_name' => (string) $membership['line_name'],
'line_status' => (string) $membership['line_status'],
'pool_id' => $membership['pool_id'],
'pool_name' => (string) $membership['pool_name'],
'pool_status' => (string) $membership['pool_status'],
'network_ready' => (bool) $membership['network_ready'],
'placement_ready' => (bool) $membership['placement_ready'],
];
}
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, array $memberships,
bool $agentReady, bool $storageReady, bool $capacityConfigured,
array $capacity, bool $networkReady): array
{
$attention = [];
if ($memberships === []) {
$attention[] = 'placement_unclassified';
}
if (!$agentReady) {
$attention[] = 'agent_not_ready';
}
if (!$storageReady) {
$attention[] = 'storage_not_ready';
}
if ((int) $capacity['unknown_instances'] > 0) {
$attention[] = 'instance_capacity_unknown';
}
$active = array_values(array_filter(
$memberships,
static fn (array $row): bool =>
$row['scheduler_state'] === 'active'
));
if ($active !== []) {
if (!(bool) $host->enabled) {
$attention[] = 'host_service_disabled';
}
if (!$capacityConfigured) {
$attention[] = 'capacity_not_configured';
}
if (!$networkReady) {
$attention[] = 'public_ip_unavailable';
}
foreach ($active as $row) {
if ($row['pool_id'] === null) {
$attention[] = 'placement_unclassified';
} elseif ($row['pool_status'] !== 'active') {
$attention[] = 'pool_inactive';
}
if ($row['line_id'] === null) {
$attention[] = 'placement_unclassified';
} elseif ($row['line_status'] !== 'active') {
$attention[] = 'line_inactive';
}
if ($row['region_id'] === null) {
$attention[] = 'placement_unclassified';
} elseif ($row['region_status'] !== 'active') {
$attention[] = 'region_inactive';
}
}
$regionIds = array_values(array_unique(array_filter(array_map(
static fn (array $row) => $row['region_id'],
$active
), static fn ($id): bool => $id !== null)));
if (count($regionIds) > 1) {
$attention[] = 'membership_region_conflict';
}
}
return array_values(array_unique($attention));
}
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' => CloudAdminPublicNetworkCapacityProjection::empty(),
];
}
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);
}
$target['ip'] = CloudAdminPublicNetworkCapacityProjection::sum([
$target['ip'],
$source['ip'] ?? CloudAdminPublicNetworkCapacityProjection::empty(),
]);
}
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;
}
}