|
|
|
|
@ -28,6 +28,85 @@ final class CloudPlacementService
|
|
|
|
|
private const GIB = 1073741824;
|
|
|
|
|
private const ACTIVE_RESERVATIONS = ['held', 'uncertain'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Advisory capacity check for an already-authorized active pool chain.
|
|
|
|
|
*
|
|
|
|
|
* Catalog construction has already joined and filtered the offer, plan,
|
|
|
|
|
* pool, line and region. Keeping this method resource-only avoids loading
|
|
|
|
|
* those models once per public offer while sharing the scheduler's exact
|
|
|
|
|
* host, reservation and address predicates.
|
|
|
|
|
*/
|
|
|
|
|
public static function isPoolCapacityAvailable(int $poolId, int $cpuCores,
|
|
|
|
|
int $memoryMib, int $diskGib): bool
|
|
|
|
|
{
|
|
|
|
|
$availability = self::poolCapacityAvailability(['single' => [
|
|
|
|
|
'pool_id' => $poolId,
|
|
|
|
|
'cpu_cores' => $cpuCores,
|
|
|
|
|
'memory_mib' => $memoryMib,
|
|
|
|
|
'disk_gib' => $diskGib,
|
|
|
|
|
]]);
|
|
|
|
|
return $availability['single'] ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Batch advisory availability without locks or resource reservation.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string,array{pool_id:int,cpu_cores:int,memory_mib:int,
|
|
|
|
|
* disk_gib:int}> $requests
|
|
|
|
|
* @return array<string,bool>
|
|
|
|
|
*/
|
|
|
|
|
public static function poolCapacityAvailability(array $requests): array
|
|
|
|
|
{
|
|
|
|
|
$availability = [];
|
|
|
|
|
$byPool = [];
|
|
|
|
|
foreach ($requests as $key => $resources) {
|
|
|
|
|
$availability[$key] = false;
|
|
|
|
|
if (!is_array($resources)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$poolId = (int) ($resources['pool_id'] ?? 0);
|
|
|
|
|
$normalized = [
|
|
|
|
|
'cpu_cores' => (int) ($resources['cpu_cores'] ?? 0),
|
|
|
|
|
'memory_mib' => (int) ($resources['memory_mib'] ?? 0),
|
|
|
|
|
'disk_gib' => (int) ($resources['disk_gib'] ?? 0),
|
|
|
|
|
];
|
|
|
|
|
if ($poolId < 1 || min($normalized) < 1) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$byPool[$poolId][$key] = $normalized;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($byPool as $poolId => $poolRequests) {
|
|
|
|
|
$candidateRows = self::candidateHostRows((int) $poolId);
|
|
|
|
|
if ($candidateRows->isEmpty()) {
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
foreach ($poolRequests as $key => $resources) {
|
|
|
|
|
foreach ($candidateRows as $candidate) {
|
|
|
|
|
$hostId = (int) $candidate->id;
|
|
|
|
|
if (isset($availableIpHosts[$hostId])
|
|
|
|
|
&& self::hasCapacity($candidate, $candidate,
|
|
|
|
|
$pendingByHost[$hostId] ?? self::emptyUsage(),
|
|
|
|
|
$resources)) {
|
|
|
|
|
$availability[$key] = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $availability;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Select a host and reserve one concrete public IP exactly once.
|
|
|
|
|
*
|
|
|
|
|
@ -81,94 +160,12 @@ final class CloudPlacementService
|
|
|
|
|
throw new CloudError('当前未开放线路选择,该销售方案暂不可购买', 409);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$freshAfter = time() - self::freshnessSeconds();
|
|
|
|
|
$candidateRows = CloudHost::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', (int) $pool->id)
|
|
|
|
|
->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)
|
|
|
|
|
->get([
|
|
|
|
|
'cloud_hosts.id',
|
|
|
|
|
'capacity_policy.configured_cpu_cores',
|
|
|
|
|
'capacity_policy.configured_memory_mib',
|
|
|
|
|
'capacity_policy.configured_disk_gib',
|
|
|
|
|
'capacity_policy.safety_cpu_cores',
|
|
|
|
|
'capacity_policy.safety_memory_mib',
|
|
|
|
|
'capacity_policy.safety_disk_gib',
|
|
|
|
|
'capacity_policy.max_instances',
|
|
|
|
|
'capacity_snapshot.cpu_total_cores',
|
|
|
|
|
'capacity_snapshot.cpu_allocated_cores',
|
|
|
|
|
'capacity_snapshot.memory_total_mib',
|
|
|
|
|
'capacity_snapshot.memory_allocated_mib',
|
|
|
|
|
'capacity_snapshot.storage_total_bytes',
|
|
|
|
|
'capacity_snapshot.storage_allocated_bytes',
|
|
|
|
|
'capacity_snapshot.instance_total_count',
|
|
|
|
|
'capacity_snapshot.last_attempt_at',
|
|
|
|
|
]);
|
|
|
|
|
$candidateIds = $candidateRows->pluck('id')
|
|
|
|
|
->map(static fn ($id): int => (int) $id)->all();
|
|
|
|
|
$pendingByHost = [];
|
|
|
|
|
if ($candidateIds !== []) {
|
|
|
|
|
foreach (CloudResourceReservation::query()
|
|
|
|
|
->from('cloud_resource_reservations as reservation')
|
|
|
|
|
->join('cloud_host_capacity_snapshots as observed',
|
|
|
|
|
'observed.host_id', '=', 'reservation.host_id')
|
|
|
|
|
->whereIn('reservation.host_id', $candidateIds)
|
|
|
|
|
->where(function ($query): void {
|
|
|
|
|
$query->whereIn('reservation.state', self::ACTIVE_RESERVATIONS)
|
|
|
|
|
->orWhere(function ($recent): void {
|
|
|
|
|
$recent->where('reservation.state', 'committed')
|
|
|
|
|
->whereColumn('reservation.updated_at', '>=',
|
|
|
|
|
'observed.last_attempt_at');
|
|
|
|
|
});
|
|
|
|
|
})->selectRaw(
|
|
|
|
|
'reservation.host_id, COUNT(*) AS instance_count, '
|
|
|
|
|
. 'COALESCE(SUM(reservation.cpu_cores),0) AS cpu_sum, '
|
|
|
|
|
. 'COALESCE(SUM(reservation.memory_mib),0) AS memory_sum, '
|
|
|
|
|
. 'COALESCE(SUM(reservation.disk_gib),0) AS disk_sum'
|
|
|
|
|
)->groupBy('reservation.host_id')->get() as $pending) {
|
|
|
|
|
$pendingByHost[(int) $pending->host_id] = $pending;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$candidateRows = self::candidateHostRows((int) $pool->id);
|
|
|
|
|
$pendingByHost = self::pendingUsageByHost($candidateRows);
|
|
|
|
|
$candidates = $candidateRows->map(static function ($row) use ($pendingByHost): array {
|
|
|
|
|
$pending = $pendingByHost[(int) $row->id] ?? null;
|
|
|
|
|
$cpuTotal = max(1, min((int) $row->cpu_total_cores,
|
|
|
|
|
(int) $row->configured_cpu_cores)
|
|
|
|
|
- (int) $row->safety_cpu_cores);
|
|
|
|
|
$memoryTotal = max(1, min((int) $row->memory_total_mib,
|
|
|
|
|
(int) $row->configured_memory_mib)
|
|
|
|
|
- (int) $row->safety_memory_mib);
|
|
|
|
|
$storageTotal = max(1, min((int) $row->storage_total_bytes,
|
|
|
|
|
(int) $row->configured_disk_gib * self::GIB)
|
|
|
|
|
- ((int) $row->safety_disk_gib * self::GIB));
|
|
|
|
|
$instanceScore = (int) $row->max_instances > 0
|
|
|
|
|
? ((int) $row->instance_total_count
|
|
|
|
|
+ (int) ($pending->instance_count ?? 0))
|
|
|
|
|
/ (int) $row->max_instances
|
|
|
|
|
: 0.0;
|
|
|
|
|
return [
|
|
|
|
|
'id' => (int) $row->id,
|
|
|
|
|
'score' => max(
|
|
|
|
|
((int) $row->cpu_allocated_cores
|
|
|
|
|
+ (int) ($pending->cpu_sum ?? 0)) / $cpuTotal,
|
|
|
|
|
((int) $row->memory_allocated_mib
|
|
|
|
|
+ (int) ($pending->memory_sum ?? 0)) / $memoryTotal,
|
|
|
|
|
((int) $row->storage_allocated_bytes
|
|
|
|
|
+ ((int) ($pending->disk_sum ?? 0) * self::GIB))
|
|
|
|
|
/ $storageTotal,
|
|
|
|
|
$instanceScore
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
$pending = $pendingByHost[(int) $row->id] ?? self::emptyUsage();
|
|
|
|
|
return ['id' => (int) $row->id,
|
|
|
|
|
'score' => self::capacityScore($row, $row, $pending)];
|
|
|
|
|
})->sort(static fn (array $left, array $right): int =>
|
|
|
|
|
($left['score'] <=> $right['score']) ?: ($left['id'] <=> $right['id'])
|
|
|
|
|
)->values()->all();
|
|
|
|
|
@ -435,51 +432,14 @@ final class CloudPlacementService
|
|
|
|
|
->orderBy('id')->lockForUpdate()->get([
|
|
|
|
|
'id', 'state', 'cpu_cores', 'memory_mib', 'disk_gib', 'updated_at',
|
|
|
|
|
]);
|
|
|
|
|
$pending = [
|
|
|
|
|
'instance_count' => 0,
|
|
|
|
|
'cpu_sum' => 0,
|
|
|
|
|
'memory_sum' => 0,
|
|
|
|
|
'disk_sum' => 0,
|
|
|
|
|
];
|
|
|
|
|
foreach ($reservationRows as $row) {
|
|
|
|
|
// Timestamps have one-second precision. Treat an equal second
|
|
|
|
|
// conservatively as not yet observed; temporary double-counting is
|
|
|
|
|
// safer than oversubscription.
|
|
|
|
|
if ((string) $row->state === 'committed'
|
|
|
|
|
&& (int) $row->updated_at < (int) $snapshot->last_attempt_at) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$pending['instance_count']++;
|
|
|
|
|
$pending['cpu_sum'] += (int) $row->cpu_cores;
|
|
|
|
|
$pending['memory_sum'] += (int) $row->memory_mib;
|
|
|
|
|
$pending['disk_sum'] += (int) $row->disk_gib;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cpuLimit = max(0, min((int) $policy->configured_cpu_cores,
|
|
|
|
|
(int) $snapshot->cpu_total_cores) - (int) $policy->safety_cpu_cores);
|
|
|
|
|
$memoryLimit = max(0, min((int) $policy->configured_memory_mib,
|
|
|
|
|
(int) $snapshot->memory_total_mib) - (int) $policy->safety_memory_mib);
|
|
|
|
|
$configuredDisk = (int) $policy->configured_disk_gib * self::GIB;
|
|
|
|
|
$safetyDisk = (int) $policy->safety_disk_gib * self::GIB;
|
|
|
|
|
$diskLimit = max(0, min($configuredDisk,
|
|
|
|
|
(int) $snapshot->storage_total_bytes) - $safetyDisk);
|
|
|
|
|
$usedCpu = (int) $snapshot->cpu_allocated_cores + $pending['cpu_sum'];
|
|
|
|
|
$usedMemory = (int) $snapshot->memory_allocated_mib
|
|
|
|
|
+ $pending['memory_sum'];
|
|
|
|
|
$usedDisk = (int) $snapshot->storage_allocated_bytes
|
|
|
|
|
+ ($pending['disk_sum'] * self::GIB);
|
|
|
|
|
$instances = (int) $snapshot->instance_total_count
|
|
|
|
|
+ $pending['instance_count'];
|
|
|
|
|
if ($usedCpu + $resources['cpu_cores'] > $cpuLimit
|
|
|
|
|
|| $usedMemory + $resources['memory_mib'] > $memoryLimit
|
|
|
|
|
|| $usedDisk + ($resources['disk_gib'] * self::GIB) > $diskLimit
|
|
|
|
|
|| ((int) $policy->max_instances > 0
|
|
|
|
|
&& $instances + 1 > (int) $policy->max_instances)) {
|
|
|
|
|
$pending = self::reservationUsage(
|
|
|
|
|
$reservationRows, (int) $snapshot->last_attempt_at
|
|
|
|
|
);
|
|
|
|
|
if (!self::hasCapacity($policy, $snapshot, $pending, $resources)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ip = CloudPublicIp::where('host_id', $hostId)->where('state', 'available')
|
|
|
|
|
->orderBy('id')->lockForUpdate()->first();
|
|
|
|
|
$ip = self::availablePublicIpQuery($hostId)->lockForUpdate()->first();
|
|
|
|
|
if ($ip === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
@ -613,6 +573,173 @@ final class CloudPlacementService
|
|
|
|
|
return [$host, $reservation];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function candidateHostRows(int $poolId)
|
|
|
|
|
{
|
|
|
|
|
$freshAfter = time() - self::freshnessSeconds();
|
|
|
|
|
return CloudHost::query()->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('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)
|
|
|
|
|
->get([
|
|
|
|
|
'cloud_hosts.id',
|
|
|
|
|
'capacity_policy.configured_cpu_cores',
|
|
|
|
|
'capacity_policy.configured_memory_mib',
|
|
|
|
|
'capacity_policy.configured_disk_gib',
|
|
|
|
|
'capacity_policy.safety_cpu_cores',
|
|
|
|
|
'capacity_policy.safety_memory_mib',
|
|
|
|
|
'capacity_policy.safety_disk_gib',
|
|
|
|
|
'capacity_policy.max_instances',
|
|
|
|
|
'capacity_snapshot.cpu_total_cores',
|
|
|
|
|
'capacity_snapshot.cpu_allocated_cores',
|
|
|
|
|
'capacity_snapshot.memory_total_mib',
|
|
|
|
|
'capacity_snapshot.memory_allocated_mib',
|
|
|
|
|
'capacity_snapshot.storage_total_bytes',
|
|
|
|
|
'capacity_snapshot.storage_allocated_bytes',
|
|
|
|
|
'capacity_snapshot.instance_total_count',
|
|
|
|
|
'capacity_snapshot.last_attempt_at',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function pendingUsageByHost($candidateRows): array
|
|
|
|
|
{
|
|
|
|
|
$observedAt = [];
|
|
|
|
|
foreach ($candidateRows as $candidate) {
|
|
|
|
|
$observedAt[(int) $candidate->id] = (int) $candidate->last_attempt_at;
|
|
|
|
|
}
|
|
|
|
|
$candidateIds = array_keys($observedAt);
|
|
|
|
|
if ($candidateIds === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$minimumObservedAt = min($observedAt);
|
|
|
|
|
$usage = [];
|
|
|
|
|
$rows = CloudResourceReservation::query()
|
|
|
|
|
->whereIn('host_id', $candidateIds)
|
|
|
|
|
->where(function ($query) use ($minimumObservedAt): void {
|
|
|
|
|
$query->whereIn('state', self::ACTIVE_RESERVATIONS)
|
|
|
|
|
->orWhere(function ($recent) use ($minimumObservedAt): void {
|
|
|
|
|
$recent->where('state', 'committed')
|
|
|
|
|
->where('updated_at', '>=', $minimumObservedAt);
|
|
|
|
|
});
|
|
|
|
|
})->get([
|
|
|
|
|
'host_id', 'state', 'cpu_cores', 'memory_mib', 'disk_gib',
|
|
|
|
|
'updated_at',
|
|
|
|
|
]);
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$hostId = (int) $row->host_id;
|
|
|
|
|
if ((string) $row->state === 'committed'
|
|
|
|
|
&& (int) $row->updated_at < $observedAt[$hostId]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$usage[$hostId] ??= self::emptyUsage();
|
|
|
|
|
$usage[$hostId]['instance_count']++;
|
|
|
|
|
$usage[$hostId]['cpu_sum'] += (int) $row->cpu_cores;
|
|
|
|
|
$usage[$hostId]['memory_sum'] += (int) $row->memory_mib;
|
|
|
|
|
$usage[$hostId]['disk_sum'] += (int) $row->disk_gib;
|
|
|
|
|
}
|
|
|
|
|
return $usage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function reservationUsage($reservationRows,
|
|
|
|
|
int $lastAttemptAt): array
|
|
|
|
|
{
|
|
|
|
|
$usage = self::emptyUsage();
|
|
|
|
|
foreach ($reservationRows as $row) {
|
|
|
|
|
// Timestamps have one-second precision. Treat an equal second as
|
|
|
|
|
// not yet observed; temporary double-counting is safer than
|
|
|
|
|
// oversubscription.
|
|
|
|
|
if ((string) $row->state === 'committed'
|
|
|
|
|
&& (int) $row->updated_at < $lastAttemptAt) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$usage['instance_count']++;
|
|
|
|
|
$usage['cpu_sum'] += (int) $row->cpu_cores;
|
|
|
|
|
$usage['memory_sum'] += (int) $row->memory_mib;
|
|
|
|
|
$usage['disk_sum'] += (int) $row->disk_gib;
|
|
|
|
|
}
|
|
|
|
|
return $usage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function emptyUsage(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'instance_count' => 0,
|
|
|
|
|
'cpu_sum' => 0,
|
|
|
|
|
'memory_sum' => 0,
|
|
|
|
|
'disk_sum' => 0,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function capacityState($policy, $snapshot,
|
|
|
|
|
array $usage): array
|
|
|
|
|
{
|
|
|
|
|
$cpuLimit = max(0, min((int) $policy->configured_cpu_cores,
|
|
|
|
|
(int) $snapshot->cpu_total_cores) - (int) $policy->safety_cpu_cores);
|
|
|
|
|
$memoryLimit = max(0, min((int) $policy->configured_memory_mib,
|
|
|
|
|
(int) $snapshot->memory_total_mib) - (int) $policy->safety_memory_mib);
|
|
|
|
|
$configuredDisk = (int) $policy->configured_disk_gib * self::GIB;
|
|
|
|
|
$safetyDisk = (int) $policy->safety_disk_gib * self::GIB;
|
|
|
|
|
$diskLimit = max(0, min($configuredDisk,
|
|
|
|
|
(int) $snapshot->storage_total_bytes) - $safetyDisk);
|
|
|
|
|
return [
|
|
|
|
|
'cpu_limit' => $cpuLimit,
|
|
|
|
|
'memory_limit' => $memoryLimit,
|
|
|
|
|
'disk_limit' => $diskLimit,
|
|
|
|
|
'cpu_used' => (int) $snapshot->cpu_allocated_cores
|
|
|
|
|
+ $usage['cpu_sum'],
|
|
|
|
|
'memory_used' => (int) $snapshot->memory_allocated_mib
|
|
|
|
|
+ $usage['memory_sum'],
|
|
|
|
|
'disk_used' => (int) $snapshot->storage_allocated_bytes
|
|
|
|
|
+ ($usage['disk_sum'] * self::GIB),
|
|
|
|
|
'instances' => (int) $snapshot->instance_total_count
|
|
|
|
|
+ $usage['instance_count'],
|
|
|
|
|
'max_instances' => (int) $policy->max_instances,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function hasCapacity($policy, $snapshot, array $usage,
|
|
|
|
|
array $resources): bool
|
|
|
|
|
{
|
|
|
|
|
$state = self::capacityState($policy, $snapshot, $usage);
|
|
|
|
|
return $state['cpu_used'] + $resources['cpu_cores'] <= $state['cpu_limit']
|
|
|
|
|
&& $state['memory_used'] + $resources['memory_mib']
|
|
|
|
|
<= $state['memory_limit']
|
|
|
|
|
&& $state['disk_used'] + ($resources['disk_gib'] * self::GIB)
|
|
|
|
|
<= $state['disk_limit']
|
|
|
|
|
&& ($state['max_instances'] < 1
|
|
|
|
|
|| $state['instances'] + 1 <= $state['max_instances']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function capacityScore($policy, $snapshot,
|
|
|
|
|
array $usage): float
|
|
|
|
|
{
|
|
|
|
|
$state = self::capacityState($policy, $snapshot, $usage);
|
|
|
|
|
$instanceScore = $state['max_instances'] > 0
|
|
|
|
|
? $state['instances'] / $state['max_instances'] : 0.0;
|
|
|
|
|
return max(
|
|
|
|
|
$state['cpu_used'] / max(1, $state['cpu_limit']),
|
|
|
|
|
$state['memory_used'] / max(1, $state['memory_limit']),
|
|
|
|
|
$state['disk_used'] / max(1, $state['disk_limit']),
|
|
|
|
|
$instanceScore
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function availablePublicIpQuery(int $hostId)
|
|
|
|
|
{
|
|
|
|
|
return CloudPublicIp::query()->where('host_id', $hostId)
|
|
|
|
|
->where('state', 'available')->orderBy('id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function freshnessSeconds(): int
|
|
|
|
|
{
|
|
|
|
|
$value = filter_var(Config::get('cloud_capacity_fresh_seconds', 120),
|
|
|
|
|
|