|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Services\Cloud\CloudTrafficPoolFreshness;
|
|
|
|
|
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_pools', static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->integer('user_id');
|
|
|
|
|
$t->integer('traffic_pool_group_id');
|
|
|
|
|
$t->string('state');
|
|
|
|
|
$t->integer('available_bytes')->default(0);
|
|
|
|
|
$t->integer('overage_bytes')->default(0);
|
|
|
|
|
$t->integer('version')->default(1);
|
|
|
|
|
$t->integer('updated_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_entitlements',
|
|
|
|
|
static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->integer('pool_id');
|
|
|
|
|
$t->integer('service_period_id');
|
|
|
|
|
$t->integer('instance_id');
|
|
|
|
|
$t->integer('user_id');
|
|
|
|
|
$t->string('quota_mode');
|
|
|
|
|
$t->string('state');
|
|
|
|
|
$t->integer('starts_at');
|
|
|
|
|
$t->integer('ends_at');
|
|
|
|
|
$t->integer('granted_bytes');
|
|
|
|
|
$t->integer('consumed_bytes');
|
|
|
|
|
$t->integer('forfeited_bytes')->default(0);
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_service_periods', static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->integer('instance_id');
|
|
|
|
|
$t->integer('user_id');
|
|
|
|
|
$t->integer('traffic_pool_group_id');
|
|
|
|
|
$t->string('traffic_quota_mode');
|
|
|
|
|
$t->integer('traffic_quota_bytes');
|
|
|
|
|
$t->string('status');
|
|
|
|
|
$t->integer('starts_at');
|
|
|
|
|
$t->integer('ends_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_instances', static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->integer('user_id');
|
|
|
|
|
$t->string('status')->default('active');
|
|
|
|
|
$t->integer('lifetime_download_bytes');
|
|
|
|
|
$t->integer('lifetime_upload_bytes');
|
|
|
|
|
$t->integer('traffic_synced_at');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_instance_cursors',
|
|
|
|
|
static function (Blueprint $t): void {
|
|
|
|
|
$t->integer('instance_id')->primary();
|
|
|
|
|
$t->integer('user_id');
|
|
|
|
|
$t->integer('pool_id');
|
|
|
|
|
$t->integer('accounted_download_bytes');
|
|
|
|
|
$t->integer('accounted_upload_bytes');
|
|
|
|
|
$t->integer('last_observed_at');
|
|
|
|
|
$t->string('state');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_scan_failures',
|
|
|
|
|
static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->string('scan_kind');
|
|
|
|
|
$t->integer('subject_id');
|
|
|
|
|
});
|
|
|
|
|
$schema->create('cloud_traffic_usage_settlement_buckets',
|
|
|
|
|
static function (Blueprint $t): void {
|
|
|
|
|
$t->increments('id');
|
|
|
|
|
$t->integer('pool_id');
|
|
|
|
|
$t->string('state');
|
|
|
|
|
$t->integer('seal_at');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$now = 2_000_000;
|
|
|
|
|
Capsule::table('cloud_traffic_pools')->insert([
|
|
|
|
|
'id' => 1, 'user_id' => 7, 'traffic_pool_group_id' => 3,
|
|
|
|
|
'state' => 'available', 'updated_at' => $now - 60,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_service_periods')->insert([
|
|
|
|
|
'id' => 10, 'instance_id' => 20, 'user_id' => 7,
|
|
|
|
|
'traffic_pool_group_id' => 3, 'traffic_quota_mode' => 'finite', 'traffic_quota_bytes' => 1000,
|
|
|
|
|
'status' => 'active', 'starts_at' => $now - 1000,
|
|
|
|
|
'ends_at' => $now + 1000,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_traffic_entitlements')->insert([
|
|
|
|
|
'id' => 30, 'pool_id' => 1, 'quota_mode' => 'finite', 'service_period_id' => 10, 'instance_id' => 20, 'user_id' => 7,
|
|
|
|
|
'state' => 'active', 'starts_at' => $now - 1000,
|
|
|
|
|
'ends_at' => $now + 1000, 'granted_bytes' => 1000,
|
|
|
|
|
'consumed_bytes' => 120, 'forfeited_bytes' => 0,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_instances')->insert([
|
|
|
|
|
'id' => 20, 'user_id' => 7, 'lifetime_download_bytes' => 100,
|
|
|
|
|
'lifetime_upload_bytes' => 20, 'traffic_synced_at' => $now - 60,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_traffic_instance_cursors')->insert([
|
|
|
|
|
'instance_id' => 20, 'user_id' => 7, 'pool_id' => 1,
|
|
|
|
|
'accounted_download_bytes' => 100, 'accounted_upload_bytes' => 20,
|
|
|
|
|
'last_observed_at' => $now - 60, 'state' => 'ready',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$current = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($current['status'] === 'current'
|
|
|
|
|
&& $current['safe_code'] === 'current'
|
|
|
|
|
&& $current['last_sample_at'] === $now - 60
|
|
|
|
|
&& $current['unsettled_instance_count'] === 0,
|
|
|
|
|
'Fully accounted counters were not projected as current.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_instances')->where('id', 20)->update([
|
|
|
|
|
'lifetime_download_bytes' => 130,
|
|
|
|
|
'traffic_synced_at' => $now - 10,
|
|
|
|
|
]);
|
|
|
|
|
$delayed = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($delayed['status'] === 'delayed'
|
|
|
|
|
&& $delayed['safe_code'] === 'settlement_delayed'
|
|
|
|
|
&& $delayed['unsettled_instance_count'] === 1
|
|
|
|
|
&& $delayed['last_sample_at'] === $now - 10,
|
|
|
|
|
'Persisted but unsettled counters did not expose the newest sample independently of booked balance.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 20)
|
|
|
|
|
->update(['state' => 'attention']);
|
|
|
|
|
$attention = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($attention['status'] === 'attention'
|
|
|
|
|
&& $attention['safe_code'] === 'cursor_attention',
|
|
|
|
|
'Attention cursor was presented as ordinary delay.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_instances')->where('id', 20)->update([
|
|
|
|
|
'lifetime_download_bytes' => 100,
|
|
|
|
|
'traffic_synced_at' => $now - 60,
|
|
|
|
|
]);
|
|
|
|
|
Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 20)
|
|
|
|
|
->update(['state' => 'ready']);
|
|
|
|
|
$cursor = Capsule::table('cloud_traffic_instance_cursors')->where('instance_id', 20);
|
|
|
|
|
$vm = Capsule::table('cloud_instances')->where('id', 20);
|
|
|
|
|
$cursor->update(['last_observed_at' => $now - 1000]);
|
|
|
|
|
$assert(CloudTrafficPoolFreshness::snapshot(7, 3, $now)['status'] === 'current',
|
|
|
|
|
'A live quiet VM required a redundant accounting heartbeat.');
|
|
|
|
|
$vm->update(['traffic_synced_at' => $now - 900]);
|
|
|
|
|
$stale = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($stale['safe_code'] === 'sample_stale' && !$stale['unsettled_instance_count'],
|
|
|
|
|
'An old live sample was counted as pending traffic.');
|
|
|
|
|
$vm->update(['status' => 'deleted']);
|
|
|
|
|
$retired = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($retired['status'] === 'current' && !$retired['stale_sample_count']
|
|
|
|
|
&& $retired['last_sample_at'] === null, 'A retired VM pinned pool freshness.');
|
|
|
|
|
$vm->update(['lifetime_download_bytes' => 400]);
|
|
|
|
|
$tail = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($tail['unsettled_instance_count'] === 1 && !$tail['stale_sample_count'],
|
|
|
|
|
'A deleted VM lost its final unaccounted 300 bytes.');
|
|
|
|
|
$vm->update(['lifetime_download_bytes' => 99, 'lifetime_upload_bytes' => 21]);
|
|
|
|
|
$assert(CloudTrafficPoolFreshness::snapshot(7, 3, $now)['status'] === 'attention',
|
|
|
|
|
'Opposite RX and TX changes hid a counter regression.');
|
|
|
|
|
$vm->update(['lifetime_download_bytes' => 100, 'lifetime_upload_bytes' => 20, 'status' => 'active', 'traffic_synced_at' => $now - 60]);
|
|
|
|
|
$cursor->update(['last_observed_at' => $now - 60]);
|
|
|
|
|
Capsule::table('cloud_traffic_usage_settlement_buckets')->insert([
|
|
|
|
|
'pool_id' => 1, 'state' => 'open', 'seal_at' => $now - 400,
|
|
|
|
|
]);
|
|
|
|
|
$overdue = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($overdue['status'] === 'delayed'
|
|
|
|
|
&& $overdue['overdue_bucket_count'] === 1,
|
|
|
|
|
'Five-minute settlement delay was hidden.');
|
|
|
|
|
Capsule::table('cloud_traffic_usage_settlement_buckets')->where('pool_id', 1)
|
|
|
|
|
->update(['seal_at' => $now - 4000]);
|
|
|
|
|
$overdueAttention = CloudTrafficPoolFreshness::snapshot(7, 3, $now);
|
|
|
|
|
$assert($overdueAttention['status'] === 'attention'
|
|
|
|
|
&& $overdueAttention['safe_code'] === 'settlement_overdue',
|
|
|
|
|
'One-hour settlement delay did not escalate to attention.');
|
|
|
|
|
|
|
|
|
|
$pending = CloudTrafficPoolFreshness::snapshot(9, 3, $now);
|
|
|
|
|
$assert($pending['status'] === 'delayed'
|
|
|
|
|
&& $pending['safe_code'] === 'projection_pending'
|
|
|
|
|
&& $pending['last_sample_at'] === null,
|
|
|
|
|
'Missing projection fabricated current accounting.');
|
|
|
|
|
$group = CloudTrafficPoolFreshness::forGroup(3, $now);
|
|
|
|
|
$assert($group['status'] === 'attention'
|
|
|
|
|
&& $group['pool_count'] === 1
|
|
|
|
|
&& $group['overdue_bucket_count'] === 1,
|
|
|
|
|
'Administrator group projection drifted from the user freshness model.');
|
|
|
|
|
|
|
|
|
|
Capsule::table('cloud_service_periods')->where('id', 10)->update(['traffic_pool_group_id' => 9]);
|
|
|
|
|
$preparingGroup = CloudTrafficPoolFreshness::forGroup(9, $now);
|
|
|
|
|
$assert($preparingGroup['pool_count'] === 0 && $preparingGroup['status'] === 'delayed',
|
|
|
|
|
'A group waiting for its first entitlement was presented as current.');
|
|
|
|
|
echo "Traffic-pool freshness projection checks passed.\n";
|