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.
150 lines
5.0 KiB
PHP
150 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateReplica;
|
|
|
|
/**
|
|
* Fill the current catalog generations onto a host after it becomes ready.
|
|
*
|
|
* Host readiness is the admission boundary. This coordinator is deliberately
|
|
* best-effort across templates: one busy or temporarily unavailable artifact
|
|
* must not prevent another independent catalog generation from being queued.
|
|
*/
|
|
final class CloudTemplateHostBackfillService
|
|
{
|
|
private const TEMPLATE_STATES = ['active', 'degraded', 'error'];
|
|
|
|
/**
|
|
* Queue every missing default generation for one ready host.
|
|
*
|
|
* @return array{host_id:int,ready:bool,considered:int,already_ready:int,
|
|
* scheduled:int,deferred:int}
|
|
*/
|
|
public static function scheduleReadyHost(
|
|
int $hostId,
|
|
array $actor,
|
|
string $reason = 'host_ready'
|
|
): array {
|
|
CloudAccess::manage($actor);
|
|
self::assertInput($hostId, $reason);
|
|
$plan = self::planReadyHost($hostId);
|
|
$report = [
|
|
'host_id' => $hostId,
|
|
'ready' => $plan['ready'],
|
|
'considered' => $plan['considered'],
|
|
'already_ready' => count($plan['already_ready']),
|
|
'scheduled' => 0,
|
|
'deferred' => 0,
|
|
];
|
|
if (!$plan['ready']) {
|
|
return $report;
|
|
}
|
|
foreach ($plan['pending'] as $templateId) {
|
|
try {
|
|
$result = CloudTemplateReplicaCoordinator::syncHost(
|
|
$templateId,
|
|
$hostId,
|
|
$actor,
|
|
$reason
|
|
);
|
|
if (($result['scheduled'] ?? false) === true) {
|
|
$report['scheduled']++;
|
|
} else {
|
|
$report['already_ready']++;
|
|
}
|
|
} catch (\Throwable $error) {
|
|
// The original host-readiness action has already committed.
|
|
// Keep this generation eligible for the periodic reconciler
|
|
// instead of converting an independent backfill into a false
|
|
// Agent/storage failure.
|
|
$report['deferred']++;
|
|
error_log(sprintf(
|
|
'cloud template backfill host=%d template=%s deferred: %s',
|
|
$hostId,
|
|
$templateId,
|
|
get_class($error)
|
|
));
|
|
}
|
|
}
|
|
return $report;
|
|
}
|
|
|
|
/**
|
|
* Read-only plan used by event hooks and future reconciliation passes.
|
|
* Only the default generation of each catalog entry is auto-filled; older
|
|
* immutable versions remain available for explicit operator repair.
|
|
*
|
|
* @return array{host_id:int,ready:bool,considered:int,
|
|
* already_ready:list<string>,pending:list<string>}
|
|
*/
|
|
public static function planReadyHost(int $hostId): array
|
|
{
|
|
if ($hostId < 1) {
|
|
throw new CloudError('云镜像补齐宿主机参数无效', 422);
|
|
}
|
|
$ready = in_array(
|
|
$hostId,
|
|
CloudAgentSettingsService::enabledHostIds(),
|
|
true
|
|
);
|
|
if (!$ready) {
|
|
return [
|
|
'host_id' => $hostId,
|
|
'ready' => false,
|
|
'considered' => 0,
|
|
'already_ready' => [],
|
|
'pending' => [],
|
|
];
|
|
}
|
|
$templates = CloudTemplate::query()
|
|
->where('is_default', 1)
|
|
->whereIn('state', self::TEMPLATE_STATES)
|
|
->orderBy('catalog_key')
|
|
->orderBy('version_label')
|
|
->orderBy('id')
|
|
->get();
|
|
$templateIds = $templates->map(
|
|
static fn (CloudTemplate $template): string =>
|
|
(string) $template->id
|
|
)->all();
|
|
$replicas = $templateIds === []
|
|
? collect()
|
|
: CloudTemplateReplica::query()
|
|
->where('host_id', $hostId)
|
|
->whereIn('template_id', $templateIds)
|
|
->get()->keyBy('template_id');
|
|
$alreadyReady = [];
|
|
$pending = [];
|
|
foreach ($templates as $template) {
|
|
$templateId = (string) $template->id;
|
|
$replica = $replicas->get($templateId);
|
|
if ($replica instanceof CloudTemplateReplica
|
|
&& CloudTemplateReplicaCoordinator::isExactReplica(
|
|
$template,
|
|
$replica
|
|
)) {
|
|
$alreadyReady[] = $templateId;
|
|
continue;
|
|
}
|
|
$pending[] = $templateId;
|
|
}
|
|
return [
|
|
'host_id' => $hostId,
|
|
'ready' => true,
|
|
'considered' => count($templateIds),
|
|
'already_ready' => $alreadyReady,
|
|
'pending' => $pending,
|
|
];
|
|
}
|
|
|
|
private static function assertInput(int $hostId, string $reason): void
|
|
{
|
|
if ($hostId < 1
|
|
|| preg_match('/^[a-z][a-z0-9_]{1,47}$/D', $reason) !== 1) {
|
|
throw new CloudError('云镜像补齐参数无效', 422);
|
|
}
|
|
}
|
|
}
|