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.
322 lines
13 KiB
PHP
322 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupRestore;
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
use App\Models\CloudHost;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
/** Owns admission and settlement for deleting an independently restored VM. */
|
|
final class CloudPortableRestoreInstanceDeletion
|
|
{
|
|
/** Fail closed before a delete can reach the host. */
|
|
public static function assertReady(
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $reservation
|
|
): void {
|
|
$context = self::context($instance, $reservation, false);
|
|
if ((string) $reservation->state !== 'committed') {
|
|
throw new CloudError('恢复实例的资源预留尚未提交', 409);
|
|
}
|
|
self::assertCommittedNetwork($context, $instance, $reservation);
|
|
}
|
|
|
|
/**
|
|
* Release the exact committed pair only after the caller proved VM absence.
|
|
*
|
|
* The Worker already owns User -> Job -> Instance. Continue with Host ->
|
|
* Restore -> Attempt, then enter the allocator's catalog -> reservation ->
|
|
* claims -> addresses hierarchy. Terminal restore rows remain immutable.
|
|
*/
|
|
public static function releaseCommitted(
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $reservation,
|
|
string $reason
|
|
): void {
|
|
if ((string) $instance->status !== 'deleted') {
|
|
throw new CloudError('实例尚未确认删除,不能释放恢复资源', 409);
|
|
}
|
|
if ($reservation->getConnection()->transactionLevel() < 1) {
|
|
throw new \LogicException(
|
|
'Portable restore deletion requires a transaction.'
|
|
);
|
|
}
|
|
|
|
$hint = self::context($instance, $reservation, true);
|
|
$host = CloudHost::where(
|
|
'id',
|
|
(int) $hint['attempt']->target_host_id
|
|
)->lockForUpdate()->first();
|
|
$restore = CloudBackupRestore::where(
|
|
'id',
|
|
(int) $hint['restore']->id
|
|
)->lockForUpdate()->first();
|
|
$attempt = CloudBackupRestoreAttempt::where(
|
|
'id',
|
|
(int) $hint['attempt']->id
|
|
)->where(
|
|
'attempt_key',
|
|
(string) $hint['attempt']->attempt_key
|
|
)->lockForUpdate()->first();
|
|
if (!$host instanceof CloudHost
|
|
|| !$restore instanceof CloudBackupRestore
|
|
|| !$attempt instanceof CloudBackupRestoreAttempt) {
|
|
throw new CloudError('恢复实例删除账本已经变化', 409);
|
|
}
|
|
|
|
$lockedHint = CloudResourceReservation::where(
|
|
'id',
|
|
(int) $reservation->id
|
|
)->first();
|
|
if (!$lockedHint instanceof CloudResourceReservation) {
|
|
throw new CloudError('恢复实例缺少资源预留账本', 409);
|
|
}
|
|
$context = self::context(
|
|
$instance,
|
|
$lockedHint,
|
|
true,
|
|
$restore,
|
|
$attempt
|
|
);
|
|
if ((int) $host->id !== (int) $attempt->target_host_id) {
|
|
throw new CloudError('恢复实例删除宿主机账本不一致', 409);
|
|
}
|
|
|
|
if ((string) $lockedHint->state === 'released') {
|
|
$released = CloudResourceReservation::where(
|
|
'id',
|
|
(int) $lockedHint->id
|
|
)->lockForUpdate()->first();
|
|
if (!$released instanceof CloudResourceReservation) {
|
|
throw new CloudError('恢复实例释放账本已经变化', 409);
|
|
}
|
|
self::context(
|
|
$instance,
|
|
$released,
|
|
true,
|
|
$restore,
|
|
$attempt
|
|
);
|
|
if (!hash_equals(
|
|
(string) $released->release_reason,
|
|
$reason
|
|
)) {
|
|
throw new CloudError('恢复实例释放原因不一致', 409);
|
|
}
|
|
CloudReleasedPublicNetworkBundleVerifier::verify(
|
|
$attempt,
|
|
$released,
|
|
(int) $instance->user_id
|
|
);
|
|
return;
|
|
}
|
|
if ((string) $lockedHint->state !== 'committed') {
|
|
throw new CloudError('恢复实例的资源预留状态不明确', 409);
|
|
}
|
|
self::assertCommittedNetwork($context, $instance, $lockedHint);
|
|
|
|
CloudPublicNetworkAllocationService::releaseCommittedBundle(
|
|
(int) $lockedHint->id,
|
|
(int) $instance->id,
|
|
(string) $attempt->operation_id,
|
|
$reason,
|
|
true
|
|
);
|
|
$locked = CloudResourceReservation::where(
|
|
'id',
|
|
(int) $lockedHint->id
|
|
)->lockForUpdate()->first();
|
|
if (!$locked instanceof CloudResourceReservation) {
|
|
throw new CloudError('恢复实例释放账本已经变化', 409);
|
|
}
|
|
self::context(
|
|
$instance,
|
|
$locked,
|
|
false,
|
|
$restore,
|
|
$attempt
|
|
);
|
|
if ((string) $locked->state !== 'committed') {
|
|
throw new CloudError('恢复实例释放账本状态不一致', 409);
|
|
}
|
|
$now = time();
|
|
$locked->state = 'released';
|
|
$locked->released_at = $now;
|
|
$locked->release_reason = $reason;
|
|
$locked->expires_at = null;
|
|
$locked->updated_at = $now;
|
|
$locked->save();
|
|
CloudReleasedPublicNetworkBundleVerifier::verify(
|
|
$attempt,
|
|
$locked,
|
|
(int) $instance->user_id
|
|
);
|
|
}
|
|
|
|
/** @return array{restore:CloudBackupRestore,attempt:CloudBackupRestoreAttempt,plan:CloudPortableRestoreTargetPlan} */
|
|
private static function context(
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $reservation,
|
|
bool $allowReleased,
|
|
?CloudBackupRestore $restore = null,
|
|
?CloudBackupRestoreAttempt $attempt = null
|
|
): array {
|
|
$attemptKey = (string) ($reservation->backup_restore_attempt_key ?? '');
|
|
if (preg_match('/^[a-f0-9]{32}$/D', $attemptKey) !== 1) {
|
|
throw new CloudError('恢复实例尝试身份无效', 409);
|
|
}
|
|
$attempt ??= CloudBackupRestoreAttempt::where(
|
|
'reservation_id',
|
|
(int) $reservation->id
|
|
)->where('attempt_key', $attemptKey)->first();
|
|
$restore ??= $attempt instanceof CloudBackupRestoreAttempt
|
|
? CloudBackupRestore::find((int) $attempt->backup_restore_id)
|
|
: null;
|
|
if (!$attempt instanceof CloudBackupRestoreAttempt
|
|
|| !$restore instanceof CloudBackupRestore) {
|
|
throw new CloudError('恢复实例删除账本不存在', 409);
|
|
}
|
|
try {
|
|
$plan = CloudPortableRestoreTargetPlan::fromHistoricalStored(
|
|
(string) $attempt->target_plan_json,
|
|
(string) $attempt->target_plan_sha256
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
throw new CloudError('恢复实例的目标计划损坏', 409);
|
|
}
|
|
$planValue = $plan->toArray();
|
|
$target = $planValue['target'];
|
|
$publicNetwork = $planValue['public_network'];
|
|
$resources = $planValue['resources'];
|
|
$reservationState = (string) $reservation->state;
|
|
if ((int) ($reservation->order_id ?? 0) !== 0
|
|
|| (int) ($reservation->backup_restore_id ?? 0) !== 0
|
|
|| (int) ($reservation->migration_source_debt_id ?? 0) !== 0
|
|
|| (int) ($reservation->offer_id ?? 0) !== 0
|
|
|| (int) $reservation->id
|
|
!== (int) ($instance->reservation_id ?? 0)
|
|
|| (int) ($reservation->instance_id ?? 0) !== (int) $instance->id
|
|
|| (int) $reservation->host_id !== (int) $instance->host_id
|
|
|| (int) $reservation->user_id !== (int) $instance->user_id
|
|
|| (int) $reservation->resource_pool_id
|
|
!== (int) ($instance->resource_pool_id ?? 0)
|
|
|| (int) ($reservation->network_attachment_id ?? 0)
|
|
!== (int) $attempt->target_network_attachment_id
|
|
|| (int) $reservation->cpu_cores
|
|
!== (int) $resources['cpu_cores']
|
|
|| (int) $reservation->memory_mib
|
|
!== (int) $resources['memory_mib']
|
|
|| (int) $reservation->disk_gib
|
|
!== (int) $resources['disk_gib']
|
|
|| (int) ($instance->order_id ?? 0) !== 0
|
|
|| (int) ($instance->offer_id ?? 0) !== 0
|
|
|| !in_array(
|
|
$reservationState,
|
|
$allowReleased ? ['committed', 'released'] : ['committed'],
|
|
true
|
|
)
|
|
|| !$attempt->matchesReservation($reservation)
|
|
|| (string) $attempt->state !== 'succeeded'
|
|
|| (string) $attempt->effect_resolution !== 'applied'
|
|
|| $attempt->active_attempt !== null
|
|
|| $attempt->effect_resolved_at === null
|
|
|| $attempt->terminal_at === null
|
|
|| (int) $attempt->target_host_id !== (int) $instance->host_id
|
|
|| !hash_equals(
|
|
strtolower((string) $attempt->target_uuid),
|
|
strtolower((string) $instance->uuid)
|
|
)
|
|
|| (int) $restore->backup_id < 1
|
|
|| (int) $planValue['backup']['id'] !== (int) $restore->backup_id
|
|
|| (string) $restore->state !== 'ready'
|
|
|| (int) ($restore->target_instance_id ?? 0)
|
|
!== (int) $instance->id
|
|
|| (int) $restore->target_host_id !== (int) $instance->host_id
|
|
|| (int) $attempt->backup_restore_id !== (int) $restore->id
|
|
|| !hash_equals(
|
|
strtolower((string) $restore->new_uuid),
|
|
strtolower((string) $attempt->target_uuid)
|
|
)
|
|
|| !hash_equals(
|
|
(string) $restore->new_name,
|
|
(string) $attempt->target_name
|
|
)
|
|
|| $restore->ready_at === null
|
|
|| (string) $restore->error_code !== ''
|
|
|| !hash_equals(
|
|
(string) $attempt->target_plan_json,
|
|
(string) $restore->selected_plan_json
|
|
)
|
|
|| !hash_equals(
|
|
(string) $attempt->target_plan_sha256,
|
|
(string) $restore->selected_plan_sha256
|
|
)
|
|
|| (int) $target['host_id'] !== (int) $instance->host_id
|
|
|| (int) $target['resource_pool_id']
|
|
!== (int) $reservation->resource_pool_id
|
|
|| (int) $attempt->target_network_attachment_id
|
|
!== (int) $publicNetwork['network_attachment_id']
|
|
|| !hash_equals(
|
|
(string) $reservation->operation_id,
|
|
(string) $attempt->operation_id
|
|
)) {
|
|
throw new CloudError('恢复实例删除账本身份不一致', 409);
|
|
}
|
|
if ((int) $attempt->generation === 1
|
|
&& ((int) ($restore->reservation_id ?? 0)
|
|
!== (int) $reservation->id
|
|
|| (int) ($restore->job_id ?? 0)
|
|
!== (int) ($attempt->job_id ?? 0)
|
|
|| !hash_equals(
|
|
(string) $restore->operation_id,
|
|
(string) $attempt->operation_id
|
|
))) {
|
|
throw new CloudError('首代恢复实例的父请求投影不一致', 409);
|
|
}
|
|
if ($reservationState === 'released'
|
|
&& ($reservation->released_at === null
|
|
|| (string) $reservation->release_reason === ''
|
|
|| $reservation->expires_at !== null)) {
|
|
throw new CloudError('恢复实例释放账本不完整', 409);
|
|
}
|
|
if ($reservationState === 'committed'
|
|
&& ($reservation->released_at !== null
|
|
|| (string) $reservation->release_reason !== '')) {
|
|
throw new CloudError('恢复实例提交账本包含释放标记', 409);
|
|
}
|
|
return compact('restore', 'attempt', 'plan');
|
|
}
|
|
|
|
/** @param array{attempt:CloudBackupRestoreAttempt} $context */
|
|
private static function assertCommittedNetwork(
|
|
array $context,
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $reservation
|
|
): void {
|
|
$attempt = $context['attempt'];
|
|
$claimSet = CloudPublicNetworkAllocationService::currentClaimSet(
|
|
(int) $reservation->id,
|
|
'committed',
|
|
(int) $instance->id
|
|
);
|
|
try {
|
|
CloudFrozenPublicNetworkIdentity::fromStored(
|
|
(int) $reservation->id,
|
|
(int) $attempt->target_host_id,
|
|
(string) $attempt->target_uuid,
|
|
(int) $attempt->target_network_attachment_id,
|
|
(string) $attempt->target_identity_json,
|
|
(string) $attempt->target_identity_sha256,
|
|
(string) $attempt->target_public_network_json,
|
|
(string) $attempt->target_public_network_sha256,
|
|
(string) $attempt->target_claim_set_sha256,
|
|
$claimSet
|
|
);
|
|
} catch (\InvalidArgumentException) {
|
|
throw new CloudError('恢复实例的冻结双栈身份不一致', 409);
|
|
}
|
|
}
|
|
}
|