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.
334 lines
14 KiB
PHP
334 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudHostAgent;
|
|
use App\Models\CloudInstanceBackup;
|
|
|
|
/**
|
|
* Read-only portable-restore target scheduling.
|
|
*
|
|
* This module returns advisory evidence only. It never creates a restore,
|
|
* instance, job, reservation, address claim or concrete IP selection. A
|
|
* admission coordinator repeats every predicate under canonical locks before
|
|
* reserving capacity and concrete dual-stack addresses.
|
|
*/
|
|
final class CloudPortableRestoreTargetService
|
|
{
|
|
/** @return list<CloudPortableRestoreTargetPlan> */
|
|
public static function candidates(
|
|
CloudInstanceBackup $backup
|
|
): array {
|
|
$backupId = (int) $backup->id;
|
|
if ($backupId < 1) {
|
|
throw new CloudError('备份缺少可信的可移植恢复证据', 409);
|
|
}
|
|
return $backup->getConnection()->transaction(
|
|
static function () use ($backupId): array {
|
|
$current = CloudInstanceBackup::where('id', $backupId)
|
|
->first();
|
|
if (!$current instanceof CloudInstanceBackup) {
|
|
throw new CloudError('备份不存在', 404);
|
|
}
|
|
return self::candidatesFromSnapshot($current);
|
|
},
|
|
1
|
|
);
|
|
}
|
|
|
|
/** @return list<CloudPortableRestoreTargetPlan> */
|
|
private static function candidatesFromSnapshot(
|
|
CloudInstanceBackup $backup
|
|
): array {
|
|
$backupEvidence = CloudPortableRestoreBackupEvidence::fromBackup(
|
|
$backup
|
|
);
|
|
$sourceContract = $backupEvidence->compatibility();
|
|
$backupIdentity = $backupEvidence->backup();
|
|
$resources = $backupEvidence->resources();
|
|
$sourceLineId = $backupIdentity['source_line_id'];
|
|
$virtualSize = $backupIdentity['virtual_size_bytes'];
|
|
$rows = self::candidateRows();
|
|
if ($rows->isEmpty()) {
|
|
return [];
|
|
}
|
|
$hostIds = $rows->pluck('id')->map(
|
|
static fn ($id): int => (int) $id
|
|
)->unique()->values()->all();
|
|
$usageByHost = CloudPanelUsageService::forHosts($hostIds);
|
|
$networkByHostLine = CloudPlacementNetworkService::
|
|
advisoryDualStackBundleEvidence($rows);
|
|
$plans = [];
|
|
foreach ($rows as $row) {
|
|
$hostId = (int) $row->id;
|
|
$lineId = (int) $row->resource_pool_line_id;
|
|
$agent = self::agentFromRow($row);
|
|
$targetContract = $agent->testedPortableRestoreCompatibility();
|
|
$usage = $usageByHost[$hostId] ?? null;
|
|
if ($targetContract === null
|
|
|| !$sourceContract->matches($targetContract)
|
|
|| (int) $row->membership_revision < 1
|
|
|| (int) $row->storage_revision < 1
|
|
|| !is_array($usage)
|
|
|| !self::hasCapacity($row, $usage, $resources)) {
|
|
continue;
|
|
}
|
|
foreach ($networkByHostLine[$hostId][$lineId] ?? [] as $network) {
|
|
if (!self::networkEvidenceMatchesAgent($network, $agent)) {
|
|
continue;
|
|
}
|
|
$evidence = [
|
|
'contract' => 1,
|
|
'backup' => $backupIdentity,
|
|
'target' => [
|
|
'host_id' => $hostId,
|
|
'host_name' => (string) $row->name,
|
|
'is_source_host' =>
|
|
$hostId === (int) $backup->source_host_id,
|
|
'resource_pool_id' =>
|
|
(int) $row->resource_pool_id,
|
|
'resource_pool_name' =>
|
|
(string) $row->resource_pool_name,
|
|
'membership_revision' =>
|
|
(int) $row->membership_revision,
|
|
'line_id' => $lineId,
|
|
'line_name' => (string) $row->line_name,
|
|
'region_id' => (int) $row->region_id,
|
|
'region_name' => (string) $row->region_name,
|
|
'country_code' => strtoupper(
|
|
(string) $row->country_code
|
|
),
|
|
'agent_revision' => (int) $agent->revision,
|
|
'agent_contract_sha256' => $targetContract->sha256(),
|
|
'storage_revision' => (int) $row->storage_revision,
|
|
],
|
|
'resources' => [
|
|
'cpu_cores' => $resources['cpu_cores'],
|
|
'memory_mib' => $resources['memory_mib'],
|
|
'disk_gib' => $resources['disk_gib'],
|
|
'bandwidth_mbps' => $resources['bandwidth_mbps'],
|
|
'system_disk_virtual_size_bytes' => $virtualSize,
|
|
],
|
|
'capacity' => self::capacityEvidence(
|
|
$row,
|
|
$usage,
|
|
$resources
|
|
),
|
|
'public_network' => [
|
|
'mode' => 'fresh_dual_stack',
|
|
'host_id' => (int) $network['host_id'],
|
|
'line_id' => (int) $network['line_id'],
|
|
'network_attachment_id' =>
|
|
(int) $network['network_attachment_id'],
|
|
'attachment_key' =>
|
|
(string) $network['attachment_key'],
|
|
'network_selector' =>
|
|
(string) $network['network_selector'],
|
|
'attachment_config_revision' =>
|
|
(int) $network['attachment_config_revision'],
|
|
'attachment_verified_agent_revision' =>
|
|
(int) $network[
|
|
'attachment_verified_agent_revision'
|
|
],
|
|
'ipv4_pool' => $network['ipv4'],
|
|
'ipv6_pool' => $network['ipv6'],
|
|
],
|
|
'vpc_inherited' => false,
|
|
];
|
|
try {
|
|
$plans[] = CloudPortableRestoreTargetPlan::fromEvidence(
|
|
$evidence
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
return $plans;
|
|
}
|
|
|
|
private static function candidateRows()
|
|
{
|
|
return CloudHost::query()->join(
|
|
'cloud_host_resource_pool_memberships as restore_membership',
|
|
'restore_membership.host_id', '=', 'cloud_hosts.id'
|
|
)->join(
|
|
'cloud_resource_pools as restore_pool',
|
|
'restore_pool.id', '=', 'restore_membership.resource_pool_id'
|
|
)->join(
|
|
'cloud_network_lines as restore_line',
|
|
'restore_line.id', '=', 'restore_pool.line_id'
|
|
)->join(
|
|
'cloud_regions as restore_region',
|
|
'restore_region.id', '=', 'restore_line.region_id'
|
|
)->join(
|
|
'cloud_host_capacity_policies as restore_capacity',
|
|
'restore_capacity.host_id', '=', 'cloud_hosts.id'
|
|
)->join(
|
|
'cloud_host_agents as restore_agent',
|
|
'restore_agent.host_id', '=', 'cloud_hosts.id'
|
|
)->join(
|
|
'cloud_host_storage_configurations as restore_storage',
|
|
'restore_storage.host_id', '=', 'cloud_hosts.id'
|
|
)->where('cloud_hosts.enabled', 1)
|
|
->where('restore_membership.scheduler_state', 'active')
|
|
->where('restore_pool.status', 'active')
|
|
->where('restore_line.status', 'active')
|
|
->where('restore_region.status', 'active')
|
|
->where('restore_agent.enabled', 1)
|
|
->where('restore_agent.last_test_status', 'healthy')
|
|
->where('restore_agent.tested_revision', '>', 0)
|
|
->whereColumn(
|
|
'restore_agent.tested_revision',
|
|
'restore_agent.revision'
|
|
)->where('restore_storage.state', 'applied')
|
|
->where('restore_storage.revision', '>', 0)
|
|
->where('restore_membership.revision', '>', 0)
|
|
->where('restore_capacity.configured_cpu_cores', '>', 0)
|
|
->where('restore_capacity.configured_memory_mib', '>', 0)
|
|
->where('restore_capacity.configured_disk_gib', '>', 0)
|
|
->orderBy('restore_membership.priority')
|
|
->orderBy('cloud_hosts.id')
|
|
->orderBy('restore_pool.id')
|
|
->get([
|
|
'cloud_hosts.id', 'cloud_hosts.name',
|
|
'restore_pool.id as resource_pool_id',
|
|
'restore_pool.name as resource_pool_name',
|
|
'restore_line.id as resource_pool_line_id',
|
|
'restore_line.name as line_name',
|
|
'restore_region.id as region_id',
|
|
'restore_region.name as region_name',
|
|
'restore_region.country_code',
|
|
'restore_membership.revision as membership_revision',
|
|
'restore_capacity.configured_cpu_cores',
|
|
'restore_capacity.configured_memory_mib',
|
|
'restore_capacity.configured_disk_gib',
|
|
'restore_capacity.safety_cpu_cores',
|
|
'restore_capacity.safety_memory_mib',
|
|
'restore_capacity.safety_disk_gib',
|
|
'restore_capacity.max_instances',
|
|
'restore_capacity.updated_at as capacity_updated_at',
|
|
'restore_storage.revision as storage_revision',
|
|
'restore_agent.enabled as agent_enabled',
|
|
'restore_agent.revision as agent_revision',
|
|
'restore_agent.tested_revision as agent_tested_revision',
|
|
'restore_agent.last_test_status as agent_last_test_status',
|
|
'restore_agent.portable_restore_contract_json as '
|
|
. 'agent_portable_restore_contract_json',
|
|
'restore_agent.portable_restore_contract_sha256 as '
|
|
. 'agent_portable_restore_contract_sha256',
|
|
]);
|
|
}
|
|
|
|
private static function agentFromRow($row): CloudHostAgent
|
|
{
|
|
$agent = new CloudHostAgent();
|
|
$agent->setRawAttributes([
|
|
'host_id' => (int) $row->id,
|
|
'enabled' => (bool) $row->agent_enabled,
|
|
'revision' => (int) $row->agent_revision,
|
|
'tested_revision' => (int) $row->agent_tested_revision,
|
|
'last_test_status' => (string) $row->agent_last_test_status,
|
|
'portable_restore_contract_json' =>
|
|
(string) $row->agent_portable_restore_contract_json,
|
|
'portable_restore_contract_sha256' =>
|
|
(string) $row->agent_portable_restore_contract_sha256,
|
|
], true);
|
|
return $agent;
|
|
}
|
|
|
|
private static function networkEvidenceMatchesAgent(
|
|
array $network,
|
|
CloudHostAgent $agent
|
|
): bool {
|
|
foreach (['ipv4' => 4, 'ipv6' => 6] as $family => $number) {
|
|
$pool = $network[$family] ?? null;
|
|
if (!is_array($pool)
|
|
|| (int) ($pool['public_ip_pool_id'] ?? 0) < 1
|
|
|| (int) ($pool['network_attachment_id'] ?? 0)
|
|
!== (int) ($network['network_attachment_id'] ?? 0)
|
|
|| (int) ($pool['address_family'] ?? 0) !== $number
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) ($pool['pool_key'] ?? '')) !== 1
|
|
|| (int) ($pool['desired_revision'] ?? 0) < 1
|
|
|| (int) ($pool['observed_revision'] ?? 0)
|
|
!== (int) $pool['desired_revision']
|
|
|| (int) ($pool['available_units'] ?? 0) < 1) {
|
|
return false;
|
|
}
|
|
}
|
|
return (int) ($network['network_attachment_id'] ?? 0) > 0
|
|
&& (int) ($network['host_id'] ?? 0) === (int) $agent->host_id
|
|
&& (int) ($network['line_id'] ?? 0) > 0
|
|
&& preg_match('/^[a-f0-9]{32}$/D',
|
|
(string) ($network['attachment_key'] ?? '')) === 1
|
|
&& CloudNetworkSelector::isValid(
|
|
$network['network_selector'] ?? null
|
|
)
|
|
&& (int) ($network['attachment_config_revision'] ?? 0) > 0
|
|
&& (int) ($network[
|
|
'attachment_verified_agent_revision'
|
|
] ?? 0) === (int) $agent->revision;
|
|
}
|
|
|
|
private static function hasCapacity($policy, array $usage,
|
|
array $resources): bool
|
|
{
|
|
if ((int) ($usage['unknown'] ?? 0) > 0) {
|
|
return false;
|
|
}
|
|
$cpuLimit = max(0, (int) $policy->configured_cpu_cores
|
|
- (int) $policy->safety_cpu_cores);
|
|
$memoryLimit = max(0, (int) $policy->configured_memory_mib
|
|
- (int) $policy->safety_memory_mib);
|
|
$diskLimit = max(0, (int) $policy->configured_disk_gib
|
|
- (int) $policy->safety_disk_gib);
|
|
$maxInstances = (int) $policy->max_instances;
|
|
return (int) ($usage['cpu_sum'] ?? PHP_INT_MAX)
|
|
+ $resources['cpu_cores'] <= $cpuLimit
|
|
&& (int) ($usage['memory_sum'] ?? PHP_INT_MAX)
|
|
+ $resources['memory_mib'] <= $memoryLimit
|
|
&& (int) ($usage['disk_sum'] ?? PHP_INT_MAX)
|
|
+ $resources['disk_gib'] <= $diskLimit
|
|
&& ($maxInstances < 1
|
|
|| (int) ($usage['instance_count'] ?? PHP_INT_MAX) + 1
|
|
<= $maxInstances);
|
|
}
|
|
|
|
private static function capacityEvidence($policy, array $usage,
|
|
array $resources): array
|
|
{
|
|
return [
|
|
'policy_updated_at' => max(
|
|
0,
|
|
(int) $policy->capacity_updated_at
|
|
),
|
|
'requested' => [
|
|
'cpu_cores' => $resources['cpu_cores'],
|
|
'memory_mib' => $resources['memory_mib'],
|
|
'disk_gib' => $resources['disk_gib'],
|
|
],
|
|
'used' => [
|
|
'instance_count' => (int) $usage['instance_count'],
|
|
'cpu_cores' => (int) $usage['cpu_sum'],
|
|
'memory_mib' => (int) $usage['memory_sum'],
|
|
'disk_gib' => (int) $usage['disk_sum'],
|
|
],
|
|
'limits' => [
|
|
'instance_count' => (int) $policy->max_instances,
|
|
'cpu_cores' => max(0,
|
|
(int) $policy->configured_cpu_cores
|
|
- (int) $policy->safety_cpu_cores),
|
|
'memory_mib' => max(0,
|
|
(int) $policy->configured_memory_mib
|
|
- (int) $policy->safety_memory_mib),
|
|
'disk_gib' => max(0,
|
|
(int) $policy->configured_disk_gib
|
|
- (int) $policy->safety_disk_gib),
|
|
],
|
|
];
|
|
}
|
|
|
|
}
|