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/CloudUserIsoService.php

1766 lines
69 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudJob;
use App\Models\CloudTemplateReplica;
use App\Models\CloudUserIso;
use App\Models\CloudUserIsoReplica;
/** PHP-owned personal ISO catalog backed by single-dispatch replica jobs. */
final class CloudUserIsoService
{
private const MAX_ISOS = 5;
private const MAX_TOTAL_BYTES = 10 * 1024 * 1024 * 1024;
public static function listing(int $userId): array
{
$rows = CloudUserIso::query()->where('user_id', $userId)
->whereIn('state', [
'importing', 'active', 'import_failed', 'deleting',
'delete_failed',
])->orderByDesc('created_at')->get()
->map(static fn (CloudUserIso $iso): array => self::dto($iso))
->all();
$active = CloudUserIso::query()->where('user_id', $userId)
->where('state', 'importing')->orderByDesc('created_at')->first();
$result = ['isos' => $rows];
if ($active !== null && (int) ($active->active_job_id ?? 0) > 0) {
$task = CloudJob::find((int) $active->active_job_id);
if ($task !== null && $task->parent_job_id === null) {
$result['active_import_job'] =
CloudJobService::acknowledgement($task);
}
}
return $result;
}
public static function import(array $input, array $actor): array
{
CloudInput::only($input, ['name', 'url']);
$userId = (int) ($actor['user_id'] ?? 0);
if ($userId < 1) {
throw new CloudError('账户身份无效', 401);
}
$name = self::name($input['name'] ?? null);
$url = self::remoteUrl($input['url'] ?? null);
$hosts = array_values(array_unique(array_map('intval',
CloudAgentSettingsService::enabledHostIds())));
sort($hosts, SORT_NUMERIC);
if ($hosts === []) {
throw new CloudError('没有可用的宿主机 Agent', 503);
}
$connection = (new CloudUserIso())->getConnection();
$parent = $connection->transaction(static function () use (
$actor, $userId, $name, $url, $hosts
): CloudJob {
[$parent, $created] = CloudJobService::reserveAggregate(
$actor, 'user_iso_import', [
'name' => $name,
'url' => $url,
'host_ids' => $hosts,
]
);
if (!$created) {
return $parent;
}
$rows = self::lockIsoRowsForImport($userId);
if ($rows->count() >= self::MAX_ISOS) {
throw new CloudError('每个用户最多保存 5 个 ISO', 409);
}
if ($rows->contains(static fn ($row): bool => in_array(
(string) $row->state, ['importing', 'deleting'], true
))) {
throw new CloudError('已有用户 ISO 任务正在执行', 409);
}
$now = time();
$iso = new CloudUserIso();
$iso->id = bin2hex(random_bytes(16));
$iso->user_id = $userId;
$iso->name = $name;
$iso->sealed_source_url = CloudSecrets::encrypt(
['url' => $url], 'user-iso-source'
);
$iso->size_bytes = 0;
$iso->sha256 = '';
$iso->state = 'importing';
$iso->import_operation_id = (string) $parent->operation_id;
$iso->active_job_id = (int) $parent->id;
$iso->created_at = $iso->updated_at = $now;
$iso->save();
foreach ($hosts as $hostId) {
$operationId = bin2hex(random_bytes(16));
$child = CloudJobService::reserveAgentMutation(
$actor, $hostId, 'user_iso_import_replica', 'POST',
'/v1/user-isos', [
'id' => (string) $iso->id,
'owner_id' => $userId,
'name' => $name,
'source_url' => $url,
], [
'owner_id' => $userId,
'user_iso_id' => (string) $iso->id,
], (int) $parent->id, $operationId
);
$replica = new CloudUserIsoReplica();
$replica->host_id = $hostId;
$replica->user_iso_id = (string) $iso->id;
$replica->user_id = $userId;
$replica->state = 'importing';
$replica->size_bytes = 0;
$replica->sha256 = '';
$replica->operation_id = $operationId;
$replica->job_id = (int) $child->id;
$replica->error_code = '';
$replica->created_at = $replica->updated_at = $now;
$replica->save();
}
return $parent;
}, 3);
return CloudJobService::acknowledgement($parent);
}
public static function delete(string $id, array $actor): array
{
if (preg_match('/^[a-f0-9]{32}$/D', $id) !== 1) {
throw new CloudError('ISO 镜像标识无效');
}
$userId = (int) ($actor['user_id'] ?? 0);
$connection = (new CloudUserIso())->getConnection();
$parent = $connection->transaction(static function () use (
$id, $actor, $userId
): CloudJob {
$iso = self::lockIsoForMutation($id, $userId);
if ($iso === null || (string) $iso->state === 'retired') {
throw new CloudError('ISO 不存在或不属于当前用户', 404);
}
$partialCleanupKind = self::settledMixedCleanupParentKind($iso);
$partialCleanup = ($partialCleanupKind === 'user_iso_import'
&& (string) $iso->state === 'import_failed')
|| ($partialCleanupKind === 'user_iso_delete'
&& (string) $iso->state === 'delete_failed');
$quotaCleanup = (string) $iso->state === 'import_failed'
&& self::quotaExceededImportCleanupAllowed($iso, false);
if ((string) $iso->state !== 'active'
&& !$quotaCleanup
&& !$partialCleanup) {
throw new CloudError(
'ISO 尚有未确认或失败的任务,禁止重复下发删除', 409
);
}
[$parent, $created] = CloudJobService::reserveAggregate(
$actor, 'user_iso_delete', ['iso_id' => $id]
);
if (!$created) {
return $parent;
}
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', $id)->orderBy('host_id')
->lockForUpdate()->get();
$allowedReplicaStates = match ($partialCleanupKind) {
'user_iso_import' => ['active', 'failed'],
'user_iso_delete' => ['delete_succeeded', 'delete_failed'],
default => ['active'],
};
if ($replicas->isEmpty() || $replicas->contains(
static fn ($row): bool => !in_array(
(string) $row->state,
$allowedReplicaStates,
true
)
)) {
throw new CloudError('ISO 副本账本不完整,禁止删除', 409);
}
if ($partialCleanupKind === 'user_iso_import') {
CloudUserIsoReplica::query()->where('user_iso_id', $id)
->where('state', 'failed')->delete();
} elseif ($partialCleanupKind === 'user_iso_delete') {
CloudUserIsoReplica::query()->where('user_iso_id', $id)
->where('state', 'delete_succeeded')->delete();
CloudUserIsoReplica::query()->where('user_iso_id', $id)
->where('state', 'delete_failed')->update([
'state' => 'active',
'error_code' => '',
'updated_at' => time(),
]);
}
if ($partialCleanup) {
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', $id)->orderBy('host_id')
->lockForUpdate()->get();
if ($replicas->isEmpty() || $replicas->contains(
static fn ($row): bool => (string) $row->state !== 'active'
)) {
throw new CloudError('ISO 部分结果账本不完整,禁止删除', 409);
}
}
$iso->state = 'deleting';
$iso->active_job_id = (int) $parent->id;
$iso->updated_at = time();
$iso->save();
foreach ($replicas as $replica) {
$operationId = bin2hex(random_bytes(16));
$child = CloudJobService::reserveAgentMutation(
$actor, (int) $replica->host_id,
'user_iso_delete_replica', 'DELETE',
'/v1/user-isos/' . rawurlencode($id)
. '?owner_id=' . $userId,
[], [
'owner_id' => $userId,
'user_iso_id' => $id,
], (int) $parent->id, $operationId
);
CloudUserIsoReplica::query()
->where('host_id', (int) $replica->host_id)
->where('user_iso_id', $id)->update([
'state' => 'deleting',
'operation_id' => $operationId,
'job_id' => (int) $child->id,
'error_code' => '',
'updated_at' => time(),
]);
}
return $parent;
}, 3);
return CloudJobService::acknowledgement($parent);
}
/** Called from the worker transaction for both receipts and deadlines. */
public static function finishJobLocked(
CloudJob $job,
string $status,
$result,
array $payload,
array $operation,
bool $recovery = false
): void {
if (!in_array((string) $job->kind, [
'user_iso_import_replica', 'user_iso_delete_replica',
], true)) {
return;
}
$isoId = is_string($payload['user_iso_id'] ?? null)
? $payload['user_iso_id'] : '';
$iso = CloudUserIso::query()->where('id', $isoId)
->lockForUpdate()->first();
$replica = CloudUserIsoReplica::query()
->where('host_id', (int) $job->host_id)
->where('user_iso_id', $isoId)
->where('job_id', (int) $job->id)->lockForUpdate()->first();
if ($iso === null || $replica === null) {
throw new CloudError('ISO 任务账本不存在', 409);
}
// Parent publication is deliberately deferred until CloudWorkerService
// has persisted this child's terminal effect. The shared recovered
// settle path then validates the full child / replica ledger.
if ((string) $job->kind === 'user_iso_import_replica') {
self::finishImportReplica($iso, $replica, $status, $result,
$operation, $recovery);
return;
}
self::finishDeleteReplica(
$iso, $replica, $status, $operation, $recovery
);
}
/** See CloudTemplateCatalogService::lockAggregateCatalogLocked(). */
public static function lockAggregateCatalogLocked(CloudJob $parent): void
{
if (!in_array((string) $parent->kind, [
'user_iso_import', 'user_iso_delete',
], true)) {
return;
}
self::isoBoundToParentLocked($parent);
}
/** See CloudTemplateCatalogService::settleRecoveredParentLocked(). */
public static function settleRecoveredParentLocked(
CloudJob $parent,
string $resolution
): void {
if (!in_array((string) $parent->kind, [
'user_iso_import', 'user_iso_delete',
], true) || (string) $parent->execution_mode !== 'aggregate'
|| ((string) $parent->phase === 'terminal'
&& ((string) $parent->outcome !== 'failed'
|| (string) $parent->effect_resolution !== 'unknown'))) {
return;
}
$iso = self::isoBoundToParentLocked($parent);
if (!in_array(
$resolution,
['applied', 'not_applied', 'mixed'],
true
)) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 汇总任务的子任务效果状态无效',
409
);
}
self::assertRecoveredReplicaProjectionLocked(
$iso,
$parent,
$resolution
);
if ($resolution === 'not_applied') {
self::restoreNotAppliedProjectionLocked($iso, $parent);
return;
}
$operation = ['effect_may_have_applied' => true];
if ((string) $parent->kind === 'user_iso_import') {
self::settleImportParent(
$iso,
(int) $parent->id,
$operation
);
} else {
self::settleDeleteParent(
$iso,
(int) $parent->id,
$operation
);
}
if ($resolution === 'mixed') {
$iso->active_job_id = null;
$iso->updated_at = time();
$iso->save();
}
}
/** Validate an already-published aggregate without changing its audit. */
public static function assertSettledParentProjectionLocked(
CloudJob $parent,
string $resolution
): void {
if (!in_array((string) $parent->kind, [
'user_iso_import', 'user_iso_delete',
], true) || (string) $parent->execution_mode !== 'aggregate') {
return;
}
if (!in_array(
$resolution,
['applied', 'not_applied', 'mixed'],
true
)) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 已发布汇总任务的子任务效果状态无效',
409
);
}
$iso = self::isoBoundToParentLocked($parent);
$children = CloudJob::where('parent_job_id', (int) $parent->id)
->orderBy('id')->lockForUpdate()->get();
if ($children->isEmpty()) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 已发布汇总任务缺少子任务账本',
409
);
}
$expectedChildKind = (string) $parent->kind === 'user_iso_import'
? 'user_iso_import_replica' : 'user_iso_delete_replica';
$childResolutions = [];
foreach ($children as $child) {
$childResolution = (string) $child->effect_resolution;
if ((int) ($child->parent_job_id ?? 0) !== (int) $parent->id
|| (int) $child->user_id !== (int) $parent->user_id
|| (string) $child->execution_mode !== 'agent'
|| (string) $child->kind !== $expectedChildKind
|| !in_array(
$childResolution,
['applied', 'not_applied'],
true
)) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 汇总任务与子任务账本关系异常',
409
);
}
$childResolutions[$childResolution] = true;
}
$actualResolution = count($childResolutions) === 1
? (string) array_key_first($childResolutions) : 'mixed';
if ($actualResolution !== $resolution) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 汇总任务与已结算效果不一致',
409
);
}
if (CloudTemplateReplica::query()->whereIn(
'job_id',
$children->map(
static fn (CloudJob $child): int => (int) $child->id
)->all()
)->exists()) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 汇总子任务被云镜像副本账本重复引用',
409
);
}
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->orderBy('host_id')->lockForUpdate()->get();
if ((string) $parent->kind === 'user_iso_import') {
if (!hash_equals(
(string) $parent->operation_id,
(string) $iso->import_operation_id
)) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 导入主记录与已发布汇总任务身份不一致',
409
);
}
if ($resolution === 'mixed') {
if ((string) $iso->state !== 'import_failed') {
throw new CloudRecoveryInstanceStateConflict(
'ISO 导入部分成功结论与目录状态不一致',
409
);
}
self::assertRecoveredReplicaProjectionLocked(
$iso,
$parent,
'mixed'
);
$iso->active_job_id = null;
$iso->updated_at = time();
$iso->save();
return;
}
if ($resolution === 'not_applied') {
if ((string) $iso->state !== 'retired'
|| (string) $iso->name !== ''
|| (string) $iso->sealed_source_url !== ''
|| (int) $iso->size_bytes !== 0
|| (string) $iso->sha256 !== ''
|| !$replicas->isEmpty()) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 导入无效果结论与清理状态不一致',
409
);
}
return;
}
if ((string) $iso->state !== 'active'
&& !((string) $iso->state === 'import_failed'
&& (string) $parent->outcome === 'failed')) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 导入已生效结论与目录状态不一致',
409
);
}
self::assertRecoveredReplicaProjectionLocked(
$iso,
$parent,
'applied'
);
return;
}
if ($resolution === 'mixed') {
if ((string) $iso->state !== 'delete_failed') {
throw new CloudRecoveryInstanceStateConflict(
'ISO 删除部分成功结论与目录状态不一致',
409
);
}
self::assertRecoveredReplicaProjectionLocked(
$iso,
$parent,
'mixed'
);
$iso->active_job_id = null;
$iso->updated_at = time();
$iso->save();
return;
}
if ($resolution === 'applied') {
if ((string) $iso->state !== 'retired'
|| (string) $iso->name !== ''
|| (string) $iso->sealed_source_url !== ''
|| (int) $iso->size_bytes !== 0
|| (string) $iso->sha256 !== ''
|| !$replicas->isEmpty()) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 删除已生效结论与清理状态不一致',
409
);
}
return;
}
if ((string) $iso->state !== 'active') {
throw new CloudRecoveryInstanceStateConflict(
'ISO 删除无效果结论未恢复目录状态',
409
);
}
if ($children->isEmpty() || $replicas->count() !== $children->count()) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 删除无效果结论的子任务与副本数量不一致',
409
);
}
foreach ($children as $child) {
$matches = $replicas->filter(
static fn (CloudUserIsoReplica $replica): bool =>
(int) ($replica->job_id ?? 0) === (int) $child->id
);
$replica = $matches->count() === 1 ? $matches->first() : null;
if ($replica === null
|| (string) $child->effect_resolution !== 'not_applied'
|| (int) $replica->host_id !== (int) $child->host_id
|| (int) $replica->user_id !== (int) $child->user_id
|| (int) $replica->user_id !== (int) $iso->user_id
|| !hash_equals(
(string) $child->operation_id,
(string) $replica->operation_id
)) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 删除无效果结论的子任务与副本身份不一致',
409
);
}
if ((string) $replica->state !== 'active'
|| (int) $replica->size_bytes !== (int) $iso->size_bytes
|| !hash_equals(
(string) $iso->sha256,
(string) $replica->sha256
)) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 删除无效果结论的副本状态或内容不一致',
409
);
}
}
}
/** See CloudTemplateCatalogService::assertRecoveredReplicaProjectionLocked(). */
private static function assertRecoveredReplicaProjectionLocked(
CloudUserIso $iso,
CloudJob $parent,
string $resolution,
bool $lockForUpdate = true
): void {
if ((int) $iso->user_id !== (int) $parent->user_id) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 主记录与汇总任务用户身份不一致',
409
);
}
if ((string) $parent->kind === 'user_iso_import'
&& !hash_equals(
(string) $parent->operation_id,
(string) $iso->import_operation_id
)) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 导入主记录与汇总任务身份不一致',
409
);
}
$childQuery = CloudJob::where(
'parent_job_id',
(int) $parent->id
)->orderBy('id');
$replicaQuery = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->orderBy('host_id');
if ($lockForUpdate) {
$childQuery->lockForUpdate();
$replicaQuery->lockForUpdate();
}
$children = $childQuery->get();
$replicas = $replicaQuery->get();
if ($children->isEmpty() || $replicas->count() !== $children->count()) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 汇总任务的子任务与副本数量不一致',
409
);
}
if (CloudTemplateReplica::query()->whereIn(
'job_id',
$children->map(
static fn (CloudJob $child): int => (int) $child->id
)->all()
)->exists()) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 汇总子任务被云镜像副本账本重复引用',
409
);
}
foreach ($children as $child) {
$childResolution = $resolution === 'mixed'
? (string) $child->effect_resolution : $resolution;
$expectedState = (string) $parent->kind === 'user_iso_import'
? ($childResolution === 'applied' ? 'active' : 'failed')
: ($childResolution === 'applied'
? 'delete_succeeded' : 'delete_failed');
$matches = $replicas->filter(
static fn (CloudUserIsoReplica $replica): bool =>
(int) ($replica->job_id ?? 0) === (int) $child->id
);
$replica = $matches->count() === 1 ? $matches->first() : null;
if ($replica === null
|| (int) $replica->host_id !== (int) $child->host_id
|| (int) $replica->user_id !== (int) $child->user_id
|| (int) $replica->user_id !== (int) $iso->user_id
|| !hash_equals(
(string) $child->operation_id,
(string) $replica->operation_id
)) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 汇总任务的子任务与副本身份不一致',
409
);
}
if ((string) $replica->state !== $expectedState
|| ((string) $parent->kind === 'user_iso_import'
&& $childResolution === 'applied'
&& ((int) $replica->size_bytes
!== (int) $iso->size_bytes
|| !hash_equals(
(string) $iso->sha256,
(string) $replica->sha256
)))
|| ((string) $parent->kind === 'user_iso_delete'
&& $childResolution === 'not_applied'
&& ((int) $replica->size_bytes
!== (int) $iso->size_bytes
|| !hash_equals(
(string) $iso->sha256,
(string) $replica->sha256
)))) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 汇总任务的副本状态或内容不一致',
409
);
}
}
}
/** Restore the resource boundary only when every host proves no effect. */
private static function restoreNotAppliedProjectionLocked(
CloudUserIso $iso,
CloudJob $parent
): void {
if ((string) $parent->kind === 'user_iso_import') {
CloudUserIsoReplica::query()->where(
'user_iso_id',
(string) $iso->id
)->delete();
$iso->name = '';
$iso->sealed_source_url = '';
$iso->size_bytes = 0;
$iso->sha256 = '';
$iso->state = 'retired';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent,
'failed',
false,
true,
'user_iso_import_not_applied'
);
return;
}
CloudUserIsoReplica::query()->where(
'user_iso_id',
(string) $iso->id
)->update([
'state' => 'active',
'error_code' => '',
'updated_at' => time(),
]);
$iso->state = 'active';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent,
'failed',
false,
true,
'user_iso_delete_not_applied'
);
}
/** Close a batch whose absolute parent deadline elapsed. */
public static function expireAggregateLocked(CloudJob $parent): void
{
if (!in_array((string) $parent->kind,
['user_iso_import', 'user_iso_delete'], true)
|| (string) $parent->phase === 'terminal') {
return;
}
$iso = CloudUserIso::query()
->where('active_job_id', (int) $parent->id)
->lockForUpdate()->first();
if ($iso === null) {
CloudJobService::completeAggregateLocked(
$parent, 'failed', true, false,
'aggregate_ledger_missing'
);
return;
}
$children = CloudJob::where('parent_job_id', (int) $parent->id)
->lockForUpdate()->get();
$effect = $children->contains(static fn ($child): bool =>
CloudDispatchLedger::mayHaveDispatched($child)
|| (bool) $child->effect_may_have_applied);
if ((string) $parent->kind === 'user_iso_import') {
CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->where('state', 'importing')->update([
'state' => 'failed',
'error_code' => 'aggregate_deadline_exceeded',
'updated_at' => time(),
]);
$iso->state = 'import_failed';
} else {
CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->where('state', 'deleting')->update([
'state' => 'delete_failed',
'error_code' => 'aggregate_deadline_exceeded',
'updated_at' => time(),
]);
$iso->state = 'delete_failed';
}
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent, 'failed', $effect, !$effect,
'aggregate_deadline_exceeded'
);
}
private static function finishImportReplica(CloudUserIso $iso,
CloudUserIsoReplica $replica, string $status, $result,
array $operation, bool $recovery): void
{
if ((string) $replica->state !== 'importing'
&& !($recovery && (string) $replica->state === 'failed')) {
throw new CloudError(
'ISO 副本状态与导入任务恢复证据不一致',
409
);
}
if ($status === 'succeeded') {
$digest = is_array($result)
? strtolower((string) ($result['sha256'] ?? '')) : '';
$bytes = is_array($result) ? filter_var(
$result['size_bytes'] ?? null,
FILTER_VALIDATE_INT,
['options' => [
'min_range' => 1,
'max_range' => self::MAX_TOTAL_BYTES,
]]
) : false;
if (!is_array($result) || array_is_list($result)
|| ($result['id'] ?? null) !== (string) $iso->id
|| (int) ($result['owner_id'] ?? 0) !== (int) $iso->user_id
|| ($result['name'] ?? null) !== (string) $iso->name
|| $bytes === false
|| preg_match('/^[a-f0-9]{64}$/D', $digest) !== 1
|| ((string) $iso->sha256 !== ''
&& !hash_equals((string) $iso->sha256, $digest))
|| ((int) $iso->size_bytes > 0
&& (int) $iso->size_bytes !== (int) $bytes)) {
throw new CloudError('Agent ISO 成功回执与请求不匹配', 502);
}
if ((string) $iso->sha256 === '') {
$iso->sha256 = $digest;
$iso->size_bytes = (int) $bytes;
$iso->updated_at = time();
$iso->save();
}
$replica->state = 'active';
$replica->size_bytes = (int) $bytes;
$replica->sha256 = $digest;
$replica->error_code = '';
} else {
$replica->state = 'failed';
$replica->error_code = self::errorCode(
$operation, 'user_iso_import_failed'
);
}
CloudUserIsoReplica::query()
->where('host_id', (int) $replica->host_id)
->where('user_iso_id', (string) $replica->user_iso_id)
->where('job_id', (int) $replica->job_id)->update([
'state' => (string) $replica->state,
'size_bytes' => (int) $replica->size_bytes,
'sha256' => (string) $replica->sha256,
'error_code' => (string) $replica->error_code,
'updated_at' => time(),
]);
}
private static function settleImportParent(CloudUserIso $iso,
int $parentId, array $operation,
?int $completingChildId = null): void
{
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)->orderBy('host_id')
->lockForUpdate()->get();
if ($replicas->isEmpty() || $replicas->contains(
static fn ($row): bool => (string) $row->state === 'importing'
)) {
return;
}
$parent = CloudJob::where('id', $parentId)->lockForUpdate()->first();
if ($parent === null || (string) $parent->kind !== 'user_iso_import') {
throw new CloudError('ISO 聚合任务不存在', 409);
}
self::assertIsoParentBindingLocked($iso, $parent);
if (CloudJobService::aggregateParentRecoveryPending(
$parent,
$completingChildId,
true
)) {
return;
}
$allActive = !$replicas->contains(static fn ($row): bool =>
(string) $row->state !== 'active');
$quotaUsed = (int) CloudUserIso::query()
->where('user_id', (int) $iso->user_id)
->where('id', '<>', (string) $iso->id)
->where('state', 'active')->sum('size_bytes');
$quotaOkay = $quotaUsed + (int) $iso->size_bytes
<= self::MAX_TOTAL_BYTES;
if ($allActive && $quotaOkay) {
$iso->state = 'active';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent, 'succeeded', true, false, '',
['iso_id' => (string) $iso->id]
);
return;
}
$effect = $replicas->contains(static fn ($row): bool =>
(string) $row->state === 'active')
|| ($operation['effect_may_have_applied'] ?? false) === true
|| CloudJob::where('parent_job_id', $parentId)
->where('effect_may_have_applied', 1)->exists();
$iso->state = 'import_failed';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent, 'failed', $effect, false,
$quotaOkay ? 'user_iso_import_failed' : 'user_iso_quota_exceeded'
);
}
private static function finishDeleteReplica(CloudUserIso $iso,
CloudUserIsoReplica $replica, string $status,
array $operation, bool $recovery): void
{
if ((string) $replica->state !== 'deleting'
&& !($recovery
&& (string) $replica->state === 'delete_failed')) {
throw new CloudError(
'ISO 副本状态与删除任务恢复证据不一致',
409
);
}
if ($status === 'succeeded') {
if (!CloudAgentCatalogDeletion::confirms(
$operation, 'user_iso', (string) $iso->id,
(int) $iso->user_id
)) {
throw new CloudError('Agent ISO 删除回执缺少清理证据', 502);
}
$replica->state = 'delete_succeeded';
$replica->error_code = '';
} else {
$replica->state = 'delete_failed';
$replica->error_code = self::errorCode(
$operation, 'user_iso_delete_failed'
);
}
CloudUserIsoReplica::query()
->where('host_id', (int) $replica->host_id)
->where('user_iso_id', (string) $replica->user_iso_id)
->where('job_id', (int) $replica->job_id)->update([
'state' => (string) $replica->state,
'error_code' => (string) $replica->error_code,
'updated_at' => time(),
]);
}
private static function settleDeleteParent(CloudUserIso $iso,
int $parentId, array $operation,
?int $completingChildId = null): void
{
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)->orderBy('host_id')
->lockForUpdate()->get();
if ($replicas->isEmpty() || $replicas->contains(static fn ($row): bool =>
(string) $row->state === 'deleting')) {
return;
}
$parent = CloudJob::where('id', $parentId)->lockForUpdate()->first();
if ($parent === null || (string) $parent->kind !== 'user_iso_delete') {
throw new CloudError('ISO 删除聚合任务不存在', 409);
}
self::assertIsoParentBindingLocked($iso, $parent);
if (CloudJobService::aggregateParentRecoveryPending(
$parent,
$completingChildId,
true
)) {
return;
}
$allDeleted = !$replicas->contains(static fn ($row): bool =>
(string) $row->state !== 'delete_succeeded');
if ($allDeleted) {
CloudUserIsoReplica::query()->where(
'user_iso_id', (string) $iso->id
)->delete();
$iso->name = '';
$iso->sealed_source_url = '';
$iso->size_bytes = 0;
$iso->sha256 = '';
$iso->state = 'retired';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent, 'succeeded', true, false, '',
['iso_id' => (string) $iso->id, 'deleted' => true]
);
return;
}
$effect = $replicas->contains(static fn ($row): bool =>
(string) $row->state === 'delete_succeeded')
|| ($operation['effect_may_have_applied'] ?? false) === true
|| CloudJob::where('parent_job_id', $parentId)
->where('effect_may_have_applied', 1)->exists();
$iso->state = 'delete_failed';
$iso->updated_at = time();
$iso->save();
CloudJobService::completeAggregateLocked(
$parent, 'failed', $effect, false, 'user_iso_delete_failed'
);
}
private static function dto(CloudUserIso $iso): array
{
$partialCleanupKind = self::settledMixedCleanupParentKind($iso);
$quotaCleanup = (string) $iso->state === 'import_failed'
&& self::quotaExceededImportCleanupAllowed($iso, false);
$partialCleanup = $partialCleanupKind !== null;
$result = [
'id' => (string) $iso->id,
'name' => (string) $iso->name,
'size_bytes' => (int) $iso->size_bytes,
'sha256' => (string) $iso->sha256,
'state' => (string) $iso->state,
'created_at' => (int) $iso->created_at,
'can_delete' => ((string) $iso->state === 'active'
&& self::activeJobSafelySettled($iso, false))
|| $quotaCleanup
|| $partialCleanup,
];
if ($partialCleanup) {
$result['cleanup_reason'] = $partialCleanupKind
=== 'user_iso_import'
? 'partial_import' : 'partial_delete';
} elseif ($quotaCleanup) {
$result['cleanup_reason'] = 'quota_exceeded_after_import';
}
return $result;
}
private static function errorCode(array $operation, string $fallback): string
{
$code = $operation['error_code'] ?? null;
return is_string($code)
&& preg_match('/^[a-z][a-z0-9_]{0,63}$/D', $code) === 1
? $code : $fallback;
}
private static function isoBoundToParentLocked(
CloudJob $parent
): CloudUserIso {
$isos = CloudUserIso::query()
->where('active_job_id', (int) $parent->id)
->orderBy('id')
->lockForUpdate()
->get();
if ($isos->count() > 1) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 汇总任务绑定了多个目录主记录',
409
);
}
if ($isos->isEmpty()) {
return self::repairMissingParentBindingLocked($parent);
}
$iso = $isos->first();
if ((int) $iso->user_id !== (int) $parent->user_id) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 汇总任务与目录主记录所属人不一致',
409
);
}
return $iso;
}
/**
* Recover a NULL/zero active_job_id only from one complete immutable
* child-to-replica identity chain. No Agent request or replacement task is
* ever created here.
*/
public static function repairMissingParentBindingLocked(
CloudJob $parent
): CloudUserIso {
$kind = (string) $parent->kind;
$expectedChildKind = match ($kind) {
'user_iso_import' => 'user_iso_import_replica',
'user_iso_delete' => 'user_iso_delete_replica',
default => null,
};
$expectedStates = match ($kind) {
'user_iso_import' => ['importing', 'import_failed', 'active'],
'user_iso_delete' => ['deleting', 'delete_failed', 'active'],
default => [],
};
if ($expectedChildKind === null
|| $expectedStates === []
|| (string) $parent->execution_mode !== 'aggregate'
|| $parent->parent_job_id !== null
|| (int) $parent->user_id < 1
|| CloudJobService::hasAggregateLocalStateConflict($parent)) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 活动任务指针缺失,且父任务状态不能证明唯一目录绑定',
409
);
}
$childIds = CloudJob::where('parent_job_id', (int) $parent->id)
->orderBy('id')->pluck('id')->map(
static fn ($id): int => (int) $id
)->all();
if ($childIds === []) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 活动任务指针缺失,且父任务没有子任务账本',
409
);
}
$candidateIds = CloudUserIsoReplica::query()
->whereIn('job_id', $childIds)
->orderBy('user_iso_id')
->pluck('user_iso_id')
->map(static fn ($id): string => (string) $id)
->unique()->values();
if ($candidateIds->isEmpty()) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 活动任务指针缺失,副本账本无法定位目录主记录',
409
);
}
if ($candidateIds->count() !== 1) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务指针缺失,副本账本指向多个目录主记录',
409
);
}
$iso = CloudUserIso::query()
->where('id', (string) $candidateIds->first())
->lockForUpdate()->first();
if ($iso === null) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 活动任务指针缺失,副本账本引用的目录主记录不存在',
409
);
}
$currentJobId = (int) ($iso->active_job_id ?? 0);
if ($currentJobId > 0 && $currentJobId !== (int) $parent->id) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务正数指针指向其他任务,不能自动改写',
409
);
}
if (!in_array((string) $iso->state, $expectedStates, true)) {
throw new CloudRecoveryInstanceStateConflict(
'ISO 活动任务指针或目录状态与原父任务不一致',
409
);
}
if ((int) $iso->user_id !== (int) $parent->user_id
|| ($kind === 'user_iso_import'
&& !hash_equals(
(string) $parent->operation_id,
(string) $iso->import_operation_id
))) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务指针缺失,目录主记录与父任务身份不一致',
409
);
}
$otherBindings = CloudUserIso::query()
->where('active_job_id', (int) $parent->id)
->where('id', '<>', (string) $iso->id)
->orderBy('id')->lockForUpdate()->get(['id']);
if (!$otherBindings->isEmpty()) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 父任务已绑定其他目录主记录,不能修复活动任务指针',
409
);
}
$children = CloudJob::where('parent_job_id', (int) $parent->id)
->orderBy('id')->lockForUpdate()->get();
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->orderBy('host_id')->lockForUpdate()->get();
if (CloudTemplateReplica::query()->whereIn(
'job_id',
$childIds
)->exists()) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务子任务被云镜像副本账本重复引用',
409
);
}
if ($children->count() !== count($childIds)
|| $replicas->count() !== $children->count()) {
throw new CloudRecoveryAggregateStateConflict(
'ISO 活动任务指针缺失,子任务与副本数量不一致',
409
);
}
foreach ($children as $child) {
$matches = $replicas->filter(
static fn (CloudUserIsoReplica $replica): bool =>
(int) ($replica->job_id ?? 0) === (int) $child->id
);
$replica = $matches->count() === 1 ? $matches->first() : null;
if ($replica === null
|| (int) ($child->parent_job_id ?? 0) !== (int) $parent->id
|| (int) $child->user_id !== (int) $parent->user_id
|| (string) $child->execution_mode !== 'agent'
|| (string) $child->kind !== $expectedChildKind
|| (int) $child->host_id < 1
|| (int) $replica->host_id !== (int) $child->host_id
|| (int) $replica->user_id !== (int) $child->user_id
|| (int) $replica->user_id !== (int) $iso->user_id
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $child->operation_id
) !== 1
|| !hash_equals(
(string) $child->operation_id,
(string) $replica->operation_id
)) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务指针缺失,子任务与副本身份不一致',
409
);
}
}
if ($currentJobId !== (int) $parent->id) {
$update = CloudUserIso::query()
->where('id', (string) $iso->id)
->where(static function ($missing): void {
$missing->whereNull('active_job_id')
->orWhere('active_job_id', '<=', 0);
});
$updated = $update->update([
'active_job_id' => (int) $parent->id,
'updated_at' => time(),
]);
if ($updated !== 1) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 活动任务指针在修复时发生并发变化',
409
);
}
$iso->refresh();
}
return $iso;
}
private static function assertIsoParentBindingLocked(
CloudUserIso $iso,
CloudJob $parent
): void {
$bound = self::isoBoundToParentLocked($parent);
if (!hash_equals((string) $bound->id, (string) $iso->id)
|| (int) $iso->active_job_id !== (int) $parent->id
|| (int) $iso->user_id !== (int) $parent->user_id) {
throw new CloudRecoveryOperationIdentityConflict(
'ISO 汇总任务与目录主记录身份不一致',
409
);
}
}
/** Lock one ISO in parent -> catalog order before a mutation. */
private static function lockIsoForMutation(
string $id,
int $userId
): ?CloudUserIso {
$snapshot = CloudUserIso::query()->where('id', $id)
->where('user_id', $userId)->first();
if ($snapshot === null) {
return null;
}
$jobId = (int) ($snapshot->active_job_id ?? 0);
$parentSnapshot = $jobId > 0
? CloudJob::where('id', $jobId)->first() : null;
$parent = self::isIsoAggregateParent($parentSnapshot)
? CloudJob::where('id', $jobId)
->where('execution_mode', 'aggregate')
->whereNull('parent_job_id')
->whereIn('kind', [
'user_iso_import',
'user_iso_delete',
])->lockForUpdate()->first()
: null;
$iso = CloudUserIso::query()->where('id', $id)
->where('user_id', $userId)->lockForUpdate()->first();
if ($iso === null) {
return null;
}
if ((int) ($iso->active_job_id ?? 0) !== $jobId) {
throw new CloudError('ISO 任务状态已变化,请刷新后重试', 409);
}
self::assertMutationBoundaryLocked($iso, $parent);
return $iso;
}
/** Never take a child/dangling pointer as the canonical parent lock. */
private static function isIsoAggregateParent(?CloudJob $job): bool
{
return $job !== null
&& (string) $job->execution_mode === 'aggregate'
&& $job->parent_job_id === null
&& in_array((string) $job->kind, [
'user_iso_import',
'user_iso_delete',
], true);
}
/** Lock all existing ISO parents before their catalog rows. */
private static function lockIsoRowsForImport(int $userId)
{
$snapshots = CloudUserIso::query()->where('user_id', $userId)
->where('state', '<>', 'retired')->orderBy('id')
->get(['id', 'active_job_id']);
$jobIds = $snapshots->map(
static fn (CloudUserIso $iso): int =>
(int) ($iso->active_job_id ?? 0)
)->filter(static fn (int $jobId): bool => $jobId > 0)
->unique()->sort()->values()->all();
$parentSnapshots = $jobIds === [] ? collect()
: CloudJob::whereIn('id', $jobIds)->get()->keyBy('id');
$parentIds = $parentSnapshots->filter(
static fn (CloudJob $job): bool =>
self::isIsoAggregateParent($job)
)->keys()->map(static fn ($id): int => (int) $id)
->sort()->values()->all();
$parents = $parentIds === [] ? collect()
: CloudJob::whereIn('id', $parentIds)
->where('execution_mode', 'aggregate')
->whereNull('parent_job_id')
->whereIn('kind', [
'user_iso_import',
'user_iso_delete',
])->orderBy('id')->lockForUpdate()->get()->keyBy('id');
$rows = CloudUserIso::query()->where('user_id', $userId)
->where('state', '<>', 'retired')->orderBy('id')
->lockForUpdate()->get();
if ($rows->count() !== $snapshots->count()) {
throw new CloudError('ISO 目录状态已变化,请刷新后重试', 409);
}
foreach ($rows as $index => $row) {
$snapshot = $snapshots->get($index);
$jobId = (int) ($snapshot->active_job_id ?? 0);
if ($snapshot === null
|| !hash_equals((string) $snapshot->id, (string) $row->id)
|| (int) ($row->active_job_id ?? 0) !== $jobId) {
throw new CloudError('ISO 目录状态已变化,请刷新后重试', 409);
}
self::assertMutationBoundaryLocked(
$row,
$jobId > 0 ? $parents->get($jobId) : null,
'已有用户 ISO 任务尚未安全收敛'
);
}
return $rows;
}
/** Do not overlap a catalog mutation with an invalid prior job ledger. */
private static function assertMutationBoundaryLocked(
CloudUserIso $iso,
?CloudJob $lockedParent = null,
string $message = 'ISO 上一任务尚未安全收敛,禁止提交新的操作'
): void {
if (!self::activeJobSafelySettled(
$iso,
true,
$lockedParent
)) {
throw new CloudError($message, 409);
}
}
private static function activeJobSafelySettled(
CloudUserIso $iso,
bool $lockForUpdate,
?CloudJob $lockedParent = null
): bool {
$jobId = (int) ($iso->active_job_id ?? 0);
if ($jobId < 1) {
return !self::missingPointerRecoveryPending($iso);
}
if ($iso->exists) {
$bindings = CloudUserIso::query()
->where('active_job_id', $jobId)
->orderBy('id');
if ($lockForUpdate) {
$bindings->lockForUpdate();
}
$bound = $bindings->get(['id']);
if ($bound->count() !== 1
|| (string) $bound->first()->id !== (string) $iso->id) {
return false;
}
}
if ($lockedParent !== null
&& (int) $lockedParent->id !== $jobId) {
return false;
}
if ($lockForUpdate && $lockedParent === null) {
return false;
}
$job = $lockedParent ?? CloudJob::where('id', $jobId)->first();
return $job !== null && self::aggregateParentSafelySettled(
$iso,
$job,
$lockForUpdate
) && self::currentParentProjectionMatches(
$iso,
$job,
$lockForUpdate
);
}
/** The current ISO must own every child and replica of its parent. */
private static function currentParentProjectionMatches(
CloudUserIso $iso,
CloudJob $parent,
bool $lockForUpdate
): bool {
$expectedChildKind = match ((string) $parent->kind) {
'user_iso_import' => 'user_iso_import_replica',
'user_iso_delete' => 'user_iso_delete_replica',
default => null,
};
$projectionStateMatches = match ((string) $parent->kind) {
'user_iso_import' => in_array(
(string) $iso->state,
['active', 'import_failed'],
true
) && hash_equals(
(string) $parent->operation_id,
(string) $iso->import_operation_id
),
'user_iso_delete' => in_array(
(string) $iso->state,
['active', 'delete_failed'],
true
),
default => false,
};
if ($expectedChildKind === null || !$projectionStateMatches) {
return false;
}
$childQuery = CloudJob::where(
'parent_job_id',
(int) $parent->id
)->orderBy('id');
if ($lockForUpdate) {
$childQuery->lockForUpdate();
}
$children = $childQuery->get();
$replicaQuery = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->orderBy('host_id');
if ($lockForUpdate) {
$replicaQuery->lockForUpdate();
}
$replicas = $replicaQuery->get();
if ($children->isEmpty()
|| $children->count() !== $replicas->count()) {
return false;
}
$childResolutions = $children->map(
static fn (CloudJob $child): string =>
(string) $child->effect_resolution
)->unique()->values();
if ($childResolutions->contains(
static fn (string $resolution): bool => !in_array(
$resolution,
['applied', 'not_applied'],
true
)
)) {
return false;
}
$actualResolution = $childResolutions->count() === 1
? (string) $childResolutions->first() : 'mixed';
$publishedResolution = $actualResolution === 'mixed'
? 'applied' : $actualResolution;
if (!hash_equals(
(string) $parent->effect_resolution,
$publishedResolution
)
|| ($actualResolution !== 'mixed'
&& (string) $parent->kind === 'user_iso_import'
&& $actualResolution !== 'applied')
|| ($actualResolution !== 'mixed'
&& (string) $parent->kind === 'user_iso_delete'
&& $actualResolution !== 'not_applied')
|| ($actualResolution === 'mixed'
&& !CloudJobService::hasPublishedPartialAggregateMarker(
$parent
))
|| ($actualResolution === 'mixed'
&& (string) $parent->kind === 'user_iso_import'
&& (string) $iso->state !== 'import_failed')
|| ($actualResolution === 'mixed'
&& (string) $parent->kind === 'user_iso_delete'
&& (string) $iso->state !== 'delete_failed')) {
return false;
}
$jobIds = $children->map(
static fn (CloudJob $child): int => (int) $child->id
)->all();
if (CloudUserIsoReplica::query()->whereIn('job_id', $jobIds)
->where('user_iso_id', '<>', (string) $iso->id)->exists()
|| CloudTemplateReplica::query()->whereIn(
'job_id',
$jobIds
)->exists()) {
return false;
}
foreach ($children as $child) {
$matches = $replicas->filter(
static fn (CloudUserIsoReplica $replica): bool =>
(int) ($replica->job_id ?? 0) === (int) $child->id
);
$replica = $matches->count() === 1 ? $matches->first() : null;
if ($replica === null
|| (int) ($child->parent_job_id ?? 0) !== (int) $parent->id
|| (int) $child->user_id !== (int) $parent->user_id
|| (string) $child->execution_mode !== 'agent'
|| (string) $child->kind !== $expectedChildKind
|| (int) $child->host_id !== (int) $replica->host_id
|| (int) $replica->user_id !== (int) $iso->user_id
|| !hash_equals(
(string) $child->operation_id,
(string) $replica->operation_id
)
|| (string) $replica->state !== match (
(string) $parent->kind
) {
'user_iso_import' => $actualResolution === 'mixed'
&& (string) $child->effect_resolution === 'not_applied'
? 'failed' : 'active',
'user_iso_delete' => $actualResolution === 'mixed'
? ((string) $child->effect_resolution === 'applied'
? 'delete_succeeded' : 'delete_failed')
: 'active',
}
|| ((string) $parent->kind === 'user_iso_import'
&& (string) $child->effect_resolution === 'not_applied'
? ((int) $replica->size_bytes !== 0
|| ((string) $replica->sha256 !== ''
&& !hash_equals(
(string) $iso->sha256,
(string) $replica->sha256
)))
: ((int) $replica->size_bytes !== (int) $iso->size_bytes
|| !hash_equals(
(string) $iso->sha256,
(string) $replica->sha256
)))) {
return false;
}
}
return true;
}
private static function aggregateParentSafelySettled(
CloudUserIso $iso,
CloudJob $job,
bool $lockForUpdate
): bool {
return !(
$job->parent_job_id !== null
|| (string) $job->execution_mode !== 'aggregate'
|| !in_array((string) $job->kind, [
'user_iso_import',
'user_iso_delete',
], true)
|| (int) $job->user_id !== (int) $iso->user_id
|| CloudJobService::hasAggregateLocalStateConflict($job)
|| (string) $job->phase !== 'terminal'
|| !in_array(
(string) $job->effect_resolution,
['applied', 'not_applied'],
true
)
|| (bool) $job->finalization_incident
|| CloudJobService::aggregateParentRecoveryPending(
$job,
null,
$lockForUpdate
));
}
/** Keep lost catalog pointers fenced before a second ISO mutation starts. */
private static function missingPointerRecoveryPending(
CloudUserIso $iso
): bool {
$isoId = (string) ($iso->id ?? '');
if ($isoId === '') {
return false;
}
$replicas = CloudUserIsoReplica::query()
->where('user_iso_id', $isoId)
->orderBy('host_id')
->get([
'host_id', 'user_id', 'job_id', 'state', 'operation_id',
]);
if ($replicas->isEmpty()) {
return false;
}
$jobIds = $replicas->map(
static fn (CloudUserIsoReplica $replica): int =>
(int) ($replica->job_id ?? 0)
)->filter(static fn (int $id): bool => $id > 0)
->unique()->values()->all();
if ($jobIds === []) {
return false;
}
if (count($jobIds) !== $replicas->count()) {
return true;
}
if (CloudTemplateReplica::query()->whereIn(
'job_id',
$jobIds
)->exists()) {
return true;
}
$children = CloudJob::whereIn('id', $jobIds)->orderBy('id')->get();
if ($children->count() !== count($jobIds)) {
return true;
}
$parentIds = [];
foreach ($children as $child) {
$matches = $replicas->filter(
static fn (CloudUserIsoReplica $replica): bool =>
(int) ($replica->job_id ?? 0) === (int) $child->id
);
$replica = $matches->count() === 1 ? $matches->first() : null;
$parentId = (int) ($child->parent_job_id ?? 0);
if ($replica === null
|| $parentId < 1
|| (string) $child->execution_mode !== 'agent'
|| (int) $child->host_id < 1
|| (int) $replica->host_id !== (int) $child->host_id
|| (int) $replica->user_id !== (int) $child->user_id
|| (int) $replica->user_id !== (int) $iso->user_id
|| preg_match(
'/^[a-f0-9]{32}$/D',
(string) $child->operation_id
) !== 1
|| !hash_equals(
(string) $child->operation_id,
(string) $replica->operation_id
)) {
return true;
}
$parentIds[$parentId] = true;
}
if (count($parentIds) !== 1) {
return true;
}
$parent = CloudJob::find((int) array_key_first($parentIds));
$expectedChildKind = match ((string) ($parent?->kind ?? '')) {
'user_iso_import' => 'user_iso_import_replica',
'user_iso_delete' => 'user_iso_delete_replica',
default => null,
};
if ($parent === null
|| $expectedChildKind === null
|| (string) $parent->execution_mode !== 'aggregate'
|| $parent->parent_job_id !== null
|| (int) $parent->user_id !== (int) $iso->user_id
|| CloudJob::where('parent_job_id', (int) $parent->id)->count()
!== $children->count()
|| CloudUserIsoReplica::query()->whereIn('job_id', $jobIds)
->where('user_iso_id', '<>', $isoId)->exists()
|| CloudTemplateReplica::query()->whereIn(
'job_id',
$jobIds
)->exists()
|| CloudUserIso::query()
->where('active_job_id', (int) $parent->id)
->where('id', '<>', $isoId)->exists()
|| ((string) $parent->kind === 'user_iso_import'
&& !hash_equals(
(string) $parent->operation_id,
(string) $iso->import_operation_id
))
|| $children->contains(static fn (CloudJob $child): bool =>
(string) $child->kind !== $expectedChildKind
|| (int) $child->user_id !== (int) $parent->user_id)) {
return true;
}
return !self::aggregateParentSafelySettled(
$iso,
$parent,
false
) || !self::currentParentProjectionMatches(
$iso,
$parent,
false
);
}
/** Return the exact settled mixed parent that permits delete-only cleanup. */
private static function settledMixedCleanupParentKind(
CloudUserIso $iso
): ?string {
if ((int) ($iso->active_job_id ?? 0) > 0
|| !in_array(
(string) $iso->state,
['import_failed', 'delete_failed'],
true
)
|| self::missingPointerRecoveryPending($iso)) {
return null;
}
$replica = CloudUserIsoReplica::query()
->where('user_iso_id', (string) $iso->id)
->whereNotNull('job_id')->orderBy('host_id')->first();
$child = $replica === null ? null : CloudJob::find(
(int) $replica->job_id
);
$parent = $child === null ? null : CloudJob::find(
(int) ($child->parent_job_id ?? 0)
);
$kind = (string) ($parent?->kind ?? '');
if ($parent === null
|| !in_array(
$kind,
['user_iso_import', 'user_iso_delete'],
true
)
|| CloudJobService::effectScope($parent) !== 'partial'
|| !self::aggregateParentSafelySettled($iso, $parent, false)
|| !self::currentParentProjectionMatches(
$iso,
$parent,
false
)) {
return null;
}
return $kind;
}
/**
* A quota-rejected import may already exist on every host. It is not made
* active (which would exceed quota), but its owner may start a new,
* independently identified delete aggregate after exact ledger recheck.
*/
private static function quotaExceededImportCleanupAllowed(
CloudUserIso $iso,
bool $lockForUpdate
): bool {
$jobId = (int) ($iso->active_job_id ?? 0);
if ((string) $iso->state !== 'import_failed' || $jobId < 1) {
return false;
}
if (!self::activeJobSafelySettled($iso, $lockForUpdate)) {
return false;
}
$query = CloudJob::where('id', $jobId);
if ($lockForUpdate) {
$query->lockForUpdate();
}
$parent = $query->first();
if ($parent === null
|| (string) $parent->kind !== 'user_iso_import'
|| (string) $parent->execution_mode !== 'aggregate'
|| (int) $parent->user_id !== (int) $iso->user_id
|| (string) $parent->phase !== 'terminal'
|| (string) $parent->outcome !== 'failed'
|| (string) $parent->status !== 'failed'
|| (string) $parent->effect_resolution !== 'applied'
|| (bool) $parent->finalization_incident
|| CloudJobService::aggregateParentRecoveryPending(
$parent,
null,
$lockForUpdate
)) {
return false;
}
try {
self::assertRecoveredReplicaProjectionLocked(
$iso,
$parent,
'applied',
$lockForUpdate
);
} catch (CloudError $error) {
return false;
}
return true;
}
private static function name($value): string
{
if (!is_string($value) || preg_match('//u', $value) !== 1) {
throw new CloudError('ISO 名称应为 1 至 64 个字符');
}
$value = trim($value);
if ($value === '' || mb_strlen($value, 'UTF-8') > 64
|| preg_match('/[\x00-\x1f\x7f]/u', $value) === 1) {
throw new CloudError('ISO 名称应为 1 至 64 个字符');
}
return $value;
}
private static function remoteUrl($value): string
{
if (!is_string($value) || strlen($value) > 2048) {
throw new CloudError('ISO 下载地址无效');
}
$value = trim($value);
$parts = parse_url($value);
if (!is_array($parts)
|| strtolower((string) ($parts['scheme'] ?? '')) !== 'https'
|| !is_string($parts['host'] ?? null) || $parts['host'] === ''
|| isset($parts['user']) || isset($parts['pass'])
|| isset($parts['fragment'])
|| (isset($parts['port']) && (int) $parts['port'] !== 443)
|| !str_ends_with(strtolower((string) ($parts['path'] ?? '')),
'.iso')) {
throw new CloudError('ISO 仅支持无需账号、使用 443 端口的 HTTPS 直链');
}
return $value;
}
}