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.
virt/web/app/Services/Cloud/CloudTemplateDeletionServic...

491 lines
20 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudJob;
use App\Models\CloudOrder;
use App\Models\CloudTemplateCatalogLock;
use App\Models\CloudTemplateRetirement;
/** Serialize template use, import and retirement without trusting stale UI data. */
final class CloudTemplateDeletionService
{
// The private engine allows at most one hour for an upload. The extra hour
// prevents a live transfer from losing its gate while still guaranteeing
// that a PHP crash cannot block catalog changes forever.
private const IMPORT_LEASE_SECONDS = 7200;
private const IMPORT_LEASE_KINDS = ['remote', 'upload'];
private const ACTIVE_STATUSES = [
'awaiting_payment', 'queued', 'running', 'uncertain',
];
/** Must run inside the order/job transaction to serialize with deletion. */
public static function assertAvailable(string $templateId): void
{
$now = time();
CloudTemplateRetirement::query()->insertOrIgnore([
'template_id' => $templateId,
'state' => 'active',
'template_created_at' => 0,
'created_at' => $now,
'updated_at' => $now,
]);
$state = CloudTemplateRetirement::where('template_id', $templateId)
->lockForUpdate()->first();
if ($state === null) {
throw new CloudError('无法锁定云镜像状态', 503);
}
if ((string) $state->state !== 'active') {
throw new CloudError('云镜像模板正在删除或已停用', 409);
}
}
/** Reserve the catalog, release the transaction, then contact the engine. */
public static function queueImport(
callable $queue, string $leaseKind = 'remote'
): array
{
$token = self::reserveImport($leaseKind);
try {
$result = $queue();
if (!is_array($result)) {
throw new CloudError('云镜像导入响应无效', 502);
}
} catch (\Throwable $error) {
self::clearImportLeaseSafely($token);
throw $error;
}
// The engine has durably accepted the download/upload job. A database
// outage here must not turn that success into an ambiguous client
// retry; the token-bound lease will also expire automatically.
self::clearImportLeaseSafely($token);
return $result;
}
/** Fetch, reconcile and optionally publish one full catalog atomically. */
public static function fetchAuthoritativeCatalog(
callable $fetch, ?callable $publish = null
): array
{
return (new CloudTemplateCatalogLock())->getConnection()->transaction(
static function () use ($fetch, $publish): array {
self::lockCatalog();
$result = $fetch();
if (!is_array($result)
|| !is_array($result['templates'] ?? null)) {
throw new CloudError('云镜像目录响应无效', 502);
}
self::reconcileAuthoritativeCatalogLocked(
$result['templates']
);
if ($publish !== null) {
$publish($result);
}
return $result;
},
3
);
}
/** Persist the retirement gate before scanning every durable reference. */
public static function begin(string $templateId, int $templateCreatedAt): void
{
if ($templateCreatedAt < 1) {
throw new CloudError('云镜像版本无效,请刷新目录后重试');
}
(new CloudTemplateRetirement())->getConnection()->transaction(
function () use ($templateId, $templateCreatedAt): void {
$catalogLock = self::lockCatalog();
self::assertNoActiveImportLeaseLocked($catalogLock);
$now = time();
CloudTemplateRetirement::query()->insertOrIgnore([
'template_id' => $templateId,
'state' => 'deleting',
'template_created_at' => $templateCreatedAt,
'created_at' => $now,
'updated_at' => $now,
]);
$retirement = CloudTemplateRetirement::where(
'template_id', $templateId
)->lockForUpdate()->first();
if ($retirement === null) {
throw new CloudError('无法锁定云镜像删除状态', 503);
}
$state = (string) $retirement->state;
$storedGeneration = max(0, (int) $retirement->template_created_at);
if (!in_array($state, ['active', 'deleting', 'deleted'], true)) {
throw new CloudError('云镜像删除状态异常,请联系管理员', 503);
}
if ($storedGeneration > 0 && $storedGeneration !== $templateCreatedAt
&& $state !== 'deleted') {
throw new CloudError('云镜像版本已变化,请刷新后重试', 409);
}
$retirement->state = 'deleting';
$retirement->template_created_at = $templateCreatedAt;
$retirement->updated_at = $now;
$retirement->save();
self::assertNoReferences($templateId);
},
3
);
}
/** Check both durable deletion and import gates under the catalog lock. */
public static function assertImportAllowed(): void
{
(new CloudTemplateCatalogLock())->getConnection()->transaction(
static function (): void {
$catalogLock = self::lockCatalog();
self::assertImportAllowedLocked($catalogLock);
},
3
);
}
/** Re-enable one stable ID only when a newer engine generation exists. */
public static function reactivateImportedTemplate(
string $templateId, int $templateCreatedAt,
?callable $publish = null
): bool {
if ($templateCreatedAt < 1) {
throw new CloudError('云镜像导入结果缺少版本信息', 502);
}
return (new CloudTemplateRetirement())->getConnection()->transaction(
static function () use (
$templateId, $templateCreatedAt, $publish
): bool {
self::lockCatalog();
$row = CloudTemplateRetirement::where('template_id', $templateId)
->lockForUpdate()->first();
if ($row === null) {
$now = time();
CloudTemplateRetirement::query()->insert([
'template_id' => $templateId,
'state' => 'active',
'template_created_at' => $templateCreatedAt,
'created_at' => $now,
'updated_at' => $now,
]);
if ($publish !== null) {
$publish();
}
return true;
}
if ((string) $row->state === 'deleting') {
// A deletion requested after this import was queued wins.
return false;
}
$storedGeneration = max(0, (int) $row->template_created_at);
if ($templateCreatedAt < $storedGeneration) {
return false;
}
if ($templateCreatedAt === $storedGeneration) {
// A same-generation PATCH/GET is current only while that
// generation is active. A deleted row must never be
// revived by a delayed import-job response.
$current = (string) $row->state === 'active';
if ($current && $publish !== null) {
$publish();
}
return $current;
}
$row->state = 'active';
$row->template_created_at = $templateCreatedAt;
$row->updated_at = time();
$row->save();
if ($publish !== null) {
$publish();
}
return true;
},
3
);
}
/** Reconcile response-loss states against one authoritative full catalog. */
public static function reconcileAuthoritativeCatalog(array $templates): void
{
(new CloudTemplateRetirement())->getConnection()->transaction(
static function () use ($templates): void {
self::lockCatalog();
self::reconcileAuthoritativeCatalogLocked($templates);
},
3
);
}
private static function reconcileAuthoritativeCatalogLocked(
array $templates
): void {
$catalog = [];
foreach ($templates as $template) {
$id = is_array($template) ? ($template['id'] ?? null) : null;
$generation = is_array($template) ? ($template['created_at'] ?? null) : null;
if (!is_string($id)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D', $id) !== 1
|| !is_int($generation) || $generation < 1
|| isset($catalog[$id])) {
throw new CloudError('云镜像目录响应无效', 502);
}
$catalog[$id] = $generation;
}
$rows = CloudTemplateRetirement::query()->lockForUpdate()->get();
foreach ($rows as $row) {
$id = (string) $row->template_id;
$state = (string) $row->state;
$stored = max(0, (int) $row->template_created_at);
$active = $catalog[$id] ?? null;
if ($state === 'deleting') {
if ($active === null) {
$row->state = 'deleted';
} elseif ($stored === 0) {
$row->template_created_at = $active;
} elseif ($active !== $stored) {
$row->state = 'active';
$row->template_created_at = $active;
} else {
continue;
}
} elseif ($state === 'deleted') {
if ($active === null) {
continue;
}
// The engine catalog is authoritative. This also repairs a
// gate written by an older PHP process that mistook an
// unsupported DELETE route for a missing template while the
// engine service was restarting. All DELETE completion paths
// take the same catalog lock, so this cannot revive a newer
// committed deletion from an older GET snapshot.
$row->state = 'active';
$row->template_created_at = $active;
} elseif ($state === 'active') {
if ($active === null) {
$row->state = 'deleted';
} elseif ($active === $stored) {
continue;
} else {
$row->template_created_at = $active;
}
} else {
throw new CloudError('云镜像状态异常,请联系管理员', 503);
}
$row->updated_at = time();
$row->save();
}
}
public static function complete(
string $templateId, int $templateCreatedAt,
?callable $publish = null
): void
{
(new CloudTemplateRetirement())->getConnection()->transaction(
static function () use (
$templateId, $templateCreatedAt, $publish
): void {
self::lockCatalog();
$row = CloudTemplateRetirement::where(
'template_id', $templateId
)->lockForUpdate()->first();
if ($row === null
|| (int) $row->template_created_at !== $templateCreatedAt
|| !in_array((string) $row->state,
['deleting', 'deleted'], true)) {
throw new CloudError(
'云镜像删除状态无法确认', 503
);
}
if ((string) $row->state === 'deleting') {
$row->state = 'deleted';
$row->updated_at = time();
$row->save();
}
if ($publish !== null) {
$publish();
}
},
3
);
}
/** Remove only a gate proven to belong to a rejected engine generation. */
public static function cancel(string $templateId, int $templateCreatedAt): void
{
(new CloudTemplateRetirement())->getConnection()->transaction(
static function () use ($templateId, $templateCreatedAt): void {
self::lockCatalog();
$row = CloudTemplateRetirement::where(
'template_id', $templateId
)->lockForUpdate()->first();
if ($row !== null && (string) $row->state === 'deleting'
&& (int) $row->template_created_at
=== $templateCreatedAt) {
$row->delete();
}
},
3
);
}
private static function reserveImport(string $leaseKind): string
{
if (!in_array($leaseKind, self::IMPORT_LEASE_KINDS, true)) {
throw new \InvalidArgumentException('云镜像导入预约类型无效');
}
$token = bin2hex(random_bytes(16));
return (new CloudTemplateCatalogLock())->getConnection()->transaction(
static function () use ($leaseKind, $token): string {
$catalogLock = self::lockCatalog();
self::assertImportAllowedLocked($catalogLock);
$now = time();
$catalogLock->lease_token = $token;
$catalogLock->lease_kind = $leaseKind;
$catalogLock->lease_until = $now + self::IMPORT_LEASE_SECONDS;
$catalogLock->updated_at = $now;
$catalogLock->save();
return $token;
},
3
);
}
private static function clearImportLeaseSafely(string $token): void
{
try {
(new CloudTemplateCatalogLock())->getConnection()->transaction(
static function () use ($token): void {
$catalogLock = self::lockCatalog();
if (hash_equals(
(string) ($catalogLock->lease_token ?? ''), $token
)) {
$catalogLock->lease_token = null;
$catalogLock->lease_kind = null;
$catalogLock->lease_until = 0;
$catalogLock->updated_at = time();
$catalogLock->save();
}
},
3
);
} catch (\Throwable $error) {
error_log('Cloud template import lease cleanup failed: '
. get_class($error));
}
}
private static function assertImportAllowedLocked(
CloudTemplateCatalogLock $catalogLock
): void {
if (CloudTemplateRetirement::where('state', 'deleting')->exists()) {
throw new CloudError(
'有云镜像删除尚未完成,请刷新目录确认后再导入', 409
);
}
self::assertNoActiveImportLeaseLocked($catalogLock);
}
private static function assertNoActiveImportLeaseLocked(
CloudTemplateCatalogLock $catalogLock
): void {
$token = (string) ($catalogLock->lease_token ?? '');
$leaseUntil = max(0, (int) ($catalogLock->lease_until ?? 0));
if ($token !== '' && $leaseUntil > time()) {
throw new CloudError(
'已有云镜像导入正在进行,请等待完成后重试', 409
);
}
if ($token !== '' || $leaseUntil !== 0
|| ($catalogLock->lease_kind ?? null) !== null) {
// A crashed PHP worker leaves a durable gate. Once its generous
// deadline passes, the next serialized operation repairs it.
$catalogLock->lease_token = null;
$catalogLock->lease_kind = null;
$catalogLock->lease_until = 0;
$catalogLock->updated_at = time();
$catalogLock->save();
}
}
private static function lockCatalog(): CloudTemplateCatalogLock
{
CloudTemplateCatalogLock::query()->insertOrIgnore([
'id' => 1, 'updated_at' => 0,
]);
$lock = CloudTemplateCatalogLock::where('id', 1)
->lockForUpdate()->first();
if ($lock === null) {
throw new CloudError('无法锁定云镜像目录', 503);
}
return $lock;
}
private static function assertNoReferences(string $templateId): void
{
foreach (CloudOrder::whereIn('status', self::ACTIVE_STATUSES)
->lockForUpdate()->get(['id', 'spec_json']) as $order) {
try {
$spec = json_decode((string) $order->spec_json, true, 32,
JSON_THROW_ON_ERROR);
} catch (\JsonException $error) {
throw new CloudError(
'未完成开通订单的数据无法核对,暂不能删除云镜像', 503
);
}
if (!is_array($spec) || array_is_list($spec)
|| !is_string($spec['source'] ?? null)) {
throw new CloudError(
'未完成开通订单的数据无法核对,暂不能删除云镜像', 503
);
}
if ($spec['source'] !== 'cloud') {
continue;
}
$referenced = $spec['template_id'] ?? null;
if (!is_string($referenced)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D', $referenced) !== 1) {
throw new CloudError(
'未完成开通订单的云镜像无法核对,暂不能删除', 503
);
}
if ($referenced === $templateId) {
throw new CloudError(
'云镜像仍被未完成的开通账单或创建任务使用,暂不能删除', 409
);
}
}
foreach (CloudJob::whereIn('kind', ['create', 'reinstall'])
->whereIn('status', ['queued', 'running', 'uncertain'])
->lockForUpdate()->get(['id', 'kind', 'payload_cipher']) as $job) {
$payload = CloudSecrets::decrypt((string) $job->payload_cipher);
$body = $payload['body'] ?? null;
if (!is_array($body) || array_is_list($body)) {
throw new CloudError(
'未完成虚拟机任务的数据无法核对,暂不能删除云镜像', 503
);
}
if ((string) $job->kind === 'create'
&& ($body['source'] ?? null) !== 'cloud') {
if (!is_string($body['source'] ?? null)) {
throw new CloudError(
'未完成开通任务的数据无法核对,暂不能删除云镜像', 503
);
}
continue;
}
$referenced = $body['template_id'] ?? null;
if (!is_string($referenced)
|| preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D', $referenced) !== 1) {
throw new CloudError(
'未完成虚拟机任务的云镜像无法核对,暂不能删除', 503
);
}
if ($referenced === $templateId) {
throw new CloudError(
'云镜像仍被未完成的创建或重装任务使用,暂不能删除', 409
);
}
}
}
}