fix(traffic): reject pooled quota mode upgrades

go
codex 3 weeks ago
parent fb64527061
commit 304d54d1e3

@ -843,6 +843,15 @@ final class CloudPlanUpgradeService
!== (int) $instance->traffic_quota_bytes) {
throw new CloudError('实例续费或流量快照不一致', 409);
}
$trafficPoolGroupId = $period->traffic_pool_group_id === null
? null : (int) $period->traffic_pool_group_id;
$trafficQuotaMode = (string) $period->traffic_quota_mode;
if ($trafficPoolGroupId !== null
&& $trafficQuotaMode !== CloudTrafficPoolPeriodBinding::quotaMode(
(int) $period->traffic_quota_bytes
)) {
throw new CloudError('实例流量池计费快照不一致', 409);
}
return [
'plan_id' => (int) $instance->plan_id,
'offer_id' => (int) $instance->offer_id,
@ -851,6 +860,8 @@ final class CloudPlanUpgradeService
'current_period_id' => (int) $period->id,
'current_period_starts_at' => (int) $period->starts_at,
'current_period_ends_at' => (int) $period->ends_at,
'traffic_pool_group_id' => $trafficPoolGroupId,
'traffic_quota_mode' => $trafficQuotaMode,
'renewal_price' => CloudMoney::decimal($renewalCents),
'renewal_days' => (int) $instance->renewal_days,
'renewal_traffic_bytes' =>
@ -906,6 +917,12 @@ final class CloudPlanUpgradeService
throw new CloudError('目标套餐必须至少提升一项资源规格', 409);
}
$traffic = max(0, (int) $plan->traffic_quota_bytes);
CloudTrafficPoolPeriodBinding::assertUpgradeQuotaTransition(
$source['traffic_pool_group_id'] === null ? null
: (int) $source['traffic_pool_group_id'],
(string) $source['traffic_quota_mode'],
$traffic
);
if (!self::quotaAtLeast(
$traffic,
(int) $source['current_traffic_quota_bytes']

@ -12,8 +12,8 @@ use App\Models\CloudTrafficPoolGroup;
*
* Live line configuration is read only while an order snapshot is created.
* Provisioning consumes that snapshot, while renewals inherit the previous
* period. Upgrades may change quota mode but never move an existing period
* to another pool.
* period. An unpooled period may follow the legacy upgrade rules, while a
* pooled period cannot change quota mode or move to another pool.
*/
final class CloudTrafficPoolPeriodBinding
{
@ -87,17 +87,33 @@ final class CloudTrafficPoolPeriodBinding
$trafficPoolGroupId
);
$quotaMode = self::quotaMode($quotaBytes);
if ($period->exists && $groupId !== null
&& in_array((string) $period->traffic_quota_mode,
['finite', 'unlimited'], true)
&& (string) $period->traffic_quota_mode !== $quotaMode) {
if ($period->exists) {
self::assertUpgradeQuotaTransition(
$groupId,
(string) $period->traffic_quota_mode,
$quotaBytes
);
}
$period->traffic_pool_group_id = $groupId;
$period->traffic_quota_mode = $quotaMode;
}
/** Reject a pooled quota-mode change before a paid upgrade is created. */
public static function assertUpgradeQuotaTransition(
?int $trafficPoolGroupId,
string $sourceMode,
int $targetQuotaBytes
): void {
$groupId = self::nullableGroupId($trafficPoolGroupId);
$targetMode = self::quotaMode($targetQuotaBytes);
if ($groupId !== null
&& in_array($sourceMode, ['finite', 'unlimited'], true)
&& $sourceMode !== $targetMode) {
throw new CloudError(
'活动服务周期不支持在有限与无限流量之间切换',
409
);
}
$period->traffic_pool_group_id = $groupId;
$period->traffic_quota_mode = $quotaMode;
}
public static function quotaMode(int $quotaBytes): string

@ -639,6 +639,74 @@ namespace {
'request_idempotency_key' => $key,
];
// A finite entitlement that already belongs to a shared pool cannot be
// converted to unlimited by an in-place upgrade. Reject it before the
// wallet, job, capacity and Agent-dispatch fences are created.
$pooledFinite = $fixture(9);
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update([
'traffic_pool_group_id' => 77,
'traffic_quota_mode' => 'finite',
]);
$pooledInstance = CloudInstance::find($pooledFinite['instanceId']);
$finiteOptions = CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
);
$optionFor($finiteOptions, $pooledFinite['targetOfferId']);
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update(['traffic_quota_mode' => 'unlimited']);
$expectError(static fn () => CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
), 409, 'A corrupt pooled quota-mode snapshot remained upgradeable.');
$connection->table('cloud_service_periods')
->where('id', $pooledFinite['periodId'])
->update(['traffic_quota_mode' => 'finite']);
$connection->table('shop')
->where('id', $pooledFinite['targetPlanId'])
->update(['traffic_quota_bytes' => 0]);
$pooledOptions = CloudPlanUpgradeService::options(
$pooledInstance,
User::find(1)
);
$assert(array_filter(
$pooledOptions['offers'],
static fn (array $option): bool =>
(int) $option['offer_id'] === $pooledFinite['targetOfferId']
) === [],
'A pooled finite period exposed an unlimited in-place upgrade.');
$beforeRejectedUpgrade = [
'balance' => (string) User::find(1)->money,
'jobs' => CloudJob::count(),
'invoices' => CloudInvoice::count(),
'target_stock' => (int) CloudOffer::find(
$pooledFinite['targetOfferId']
)->stock,
'reservation_offer' => (int) CloudResourceReservation::find(
$pooledFinite['reservationId']
)->offer_id,
];
$expectError(static fn () => CloudPlanUpgradeService::enqueue(
$pooledInstance,
User::find(1),
$actor('upgrade-pooled-unlimited-0009'),
$pooledFinite['targetOfferId'],
'5.00'
), 409, 'A pooled finite period was charged for an unlimited upgrade.');
$assert((string) User::find(1)->money
=== $beforeRejectedUpgrade['balance']
&& CloudJob::count() === $beforeRejectedUpgrade['jobs']
&& CloudInvoice::count() === $beforeRejectedUpgrade['invoices']
&& (int) CloudOffer::find($pooledFinite['targetOfferId'])->stock
=== $beforeRejectedUpgrade['target_stock']
&& (int) CloudResourceReservation::find(
$pooledFinite['reservationId']
)->offer_id === $beforeRejectedUpgrade['reservation_offer'],
'Rejected pooled finite-to-unlimited upgrade changed paid fences.');
// Unknown keeps every paid fence; later not_applied restores capacity,
// inventory and balance exactly once.
$failed = $fixture(1);

Loading…
Cancel
Save