|
|
|
|
@ -5,10 +5,13 @@ namespace App\Services\Cloud;
|
|
|
|
|
use App\Models\CloudHost;
|
|
|
|
|
use App\Models\CloudHostCapacityPolicy;
|
|
|
|
|
use App\Models\CloudHostCapacitySnapshot;
|
|
|
|
|
use App\Models\CloudHostNetworkAttachment;
|
|
|
|
|
use App\Models\CloudHostResourcePoolMembership;
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Models\CloudNetworkLine;
|
|
|
|
|
use App\Models\CloudOffer;
|
|
|
|
|
use App\Models\CloudPublicIp;
|
|
|
|
|
use App\Models\CloudPublicIpPool;
|
|
|
|
|
use App\Models\CloudRegion;
|
|
|
|
|
use App\Models\CloudResourcePool;
|
|
|
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
|
@ -26,6 +29,7 @@ use App\Services\Config;
|
|
|
|
|
final class CloudPlacementService
|
|
|
|
|
{
|
|
|
|
|
private const GIB = 1073741824;
|
|
|
|
|
private const MAX_PUBLIC_IPS_PER_POOL = 254;
|
|
|
|
|
private const ACTIVE_RESERVATIONS = ['held', 'uncertain'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -82,15 +86,7 @@ final class CloudPlacementService
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$pendingByHost = self::pendingUsageByHost($candidateRows);
|
|
|
|
|
$availableIpHosts = array_fill_keys(
|
|
|
|
|
CloudPublicIp::query()->whereIn('host_id',
|
|
|
|
|
$candidateRows->pluck('id')->all())
|
|
|
|
|
->where('state', 'available')->distinct()
|
|
|
|
|
->pluck('host_id')->map(
|
|
|
|
|
static fn ($id): int => (int) $id
|
|
|
|
|
)->all(),
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
$availableIpHosts = self::availableIpHosts($candidateRows);
|
|
|
|
|
foreach ($poolRequests as $key => $resources) {
|
|
|
|
|
foreach ($candidateRows as $candidate) {
|
|
|
|
|
$hostId = (int) $candidate->id;
|
|
|
|
|
@ -191,6 +187,7 @@ final class CloudPlacementService
|
|
|
|
|
$ip = CloudPublicIp::where('id', (int) $reservation->public_ip_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($ip === null || (int) $ip->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| !self::reservationNetworkMatches($reservation, $ip)
|
|
|
|
|
|| (int) ($ip->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))
|
|
|
|
|
@ -238,6 +235,7 @@ final class CloudPlacementService
|
|
|
|
|
$ip = CloudPublicIp::where('id', (int) $reservation->public_ip_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($ip === null || (int) $ip->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| !self::reservationNetworkMatches($reservation, $ip)
|
|
|
|
|
|| (int) ($ip->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))) {
|
|
|
|
|
@ -281,6 +279,7 @@ final class CloudPlacementService
|
|
|
|
|
$ip = CloudPublicIp::where('id', (int) $reservation->public_ip_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($ip === null || (int) $ip->host_id !== (int) $reservation->host_id
|
|
|
|
|
|| !self::reservationNetworkMatches($reservation, $ip)
|
|
|
|
|
|| (int) ($ip->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| !hash_equals((string) $reservation->operation_id,
|
|
|
|
|
(string) ($ip->operation_id ?? ''))
|
|
|
|
|
@ -318,22 +317,55 @@ final class CloudPlacementService
|
|
|
|
|
|
|
|
|
|
/** Atomically settle the PHP-side address ledger for a network-change job. */
|
|
|
|
|
public static function finishNetworkChange(CloudInstance $instance,
|
|
|
|
|
string $operationId, int $sourceIpId, int $targetIpId, string $outcome): void
|
|
|
|
|
string $operationId, int $sourceIpId, int $targetIpId,
|
|
|
|
|
int $networkAttachmentId, int $sourcePoolId, int $targetPoolId,
|
|
|
|
|
string $outcome): void
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($outcome, ['succeeded', 'failed', 'uncertain'], true)
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1
|
|
|
|
|
|| $sourceIpId < 1 || $targetIpId < 1 || $sourceIpId === $targetIpId) {
|
|
|
|
|
|| min($sourceIpId, $targetIpId, $networkAttachmentId,
|
|
|
|
|
$sourcePoolId, $targetPoolId) < 1
|
|
|
|
|
|| $sourceIpId === $targetIpId) {
|
|
|
|
|
throw new CloudError('公网 IP 变更账本参数无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$host = CloudHost::where('id', (int) $instance->host_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
|
|
|
|
|
// Host is the sentinel for every host-scoped writer. Lock attachment
|
|
|
|
|
// and pool rows in numeric order before the reservation/IP ledger so
|
|
|
|
|
// placement and network changes cannot invert their lock hierarchy.
|
|
|
|
|
$attachment = CloudHostNetworkAttachment::where('id', $networkAttachmentId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$poolIds = array_values(array_unique([$sourcePoolId, $targetPoolId]));
|
|
|
|
|
sort($poolIds, SORT_NUMERIC);
|
|
|
|
|
$pools = CloudPublicIpPool::whereIn('id', $poolIds)->orderBy('id')
|
|
|
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
|
|
|
$sourcePool = $pools->get($sourcePoolId);
|
|
|
|
|
$targetPool = $pools->get($targetPoolId);
|
|
|
|
|
$reservation = $instance->reservation_id === null ? null
|
|
|
|
|
: CloudResourceReservation::where('id', (int) $instance->reservation_id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($host === null || $reservation === null
|
|
|
|
|
$reservationPool = $reservation === null ? null
|
|
|
|
|
: CloudResourcePool::find((int) $reservation->resource_pool_id);
|
|
|
|
|
if ($host === null || $attachment === null
|
|
|
|
|
|| $sourcePool === null || $targetPool === null
|
|
|
|
|
|| $reservationPool === null
|
|
|
|
|
|| (int) $attachment->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $reservation->resource_pool_id
|
|
|
|
|
!== (int) ($instance->resource_pool_id ?? 0)
|
|
|
|
|
|| (int) $attachment->line_id !== (int) $reservationPool->line_id
|
|
|
|
|
|| (int) $sourcePool->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $targetPool->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $sourcePool->network_attachment_id !== $networkAttachmentId
|
|
|
|
|
|| (int) $targetPool->network_attachment_id !== $networkAttachmentId
|
|
|
|
|
|| preg_match('/^[a-f0-9]{32}$/D', (string) $targetPool->pool_key) !== 1
|
|
|
|
|
|| $reservation === null
|
|
|
|
|
|| (string) $reservation->state !== 'committed'
|
|
|
|
|
|| (int) $reservation->host_id !== (int) $host->id
|
|
|
|
|
|| (int) ($reservation->instance_id ?? 0) !== (int) $instance->id
|
|
|
|
|
|| (int) ($reservation->network_attachment_id ?? 0)
|
|
|
|
|
!== $networkAttachmentId
|
|
|
|
|
|| (int) ($reservation->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|
|
|
|| (int) $reservation->public_ip_id !== $sourceIpId) {
|
|
|
|
|
throw new CloudError('虚拟机公网 IP 资源预留不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
@ -347,6 +379,8 @@ final class CloudPlacementService
|
|
|
|
|
if ($source === null || $target === null
|
|
|
|
|
|| (int) $source->host_id !== (int) $host->id
|
|
|
|
|
|| (int) $target->host_id !== (int) $host->id
|
|
|
|
|
|| (int) ($source->public_ip_pool_id ?? 0) !== $sourcePoolId
|
|
|
|
|
|| (int) ($target->public_ip_pool_id ?? 0) !== $targetPoolId
|
|
|
|
|
|| (string) $source->state !== 'assigned'
|
|
|
|
|
|| (int) ($source->reservation_id ?? 0) !== (int) $reservation->id
|
|
|
|
|
|| (int) ($source->instance_id ?? 0) !== (int) $instance->id
|
|
|
|
|
@ -358,6 +392,11 @@ final class CloudPlacementService
|
|
|
|
|
|| !hash_equals($operationId, (string) ($target->operation_id ?? ''))) {
|
|
|
|
|
throw new CloudError('公网 IP 变更账本关联不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
if ($outcome === 'succeeded'
|
|
|
|
|
&& (!self::publicIpBelongsToPool($source, $sourcePool)
|
|
|
|
|
|| !self::publicIpBelongsToPool($target, $targetPool))) {
|
|
|
|
|
throw new CloudError('公网 IP 变更结果与地址池账本不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$now = time();
|
|
|
|
|
if ($outcome === 'uncertain') {
|
|
|
|
|
@ -386,7 +425,8 @@ final class CloudPlacementService
|
|
|
|
|
$source->save();
|
|
|
|
|
|
|
|
|
|
$reservation->public_ip_id = $targetIpId;
|
|
|
|
|
$reservation->updated_at = $now;
|
|
|
|
|
$reservation->network_attachment_id = $networkAttachmentId;
|
|
|
|
|
$reservation->public_ip_pool_id = $targetPoolId;
|
|
|
|
|
$reservation->save();
|
|
|
|
|
|
|
|
|
|
$target->state = 'assigned';
|
|
|
|
|
@ -405,20 +445,59 @@ final class CloudPlacementService
|
|
|
|
|
// placement, inventory refresh and administrator policy changes before
|
|
|
|
|
// any snapshot or concrete IP row is locked.
|
|
|
|
|
$host = CloudHost::where('id', $hostId)->lockForUpdate()->first();
|
|
|
|
|
$membership = CloudHostResourcePoolMembership::where('host_id', $hostId)
|
|
|
|
|
->where('resource_pool_id', (int) $pool->id)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$policy = CloudHostCapacityPolicy::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
$snapshot = CloudHostCapacitySnapshot::where('host_id', $hostId)
|
|
|
|
|
->lockForUpdate()->first();
|
|
|
|
|
if ($policy === null || $host === null || $snapshot === null
|
|
|
|
|
|| (int) $host->resource_pool_id !== (int) $pool->id
|
|
|
|
|
if ($host === null || $membership === null || $policy === null
|
|
|
|
|
|| $snapshot === null
|
|
|
|
|
|| (string) $membership->scheduler_state !== 'active'
|
|
|
|
|
|| !(bool) $host->enabled
|
|
|
|
|
|| (string) $host->scheduler_state !== 'active'
|
|
|
|
|
|| (string) $host->health_state !== 'healthy'
|
|
|
|
|
|| (string) $snapshot->sync_state !== 'fresh'
|
|
|
|
|
|| (int) $snapshot->last_attempt_at < time() - self::freshnessSeconds()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attachments = CloudHostNetworkAttachment::where('host_id', $hostId)
|
|
|
|
|
->where('line_id', (int) $pool->line_id)
|
|
|
|
|
->where('network_selector', (string) $host->default_network)
|
|
|
|
|
->where('state', 'active')->where('sync_state', 'fresh')
|
|
|
|
|
->whereColumn('observed_revision', '>=', 'desired_revision')
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
if ($attachments->isEmpty()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$attachmentById = $attachments->keyBy('id');
|
|
|
|
|
$publicPools = CloudPublicIpPool::where('host_id', $hostId)
|
|
|
|
|
->whereIn('network_attachment_id', $attachments->pluck('id')->all())
|
|
|
|
|
->where('state', 'active')->where('sync_state', 'fresh')
|
|
|
|
|
->whereColumn('observed_revision', '>=', 'desired_revision')
|
|
|
|
|
// Lock pool rows in primary-key order. Selection preference is
|
|
|
|
|
// applied in memory only after the complete row set is locked.
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get();
|
|
|
|
|
$eligiblePools = [];
|
|
|
|
|
foreach ($publicPools as $publicPool) {
|
|
|
|
|
if (preg_match('/^[a-f0-9]{32}$/D',
|
|
|
|
|
(string) $publicPool->pool_key) === 1) {
|
|
|
|
|
$eligiblePools[] = $publicPool;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
usort($eligiblePools, static function ($left, $right) use ($attachmentById): int {
|
|
|
|
|
$leftAttachment = $attachmentById->get((int) $left->network_attachment_id);
|
|
|
|
|
$rightAttachment = $attachmentById->get((int) $right->network_attachment_id);
|
|
|
|
|
return ((int) $leftAttachment->priority <=> (int) $rightAttachment->priority)
|
|
|
|
|
?: ((int) $left->priority <=> (int) $right->priority)
|
|
|
|
|
?: ((int) $leftAttachment->id <=> (int) $rightAttachment->id)
|
|
|
|
|
?: ((int) $left->id <=> (int) $right->id);
|
|
|
|
|
});
|
|
|
|
|
if ($eligiblePools === []) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This must be a current locking read, not an aggregate consistent
|
|
|
|
|
// read. Under InnoDB REPEATABLE READ a transaction may have opened its
|
|
|
|
|
// snapshot while waiting for the host sentinel; a normal SUM could
|
|
|
|
|
@ -439,10 +518,33 @@ final class CloudPlacementService
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ip = self::availablePublicIpQuery($hostId)->lockForUpdate()->first();
|
|
|
|
|
if ($ip === null) {
|
|
|
|
|
$ip = null;
|
|
|
|
|
$selectedPool = null;
|
|
|
|
|
foreach ($eligiblePools as $publicPool) {
|
|
|
|
|
$candidateIps = self::availablePublicIpQuery(
|
|
|
|
|
$hostId, (int) $publicPool->id,
|
|
|
|
|
(int) $publicPool->guest_prefix_length
|
|
|
|
|
)->limit(self::MAX_PUBLIC_IPS_PER_POOL + 1)->lockForUpdate()->get();
|
|
|
|
|
if ($candidateIps->count() > self::MAX_PUBLIC_IPS_PER_POOL) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ($candidateIps as $candidateIp) {
|
|
|
|
|
if (self::publicIpBelongsToPool($candidateIp, $publicPool)) {
|
|
|
|
|
$ip = $candidateIp;
|
|
|
|
|
$selectedPool = $publicPool;
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($ip === null || $selectedPool === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$selectedAttachment = $attachmentById->get(
|
|
|
|
|
(int) $selectedPool->network_attachment_id
|
|
|
|
|
);
|
|
|
|
|
if ($selectedAttachment === null) {
|
|
|
|
|
throw new CloudError('公网地址池与网络接入关联不一致', 409);
|
|
|
|
|
}
|
|
|
|
|
$operationId = bin2hex(random_bytes(16));
|
|
|
|
|
$reservation = new CloudResourceReservation();
|
|
|
|
|
$reservation->reservation_key = bin2hex(random_bytes(16));
|
|
|
|
|
@ -453,6 +555,8 @@ final class CloudPlacementService
|
|
|
|
|
$reservation->host_id = $hostId;
|
|
|
|
|
$reservation->user_id = $order->user_id;
|
|
|
|
|
$reservation->instance_id = null;
|
|
|
|
|
$reservation->network_attachment_id = $selectedAttachment->id;
|
|
|
|
|
$reservation->public_ip_pool_id = $selectedPool->id;
|
|
|
|
|
$reservation->public_ip_id = $ip->id;
|
|
|
|
|
$reservation->cpu_cores = $resources['cpu_cores'];
|
|
|
|
|
$reservation->memory_mib = $resources['memory_mib'];
|
|
|
|
|
@ -481,11 +585,14 @@ final class CloudPlacementService
|
|
|
|
|
'host' => $host,
|
|
|
|
|
'reservation' => $reservation,
|
|
|
|
|
'public_ip' => $ip,
|
|
|
|
|
'payload' => self::placementPayload($payload, $host, $ip),
|
|
|
|
|
'payload' => self::placementPayload(
|
|
|
|
|
$payload, $selectedAttachment, $selectedPool, $ip
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function placementPayload(array $payload, CloudHost $host,
|
|
|
|
|
private static function placementPayload(array $payload,
|
|
|
|
|
CloudHostNetworkAttachment $attachment, CloudPublicIpPool $publicPool,
|
|
|
|
|
CloudPublicIp $ip): array
|
|
|
|
|
{
|
|
|
|
|
if (($payload['method'] ?? null) !== 'POST'
|
|
|
|
|
@ -493,7 +600,8 @@ final class CloudPlacementService
|
|
|
|
|
|| !is_array($payload['body'] ?? null)) {
|
|
|
|
|
throw new CloudError('开通订单的加密请求无效', 409);
|
|
|
|
|
}
|
|
|
|
|
$payload['body']['network'] = (string) $host->default_network;
|
|
|
|
|
$payload['body']['network'] = (string) $attachment->network_selector;
|
|
|
|
|
$payload['body']['public_pool_key'] = (string) $publicPool->pool_key;
|
|
|
|
|
$payload['body']['public_ip_cidr'] = (string) $ip->address . '/'
|
|
|
|
|
. (int) $ip->prefix_length;
|
|
|
|
|
// For a configured host pool the engine owns these trusted values.
|
|
|
|
|
@ -577,22 +685,31 @@ final class CloudPlacementService
|
|
|
|
|
{
|
|
|
|
|
$freshAfter = time() - self::freshnessSeconds();
|
|
|
|
|
return CloudHost::query()->join(
|
|
|
|
|
'cloud_host_resource_pool_memberships as pool_membership',
|
|
|
|
|
'pool_membership.host_id', '=', 'cloud_hosts.id'
|
|
|
|
|
)->join(
|
|
|
|
|
'cloud_resource_pools as membership_pool',
|
|
|
|
|
'membership_pool.id', '=', 'pool_membership.resource_pool_id'
|
|
|
|
|
)->join(
|
|
|
|
|
'cloud_host_capacity_policies as capacity_policy',
|
|
|
|
|
'capacity_policy.host_id', '=', 'cloud_hosts.id'
|
|
|
|
|
)->join(
|
|
|
|
|
'cloud_host_capacity_snapshots as capacity_snapshot',
|
|
|
|
|
'capacity_snapshot.host_id', '=', 'cloud_hosts.id'
|
|
|
|
|
)->where('cloud_hosts.resource_pool_id', $poolId)
|
|
|
|
|
)->where('pool_membership.resource_pool_id', $poolId)
|
|
|
|
|
->where('pool_membership.scheduler_state', 'active')
|
|
|
|
|
->where('cloud_hosts.enabled', 1)
|
|
|
|
|
->where('cloud_hosts.scheduler_state', 'active')
|
|
|
|
|
->where('cloud_hosts.health_state', 'healthy')
|
|
|
|
|
->where('capacity_snapshot.sync_state', 'fresh')
|
|
|
|
|
->where('capacity_snapshot.last_attempt_at', '>=', $freshAfter)
|
|
|
|
|
->where('capacity_policy.configured_cpu_cores', '>', 0)
|
|
|
|
|
->where('capacity_policy.configured_memory_mib', '>', 0)
|
|
|
|
|
->where('capacity_policy.configured_disk_gib', '>', 0)
|
|
|
|
|
->distinct()
|
|
|
|
|
->get([
|
|
|
|
|
'cloud_hosts.id',
|
|
|
|
|
'cloud_hosts.default_network',
|
|
|
|
|
'membership_pool.line_id as resource_pool_line_id',
|
|
|
|
|
'capacity_policy.configured_cpu_cores',
|
|
|
|
|
'capacity_policy.configured_memory_mib',
|
|
|
|
|
'capacity_policy.configured_disk_gib',
|
|
|
|
|
@ -650,6 +767,71 @@ final class CloudPlacementService
|
|
|
|
|
return $usage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return hosts with at least one concrete address in an eligible pool.
|
|
|
|
|
*
|
|
|
|
|
* This remains an advisory, non-locking read. The payment path repeats
|
|
|
|
|
* every predicate under the host, attachment, pool and IP row locks.
|
|
|
|
|
*
|
|
|
|
|
* @return array<int,bool>
|
|
|
|
|
*/
|
|
|
|
|
private static function availableIpHosts($candidateRows): array
|
|
|
|
|
{
|
|
|
|
|
$hostIds = $candidateRows->pluck('id')->map(
|
|
|
|
|
static fn ($id): int => (int) $id
|
|
|
|
|
)->unique()->values()->all();
|
|
|
|
|
if ($hostIds === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$lineId = (int) ($candidateRows->first()->resource_pool_line_id ?? 0);
|
|
|
|
|
if ($lineId < 1) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$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 eligible_host',
|
|
|
|
|
'eligible_host.id', '=', 'cloud_public_ips.host_id'
|
|
|
|
|
)->whereIn('cloud_public_ips.host_id', $hostIds)
|
|
|
|
|
->where('cloud_public_ips.state', 'available')
|
|
|
|
|
->whereNull('cloud_public_ips.reservation_id')
|
|
|
|
|
->whereNull('cloud_public_ips.instance_id')
|
|
|
|
|
->whereNull('cloud_public_ips.operation_id')
|
|
|
|
|
->whereColumn('cloud_public_ips.host_id', 'public_pool.host_id')
|
|
|
|
|
->whereColumn('cloud_public_ips.prefix_length',
|
|
|
|
|
'public_pool.guest_prefix_length')
|
|
|
|
|
->whereColumn('cloud_public_ips.host_id', 'network_attachment.host_id')
|
|
|
|
|
->whereColumn('network_attachment.network_selector',
|
|
|
|
|
'eligible_host.default_network')
|
|
|
|
|
->where('network_attachment.line_id', $lineId)
|
|
|
|
|
->where('network_attachment.state', 'active')
|
|
|
|
|
->where('network_attachment.sync_state', 'fresh')
|
|
|
|
|
->whereColumn('network_attachment.observed_revision', '>=',
|
|
|
|
|
'network_attachment.desired_revision')
|
|
|
|
|
->where('public_pool.state', 'active')
|
|
|
|
|
->where('public_pool.sync_state', 'fresh')
|
|
|
|
|
->whereColumn('public_pool.observed_revision', '>=',
|
|
|
|
|
'public_pool.desired_revision')
|
|
|
|
|
->distinct()->get([
|
|
|
|
|
'cloud_public_ips.host_id', 'cloud_public_ips.address',
|
|
|
|
|
'public_pool.pool_key', 'public_pool.cidr',
|
|
|
|
|
]);
|
|
|
|
|
$available = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
if (preg_match('/^[a-f0-9]{32}$/D', (string) $row->pool_key) === 1
|
|
|
|
|
&& self::ipv4BelongsToCidr(
|
|
|
|
|
(string) $row->address, (string) $row->cidr
|
|
|
|
|
)) {
|
|
|
|
|
$available[(int) $row->host_id] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $available;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function reservationUsage($reservationRows,
|
|
|
|
|
int $lastAttemptAt): array
|
|
|
|
|
{
|
|
|
|
|
@ -734,10 +916,75 @@ final class CloudPlacementService
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function availablePublicIpQuery(int $hostId)
|
|
|
|
|
private static function availablePublicIpQuery(int $hostId, int $publicPoolId,
|
|
|
|
|
int $prefixLength)
|
|
|
|
|
{
|
|
|
|
|
return CloudPublicIp::query()->where('host_id', $hostId)
|
|
|
|
|
->where('state', 'available')->orderBy('id');
|
|
|
|
|
->where('public_ip_pool_id', $publicPoolId)
|
|
|
|
|
->where('prefix_length', $prefixLength)
|
|
|
|
|
->where('state', 'available')
|
|
|
|
|
->whereNull('reservation_id')->whereNull('instance_id')
|
|
|
|
|
->whereNull('operation_id')->orderBy('id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Nullable identities keep pre-migration in-flight jobs operable. A row
|
|
|
|
|
* that has started adopting the new identity must be complete and exact.
|
|
|
|
|
*/
|
|
|
|
|
private static function reservationNetworkMatches(
|
|
|
|
|
CloudResourceReservation $reservation, CloudPublicIp $ip): bool
|
|
|
|
|
{
|
|
|
|
|
if ($reservation->network_attachment_id === null
|
|
|
|
|
&& $reservation->public_ip_pool_id === null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return (int) ($reservation->network_attachment_id ?? 0) > 0
|
|
|
|
|
&& (int) ($reservation->public_ip_pool_id ?? 0) > 0
|
|
|
|
|
&& (int) ($ip->public_ip_pool_id ?? 0)
|
|
|
|
|
=== (int) $reservation->public_ip_pool_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function publicIpBelongsToPool(CloudPublicIp $ip,
|
|
|
|
|
CloudPublicIpPool $pool): bool
|
|
|
|
|
{
|
|
|
|
|
if ((int) $ip->prefix_length !== (int) $pool->guest_prefix_length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return self::ipv4BelongsToCidr(
|
|
|
|
|
(string) $ip->address, (string) $pool->cidr
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipv4BelongsToCidr(string $addressValue,
|
|
|
|
|
string $cidr): bool
|
|
|
|
|
{
|
|
|
|
|
$parts = explode('/', $cidr, 2);
|
|
|
|
|
if (count($parts) !== 2 || !ctype_digit($parts[1])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$prefix = (int) $parts[1];
|
|
|
|
|
$address = self::ipv4Number($addressValue);
|
|
|
|
|
$network = self::ipv4Number($parts[0]);
|
|
|
|
|
if ($address === null || $network === null || $prefix < 24 || $prefix > 30) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$block = 2 ** (32 - $prefix);
|
|
|
|
|
$base = (int) (floor($network / $block) * $block);
|
|
|
|
|
return $address > $base && $address < $base + $block - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function ipv4Number(string $value): ?int
|
|
|
|
|
{
|
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$packed = inet_pton($value);
|
|
|
|
|
if ($packed === false || strlen($packed) !== 4) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$parts = unpack('Nnumber', $packed);
|
|
|
|
|
return is_array($parts) && isset($parts['number'])
|
|
|
|
|
? (int) sprintf('%u', $parts['number']) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function freshnessSeconds(): int
|
|
|
|
|
|