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.
198 lines
7.8 KiB
PHP
198 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudOffer;
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateReplica;
|
|
|
|
/** One placement-shaped view of where an immutable template is usable. */
|
|
final class CloudTemplateAvailabilityService
|
|
{
|
|
/**
|
|
* Translate exact replicas into public offer and region identifiers.
|
|
* Host, pool and replica identities remain internal.
|
|
*
|
|
* @param iterable<CloudTemplate> $templates
|
|
* @return array<string,array{offer_ids:list<int>,region_ids:list<int>}>
|
|
*/
|
|
public static function deploymentAvailability(iterable $templates): array
|
|
{
|
|
$templateIds = [];
|
|
foreach ($templates as $template) {
|
|
if ($template instanceof CloudTemplate
|
|
&& trim((string) $template->id) !== '') {
|
|
$templateIds[(string) $template->id] = true;
|
|
}
|
|
}
|
|
$templateIds = array_keys($templateIds);
|
|
if ($templateIds === []) {
|
|
return [];
|
|
}
|
|
$poolRegions = self::eligiblePoolRegions($templateIds);
|
|
$poolIds = [];
|
|
foreach ($poolRegions as $regions) {
|
|
foreach ($regions as $poolId => $_regionId) {
|
|
$poolIds[(int) $poolId] = true;
|
|
}
|
|
}
|
|
$offersByPool = [];
|
|
if ($poolIds !== []) {
|
|
$offers = CloudOffer::query()->from('cloud_offers as offer')
|
|
->join('shop as plan', 'plan.id', '=', 'offer.plan_id')
|
|
->whereIn('offer.resource_pool_id', array_keys($poolIds))
|
|
->where('offer.status', 1)
|
|
->where('offer.currency', 'USD')
|
|
->where('plan.status', 1)
|
|
->orderBy('offer.id')
|
|
->get([
|
|
'offer.id',
|
|
'offer.resource_pool_id',
|
|
]);
|
|
foreach ($offers as $offer) {
|
|
$offersByPool[(int) $offer->resource_pool_id][] =
|
|
(int) $offer->id;
|
|
}
|
|
}
|
|
$availability = [];
|
|
foreach ($templateIds as $templateId) {
|
|
$offerIds = [];
|
|
$regionIds = [];
|
|
foreach ($poolRegions[$templateId] ?? [] as $poolId => $regionId) {
|
|
foreach ($offersByPool[(int) $poolId] ?? [] as $offerId) {
|
|
$offerIds[$offerId] = true;
|
|
$regionIds[(int) $regionId] = true;
|
|
}
|
|
}
|
|
$offerIds = array_keys($offerIds);
|
|
$regionIds = array_keys($regionIds);
|
|
sort($offerIds, SORT_NUMERIC);
|
|
sort($regionIds, SORT_NUMERIC);
|
|
$availability[$templateId] = [
|
|
'offer_ids' => $offerIds,
|
|
'region_ids' => $regionIds,
|
|
];
|
|
}
|
|
return $availability;
|
|
}
|
|
|
|
/** At least one scheduler-ready host in this pool owns the exact replica. */
|
|
public static function poolHasExactReplica(
|
|
CloudTemplate $template,
|
|
int $poolId
|
|
): bool {
|
|
if ($poolId < 1) {
|
|
return false;
|
|
}
|
|
$rows = self::eligiblePoolRegions([(string) $template->id], $poolId);
|
|
return isset($rows[(string) $template->id][$poolId]);
|
|
}
|
|
|
|
/** Reinstall is host-bound and does not depend on current sales capacity. */
|
|
public static function hostHasExactReplica(
|
|
CloudTemplate $template,
|
|
int $hostId
|
|
): bool {
|
|
if ($hostId < 1) {
|
|
return false;
|
|
}
|
|
$replica = CloudTemplateReplica::query()
|
|
->from('cloud_template_replicas as replica')
|
|
->join('cloud_hosts as host', 'host.id', '=', 'replica.host_id')
|
|
->join('cloud_host_agents as agent', 'agent.host_id', '=',
|
|
'replica.host_id')
|
|
->join('cloud_host_storage_configurations as storage',
|
|
'storage.host_id', '=', 'replica.host_id')
|
|
->where('replica.host_id', $hostId)
|
|
->where('replica.template_id', (string) $template->id)
|
|
->where('host.enabled', 1)
|
|
->where('agent.enabled', 1)
|
|
->where('agent.last_test_status', 'healthy')
|
|
->where('agent.tested_revision', '>', 0)
|
|
->whereColumn('agent.tested_revision', 'agent.revision')
|
|
->where('storage.state', 'applied')
|
|
->first(['replica.*']);
|
|
return $replica instanceof CloudTemplateReplica
|
|
&& CloudTemplateReplicaCoordinator::isExactReplica(
|
|
$template,
|
|
$replica
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param list<string> $templateIds
|
|
* @return array<string,array<int,int>> template => pool => region
|
|
*/
|
|
private static function eligiblePoolRegions(
|
|
array $templateIds,
|
|
?int $onlyPoolId = null
|
|
): array {
|
|
$query = CloudTemplateReplica::query()
|
|
->from('cloud_template_replicas as replica')
|
|
->join('cloud_templates as template', 'template.id', '=',
|
|
'replica.template_id')
|
|
->join('cloud_hosts as host', 'host.id', '=', 'replica.host_id')
|
|
->join('cloud_host_agents as agent', 'agent.host_id', '=',
|
|
'replica.host_id')
|
|
->join('cloud_host_storage_configurations as storage',
|
|
'storage.host_id', '=', 'replica.host_id')
|
|
->join('cloud_host_resource_pool_memberships as membership',
|
|
'membership.host_id', '=', 'replica.host_id')
|
|
->join('cloud_host_capacity_policies as capacity',
|
|
'capacity.host_id', '=', 'replica.host_id')
|
|
->join('cloud_resource_pools as pool', 'pool.id', '=',
|
|
'membership.resource_pool_id')
|
|
->join('cloud_network_lines as line', 'line.id', '=',
|
|
'pool.line_id')
|
|
->join('cloud_regions as region', 'region.id', '=',
|
|
'line.region_id')
|
|
->whereIn('replica.template_id', $templateIds)
|
|
->where('replica.state', 'active')
|
|
->whereColumn('replica.revision', 'template.revision')
|
|
->whereColumn('replica.generation_token',
|
|
'template.generation_token')
|
|
->whereColumn('replica.image_bytes', 'template.image_bytes')
|
|
->whereColumn('replica.sha256', 'template.sha256')
|
|
->where('host.enabled', 1)
|
|
->where('agent.enabled', 1)
|
|
->where('agent.last_test_status', 'healthy')
|
|
->where('agent.tested_revision', '>', 0)
|
|
->whereColumn('agent.tested_revision', 'agent.revision')
|
|
->where('storage.state', 'applied')
|
|
->where('membership.scheduler_state', 'active')
|
|
->where('capacity.configured_cpu_cores', '>', 0)
|
|
->where('capacity.configured_memory_mib', '>', 0)
|
|
->where('capacity.configured_disk_gib', '>', 0)
|
|
->where('pool.status', 'active')
|
|
->where('line.status', 'active')
|
|
->where('region.status', 'active');
|
|
if (!CloudNetworkLineFeature::enabled()) {
|
|
$query->where('line.is_default', 1);
|
|
}
|
|
if ($onlyPoolId !== null) {
|
|
$query->where('pool.id', $onlyPoolId);
|
|
}
|
|
$rows = $query->orderBy('replica.template_id')
|
|
->orderBy('pool.id')->orderBy('host.id')->get([
|
|
'replica.template_id',
|
|
'pool.id as pool_id',
|
|
'pool.line_id as resource_pool_line_id',
|
|
'region.id as region_id',
|
|
'host.id',
|
|
]);
|
|
$readyHostLines =
|
|
CloudPlacementNetworkService::advisoryAvailableHostLines($rows);
|
|
$result = [];
|
|
foreach ($rows as $row) {
|
|
if (!($readyHostLines[(int) $row->id][
|
|
(int) $row->resource_pool_line_id
|
|
] ?? false)) {
|
|
continue;
|
|
}
|
|
$result[(string) $row->template_id][(int) $row->pool_id] =
|
|
(int) $row->region_id;
|
|
}
|
|
return $result;
|
|
}
|
|
}
|