|
|
|
|
@ -478,14 +478,32 @@ final class CloudPlacementService
|
|
|
|
|
]);
|
|
|
|
|
$pendingByHost = self::pendingUsageByHost($candidates);
|
|
|
|
|
$networkByHostLine = CloudPlacementNetworkService::
|
|
|
|
|
advisoryAvailableHostLineDetails($candidates);
|
|
|
|
|
advisoryDualStackBundleEvidence($candidates);
|
|
|
|
|
$result = [];
|
|
|
|
|
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
|
|
|
|
|
$network = $networkByHostLine[$hostId][$lineId] ?? [];
|
|
|
|
|
$availableBundles = 0;
|
|
|
|
|
$selectors = [];
|
|
|
|
|
foreach ($network as $bundle) {
|
|
|
|
|
if (!is_array($bundle)
|
|
|
|
|
|| !CloudNetworkSelector::isValid(
|
|
|
|
|
$bundle['network_selector'] ?? null
|
|
|
|
|
)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$count = min(
|
|
|
|
|
(int) ($bundle['ipv4']['available_units'] ?? 0),
|
|
|
|
|
(int) ($bundle['ipv6']['available_units'] ?? 0)
|
|
|
|
|
);
|
|
|
|
|
if ($count < 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$availableBundles += $count;
|
|
|
|
|
$selectors[(string) $bundle['network_selector']] = true;
|
|
|
|
|
}
|
|
|
|
|
if ($availableBundles < 1
|
|
|
|
|
|| !self::hasCapacity(
|
|
|
|
|
$candidate,
|
|
|
|
|
$pendingByHost[$hostId] ?? self::emptyUsage(),
|
|
|
|
|
@ -507,10 +525,10 @@ final class CloudPlacementService
|
|
|
|
|
(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'],
|
|
|
|
|
'network_selectors' => array_keys($selectors),
|
|
|
|
|
// Keep the presentation key while changing its unit from a
|
|
|
|
|
// lone IPv4 row to one complete IPv4+IPv6 bundle.
|
|
|
|
|
'available_ip_count' => $availableBundles,
|
|
|
|
|
'is_source_host' => $hostId === $sourceHostId,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
@ -538,6 +556,153 @@ final class CloudPlacementService
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reserve one exact dual-stack target for a still-gated maintenance
|
|
|
|
|
* migration. Legacy singular address columns deliberately remain NULL;
|
|
|
|
|
* the two active claim rows are the only network ownership authority.
|
|
|
|
|
*
|
|
|
|
|
* @return array{reservation:CloudResourceReservation,host:CloudHost,
|
|
|
|
|
* public_network:CloudPublicNetworkSnapshot,
|
|
|
|
|
* target_agent_revision:int}
|
|
|
|
|
*/
|
|
|
|
|
public static function reserveMaintenanceMigrationTarget(
|
|
|
|
|
CloudBackupRestore $restore,
|
|
|
|
|
int $userId,
|
|
|
|
|
array $resources,
|
|
|
|
|
int $requiredResourcePoolId,
|
|
|
|
|
int $additionalDiskGib = 0
|
|
|
|
|
): array {
|
|
|
|
|
if ($restore->backup_id !== null
|
|
|
|
|
|| (string) $restore->state !== 'reserved'
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $restore->operation_id) !== 1) {
|
|
|
|
|
throw new CloudError('维护迁移目标意图无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$resources = self::upgradeResources($resources);
|
|
|
|
|
$capacityResources = $resources;
|
|
|
|
|
$capacityResources['disk_gib'] += max(0, $additionalDiskGib);
|
|
|
|
|
$hostId = (int) $restore->target_host_id;
|
|
|
|
|
if ($hostId < 1 || $userId < 1 || $requiredResourcePoolId < 1) {
|
|
|
|
|
throw new CloudError('维护迁移目标身份无效', 409);
|
|
|
|
|
}
|
|
|
|
|
return (new CloudResourceReservation())->getConnection()->transaction(
|
|
|
|
|
static function () use (
|
|
|
|
|
$restore,
|
|
|
|
|
$userId,
|
|
|
|
|
$resources,
|
|
|
|
|
$capacityResources,
|
|
|
|
|
$hostId,
|
|
|
|
|
$requiredResourcePoolId
|
|
|
|
|
): array {
|
|
|
|
|
$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 = $lineRef === null ? null
|
|
|
|
|
: CloudNetworkLine::where('id', (int) $lineRef->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$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
|
|
|
|
|
)->lockForUpdate()->first();
|
|
|
|
|
$agent = CloudHostAgent::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($host === null || $membership === null || $pool === null
|
|
|
|
|
|| $line === null || $region === null || $policy === null
|
|
|
|
|
|| $storage === null || !$storage->isApplied()
|
|
|
|
|
|| $agent === null || !$agent->isReady()
|
|
|
|
|
|| !(bool) $host->enabled
|
|
|
|
|
|| (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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$reservations = CloudResourceReservation::where(
|
|
|
|
|
'host_id',
|
|
|
|
|
$hostId
|
|
|
|
|
)->whereIn('state', ['held', 'uncertain', 'committed'])
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
if (!self::hasCapacity(
|
|
|
|
|
$policy,
|
|
|
|
|
CloudPanelUsageService::forHost($hostId, $reservations),
|
|
|
|
|
$capacityResources
|
|
|
|
|
)) {
|
|
|
|
|
throw new CloudError('目标宿主机没有足够的可用容量', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reservation = new CloudResourceReservation();
|
|
|
|
|
$reservation->reservation_key = bin2hex(random_bytes(16));
|
|
|
|
|
$reservation->operation_id = (string) $restore->operation_id;
|
|
|
|
|
$reservation->order_id = null;
|
|
|
|
|
$reservation->backup_restore_id = (int) $restore->id;
|
|
|
|
|
$reservation->backup_restore_attempt_key = null;
|
|
|
|
|
$reservation->migration_source_debt_id = null;
|
|
|
|
|
$reservation->offer_id = null;
|
|
|
|
|
$reservation->resource_pool_id = (int) $pool->id;
|
|
|
|
|
$reservation->host_id = $hostId;
|
|
|
|
|
$reservation->user_id = $userId;
|
|
|
|
|
$reservation->instance_id = null;
|
|
|
|
|
$reservation->network_attachment_id = null;
|
|
|
|
|
$reservation->public_ip_pool_id = null;
|
|
|
|
|
$reservation->public_ip_id = null;
|
|
|
|
|
$reservation->cpu_cores = $resources['cpu_cores'];
|
|
|
|
|
$reservation->memory_mib = $resources['memory_mib'];
|
|
|
|
|
$reservation->disk_gib = $capacityResources['disk_gib'];
|
|
|
|
|
$reservation->state = 'held';
|
|
|
|
|
$reservation->expires_at = null;
|
|
|
|
|
$reservation->released_at = null;
|
|
|
|
|
$reservation->release_reason = '';
|
|
|
|
|
$reservation->created_at =
|
|
|
|
|
$reservation->updated_at = time();
|
|
|
|
|
$reservation->save();
|
|
|
|
|
try {
|
|
|
|
|
$publicNetwork = CloudPublicNetworkAllocationService::
|
|
|
|
|
reserveBundle(
|
|
|
|
|
$hostId,
|
|
|
|
|
(int) $line->id,
|
|
|
|
|
(int) $reservation->id,
|
|
|
|
|
(string) $restore->operation_id
|
|
|
|
|
);
|
|
|
|
|
} catch (CloudPublicNetworkCapacityUnavailable) {
|
|
|
|
|
throw new CloudError(
|
|
|
|
|
'目标宿主机没有可分配的双栈公网地址',
|
|
|
|
|
409
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'reservation' => $reservation,
|
|
|
|
|
'host' => $host,
|
|
|
|
|
'public_network' => $publicNetwork,
|
|
|
|
|
'target_agent_revision' => (int) $agent->revision,
|
|
|
|
|
];
|
|
|
|
|
},
|
|
|
|
|
3
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reserve target capacity and a fresh IPv4 for an administrator restore.
|
|
|
|
|
* The restore row and operation ID are created before this method runs.
|
|
|
|
|
|