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.
252 lines
9.3 KiB
PHP
252 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudOrder;
|
|
use App\Models\CloudResourceReservation;
|
|
|
|
/** Owns delete admission and post-absence release for an instance placement. */
|
|
final class CloudInstanceResourceRelease
|
|
{
|
|
/** Fail closed before a delete request can reach an Agent. */
|
|
public static function assertCanDispatchDelete(
|
|
CloudInstance $instance
|
|
): void {
|
|
$reservationId = (int) ($instance->reservation_id ?? 0);
|
|
if ($reservationId < 1) {
|
|
if ($instance->order_id === null) {
|
|
return;
|
|
}
|
|
throw new CloudError('实例缺少资源预留账本', 409);
|
|
}
|
|
|
|
$reservation = CloudResourceReservation::where(
|
|
'id',
|
|
$reservationId
|
|
)->first();
|
|
if (!$reservation instanceof CloudResourceReservation) {
|
|
throw new CloudError('实例缺少资源预留账本', 409);
|
|
}
|
|
|
|
if (self::isOrderOwned($reservation)) {
|
|
self::assertOrderOwned($instance, $reservation, ['committed']);
|
|
CloudPublicNetworkAllocationService::currentClaimSet(
|
|
$reservationId,
|
|
'committed',
|
|
(int) $instance->id
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (self::isRestoreAttemptOwned($reservation)) {
|
|
CloudPortableRestoreInstanceDeletion::assertReady(
|
|
$instance,
|
|
$reservation
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw new CloudError('实例资源预留来源无法确认', 409);
|
|
}
|
|
|
|
/** Release placement only after the caller proved the domain is absent. */
|
|
public static function releaseAfterConfirmedDelete(
|
|
CloudInstance $instance,
|
|
string $reason
|
|
): void {
|
|
if ((string) $instance->status !== 'deleted') {
|
|
throw new CloudError('实例尚未确认删除,不能释放资源', 409);
|
|
}
|
|
$reservationId = (int) ($instance->reservation_id ?? 0);
|
|
if ($reservationId < 1) {
|
|
if ($instance->order_id === null) {
|
|
return;
|
|
}
|
|
throw new CloudError('已删除实例缺少资源预留账本', 409);
|
|
}
|
|
|
|
(new CloudResourceReservation())->getConnection()->transaction(
|
|
static function () use (
|
|
$instance,
|
|
$reservationId,
|
|
$reason
|
|
): void {
|
|
// Read only enough source identity to select the authority.
|
|
// The allocator retains the canonical host -> catalog ->
|
|
// reservation -> claim/address lock order for actual release.
|
|
$hint = CloudResourceReservation::where(
|
|
'id',
|
|
$reservationId
|
|
)->first();
|
|
if (!$hint instanceof CloudResourceReservation) {
|
|
throw new CloudError(
|
|
'已删除实例的资源预留账本不存在',
|
|
409
|
|
);
|
|
}
|
|
|
|
$releaseReason = self::releaseReason($reason);
|
|
if (self::isOrderOwned($hint)) {
|
|
$order = self::assertOrderOwned(
|
|
$instance,
|
|
$hint,
|
|
['committed', 'released']
|
|
);
|
|
if ((string) $hint->state === 'released') {
|
|
return;
|
|
}
|
|
CloudPublicNetworkAllocationService::
|
|
releaseCommittedBundle(
|
|
$reservationId,
|
|
(int) $instance->id,
|
|
(string) $hint->operation_id,
|
|
$releaseReason,
|
|
true
|
|
);
|
|
$reservation = CloudResourceReservation::where(
|
|
'id',
|
|
$reservationId
|
|
)->lockForUpdate()->first();
|
|
self::assertOrderReservation(
|
|
$order,
|
|
$reservation,
|
|
['committed']
|
|
);
|
|
if ((int) ($reservation->instance_id ?? 0)
|
|
!== (int) $instance->id) {
|
|
throw new CloudError(
|
|
'已删除实例的资源归属不一致',
|
|
409
|
|
);
|
|
}
|
|
self::markReservationReleased(
|
|
$reservation,
|
|
$releaseReason
|
|
);
|
|
return;
|
|
}
|
|
|
|
if (self::isRestoreAttemptOwned($hint)) {
|
|
CloudPortableRestoreInstanceDeletion::releaseCommitted(
|
|
$instance,
|
|
$hint,
|
|
$releaseReason
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw new CloudError('已删除实例的资源来源无效', 409);
|
|
},
|
|
3
|
|
);
|
|
}
|
|
|
|
private static function isOrderOwned(
|
|
CloudResourceReservation $reservation
|
|
): bool {
|
|
return (int) ($reservation->order_id ?? 0) > 0
|
|
&& (int) ($reservation->offer_id ?? 0) > 0
|
|
&& (int) ($reservation->backup_restore_id ?? 0) === 0
|
|
&& (string) ($reservation->backup_restore_attempt_key ?? '') === ''
|
|
&& (int) ($reservation->migration_source_debt_id ?? 0) === 0;
|
|
}
|
|
|
|
private static function isRestoreAttemptOwned(
|
|
CloudResourceReservation $reservation
|
|
): bool {
|
|
return (int) ($reservation->order_id ?? 0) === 0
|
|
&& (int) ($reservation->offer_id ?? 0) === 0
|
|
&& (int) ($reservation->backup_restore_id ?? 0) === 0
|
|
&& (int) ($reservation->migration_source_debt_id ?? 0) === 0
|
|
&& preg_match(
|
|
'/^[a-f0-9]{32}$/D',
|
|
(string) ($reservation->backup_restore_attempt_key ?? '')
|
|
) === 1;
|
|
}
|
|
|
|
private static function assertOrderOwned(
|
|
CloudInstance $instance,
|
|
CloudResourceReservation $reservation,
|
|
array $allowedStates
|
|
): CloudOrder {
|
|
$order = CloudOrder::where(
|
|
'id',
|
|
(int) ($reservation->order_id ?? 0)
|
|
)->first();
|
|
if (!$order instanceof CloudOrder
|
|
|| (int) ($instance->order_id ?? 0) !== (int) $order->id
|
|
|| (int) ($instance->reservation_id ?? 0)
|
|
!== (int) $reservation->id
|
|
|| (int) ($instance->offer_id ?? 0) !== (int) $order->offer_id
|
|
|| (int) $instance->user_id !== (int) $order->user_id
|
|
|| (int) $instance->host_id !== (int) $order->host_id
|
|
|| (int) ($instance->resource_pool_id ?? 0)
|
|
!== (int) $order->resource_pool_id
|
|
|| (int) ($reservation->instance_id ?? 0)
|
|
!== (int) $instance->id) {
|
|
throw new CloudError('实例的订单资源账本不一致', 409);
|
|
}
|
|
self::assertOrderReservation($order, $reservation, $allowedStates);
|
|
return $order;
|
|
}
|
|
|
|
private static function assertOrderReservation(
|
|
CloudOrder $order,
|
|
?CloudResourceReservation $reservation,
|
|
array $allowedStates
|
|
): void {
|
|
if (!$reservation instanceof CloudResourceReservation
|
|
|| (int) ($reservation->order_id ?? 0) !== (int) $order->id
|
|
|| (int) ($reservation->user_id ?? 0) !== (int) $order->user_id
|
|
|| (int) ($reservation->offer_id ?? 0) !== (int) $order->offer_id
|
|
|| (int) ($reservation->resource_pool_id ?? 0)
|
|
!== (int) $order->resource_pool_id
|
|
|| (int) ($reservation->host_id ?? 0)
|
|
!== (int) $order->host_id
|
|
|| (int) ($order->reservation_id ?? 0)
|
|
!== (int) ($reservation->id ?? 0)
|
|
|| (int) ($reservation->backup_restore_id ?? 0) !== 0
|
|
|| (string) ($reservation->backup_restore_attempt_key ?? '') !== ''
|
|
|| (int) ($reservation->migration_source_debt_id ?? 0) !== 0
|
|
|| preg_match(
|
|
'/^[a-f0-9]{32}$/D',
|
|
(string) ($reservation->operation_id ?? '')
|
|
) !== 1
|
|
|| !in_array(
|
|
(string) ($reservation->state ?? ''),
|
|
$allowedStates,
|
|
true
|
|
)) {
|
|
throw new CloudError('开通订单的资源预留账本不一致', 409);
|
|
}
|
|
}
|
|
|
|
private static function releaseReason(string $reason): string
|
|
{
|
|
$reason = strtolower(trim($reason));
|
|
$reason = preg_replace('/[^a-z0-9_]+/', '_', $reason) ?? '';
|
|
$reason = trim($reason, '_');
|
|
if ($reason === '' || preg_match('/^[a-z]/D', $reason) !== 1) {
|
|
$reason = 'released' . ($reason === '' ? '' : '_' . $reason);
|
|
}
|
|
if (strlen($reason) < 3) {
|
|
$reason = 'released';
|
|
}
|
|
return substr($reason, 0, 64);
|
|
}
|
|
|
|
private static function markReservationReleased(
|
|
CloudResourceReservation $reservation,
|
|
string $reason
|
|
): void {
|
|
$now = time();
|
|
$reservation->state = 'released';
|
|
$reservation->released_at = $now;
|
|
$reservation->release_reason = $reason;
|
|
$reservation->expires_at = null;
|
|
$reservation->updated_at = $now;
|
|
$reservation->save();
|
|
}
|
|
}
|