feat(traffic): freeze pool identity per service period
parent
33e6a1ae72
commit
1f7a6d9a19
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudNetworkLine;
|
||||
use App\Models\CloudResourcePool;
|
||||
use App\Models\CloudServicePeriod;
|
||||
use App\Models\CloudTrafficPoolGroup;
|
||||
|
||||
/**
|
||||
* Freezes the traffic-pool identity at the service-period boundary.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
final class CloudTrafficPoolPeriodBinding
|
||||
{
|
||||
/** @return array{traffic_pool_group_id:?int,traffic_quota_mode:string} */
|
||||
public static function purchaseSnapshot(
|
||||
CloudResourcePool $resourcePool,
|
||||
CloudNetworkLine $line,
|
||||
int $quotaBytes
|
||||
): array {
|
||||
if ((int) $resourcePool->line_id !== (int) $line->id) {
|
||||
throw new CloudError('资源池与线路配置不一致', 409);
|
||||
}
|
||||
$groupId = self::nullableGroupId(
|
||||
$line->traffic_pool_group_id ?? null
|
||||
);
|
||||
if ($groupId !== null
|
||||
&& CloudTrafficPoolGroup::find($groupId) === null) {
|
||||
throw new CloudError('线路绑定的流量池分组不存在', 409);
|
||||
}
|
||||
return [
|
||||
'traffic_pool_group_id' => $groupId,
|
||||
'traffic_quota_mode' => self::quotaMode($quotaBytes),
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array{traffic_pool_group_id:?int,traffic_quota_mode:string} */
|
||||
public static function orderSnapshot(array $spec, int $quotaBytes): array
|
||||
{
|
||||
if (!array_key_exists('traffic_pool_group_id', $spec)
|
||||
|| !array_key_exists('traffic_quota_mode', $spec)) {
|
||||
throw new CloudError('订单缺少不可变的流量池计费快照', 409);
|
||||
}
|
||||
$snapshotGroup = $spec['traffic_pool_group_id'];
|
||||
if ($snapshotGroup !== null
|
||||
&& (!is_int($snapshotGroup) || $snapshotGroup < 1)) {
|
||||
throw new CloudError('订单流量池分组快照无效', 409);
|
||||
}
|
||||
$groupId = $snapshotGroup;
|
||||
$expectedMode = self::quotaMode($quotaBytes);
|
||||
$snapshotMode = $spec['traffic_quota_mode'];
|
||||
if (!is_string($snapshotMode)
|
||||
|| !in_array($snapshotMode, ['finite', 'unlimited'], true)
|
||||
|| $snapshotMode !== $expectedMode) {
|
||||
throw new CloudError('订单流量池计费快照不一致', 409);
|
||||
}
|
||||
return [
|
||||
'traffic_pool_group_id' => $groupId,
|
||||
'traffic_quota_mode' => $snapshotMode,
|
||||
];
|
||||
}
|
||||
|
||||
public static function bindPeriod(
|
||||
CloudServicePeriod $period,
|
||||
?int $trafficPoolGroupId,
|
||||
int $quotaBytes
|
||||
): void {
|
||||
$groupId = self::nullableGroupId(
|
||||
$trafficPoolGroupId
|
||||
);
|
||||
$period->traffic_pool_group_id = $groupId;
|
||||
$period->traffic_quota_mode = self::quotaMode($quotaBytes);
|
||||
}
|
||||
|
||||
public static function quotaMode(int $quotaBytes): string
|
||||
{
|
||||
if ($quotaBytes < 0) {
|
||||
throw new CloudError('服务周期流量额度不能为负数', 409);
|
||||
}
|
||||
return $quotaBytes === 0 ? 'unlimited' : 'finite';
|
||||
}
|
||||
|
||||
private static function nullableGroupId(mixed $value): ?int
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
if (is_int($value) && $value > 0) {
|
||||
return $value;
|
||||
}
|
||||
if (is_string($value)
|
||||
&& preg_match('/^[1-9][0-9]*$/D', $value) === 1
|
||||
&& (int) $value > 0) {
|
||||
return (int) $value;
|
||||
}
|
||||
throw new CloudError('流量池分组快照无效', 409);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Models\CloudNetworkLine;
|
||||
use App\Models\CloudResourcePool;
|
||||
use App\Models\CloudServicePeriod;
|
||||
use App\Services\Cloud\CloudError;
|
||||
use App\Services\Cloud\CloudTrafficPoolPeriodBinding;
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
$expectConflict = static function (callable $callable, string $message) use (
|
||||
$assert
|
||||
): void {
|
||||
try {
|
||||
$callable();
|
||||
} catch (CloudError $error) {
|
||||
$assert($error->httpStatus === 409, $message . ' Wrong status.');
|
||||
return;
|
||||
}
|
||||
throw new RuntimeException($message);
|
||||
};
|
||||
|
||||
$capsule = new Capsule();
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => ':memory:',
|
||||
], 'default');
|
||||
$capsule->setAsGlobal();
|
||||
$capsule->bootEloquent();
|
||||
$capsule->schema('default')->create(
|
||||
'cloud_traffic_pool_groups',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('code')->unique();
|
||||
$table->string('name');
|
||||
$table->string('mode');
|
||||
$table->integer('created_at');
|
||||
$table->integer('updated_at');
|
||||
}
|
||||
);
|
||||
Capsule::table('cloud_traffic_pool_groups')->insert([
|
||||
'id' => 7,
|
||||
'code' => 'premium',
|
||||
'name' => 'Premium',
|
||||
'mode' => 'disabled',
|
||||
'created_at' => 1,
|
||||
'updated_at' => 1,
|
||||
]);
|
||||
|
||||
$pool = new CloudResourcePool();
|
||||
$pool->id = 3;
|
||||
$pool->line_id = 5;
|
||||
$line = new CloudNetworkLine();
|
||||
$line->id = 5;
|
||||
$line->traffic_pool_group_id = 7;
|
||||
$snapshot = CloudTrafficPoolPeriodBinding::purchaseSnapshot(
|
||||
$pool,
|
||||
$line,
|
||||
1024
|
||||
);
|
||||
$assert($snapshot === [
|
||||
'traffic_pool_group_id' => 7,
|
||||
'traffic_quota_mode' => 'finite',
|
||||
], 'Purchase did not freeze the configured line group.');
|
||||
|
||||
$line->traffic_pool_group_id = null;
|
||||
$unbound = CloudTrafficPoolPeriodBinding::purchaseSnapshot($pool, $line, 0);
|
||||
$assert($unbound === [
|
||||
'traffic_pool_group_id' => null,
|
||||
'traffic_quota_mode' => 'unlimited',
|
||||
], 'An unbound line did not preserve the disabled accounting path.');
|
||||
|
||||
$line->traffic_pool_group_id = 99;
|
||||
$expectConflict(
|
||||
static fn (): array =>
|
||||
CloudTrafficPoolPeriodBinding::purchaseSnapshot($pool, $line, 100),
|
||||
'A line referencing a missing traffic-pool group was accepted.'
|
||||
);
|
||||
|
||||
$orderBinding = CloudTrafficPoolPeriodBinding::orderSnapshot($snapshot, 1024);
|
||||
$assert($orderBinding['traffic_pool_group_id'] === 7,
|
||||
'A live line change altered the immutable order snapshot.');
|
||||
$expectConflict(
|
||||
static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([
|
||||
'traffic_pool_group_id' => 7,
|
||||
'traffic_quota_mode' => 'unlimited',
|
||||
], 100),
|
||||
'A quota-mode mismatch in the order snapshot was accepted.'
|
||||
);
|
||||
$deletedCatalogBinding = CloudTrafficPoolPeriodBinding::orderSnapshot([
|
||||
'traffic_pool_group_id' => 99,
|
||||
'traffic_quota_mode' => 'finite',
|
||||
], 100);
|
||||
$assert($deletedCatalogBinding['traffic_pool_group_id'] === 99,
|
||||
'Finalization incorrectly depended on the mutable group catalog.');
|
||||
$expectConflict(
|
||||
static fn (): array =>
|
||||
CloudTrafficPoolPeriodBinding::orderSnapshot([], 100),
|
||||
'A missing clean-break order snapshot was accepted.'
|
||||
);
|
||||
$expectConflict(
|
||||
static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([
|
||||
'traffic_pool_group_id' => '',
|
||||
'traffic_quota_mode' => 'finite',
|
||||
], 100),
|
||||
'An empty-string order group bypassed the immutable mapping.'
|
||||
);
|
||||
$expectConflict(
|
||||
static fn (): array => CloudTrafficPoolPeriodBinding::orderSnapshot([
|
||||
'traffic_pool_group_id' => '7',
|
||||
'traffic_quota_mode' => 'finite',
|
||||
], 100),
|
||||
'A string order group bypassed the canonical snapshot type.'
|
||||
);
|
||||
$expectConflict(
|
||||
static fn (): string => CloudTrafficPoolPeriodBinding::quotaMode(-1),
|
||||
'A negative period quota was silently converted to unlimited.'
|
||||
);
|
||||
|
||||
$period = new CloudServicePeriod();
|
||||
CloudTrafficPoolPeriodBinding::bindPeriod($period, 7, 0);
|
||||
$assert((int) $period->traffic_pool_group_id === 7
|
||||
&& (string) $period->traffic_quota_mode === 'unlimited',
|
||||
'A period did not retain its group while switching quota mode.');
|
||||
CloudTrafficPoolPeriodBinding::bindPeriod($period, 7, 2048);
|
||||
$assert((int) $period->traffic_pool_group_id === 7
|
||||
&& (string) $period->traffic_quota_mode === 'finite',
|
||||
'A finite upgrade changed the frozen group or quota mode.');
|
||||
|
||||
echo "Cloud traffic pool period binding checks passed.\n";
|
||||
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
$root = dirname(__DIR__);
|
||||
$read = static function (string $path) use ($root): string {
|
||||
$source = file_get_contents($root . '/' . $path);
|
||||
if (!is_string($source)) {
|
||||
throw new RuntimeException('Unable to read ' . $path);
|
||||
}
|
||||
return $source;
|
||||
};
|
||||
$assert = static function (bool $condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$order = $read('app/Services/Cloud/CloudOrderService.php');
|
||||
$worker = $read('app/Services/Cloud/CloudWorkerService.php');
|
||||
$renewal = $read('app/Services/Cloud/CloudRenewalService.php');
|
||||
$upgrade = $read('app/Services/Cloud/CloudPlanUpgradeService.php');
|
||||
|
||||
$assert(str_contains($order,
|
||||
'CloudTrafficPoolPeriodBinding::purchaseSnapshot(')
|
||||
&& str_contains($order,
|
||||
"\$safeSpec['traffic_pool_group_id'] =")
|
||||
&& str_contains($order,
|
||||
"\$safeSpec['traffic_quota_mode'] ="),
|
||||
'Purchase does not freeze the line traffic-pool binding.');
|
||||
$assert(str_contains($worker,
|
||||
'CloudTrafficPoolPeriodBinding::orderSnapshot(')
|
||||
&& str_contains($worker,
|
||||
"\$trafficPoolBinding['traffic_pool_group_id']")
|
||||
&& str_contains($worker,
|
||||
'CloudTrafficPoolPeriodBinding::bindPeriod('),
|
||||
'Provisioning does not consume the immutable order binding.');
|
||||
$preDispatch = strpos($worker,
|
||||
'self::assertCreateTrafficPoolSnapshot($job)');
|
||||
$dispatchClaim = strpos($worker, "'phase' => 'dispatching'");
|
||||
$assert($preDispatch !== false && $dispatchClaim !== false
|
||||
&& $preDispatch < $dispatchClaim,
|
||||
'Create snapshot validation no longer happens before Agent dispatch.');
|
||||
$assert(str_contains($renewal,
|
||||
'CloudTrafficPoolPeriodBinding::bindPeriod(')
|
||||
&& str_contains($renewal,
|
||||
'$lastPeriod->traffic_pool_group_id'),
|
||||
'Renewal does not inherit the preceding service-period group.');
|
||||
$assert(str_contains($upgrade,
|
||||
'CloudTrafficPoolPeriodBinding::bindPeriod(')
|
||||
&& str_contains($upgrade,
|
||||
'$period->traffic_pool_group_id'),
|
||||
'Upgrade no longer preserves the current service-period group.');
|
||||
|
||||
foreach ([$order, $worker, $renewal, $upgrade] as $source) {
|
||||
$assert(!str_contains($source, 'CloudTrafficPoolAccounting::'),
|
||||
'A financial or provisioning transaction directly locks pool accounting.');
|
||||
}
|
||||
|
||||
echo "Cloud traffic pool period integration contract passed.\n";
|
||||
Loading…
Reference in New Issue