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.
191 lines
8.1 KiB
PHP
191 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudStorageArtifact;
|
|
use App\Models\CloudStorageOperation;
|
|
use App\Models\CloudStoragePublication;
|
|
use App\Models\CloudStorageReplica;
|
|
|
|
/**
|
|
* Closes the storage publication outbox only after its business owner commits.
|
|
*
|
|
* The caller supplies every frozen identity. A valid primary receipt that
|
|
* belongs to another artifact or owner can therefore never finalize this
|
|
* publication accidentally. Repeating the exact same completion is
|
|
* idempotent; no failed or retired publication is reopened here.
|
|
*/
|
|
final class CloudStoragePublicationFinalizerService
|
|
{
|
|
/** @return array{publication_key:string,artifact_id:int,state:string,finalized_at:int} */
|
|
public static function complete(
|
|
string $ownerType,
|
|
int|string $ownerId,
|
|
int $artifactId,
|
|
string $primaryOperationId
|
|
): array {
|
|
$owner = self::owner($ownerType, $ownerId);
|
|
if ($artifactId < 1) {
|
|
throw new CloudError('对象存储制品身份无效', 422);
|
|
}
|
|
if (preg_match('/^[a-f0-9]{32}$/D', $primaryOperationId) !== 1) {
|
|
throw new CloudError('对象存储发布操作标识无效', 422);
|
|
}
|
|
|
|
$connection = (new CloudStorageOperation())->getConnection();
|
|
return $connection->transaction(static function () use (
|
|
$ownerType,
|
|
$owner,
|
|
$artifactId,
|
|
$primaryOperationId
|
|
): array {
|
|
$operation = CloudStorageOperation::query()
|
|
->where('operation_id', $primaryOperationId)
|
|
->lockForUpdate()->first();
|
|
if (!$operation instanceof CloudStorageOperation
|
|
|| (string) $operation->kind !== 'publish'
|
|
|| (int) $operation->artifact_id !== $artifactId) {
|
|
throw new CloudError(
|
|
'对象存储发布操作与制品身份不一致',
|
|
409
|
|
);
|
|
}
|
|
$artifact = CloudStorageArtifact::query()
|
|
->whereKey($artifactId)->lockForUpdate()->first();
|
|
$replicas = CloudStorageReplica::query()
|
|
->where('artifact_id', $artifactId)->orderBy('id')
|
|
->lockForUpdate()->get();
|
|
$publication = CloudStoragePublication::query()
|
|
->where('artifact_id', $artifactId)
|
|
->lockForUpdate()->first();
|
|
if (!$artifact instanceof CloudStorageArtifact
|
|
|| !$publication instanceof CloudStoragePublication
|
|
|| (string) $artifact->kind !== $ownerType
|
|
|| !self::publicationOwnedBy($publication, $owner)) {
|
|
throw new CloudError('对象存储发布归属不一致', 409);
|
|
}
|
|
if ((string) $operation->phase !== 'terminal'
|
|
|| (string) $operation->outcome !== 'succeeded'
|
|
|| (string) $operation->effect_resolution !== 'applied'
|
|
|| (bool) $operation->effect_may_have_applied
|
|
|| (bool) $operation->safe_to_resubmit) {
|
|
throw new CloudError('对象存储主副本尚未可信结算', 409);
|
|
}
|
|
if ((string) $publication->state === 'completed') {
|
|
if ((int) ($publication->coverage_ready_at ?? 0) < 1
|
|
|| (int) ($publication->finalized_at ?? 0) < 1) {
|
|
throw new CloudError('对象存储发布完成账本不完整', 409);
|
|
}
|
|
return self::view($publication);
|
|
}
|
|
if (!in_array((string) $artifact->state,
|
|
['available', 'protected'], true)) {
|
|
throw new CloudError('对象存储制品不在可最终化状态', 409);
|
|
}
|
|
$primary = $replicas->firstWhere(
|
|
'id',
|
|
(int) $operation->replica_id
|
|
);
|
|
if (!$primary instanceof CloudStorageReplica
|
|
|| (string) $primary->role !== 'primary'
|
|
|| (string) $primary->state !== 'ready'
|
|
|| (int) $primary->size_bytes !== (int) $artifact->size_bytes
|
|
|| !hash_equals(
|
|
(string) $artifact->sha256,
|
|
(string) $primary->sha256
|
|
)
|
|
|| (int) $primary->verified_at < 1) {
|
|
throw new CloudError('对象存储主副本身份尚未验证', 409);
|
|
}
|
|
$ready = $replicas->filter(static function (
|
|
CloudStorageReplica $replica
|
|
) use ($artifact): bool {
|
|
return (string) $replica->state === 'ready'
|
|
&& (int) $replica->size_bytes
|
|
=== (int) $artifact->size_bytes
|
|
&& hash_equals(
|
|
(string) $artifact->sha256,
|
|
(string) $replica->sha256
|
|
)
|
|
&& (int) $replica->verified_at > 0;
|
|
})->count();
|
|
if ($ready < (int) $artifact->required_copies) {
|
|
throw new CloudError('对象存储发布保护副本尚未就绪', 409);
|
|
}
|
|
if ((string) $publication->state !== 'ready_to_finalize'
|
|
|| (int) ($publication->coverage_ready_at ?? 0) < 1) {
|
|
throw new CloudError('对象存储发布尚未进入可最终化状态', 409);
|
|
}
|
|
$now = time();
|
|
$publication->state = 'completed';
|
|
$publication->attempts = (int) $publication->attempts + 1;
|
|
$publication->available_at = 0;
|
|
$publication->lease_token = null;
|
|
$publication->lease_until = 0;
|
|
$publication->last_error_code = '';
|
|
$publication->finalized_at = $now;
|
|
$publication->updated_at = $now;
|
|
$publication->save();
|
|
return self::view($publication);
|
|
}, 3);
|
|
}
|
|
|
|
/** @return array{backup_id:?int,template_import_intent_id:?string,rescue_template_id:?string} */
|
|
private static function owner(string $ownerType, int|string $ownerId): array
|
|
{
|
|
$owner = [
|
|
'backup_id' => null,
|
|
'template_import_intent_id' => null,
|
|
'rescue_template_id' => null,
|
|
];
|
|
if ($ownerType === 'backup') {
|
|
$value = filter_var($ownerId, FILTER_VALIDATE_INT, [
|
|
'options' => ['min_range' => 1],
|
|
]);
|
|
if ($value === false) {
|
|
throw new CloudError('备份对象存储发布归属无效', 422);
|
|
}
|
|
$owner['backup_id'] = (int) $value;
|
|
return $owner;
|
|
}
|
|
if (!is_string($ownerId)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $ownerId) !== 1) {
|
|
throw new CloudError('对象存储发布归属无效', 422);
|
|
}
|
|
if ($ownerType === 'template') {
|
|
$owner['template_import_intent_id'] = $ownerId;
|
|
return $owner;
|
|
}
|
|
if ($ownerType === 'rescue') {
|
|
$owner['rescue_template_id'] = $ownerId;
|
|
return $owner;
|
|
}
|
|
throw new CloudError('对象存储制品类型无效', 422);
|
|
}
|
|
|
|
/** @param array{backup_id:?int,template_import_intent_id:?string,rescue_template_id:?string} $owner */
|
|
private static function publicationOwnedBy(
|
|
CloudStoragePublication $publication,
|
|
array $owner
|
|
): bool {
|
|
return (int) ($publication->backup_id ?? 0)
|
|
=== (int) ($owner['backup_id'] ?? 0)
|
|
&& (string) ($publication->template_import_intent_id ?? '')
|
|
=== (string) ($owner['template_import_intent_id'] ?? '')
|
|
&& (string) ($publication->rescue_template_id ?? '')
|
|
=== (string) ($owner['rescue_template_id'] ?? '');
|
|
}
|
|
|
|
/** @return array{publication_key:string,artifact_id:int,state:string,finalized_at:int} */
|
|
private static function view(
|
|
CloudStoragePublication $publication
|
|
): array {
|
|
return [
|
|
'publication_key' => (string) $publication->publication_key,
|
|
'artifact_id' => (int) $publication->artifact_id,
|
|
'state' => (string) $publication->state,
|
|
'finalized_at' => (int) $publication->finalized_at,
|
|
];
|
|
}
|
|
}
|