> */ 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> */ 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 $replicas * @param list $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> $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); } } }