|
|
|
|
@ -368,75 +368,99 @@ final class CloudPlacementService
|
|
|
|
|
array $resources): array
|
|
|
|
|
{
|
|
|
|
|
$resources = self::upgradeResources($resources);
|
|
|
|
|
$candidates = CloudHost::query()->join(
|
|
|
|
|
'cloud_host_resource_pool_memberships as restore_membership',
|
|
|
|
|
'restore_membership.host_id', '=', 'cloud_hosts.id'
|
|
|
|
|
)->join(
|
|
|
|
|
'cloud_resource_pools as restore_resource_pool',
|
|
|
|
|
'restore_resource_pool.id', '=',
|
|
|
|
|
'restore_membership.resource_pool_id'
|
|
|
|
|
)->join(
|
|
|
|
|
'cloud_network_lines as restore_line',
|
|
|
|
|
'restore_line.id', '=', 'restore_resource_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_resource_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_capacity.configured_cpu_cores', '>', 0)
|
|
|
|
|
->where('restore_capacity.configured_memory_mib', '>', 0)
|
|
|
|
|
->where('restore_capacity.configured_disk_gib', '>', 0)
|
|
|
|
|
->orderBy('cloud_hosts.id')
|
|
|
|
|
->orderBy('restore_resource_pool.id')
|
|
|
|
|
->get([
|
|
|
|
|
'cloud_hosts.id',
|
|
|
|
|
'cloud_hosts.name',
|
|
|
|
|
'restore_resource_pool.id as resource_pool_id',
|
|
|
|
|
'restore_resource_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_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',
|
|
|
|
|
]);
|
|
|
|
|
$pendingByHost = self::pendingUsageByHost($candidates);
|
|
|
|
|
$networkByHostLine = CloudPlacementNetworkService::
|
|
|
|
|
advisoryAvailableHostLineDetails($candidates);
|
|
|
|
|
$result = [];
|
|
|
|
|
$hosts = CloudHost::where('enabled', 1)
|
|
|
|
|
->orderBy('id')->get();
|
|
|
|
|
foreach ($hosts as $host) {
|
|
|
|
|
if ((string) $host->default_network !== 'bridge:br0') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$memberships = CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id', (int) $host->id
|
|
|
|
|
)->where('scheduler_state', 'active')->orderBy('id')->get();
|
|
|
|
|
$membership = $memberships->count() === 1
|
|
|
|
|
? $memberships->first() : null;
|
|
|
|
|
$pool = $membership === null ? null
|
|
|
|
|
: CloudResourcePool::find((int) $membership->resource_pool_id);
|
|
|
|
|
$line = $pool === null ? null
|
|
|
|
|
: CloudNetworkLine::find((int) $pool->line_id);
|
|
|
|
|
$region = $line === null ? null
|
|
|
|
|
: CloudRegion::find((int) $line->region_id);
|
|
|
|
|
$policy = CloudHostCapacityPolicy::find((int) $host->id);
|
|
|
|
|
$agent = CloudHostAgent::find((int) $host->id);
|
|
|
|
|
$storage = CloudHostStorageConfiguration::find((int) $host->id);
|
|
|
|
|
if ($pool === null || $line === null || $region === null
|
|
|
|
|
|| (string) $pool->status !== 'active'
|
|
|
|
|
|| (string) $line->status !== 'active'
|
|
|
|
|
|| (string) $region->status !== 'active'
|
|
|
|
|
|| $policy === null || $agent === null || !$agent->isReady()
|
|
|
|
|
|| $storage === null || !$storage->isApplied()
|
|
|
|
|
foreach ($candidates as $candidate) {
|
|
|
|
|
$hostId = (int) $candidate->id;
|
|
|
|
|
$lineId = (int) $candidate->resource_pool_line_id;
|
|
|
|
|
$network = $networkByHostLine[$hostId][$lineId] ?? null;
|
|
|
|
|
if (!is_array($network)
|
|
|
|
|
|| (int) ($network['available_ip_count'] ?? 0) < 1
|
|
|
|
|
|| !self::hasCapacity(
|
|
|
|
|
$policy,
|
|
|
|
|
CloudPanelUsageService::forHost((int) $host->id),
|
|
|
|
|
$candidate,
|
|
|
|
|
$pendingByHost[$hostId] ?? self::emptyUsage(),
|
|
|
|
|
$resources
|
|
|
|
|
)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$attachmentIds = CloudHostNetworkAttachment::where(
|
|
|
|
|
'host_id', (int) $host->id
|
|
|
|
|
)->where('line_id', (int) $line->id)
|
|
|
|
|
->where('network_selector', 'bridge:br0')
|
|
|
|
|
->where('state', 'active')->where('sync_state', 'applied')
|
|
|
|
|
->whereColumn('observed_revision', '>=', 'desired_revision')
|
|
|
|
|
->pluck('id')->all();
|
|
|
|
|
if ($attachmentIds === []) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$availableIpCount = CloudPublicIp::query()
|
|
|
|
|
->join('cloud_public_ip_pools as restore_pool',
|
|
|
|
|
'restore_pool.id', '=', 'cloud_public_ips.public_ip_pool_id')
|
|
|
|
|
->where('cloud_public_ips.host_id', (int) $host->id)
|
|
|
|
|
->whereIn('restore_pool.network_attachment_id', $attachmentIds)
|
|
|
|
|
->where('restore_pool.state', 'active')
|
|
|
|
|
->where('restore_pool.sync_state', 'applied')
|
|
|
|
|
->whereColumn('restore_pool.observed_revision', '>=',
|
|
|
|
|
'restore_pool.desired_revision')
|
|
|
|
|
->where('cloud_public_ips.state', 'available')
|
|
|
|
|
->whereNull('cloud_public_ips.reservation_id')
|
|
|
|
|
->whereNull('cloud_public_ips.instance_id')
|
|
|
|
|
->whereNull('cloud_public_ips.operation_id')->count();
|
|
|
|
|
if ((int) $availableIpCount < 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$result[] = [
|
|
|
|
|
'id' => (string) $host->id,
|
|
|
|
|
'name' => (string) $host->name,
|
|
|
|
|
'region' => (string) $region->name,
|
|
|
|
|
'resource_pool_id' => (string) $pool->id,
|
|
|
|
|
'resource_pool' => (string) $pool->name,
|
|
|
|
|
'network' => 'bridge:br0',
|
|
|
|
|
'available_ip_count' => (int) $availableIpCount,
|
|
|
|
|
'is_source_host' => (int) $host->id === $sourceHostId,
|
|
|
|
|
'id' => (string) $hostId,
|
|
|
|
|
'name' => (string) $candidate->name,
|
|
|
|
|
'region_id' => (string) $candidate->region_id,
|
|
|
|
|
'region' => (string) $candidate->region_name,
|
|
|
|
|
'country_code' => strtoupper(
|
|
|
|
|
(string) $candidate->country_code
|
|
|
|
|
),
|
|
|
|
|
'line_id' => (string) $lineId,
|
|
|
|
|
'line' => (string) $candidate->line_name,
|
|
|
|
|
'resource_pool_id' =>
|
|
|
|
|
(string) $candidate->resource_pool_id,
|
|
|
|
|
'resource_pool' =>
|
|
|
|
|
(string) $candidate->resource_pool_name,
|
|
|
|
|
'network_selectors' =>
|
|
|
|
|
array_values($network['network_selectors'] ?? []),
|
|
|
|
|
'available_ip_count' =>
|
|
|
|
|
(int) $network['available_ip_count'],
|
|
|
|
|
'is_source_host' => $hostId === $sourceHostId,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
@ -471,32 +495,33 @@ final class CloudPlacementService
|
|
|
|
|
* public_ip:CloudPublicIp,network:array<string,mixed>}
|
|
|
|
|
*/
|
|
|
|
|
public static function reserveBackupRestore(CloudBackupRestore $restore,
|
|
|
|
|
int $userId, array $resources,
|
|
|
|
|
?int $requiredResourcePoolId = null,
|
|
|
|
|
int $userId, array $resources, int $requiredResourcePoolId,
|
|
|
|
|
int $additionalDiskGib = 0): array
|
|
|
|
|
{
|
|
|
|
|
$resources = self::upgradeResources($resources);
|
|
|
|
|
$capacityResources = $resources;
|
|
|
|
|
$capacityResources['disk_gib'] += max(0, $additionalDiskGib);
|
|
|
|
|
$hostId = (int) $restore->target_host_id;
|
|
|
|
|
$host = CloudHost::where('id', $hostId)->lockForUpdate()->first();
|
|
|
|
|
$memberships = CloudHostResourcePoolMembership::where('host_id', $hostId)
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
$membership = $memberships->first(static fn ($row): bool =>
|
|
|
|
|
(string) $row->scheduler_state === 'active');
|
|
|
|
|
if ($memberships->filter(static fn ($row): bool =>
|
|
|
|
|
(string) $row->scheduler_state === 'active')->count() !== 1) {
|
|
|
|
|
$membership = null;
|
|
|
|
|
}
|
|
|
|
|
$pool = $membership === null ? null
|
|
|
|
|
: CloudResourcePool::where('id', (int) $membership->resource_pool_id)
|
|
|
|
|
if ($hostId < 1 || $requiredResourcePoolId < 1) {
|
|
|
|
|
throw new CloudError('备份恢复目标身份无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$poolRef = CloudResourcePool::find($requiredResourcePoolId);
|
|
|
|
|
$lineRef = $poolRef === null ? null
|
|
|
|
|
: CloudNetworkLine::find((int) $poolRef->line_id);
|
|
|
|
|
$region = $lineRef === null ? null
|
|
|
|
|
: CloudRegion::where('id', (int) $lineRef->region_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$line = $pool === null ? null
|
|
|
|
|
: CloudNetworkLine::where('id', (int) $pool->line_id)
|
|
|
|
|
$line = $lineRef === null ? null
|
|
|
|
|
: CloudNetworkLine::where('id', (int) $lineRef->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$region = $line === null ? null
|
|
|
|
|
: CloudRegion::where('id', (int) $line->region_id)
|
|
|
|
|
$pool = $poolRef === null ? null
|
|
|
|
|
: CloudResourcePool::where('id', (int) $poolRef->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$host = CloudHost::where('id', $hostId)->lockForUpdate()->first();
|
|
|
|
|
$membership = CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id', $hostId
|
|
|
|
|
)->where('resource_pool_id', $requiredResourcePoolId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$policy = CloudHostCapacityPolicy::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$storage = CloudHostStorageConfiguration::where('host_id', $hostId)
|
|
|
|
|
@ -508,32 +533,24 @@ final class CloudPlacementService
|
|
|
|
|
|| $storage === null || !$storage->isApplied()
|
|
|
|
|
|| $agent === null || !$agent->isReady()
|
|
|
|
|
|| !(bool) $host->enabled
|
|
|
|
|
|| (string) $host->default_network !== 'bridge:br0'
|
|
|
|
|
|| (string) $membership->scheduler_state !== 'active'
|
|
|
|
|
|| (int) $membership->resource_pool_id !== $requiredResourcePoolId
|
|
|
|
|
|| (int) $pool->id !== $requiredResourcePoolId
|
|
|
|
|
|| (int) $pool->line_id !== (int) $line->id
|
|
|
|
|
|| (int) $line->region_id !== (int) $region->id
|
|
|
|
|
|| (string) $pool->status !== 'active'
|
|
|
|
|
|| (string) $line->status !== 'active'
|
|
|
|
|
|| (string) $region->status !== 'active') {
|
|
|
|
|
throw new CloudError('所选目标宿主机暂不支持备份恢复', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($requiredResourcePoolId !== null
|
|
|
|
|
&& (int) $pool->id !== $requiredResourcePoolId) {
|
|
|
|
|
throw new CloudError('维护迁移只能选择同一资源池的宿主机', 409);
|
|
|
|
|
$networkCandidates = CloudPlacementNetworkService::lockedCandidates(
|
|
|
|
|
$hostId,
|
|
|
|
|
(int) $line->id,
|
|
|
|
|
$agent
|
|
|
|
|
);
|
|
|
|
|
if ($networkCandidates === []) {
|
|
|
|
|
throw new CloudError('目标宿主机的线路网络接入尚未就绪', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachments = CloudHostNetworkAttachment::where('host_id', $hostId)
|
|
|
|
|
->where('line_id', (int) $line->id)
|
|
|
|
|
->where('network_selector', 'bridge:br0')
|
|
|
|
|
->where('state', 'active')->where('sync_state', 'applied')
|
|
|
|
|
->whereColumn('observed_revision', '>=', 'desired_revision')
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
if ($attachments->isEmpty()) {
|
|
|
|
|
throw new CloudError('目标宿主机的 br0 网络接入尚未就绪', 409);
|
|
|
|
|
}
|
|
|
|
|
$attachmentById = $attachments->keyBy('id');
|
|
|
|
|
$pools = CloudPublicIpPool::where('host_id', $hostId)
|
|
|
|
|
->whereIn('network_attachment_id', $attachments->pluck('id')->all())
|
|
|
|
|
->where('state', 'active')->where('sync_state', 'applied')
|
|
|
|
|
->whereColumn('observed_revision', '>=', 'desired_revision')
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
$reservations = CloudResourceReservation::where('host_id', $hostId)
|
|
|
|
|
->whereIn('state', ['held', 'uncertain', 'committed'])
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
@ -545,35 +562,16 @@ final class CloudPlacementService
|
|
|
|
|
throw new CloudError('目标宿主机没有足够的可用容量', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$selectedPool = null;
|
|
|
|
|
$ip = null;
|
|
|
|
|
foreach ($pools as $candidatePool) {
|
|
|
|
|
if (preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $candidatePool->pool_key) !== 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach (self::availablePublicIpQuery(
|
|
|
|
|
$hostId,
|
|
|
|
|
(int) $candidatePool->id,
|
|
|
|
|
(int) $candidatePool->guest_prefix_length
|
|
|
|
|
)->limit(self::MAX_PUBLIC_IPS_PER_POOL + 1)
|
|
|
|
|
->lockForUpdate()->get() as $candidateIp) {
|
|
|
|
|
if (self::publicIpBelongsToPool($candidateIp, $candidatePool)) {
|
|
|
|
|
$selectedPool = $candidatePool;
|
|
|
|
|
$ip = $candidateIp;
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($selectedPool === null || $ip === null) {
|
|
|
|
|
throw new CloudError('目标宿主机没有可分配的公网 IP', 409);
|
|
|
|
|
}
|
|
|
|
|
$attachment = $attachmentById->get(
|
|
|
|
|
(int) $selectedPool->network_attachment_id
|
|
|
|
|
$network = CloudPlacementNetworkService::lockAvailableSelection(
|
|
|
|
|
$hostId,
|
|
|
|
|
$networkCandidates
|
|
|
|
|
);
|
|
|
|
|
if ($attachment === null) {
|
|
|
|
|
throw new CloudError('目标宿主机公网地址池关系不完整', 409);
|
|
|
|
|
if ($network === null) {
|
|
|
|
|
throw new CloudError('目标宿主机没有可分配的公网 IP', 409);
|
|
|
|
|
}
|
|
|
|
|
$attachment = $network['attachment'];
|
|
|
|
|
$selectedPool = $network['public_pool'];
|
|
|
|
|
$ip = $network['public_ip'];
|
|
|
|
|
$dns = self::poolDnsServers($selectedPool);
|
|
|
|
|
$reservation = new CloudResourceReservation();
|
|
|
|
|
$reservation->reservation_key = bin2hex(random_bytes(16));
|
|
|
|
|
@ -612,7 +610,7 @@ final class CloudPlacementService
|
|
|
|
|
'host' => $host,
|
|
|
|
|
'public_ip' => $ip,
|
|
|
|
|
'network' => [
|
|
|
|
|
'network' => 'bridge:br0',
|
|
|
|
|
'network' => (string) $attachment->network_selector,
|
|
|
|
|
'public_pool_key' => (string) $selectedPool->pool_key,
|
|
|
|
|
'public_pool_cidr' => (string) $selectedPool->cidr,
|
|
|
|
|
'public_ip_cidr' => (string) $ip->address . '/'
|
|
|
|
|
@ -661,13 +659,27 @@ final class CloudPlacementService
|
|
|
|
|
'id',
|
|
|
|
|
(int) $reservation->network_attachment_id
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$resourcePool = $reservation === null ? null
|
|
|
|
|
: CloudResourcePool::find((int) $reservation->resource_pool_id);
|
|
|
|
|
$membership = $reservation === null ? null
|
|
|
|
|
: CloudHostResourcePoolMembership::where(
|
|
|
|
|
'host_id',
|
|
|
|
|
(int) $reservation->host_id
|
|
|
|
|
)->where(
|
|
|
|
|
'resource_pool_id',
|
|
|
|
|
(int) $reservation->resource_pool_id
|
|
|
|
|
)->first();
|
|
|
|
|
if ($reservation === null || $ip === null || $pool === null
|
|
|
|
|
|| $attachment === null
|
|
|
|
|
|| $attachment === null || $resourcePool === null
|
|
|
|
|
|| $membership === null
|
|
|
|
|
|| (int) $restore->reservation_id !== (int) $reservation->id
|
|
|
|
|
|| (int) $restore->target_host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $reservation->backup_restore_id !== (int) $restore->id
|
|
|
|
|
|| (int) $reservation->instance_id !== $instanceId
|
|
|
|
|
|| (string) $reservation->state !== 'held'
|
|
|
|
|
|| (string) $membership->scheduler_state !== 'active'
|
|
|
|
|
|| (int) $attachment->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $attachment->line_id !== (int) $resourcePool->line_id
|
|
|
|
|
|| (int) $pool->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| (int) $pool->network_attachment_id !== (int) $attachment->id
|
|
|
|
|
|| (int) $ip->host_id !== (int) $reservation->host_id
|
|
|
|
|
@ -685,7 +697,9 @@ final class CloudPlacementService
|
|
|
|
|
)
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $pool->pool_key) !== 1
|
|
|
|
|
|| (string) $attachment->network_selector !== 'bridge:br0') {
|
|
|
|
|
|| !CloudNetworkSelector::isValid(
|
|
|
|
|
(string) $attachment->network_selector
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudRecoveryInstanceStateConflict(
|
|
|
|
|
'维护迁移目标预留在源实例停机后发生变化',
|
|
|
|
|
409
|
|
|
|
|
@ -694,7 +708,7 @@ final class CloudPlacementService
|
|
|
|
|
return [
|
|
|
|
|
'reservation' => $reservation,
|
|
|
|
|
'network' => [
|
|
|
|
|
'network' => 'bridge:br0',
|
|
|
|
|
'network' => (string) $attachment->network_selector,
|
|
|
|
|
'public_pool_key' => (string) $pool->pool_key,
|
|
|
|
|
'public_pool_cidr' => (string) $pool->cidr,
|
|
|
|
|
'public_ip_cidr' => (string) $ip->address . '/'
|
|
|
|
|
|