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.
165 lines
5.2 KiB
PHP
165 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudTemplate;
|
|
use App\Models\CloudTemplateReplica;
|
|
|
|
/**
|
|
* The one executable state definition for template masters and host replicas.
|
|
*
|
|
* A template is available when at least one eligible host owns the exact
|
|
* immutable generation. Missing or failed replicas degrade availability; they
|
|
* do not rewrite the master artifact or make healthy replicas unusable.
|
|
*/
|
|
final class CloudTemplateStateMachine
|
|
{
|
|
/** @var array<string,list<string>> */
|
|
private const TEMPLATE_TRANSITIONS = [
|
|
'preparing' => ['importing', 'source_failed', 'source_unknown'],
|
|
'source_failed' => ['retired'],
|
|
'source_unknown' => ['retired'],
|
|
'importing' => ['active', 'degraded', 'error', 'retired'],
|
|
'active' => ['active', 'degraded', 'error', 'deleting'],
|
|
'degraded' => ['active', 'degraded', 'error', 'deleting', 'retired'],
|
|
'error' => ['active', 'degraded', 'error', 'deleting', 'retired'],
|
|
'deleting' => ['active', 'degraded', 'delete_failed', 'retired'],
|
|
'delete_failed' => ['active', 'degraded', 'deleting', 'retired'],
|
|
'retired' => [],
|
|
];
|
|
|
|
/** @var array<string,list<string>> */
|
|
private const REPLICA_TRANSITIONS = [
|
|
'pending' => ['importing'],
|
|
'importing' => ['active', 'failed'],
|
|
'failed' => ['importing', 'active', 'failed'],
|
|
'active' => ['active', 'stale', 'failed', 'syncing', 'deleting'],
|
|
'stale' => ['active', 'stale', 'failed', 'syncing', 'deleting'],
|
|
'syncing' => ['active', 'stale'],
|
|
'deleting' => ['delete_succeeded', 'delete_failed'],
|
|
'delete_failed' => [
|
|
'active', 'stale', 'deleting', 'delete_failed', 'delete_succeeded',
|
|
],
|
|
'delete_succeeded' => [],
|
|
];
|
|
|
|
public static function moveTemplate(
|
|
CloudTemplate $template,
|
|
string $next
|
|
): void {
|
|
self::assertTransition(
|
|
self::TEMPLATE_TRANSITIONS,
|
|
(string) $template->state,
|
|
$next,
|
|
'云镜像主记录'
|
|
);
|
|
$template->state = $next;
|
|
}
|
|
|
|
public static function moveReplica(
|
|
CloudTemplateReplica $replica,
|
|
string $next
|
|
): void {
|
|
self::assertTransition(
|
|
self::REPLICA_TRANSITIONS,
|
|
(string) $replica->state,
|
|
$next,
|
|
'云镜像宿主机副本'
|
|
);
|
|
$replica->state = $next;
|
|
}
|
|
|
|
public static function assertTemplateTransition(
|
|
string $current,
|
|
string $next
|
|
): void {
|
|
self::assertTransition(
|
|
self::TEMPLATE_TRANSITIONS,
|
|
$current,
|
|
$next,
|
|
'云镜像主记录'
|
|
);
|
|
}
|
|
|
|
public static function assertReplicaTransition(
|
|
string $current,
|
|
string $next
|
|
): void {
|
|
self::assertTransition(
|
|
self::REPLICA_TRANSITIONS,
|
|
$current,
|
|
$next,
|
|
'云镜像宿主机副本'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Project catalog availability from exact replicas on currently eligible
|
|
* hosts. Replicas on disabled hosts do not make a template healthy.
|
|
*
|
|
* @param iterable<CloudTemplateReplica> $replicas
|
|
* @param list<int> $eligibleHostIds
|
|
*/
|
|
public static function availability(
|
|
CloudTemplate $template,
|
|
iterable $replicas,
|
|
array $eligibleHostIds
|
|
): string {
|
|
$eligible = [];
|
|
foreach ($eligibleHostIds as $hostId) {
|
|
$hostId = (int) $hostId;
|
|
if ($hostId > 0) {
|
|
$eligible[$hostId] = true;
|
|
}
|
|
}
|
|
if ($eligible === []) {
|
|
return 'error';
|
|
}
|
|
|
|
$ready = [];
|
|
foreach ($replicas as $replica) {
|
|
$hostId = (int) $replica->host_id;
|
|
if (!isset($eligible[$hostId])
|
|
|| (string) $replica->state !== 'active'
|
|
|| (int) $replica->revision !== (int) $template->revision
|
|
|| !hash_equals(
|
|
(string) $template->generation_token,
|
|
(string) $replica->generation_token
|
|
)
|
|
|| (int) $replica->image_bytes !== (int) $template->image_bytes
|
|
|| !hash_equals(
|
|
(string) $template->sha256,
|
|
(string) $replica->sha256
|
|
)) {
|
|
continue;
|
|
}
|
|
$ready[$hostId] = true;
|
|
}
|
|
if ($ready === []) {
|
|
return 'error';
|
|
}
|
|
return count($ready) === count($eligible) ? 'active' : 'degraded';
|
|
}
|
|
|
|
/** @param array<string,list<string>> $definition */
|
|
private static function assertTransition(
|
|
array $definition,
|
|
string $current,
|
|
string $next,
|
|
string $label
|
|
): void {
|
|
if (array_key_exists($current, $definition) && $current === $next) {
|
|
return;
|
|
}
|
|
if (!array_key_exists($current, $definition)
|
|
|| !in_array($next, $definition[$current], true)) {
|
|
throw new CloudError(sprintf(
|
|
'%s状态不能从 %s 变更为 %s',
|
|
$label,
|
|
$current === '' ? '(empty)' : $current,
|
|
$next === '' ? '(empty)' : $next
|
|
), 409);
|
|
}
|
|
}
|
|
}
|