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/CloudTrafficStorageHealthSe...

181 lines
8.0 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudTrafficCheckpointRetention;
use App\Services\Cloud\CloudTrafficSettlementRetention;
use App\Services\Cloud\CloudTrafficStorageHealthService;
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->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'log');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$main = $capsule->schema('default');
$main->create('cloud_traffic_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
});
$main->create('cloud_traffic_usage_settlement_buckets',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('state');
$table->integer('pool_id')->default(1);
$table->integer('seal_at');
$table->binary('event_identity');
$table->integer('updated_at');
});
$main->create('cloud_traffic_ledger_events',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('event_key')->unique();
});
$main->create('cloud_traffic_instance_cursors',
static function (Blueprint $table): void {
$table->integer('instance_id')->primary();
$table->string('state');
$table->integer('accounted_download_bytes')->default(0);
$table->integer('accounted_upload_bytes')->default(0);
$table->integer('last_observed_at')->default(0);
});
$main->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$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')->default(0);
});
$main->create('cloud_traffic_pool_groups',
static function (Blueprint $table): void {
$table->increments('id');
$table->string('mode');
});
$main->create('cloud_service_periods',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id');
$table->integer('traffic_pool_group_id');
$table->integer('starts_at')->default(0);
$table->integer('ends_at')->default(99999999);
$table->string('status')->default('active');
$table->string('traffic_quota_mode')->default('finite');
});
$main->create('cloud_traffic_scan_failures',
static function (Blueprint $table): void {
$table->increments('id');
$table->integer('last_failed_at');
});
$capsule->schema('log')->create('cloud_vm_traffic_checkpoints',
static function (Blueprint $table): void {
$table->integer('instance_id');
$table->integer('first_observed_at');
});
$now = 20 * 86400;
$healthy = CloudTrafficStorageHealthService::snapshot($now);
$assert($healthy['status'] === 'ok'
&& $healthy['metrics']['settlement_buckets_due'] === 0
&& $healthy['metrics']['open_buckets_due'] === 0
&& $healthy['metrics']['unsettled_instances'] === 0
&& $healthy['metrics']['hourly_checkpoints_due'] === 0
&& $healthy['metrics']['cursor_attention_instances'] === 0,
'Empty compaction queues did not report a healthy bounded state.');
Capsule::table('cloud_traffic_instance_cursors')->insert(['instance_id' => 99, 'state' => 'attention']);
$isolated = CloudTrafficStorageHealthService::snapshot($now);
$assert($isolated['status'] === 'warning' && $isolated['metrics']['cursor_attention_instances'] === 1
&& str_contains($isolated['message'], '异常 1 个'), 'Attention was hidden without a due checkpoint.');
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 99)->delete();
$settlementCutoff = $now
- CloudTrafficSettlementRetention::RETENTION_SECONDS;
$checkpointCutoff = $now
- CloudTrafficCheckpointRetention::RETENTION_SECONDS;
$capsule->connection('default')
->table('cloud_traffic_usage_settlement_buckets')->insert([
['state' => 'sealed', 'seal_at' => $now - 86400,
'event_identity' => random_bytes(16),
'updated_at' => $settlementCutoff - 7200],
['state' => 'open', 'seal_at' => $now - 7200,
'event_identity' => str_repeat('a', 16),
'updated_at' => $now - 7200],
]);
$capsule->connection('default')->table(
'cloud_traffic_ledger_events'
)->insert([
'event_key' => 'traffic-settlement:v2:'
. bin2hex(str_repeat('a', 16)),
]);
$capsule->connection('log')->table('cloud_vm_traffic_checkpoints')->insert([
['instance_id' => 3, 'first_observed_at' => $checkpointCutoff - 7200],
['instance_id' => 4, 'first_observed_at' => $checkpointCutoff + 60],
]);
$capsule->connection('default')->table(
'cloud_traffic_instance_cursors'
)->insert([
['instance_id' => 3, 'state' => 'attention',
'accounted_download_bytes' => 0, 'accounted_upload_bytes' => 0,
'last_observed_at' => 0],
['instance_id' => 5, 'state' => 'ready',
'accounted_download_bytes' => 10, 'accounted_upload_bytes' => 2,
'last_observed_at' => $now - 1200],
]);
$capsule->connection('default')->table('cloud_instances')->insert([
['id' => 3, 'lifetime_download_bytes' => 0,
'lifetime_upload_bytes' => 0, 'traffic_synced_at' => 0],
['id' => 5, 'lifetime_download_bytes' => 20,
'lifetime_upload_bytes' => 2, 'traffic_synced_at' => $now - 600],
]);
$capsule->connection('default')->table(
'cloud_traffic_pool_groups'
)->insert(['id' => 1, 'mode' => 'enforced']);
$capsule->connection('default')->table('cloud_service_periods')->insert([
['id' => 1, 'instance_id' => 5, 'traffic_pool_group_id' => 1],
]);
$capsule->connection('default')->table(
'cloud_traffic_scan_failures'
)->insert(['last_failed_at' => $now - 60]);
$warning = CloudTrafficStorageHealthService::snapshot($now);
$assert($warning['status'] === 'warning'
&& $warning['metrics']['settlement_buckets_due'] === 1
&& $warning['metrics']['open_buckets_due'] === 1
&& $warning['metrics']['oldest_open_bucket_lag_seconds'] === 7200
&& $warning['metrics']['settlement_identity_conflicts'] === 1
&& $warning['metrics']['unsettled_instances'] === 1
&& $warning['metrics']['stale_cursor_instances'] === 1
&& $warning['metrics']['recent_accounting_failures'] === 1
&& $warning['metrics']['hourly_checkpoints_due'] === 1
&& $warning['metrics']['hourly_checkpoint_instances_due'] === 1
&& $warning['metrics']['cursor_attention_instances'] === 1
&& $warning['metrics']['oldest_due_lag_seconds'] === 7200
&& str_contains($warning['message'], '到期未封账 1 个')
&& str_contains($warning['message'], '身份冲突 1 个')
&& str_contains($warning['message'], '结算账务需立即核对')
&& str_contains($warning['message'], '小时检查点 1 行'),
'Compaction backlog or attention state was hidden from operator health.');
Capsule::table('cloud_instances')->where('id', 5)->update(['traffic_synced_at' => $now]);
$freshInput = CloudTrafficStorageHealthService::snapshot($now);
$assert($freshInput['metrics']['stale_cursor_instances'] === 1 && $freshInput['metrics']['stale_sample_instances'] === 0,
'Fresh collection hid outstanding accounting.');
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 5)->update(['accounted_download_bytes' => 20]);
Capsule::table('cloud_instances')->where('id', 5)->update(['traffic_synced_at' => $now - 600]);
$quiet = CloudTrafficStorageHealthService::snapshot($now);
$assert($quiet['metrics']['stale_cursor_instances'] === 0 && $quiet['metrics']['stale_sample_instances'] === 1,
'An old sample was counted as accounting backlog.');
echo "Traffic storage health summary contracts passed.\n";