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.
virt/web/tests/CloudTrafficPoolAttentionIn...

407 lines
16 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Command\CloudTrafficPoolAttention;
use App\Models\CloudInstance;
use App\Models\CloudServicePeriod;
use App\Models\CloudTrafficLedgerEvent;
use App\Models\CloudTrafficPool;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudTrafficPoolAccounting;
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);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->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');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('current_period_id')->nullable();
$table->integer('lifetime_download_bytes')->default(0);
$table->integer('lifetime_upload_bytes')->default(0);
$table->integer('traffic_synced_at')->default(0);
$table->string('status')->default('active');
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_service_periods', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('plan_id')->nullable();
$table->integer('order_id')->nullable();
$table->integer('renewal_id')->nullable();
$table->integer('traffic_pool_group_id')->nullable();
$table->string('traffic_quota_mode')->default('finite');
$table->integer('starts_at');
$table->integer('ends_at');
$table->integer('traffic_quota_bytes')->default(0);
$table->integer('download_base_bytes')->nullable();
$table->integer('upload_base_bytes')->nullable();
$table->integer('download_used_bytes')->default(0);
$table->integer('upload_used_bytes')->default(0);
$table->string('status')->default('active');
$table->integer('activated_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_traffic_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('traffic_pool_group_id');
$table->string('state');
$table->integer('available_bytes')->default(0);
$table->integer('overage_bytes')->default(0);
$table->integer('version')->default(0);
$table->integer('last_sequence')->default(0);
$table->integer('last_event_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique(['user_id', 'traffic_pool_group_id']);
});
$schema->create('cloud_traffic_entitlements', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('pool_id');
$table->integer('user_id');
$table->integer('instance_id');
$table->integer('service_period_id')->unique();
$table->integer('starts_at');
$table->integer('ends_at');
$table->string('quota_mode');
$table->integer('granted_bytes')->default(0);
$table->integer('consumed_bytes')->default(0);
$table->integer('forfeited_bytes')->default(0);
$table->string('state');
$table->integer('activated_at')->nullable();
$table->integer('closed_at')->nullable();
$table->integer('version')->default(0);
$table->integer('created_at');
$table->integer('updated_at');
});
$schema->create('cloud_traffic_ledger_events', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('pool_id');
$table->integer('user_id');
$table->integer('pool_sequence');
$table->string('event_key')->unique();
$table->string('event_type');
$table->string('source_type');
$table->string('source_id');
$table->integer('source_revision');
$table->string('payload_hash');
$table->integer('instance_id')->nullable();
$table->integer('service_period_id')->nullable();
$table->integer('entitlement_id')->nullable();
$table->integer('amount_bytes');
$table->integer('effective_at');
$table->integer('observed_at')->nullable();
$table->text('payload_json');
$table->integer('created_at');
$table->unique(['pool_id', 'pool_sequence']);
});
$schema->create('cloud_traffic_usage_settlement_buckets', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('pool_id');
$table->integer('user_id');
$table->integer('instance_id');
$table->integer('service_period_id');
$table->integer('entitlement_id')->nullable();
$table->string('allocation_kind');
$table->integer('allocation_target_id');
$table->integer('bucket_start');
$table->integer('bucket_seconds');
$table->integer('seal_at');
$table->integer('bucket_revision');
$table->integer('amount_bytes');
$table->integer('download_bytes');
$table->integer('upload_bytes');
$table->integer('sample_count');
$table->integer('first_observed_at');
$table->integer('last_observed_at');
$table->string('state');
$table->integer('sealed_event_id')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique([
'pool_id', 'instance_id', 'service_period_id', 'allocation_kind',
'allocation_target_id', 'bucket_start', 'bucket_seconds',
'bucket_revision',
], 'traffic_settlement_identity');
});
$schema->create('cloud_traffic_instance_cursors', static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->integer('user_id');
$table->integer('pool_id');
$table->integer('accounted_download_bytes')->default(0);
$table->integer('accounted_upload_bytes')->default(0);
$table->integer('last_observed_at')->default(0);
$table->integer('last_event_id')->nullable();
$table->string('state');
$table->string('attention_code');
$table->integer('version')->default(0);
$table->integer('updated_at');
});
$now = time();
Capsule::table('cloud_traffic_pool_groups')->insert([
['id' => 1, 'code' => 'pool-a', 'name' => 'Pool A', 'mode' => 'shadow',
'created_at' => $now, 'updated_at' => $now],
['id' => 2, 'code' => 'pool-b', 'name' => 'Pool B', 'mode' => 'shadow',
'created_at' => $now, 'updated_at' => $now],
]);
$makeAttention = static function (
int $instanceId,
int $periodId,
int $userId,
int $groupId
) use ($now): array {
$instance = new CloudInstance();
$instance->id = $instanceId;
$instance->user_id = $userId;
$instance->current_period_id = $periodId;
$instance->lifetime_download_bytes = 0;
$instance->lifetime_upload_bytes = 0;
$instance->traffic_synced_at = $now;
$instance->status = 'active';
$instance->created_at = $now;
$instance->updated_at = $now;
$instance->save();
$period = new CloudServicePeriod();
$period->id = $periodId;
$period->instance_id = $instanceId;
$period->user_id = $userId;
$period->traffic_pool_group_id = $groupId;
$period->traffic_quota_mode = 'finite';
$period->starts_at = $now - 10;
$period->ends_at = $now + 3600;
$period->traffic_quota_bytes = 1000;
$period->download_base_bytes = 0;
$period->upload_base_bytes = 0;
$period->download_used_bytes = 0;
$period->upload_used_bytes = 0;
$period->status = 'active';
$period->activated_at = $now - 10;
$period->created_at = $now;
$period->updated_at = $now;
$period->save();
CloudTrafficPoolAccounting::reconcileServicePeriod(
$periodId,
'attention-cli-period:' . $periodId,
$now
);
$instance->lifetime_download_bytes = 100;
$instance->traffic_synced_at = $now + 1;
$instance->save();
CloudTrafficPoolAccounting::settleInstanceUsage($instanceId, $now + 1);
$instance->lifetime_download_bytes = 90;
$instance->traffic_synced_at = $now + 2;
$instance->save();
$attention = CloudTrafficPoolAccounting::settleInstanceUsage(
$instanceId,
$now + 2
);
return [$instance, $attention];
};
$run = static function (array $arguments): array {
ob_start();
try {
$status = CloudTrafficPoolAttention::run(array_merge([
'xcat', 'cloudTrafficPoolAttention',
], $arguments));
$output = (string) ob_get_clean();
return [$status, json_decode($output, true, 512, JSON_THROW_ON_ERROR)];
} catch (Throwable $error) {
ob_end_clean();
throw $error;
}
};
[$instance, $attention] = $makeAttention(11, 101, 7, 1);
$poolId = (int) $attention['pool_id'];
$beforePool = CloudTrafficPool::findOrFail($poolId);
$beforeLedger = CloudTrafficLedgerEvent::count();
[$status, $inspection] = $run(['inspect', '--pool-id=' . $poolId]);
$afterInspect = CloudTrafficPool::findOrFail($poolId);
$assert($status === 0 && $inspection['state'] === 'attention'
&& $inspection['version'] === (int) $beforePool->version
&& $inspection['projection_valid'] === true
&& !array_key_exists('user_id', $inspection)
&& !array_key_exists('instance_id', $inspection)
&& !array_key_exists('attention_cursor_ids', $inspection)
&& !array_key_exists('unresolved_attention_event_ids', $inspection)
&& $inspection['attention_cursor_count'] === 1
&& $inspection['unresolved_attention_event_count'] === 1
&& !str_contains(json_encode($inspection, JSON_THROW_ON_ERROR),
'entitlement:')
&& CloudTrafficLedgerEvent::count() === $beforeLedger
&& (int) $afterInspect->version === (int) $beforePool->version,
'Inspect changed accounting state or leaked user/instance identity.');
$missingResolutionUuid = '99999999-9999-4999-8999-999999999999';
[$status, $missingResolution] = $run([
'inspect-resolution',
'--resolution-key=' . $missingResolutionUuid,
]);
$assert($status === 0 && $missingResolution['committed'] === false
&& $missingResolution['resolution_key'] === $missingResolutionUuid,
'A missing resolution UUID was not reported read-only.');
$unfixedUuid = '11111111-1111-4111-8111-111111111111';
$unfixedConfirm = 'RESOLVE_TRAFFIC_POOL:' . $poolId . ':'
. (int) $beforePool->version . ':' . $unfixedUuid;
try {
$run([
'resolve', '--pool-id=' . $poolId,
'--expected-version=' . (int) $beforePool->version,
'--resolution-key=' . $unfixedUuid, '--apply',
'--confirm=' . $unfixedConfirm,
]);
throw new RuntimeException('Unfixed counter regression was resolved.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& CloudTrafficLedgerEvent::where(
'event_type', 'attention_resolved'
)->count() === 0,
'Unfixed attention changed the immutable ledger.');
}
$instance->lifetime_download_bytes = 125;
$instance->traffic_synced_at = $now + 3;
$instance->save();
$resolvedUuid = '22222222-2222-4222-8222-222222222222';
$version = (int) CloudTrafficPool::findOrFail($poolId)->version;
$confirm = 'RESOLVE_TRAFFIC_POOL:' . $poolId . ':' . $version . ':'
. $resolvedUuid;
[$status, $resolved] = $run([
'resolve', '--pool-id=' . $poolId,
'--expected-version=' . $version,
'--resolution-key=' . $resolvedUuid, '--apply',
'--confirm=' . $confirm,
]);
$assert($status === 0 && $resolved['state'] === 'available'
&& $resolved['before_version'] === $version
&& $resolved['after_version'] > $version
&& CloudTrafficLedgerEvent::where(
'event_key', 'attention-resolution:' . $resolvedUuid
)->count() === 1,
'Verified CLI recovery did not create one audited resolution generation.');
[$status, $resolutionInspection] = $run([
'inspect-resolution', '--resolution-key=' . $resolvedUuid,
]);
$assert($status === 0 && $resolutionInspection['committed'] === true
&& $resolutionInspection['pool_id'] === $poolId
&& $resolutionInspection['expected_version'] === $version
&& $resolutionInspection['current_pool_state'] === 'available',
'A lost resolve response cannot be reconciled by immutable UUID.');
$resolvedCount = CloudTrafficLedgerEvent::where(
'event_type', 'attention_resolved'
)->count();
try {
$run([
'resolve', '--pool-id=' . $poolId,
'--expected-version=' . $version,
'--resolution-key=' . $resolvedUuid, '--apply',
'--confirm=' . $confirm,
]);
throw new RuntimeException('A used resolution key was accepted again.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), 'resolution-key 已使用')
&& str_contains($error->getMessage(), 'inspect-resolution')
&& CloudTrafficLedgerEvent::where(
'event_type', 'attention_resolved'
)->count() === $resolvedCount,
'Repeated recovery wrote another resolution event.');
}
[$secondInstance, $secondAttention] = $makeAttention(12, 102, 8, 2);
$secondPoolId = (int) $secondAttention['pool_id'];
$secondVersion = (int) $secondAttention['version'];
$crossConfirm = 'RESOLVE_TRAFFIC_POOL:' . $secondPoolId . ':'
. $secondVersion . ':' . $resolvedUuid;
try {
$run([
'resolve', '--pool-id=' . $secondPoolId,
'--expected-version=' . $secondVersion,
'--resolution-key=' . $resolvedUuid, '--apply',
'--confirm=' . $crossConfirm,
]);
throw new RuntimeException('A cross-pool resolution UUID was reused.');
} catch (RuntimeException $error) {
$assert(str_contains($error->getMessage(), 'resolution-key 已使用')
&& str_contains($error->getMessage(), 'inspect-resolution')
&& (string) CloudTrafficPool::findOrFail($secondPoolId)->state
=== 'attention',
'Cross-pool key reuse changed the second pool.');
}
$secondInstance->lifetime_download_bytes = 125;
$secondInstance->traffic_synced_at = $now + 3;
$secondInstance->save();
$beforeCrossPoolConflict = CloudTrafficPool::findOrFail($secondPoolId);
try {
CloudTrafficPoolAccounting::resolveAttention(
$secondPoolId,
(int) $beforeCrossPoolConflict->version,
$resolvedUuid,
$now + 3
);
throw new RuntimeException('Core accounting reused a resolution UUID.');
} catch (RuntimeException $error) {
$assert(str_contains(
$error->getMessage(),
'traffic_event_idempotency_conflict'
)
&& (string) CloudTrafficPool::findOrFail($secondPoolId)->state
=== 'attention'
&& CloudTrafficLedgerEvent::where(
'event_type', 'attention_resolved'
)->count() === $resolvedCount,
'Global resolution UUID conflict did not roll back atomically.');
}
$secondPool = CloudTrafficPool::findOrFail($secondPoolId);
$staleUuid = '33333333-3333-4333-8333-333333333333';
$staleVersion = max(1, (int) $secondPool->version - 1);
$staleConfirm = 'RESOLVE_TRAFFIC_POOL:' . $secondPoolId . ':'
. $staleVersion . ':' . $staleUuid;
try {
$run([
'resolve', '--pool-id=' . $secondPoolId,
'--expected-version=' . $staleVersion,
'--resolution-key=' . $staleUuid, '--apply',
'--confirm=' . $staleConfirm,
]);
throw new RuntimeException('A stale pool version was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409
&& (string) CloudTrafficPool::findOrFail($secondPoolId)->state
=== 'attention',
'Stale-version recovery changed the pool.');
}
echo "Cloud traffic-pool attention CLI integration checks passed.\n";