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.
464 lines
20 KiB
PHP
464 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudStorageArtifact;
|
|
use App\Models\CloudStorageBackend;
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Models\CloudStorageOperation;
|
|
use App\Models\CloudStoragePublication;
|
|
use App\Models\CloudStorageReplica;
|
|
|
|
/**
|
|
* Administrator recovery for provider reconciliation and local finalization.
|
|
*
|
|
* It exposes no object key, upload id or provider capability. Provider items
|
|
* re-open only their original read-only reconciliation; finalization items
|
|
* re-open only the existing local publication outbox.
|
|
*/
|
|
final class CloudStorageReplicaAttentionService
|
|
{
|
|
/** @return array<string,mixed> */
|
|
public static function listView(array $input = []): array
|
|
{
|
|
CloudInput::only($input, [
|
|
'page', 'per_page', 'finalization_page',
|
|
]);
|
|
$page = self::boundedInt($input['page'] ?? 1, 1, 100000);
|
|
$finalizationPage = self::boundedInt(
|
|
$input['finalization_page'] ?? 1,
|
|
1,
|
|
100000
|
|
);
|
|
$perPage = self::boundedInt($input['per_page'] ?? 20, 1, 50);
|
|
$immutableUnknown = CloudStorageImmutablePrimaryPublication::
|
|
unknownAttentionErrors();
|
|
$immutableNotApplied = CloudStorageImmutablePrimaryPublication::
|
|
notAppliedAttentionErrors();
|
|
$base = CloudStorageOperation::query()
|
|
->where('phase', 'terminal')
|
|
->where('outcome', 'failed')
|
|
->where('safe_to_resubmit', 0)
|
|
->where(static function ($attention) use (
|
|
$immutableUnknown,
|
|
$immutableNotApplied
|
|
): void {
|
|
$attention->where(static function ($copy): void {
|
|
$copy->where('kind', 'copy')
|
|
->where('effect_resolution', 'unknown')
|
|
->where('effect_may_have_applied', 1)
|
|
->whereIn(
|
|
'error_code',
|
|
CloudStorageArtifactService::
|
|
replicaAttentionErrors()
|
|
);
|
|
})->orWhere(static function ($primary): void {
|
|
$primary->where('kind', 'publish')
|
|
->where('effect_resolution', 'unknown')
|
|
->where('effect_may_have_applied', 1)
|
|
->whereIn(
|
|
'error_code',
|
|
CloudStorageArtifactService::
|
|
primaryPreparationAttentionErrors()
|
|
);
|
|
})->orWhere(static function ($immutable) use (
|
|
$immutableUnknown,
|
|
$immutableNotApplied
|
|
): void {
|
|
$immutable->where('kind', 'publish')
|
|
->where(static function ($shape) use (
|
|
$immutableUnknown,
|
|
$immutableNotApplied
|
|
): void {
|
|
$shape->where(static function ($unknown) use (
|
|
$immutableUnknown
|
|
): void {
|
|
$unknown->whereIn(
|
|
'error_code',
|
|
$immutableUnknown
|
|
)->where('effect_resolution', 'unknown')
|
|
->where('effect_may_have_applied', 1);
|
|
})->orWhere(static function ($notApplied) use (
|
|
$immutableNotApplied
|
|
): void {
|
|
$notApplied->whereIn(
|
|
'error_code',
|
|
$immutableNotApplied
|
|
)->where(
|
|
'effect_resolution',
|
|
'not_applied'
|
|
)->where('effect_may_have_applied', 0);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
$total = (clone $base)->count();
|
|
$operations = $base->orderByDesc('terminal_at')
|
|
->orderByDesc('id')
|
|
->offset(($page - 1) * $perPage)
|
|
->limit($perPage)
|
|
->get();
|
|
$artifactIds = $operations->pluck('artifact_id')->unique()->all();
|
|
$backendIds = $operations->pluck('target_backend_id')
|
|
->merge($operations->pluck('source_backend_id'))
|
|
->filter()->unique()->all();
|
|
$artifacts = CloudStorageArtifact::query()
|
|
->whereIn('id', $artifactIds)->get()->keyBy('id');
|
|
$backends = CloudStorageBackend::query()
|
|
->whereIn('id', $backendIds)->get()->keyBy('id');
|
|
$replicas = CloudStorageReplica::query()
|
|
->whereIn('artifact_id', $artifactIds)->get();
|
|
$publishArtifactIds = $operations
|
|
->where('kind', 'publish')->pluck('artifact_id')->unique()->all();
|
|
$publications = collect();
|
|
$backups = collect();
|
|
if ($publishArtifactIds !== []) {
|
|
$publications = CloudStoragePublication::query()
|
|
->whereIn('artifact_id', $publishArtifactIds)
|
|
->get()->keyBy('artifact_id');
|
|
$backupIds = $publications->pluck('backup_id')
|
|
->filter()->unique()->all();
|
|
if ($backupIds !== []) {
|
|
$backups = CloudInstanceBackup::query()
|
|
->whereIn('id', $backupIds)->get()->keyBy('id');
|
|
}
|
|
}
|
|
$sourceReplicas = [];
|
|
foreach ($replicas as $replica) {
|
|
$sourceReplicas[(int) $replica->artifact_id
|
|
. ':' . (int) $replica->backend_id] = $replica;
|
|
}
|
|
$items = [];
|
|
foreach ($operations as $operation) {
|
|
$artifact = $artifacts->get((int) $operation->artifact_id);
|
|
$backend = $backends->get((int) $operation->target_backend_id);
|
|
$source = $sourceReplicas[(int) $operation->artifact_id
|
|
. ':' . (int) $operation->source_backend_id] ?? null;
|
|
$target = $sourceReplicas[(int) $operation->artifact_id
|
|
. ':' . (int) $operation->target_backend_id] ?? null;
|
|
$copy = (string) $operation->kind === 'copy';
|
|
$immutable = !$copy && in_array(
|
|
(string) $operation->error_code,
|
|
CloudStorageImmutablePrimaryPublication::attentionErrors(),
|
|
true
|
|
);
|
|
$publication = $copy ? null
|
|
: $publications->get((int) $operation->artifact_id);
|
|
$backup = $publication instanceof CloudStoragePublication
|
|
&& $publication->backup_id !== null
|
|
? $backups->get((int) $publication->backup_id) : null;
|
|
$baseRetry = $artifact instanceof CloudStorageArtifact
|
|
&& !in_array((string) $artifact->state,
|
|
['retiring', 'deleted'], true)
|
|
&& ((string) $artifact->state !== 'delete_attention'
|
|
|| $copy)
|
|
&& $target instanceof CloudStorageReplica
|
|
&& (string) $target->state === 'degraded'
|
|
&& hash_equals(
|
|
(string) $operation->error_code,
|
|
(string) $target->error_code
|
|
);
|
|
$immutableOwner = self::immutableOwner($publication);
|
|
$canRetry = false;
|
|
$ownerType = 'backup';
|
|
$ownerId = (int) ($publication?->backup_id ?? 0);
|
|
$ownerText = '备份 #' . $ownerId;
|
|
if ($copy) {
|
|
$canRetry = $baseRetry
|
|
&& $source instanceof CloudStorageReplica
|
|
&& (string) $source->state === 'ready'
|
|
&& (int) $source->size_bytes
|
|
=== (int) $artifact->size_bytes
|
|
&& hash_equals(
|
|
(string) $artifact->sha256,
|
|
(string) $source->sha256
|
|
);
|
|
$ownerType = 'artifact';
|
|
$ownerId = (int) $operation->artifact_id;
|
|
$ownerText = '制品 #' . $ownerId;
|
|
} elseif ($immutable && $immutableOwner !== null) {
|
|
$canRetry = $baseRetry
|
|
&& $publication instanceof CloudStoragePublication
|
|
&& (string) $publication->state === 'failed'
|
|
&& hash_equals(
|
|
(string) $operation->error_code,
|
|
(string) $publication->last_error_code
|
|
)
|
|
&& (string) $artifact->kind
|
|
=== (string) $immutableOwner['type'];
|
|
$ownerType = (string) $immutableOwner['type'];
|
|
$ownerId = (string) $immutableOwner['id'];
|
|
$ownerText = ($ownerType === 'template'
|
|
? '云模板导入 ' : '救援 ISO ') . $ownerId;
|
|
} else {
|
|
$canRetry = $baseRetry
|
|
&& $publication instanceof CloudStoragePublication
|
|
&& (string) $publication->state === 'failed'
|
|
&& hash_equals(
|
|
(string) $operation->error_code,
|
|
(string) $publication->last_error_code
|
|
)
|
|
&& $backup instanceof CloudInstanceBackup
|
|
&& in_array((string) $backup->state,
|
|
['creating', 'uncertain'], true)
|
|
&& $backup->active_job_id === null
|
|
&& $backup->last_operation_id === null
|
|
&& (int) $backup->storage_artifact_id
|
|
=== (int) $artifact->id;
|
|
}
|
|
$items[] = [
|
|
'operation_id' => (string) $operation->operation_id,
|
|
'operation_kind' => (string) $operation->kind,
|
|
'operation_type' => $copy
|
|
? '副本复制'
|
|
: ($immutable
|
|
? '不可变主副本发布' : '主副本发布准备'),
|
|
'artifact_id' => (int) $operation->artifact_id,
|
|
'artifact_kind' => $artifact instanceof CloudStorageArtifact
|
|
? (string) $artifact->kind : 'unknown',
|
|
'artifact_state' => $artifact instanceof CloudStorageArtifact
|
|
? (string) $artifact->state : 'missing',
|
|
'backend_id' => (int) $operation->target_backend_id,
|
|
'backend_name' => $backend instanceof CloudStorageBackend
|
|
? (string) $backend->name : '未知后端',
|
|
'owner_type' => $ownerType,
|
|
'owner_id' => $ownerId,
|
|
'owner_text' => $ownerText,
|
|
'error_code' => (string) $operation->error_code,
|
|
'error_text' => self::errorText(
|
|
(string) $operation->error_code
|
|
),
|
|
'attention_at' => (int) ($operation->terminal_at ?? 0),
|
|
'can_retry' => $canRetry,
|
|
];
|
|
}
|
|
$finalizationBase = CloudStoragePublication::query()
|
|
->where('state', 'failed')
|
|
->where('last_error_code',
|
|
'storage_publication_finalization_attention')
|
|
->whereNull('backup_id')
|
|
->where(static function ($owner): void {
|
|
$owner->whereNotNull('template_import_intent_id')
|
|
->orWhereNotNull('rescue_template_id');
|
|
});
|
|
$finalizationTotal = (clone $finalizationBase)->count();
|
|
$finalizations = $finalizationBase->orderByDesc('updated_at')
|
|
->orderByDesc('id')
|
|
->offset(($finalizationPage - 1) * $perPage)
|
|
->limit($perPage)->get();
|
|
$finalizationArtifacts = CloudStorageArtifact::query()
|
|
->whereIn('id', $finalizations->pluck('artifact_id')->all())
|
|
->get()->keyBy('id');
|
|
$finalizationItems = [];
|
|
foreach ($finalizations as $publication) {
|
|
$artifact = $finalizationArtifacts->get(
|
|
(int) $publication->artifact_id
|
|
);
|
|
$templateOwner = (string) (
|
|
$publication->template_import_intent_id ?? ''
|
|
);
|
|
$rescueOwner = (string) (
|
|
$publication->rescue_template_id ?? ''
|
|
);
|
|
$ownerType = $templateOwner !== '' ? 'template' : 'rescue';
|
|
$ownerId = $templateOwner !== '' ? $templateOwner : $rescueOwner;
|
|
$canRetry = $artifact instanceof CloudStorageArtifact
|
|
&& (string) $artifact->kind === $ownerType
|
|
&& in_array((string) $artifact->state,
|
|
['available', 'protected'], true)
|
|
&& preg_match('/^[a-f0-9]{32}$/D', $ownerId) === 1;
|
|
$finalizationItems[] = [
|
|
'operation_id' => (string) $publication->publication_key,
|
|
'operation_kind' => 'finalize',
|
|
'operation_type' => '业务目录最终化',
|
|
'artifact_id' => (int) $publication->artifact_id,
|
|
'artifact_kind' => $artifact instanceof CloudStorageArtifact
|
|
? (string) $artifact->kind : 'unknown',
|
|
'artifact_state' => $artifact instanceof CloudStorageArtifact
|
|
? (string) $artifact->state : 'missing',
|
|
'backend_id' => 0,
|
|
'backend_name' => '不涉及远端写入',
|
|
'owner_type' => $ownerType,
|
|
'owner_id' => $ownerId,
|
|
'owner_text' => ($ownerType === 'template'
|
|
? '云模板导入 ' : '救援 ISO ') . $ownerId,
|
|
'error_code' => (string) $publication->last_error_code,
|
|
'error_text' => '目录最终化连续失败,已停止自动重试',
|
|
'attention_at' => (int) $publication->updated_at,
|
|
'can_retry' => $canRetry,
|
|
];
|
|
}
|
|
$pages = max(1, (int) ceil($total / $perPage));
|
|
$finalizationPages = max(
|
|
1,
|
|
(int) ceil($finalizationTotal / $perPage)
|
|
);
|
|
return [
|
|
'summary' => [
|
|
'count' => $total + $finalizationTotal,
|
|
'provider_operation_count' => $total,
|
|
'finalization_count' => $finalizationTotal,
|
|
],
|
|
'items' => $items,
|
|
'finalization_items' => $finalizationItems,
|
|
'pagination' => [
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'total' => $total,
|
|
'total_pages' => $pages,
|
|
],
|
|
'finalization_pagination' => [
|
|
'page' => $finalizationPage,
|
|
'per_page' => $perPage,
|
|
'total' => $finalizationTotal,
|
|
'total_pages' => $finalizationPages,
|
|
],
|
|
];
|
|
}
|
|
|
|
/** @return array{operation_id:string,operation_kind:string,phase:string} */
|
|
public static function retry(
|
|
string $operationId,
|
|
?string $expectedKind = null
|
|
): array
|
|
{
|
|
if (preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1) {
|
|
throw new CloudError('对象存储核对操作标识无效', 422);
|
|
}
|
|
if ($expectedKind !== null
|
|
&& !in_array($expectedKind,
|
|
['copy', 'publish', 'finalize'], true)) {
|
|
throw new CloudError('对象存储人工处理类型无效', 422);
|
|
}
|
|
if ($expectedKind === 'finalize') {
|
|
return self::retryFinalization($operationId);
|
|
}
|
|
$operation = CloudStorageOperation::query()
|
|
->where('operation_id', $operationId)->first();
|
|
if (!$operation instanceof CloudStorageOperation) {
|
|
if ($expectedKind !== null) {
|
|
throw new CloudError('对象存储核对操作不存在', 404);
|
|
}
|
|
return self::retryFinalization($operationId);
|
|
}
|
|
$kind = (string) $operation->kind;
|
|
if ($expectedKind !== null && $kind !== $expectedKind) {
|
|
throw new CloudError('对象存储人工处理类型不一致', 409);
|
|
}
|
|
if ($kind === 'copy') {
|
|
CloudStorageArtifactService::reopenReplicaAttention(
|
|
$operationId,
|
|
time()
|
|
);
|
|
} elseif ($kind === 'publish') {
|
|
if (in_array(
|
|
(string) $operation->error_code,
|
|
CloudStorageImmutablePrimaryPublication::attentionErrors(),
|
|
true
|
|
)) {
|
|
CloudStorageImmutablePrimaryPublication::reopenAttention(
|
|
$operationId,
|
|
time()
|
|
);
|
|
} else {
|
|
CloudStorageArtifactService::reopenPrimaryPreparationAttention(
|
|
$operationId,
|
|
time()
|
|
);
|
|
}
|
|
} else {
|
|
throw new CloudError('对象存储操作不支持人工核对', 409);
|
|
}
|
|
return [
|
|
'operation_id' => $operationId,
|
|
'operation_kind' => $kind,
|
|
'phase' => 'reconciling',
|
|
];
|
|
}
|
|
|
|
/** @return array{operation_id:string,operation_kind:string,phase:string} */
|
|
private static function retryFinalization(string $publicationKey): array
|
|
{
|
|
$publication = CloudStoragePublication::query()
|
|
->where('publication_key', $publicationKey)->first();
|
|
if (!$publication instanceof CloudStoragePublication) {
|
|
throw new CloudError('对象存储核对操作不存在', 404);
|
|
}
|
|
CloudStoragePublicationOutboxService::reopenAttention(
|
|
(int) $publication->id,
|
|
time()
|
|
);
|
|
return [
|
|
'operation_id' => $publicationKey,
|
|
'operation_kind' => 'finalize',
|
|
'phase' => 'ready_to_finalize',
|
|
];
|
|
}
|
|
|
|
private static function boundedInt($value, int $minimum, int $maximum): int
|
|
{
|
|
$validated = filter_var($value, FILTER_VALIDATE_INT, [
|
|
'options' => [
|
|
'min_range' => $minimum,
|
|
'max_range' => $maximum,
|
|
],
|
|
]);
|
|
if ($validated === false) {
|
|
throw new CloudError('对象存储人工核对分页参数无效', 422);
|
|
}
|
|
return (int) $validated;
|
|
}
|
|
|
|
private static function errorText(string $errorCode): string
|
|
{
|
|
return match ($errorCode) {
|
|
'replica_multipart_identity_ambiguous' =>
|
|
'同一对象键存在多个分段上传会话',
|
|
'replica_multipart_identity_mismatch' =>
|
|
'记录的分段上传会话与远端不一致',
|
|
'replica_multipart_identity_untrusted' =>
|
|
'远端分段上传会话无法归属到原操作',
|
|
'primary_multipart_identity_ambiguous' =>
|
|
'主副本对象键存在多个分段上传会话',
|
|
'primary_multipart_identity_mismatch' =>
|
|
'主副本记录的分段上传会话与远端不一致',
|
|
'primary_multipart_identity_untrusted' =>
|
|
'主副本远端会话无法归属到原发布操作',
|
|
'immutable_primary_identity_conflict' =>
|
|
'远端对象身份与冻结制品不一致',
|
|
'immutable_primary_reconcile_exhausted' =>
|
|
'主副本只读核对连续失败',
|
|
'immutable_primary_publish_retry_exhausted' =>
|
|
'主副本已确认未写入,但自动发布重试已用尽',
|
|
'immutable_primary_absence_retry_exhausted' =>
|
|
'主副本已稳定确认不存在,但自动发布重试已用尽',
|
|
default => '分段上传身份需要人工核对',
|
|
};
|
|
}
|
|
|
|
/** @return array{type:string,id:string}|null */
|
|
private static function immutableOwner(
|
|
?CloudStoragePublication $publication
|
|
): ?array {
|
|
if (!$publication instanceof CloudStoragePublication
|
|
|| $publication->backup_id !== null) {
|
|
return null;
|
|
}
|
|
$template = (string) (
|
|
$publication->template_import_intent_id ?? ''
|
|
);
|
|
$rescue = (string) ($publication->rescue_template_id ?? '');
|
|
if (($template === '') === ($rescue === '')) {
|
|
return null;
|
|
}
|
|
$id = $template !== '' ? $template : $rescue;
|
|
if (preg_match('/^[a-f0-9]{32}$/D', $id) !== 1) {
|
|
return null;
|
|
}
|
|
return [
|
|
'type' => $template !== '' ? 'template' : 'rescue',
|
|
'id' => $id,
|
|
];
|
|
}
|
|
}
|