fix(cloud): release upgraded instances from current reservation

go
codex 36 minutes ago
parent da9e473b64
commit fd59f3f933

@ -108,8 +108,8 @@ final class CloudInstanceResourceRelease
'id',
$reservationId
)->lockForUpdate()->first();
self::assertOrderReservation(
$order,
self::assertOrderOwned(
$instance,
$reservation,
['committed']
);
@ -176,52 +176,22 @@ final class CloudInstanceResourceRelease
)->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) {
|| (int) ($order->reservation_id ?? 0)
!== (int) $reservation->id) {
throw new CloudError('实例的订单资源账本不一致', 409);
}
self::assertOrderReservation($order, $reservation, $allowedStates);
CloudPlacementService::assertCurrentOrderPlacement(
$instance,
$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));

@ -34,6 +34,50 @@ final class CloudPlacementService
private const GIB = 1073741824;
private const MAX_PUBLIC_IPS_PER_POOL = 254;
/**
* Validate the current order-owned placement of an existing instance.
*
* The order keeps the immutable offer sold at provisioning time. A later
* grow-only upgrade changes the instance and reservation offer together,
* so current capacity ownership must be derived from that pair instead of
* comparing either row with the historical order offer.
*/
public static function assertCurrentOrderPlacement(
CloudInstance $instance,
?CloudResourceReservation $reservation,
array $allowedStates
): void {
if (!$reservation instanceof CloudResourceReservation
|| (int) ($instance->order_id ?? 0) < 1
|| (int) ($reservation->order_id ?? 0)
!== (int) $instance->order_id
|| (int) ($instance->reservation_id ?? 0)
!== (int) $reservation->id
|| (int) ($instance->offer_id ?? 0) < 1
|| (int) ($reservation->offer_id ?? 0)
!== (int) $instance->offer_id
|| (int) $reservation->user_id !== (int) $instance->user_id
|| (int) $reservation->host_id !== (int) $instance->host_id
|| (int) $reservation->resource_pool_id
!== (int) ($instance->resource_pool_id ?? 0)
|| (int) ($reservation->instance_id ?? 0)
!== (int) $instance->id
|| (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);
}
}
/**
* Advisory capacity check for an already-authorized active pool chain.
*

@ -407,6 +407,31 @@ try {
} finally {
$committedInstance->getConnection()->rollBack();
}
// A grow-only upgrade keeps the original order offer as immutable purchase
// history while moving the instance and its committed reservation together.
Capsule::table('cloud_instances')->where('id', 900)->update([
'offer_id' => 51,
]);
Capsule::table('cloud_resource_reservations')->where('id', 1001)->update([
'offer_id' => 51,
'cpu_cores' => 4,
'memory_mib' => 4096,
'disk_gib' => 40,
]);
$committedInstance = CloudInstance::findOrFail(900);
$committedInstance->getConnection()->beginTransaction();
try {
Capsule::table('cloud_resource_reservations')->where('id', 1001)
->update(['offer_id' => 52]);
$rejects(
static fn () => CloudInstanceResourceRelease::
assertCanDispatchDelete($committedInstance),
'A current instance/reservation offer mismatch reached delete dispatch.'
);
} finally {
$committedInstance->getConnection()->rollBack();
}
CloudInstanceResourceRelease::assertCanDispatchDelete($committedInstance);
$committedInstance->status = 'deleted';
$committedInstance->save();
@ -419,7 +444,11 @@ $assert(Capsule::table('cloud_resource_reservations')->where('id', 1001)
&& Capsule::table('cloud_resource_reservation_ips')
->where('reservation_id', 1001)->where('state', 'released')->count() === 2
&& Capsule::table('cloud_public_ips')->where('instance_id', 900)
->count() === 0,
->count() === 0
&& (int) Capsule::table('cloud_orders')->where('id', 1)
->value('offer_id') === 50
&& (int) Capsule::table('cloud_resource_reservations')->where('id', 1001)
->value('offer_id') === 51,
'Confirmed instance deletion did not release its committed network bundle.');
$ipLedger = Capsule::table('cloud_public_ips')->orderBy('id')->get()

Loading…
Cancel
Save