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

1182 lines
44 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Models\CloudServicePeriod;
use App\Models\CloudTrafficEntitlement;
use App\Models\CloudTrafficInstanceCursor;
use App\Models\CloudTrafficLedgerEvent;
use App\Models\CloudTrafficPool;
use App\Services\Cloud\CloudTrafficPoolAccounting;
use App\Services\Cloud\CloudTrafficSettlementRetention;
use App\Services\Cloud\CloudError;
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('scheduled');
$table->integer('activated_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique('order_id');
$table->unique('renewal_id');
});
$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('seal_at');
$table->integer('bucket_revision');
$table->binary('event_identity')->unique();
$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_revision',
], 'traffic_settlement_identity');
});
$schema->create('cloud_traffic_settlement_receipts', 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('max_revision');
$table->integer('last_event_id');
$table->integer('compacted_amount_bytes');
$table->integer('compacted_event_count');
$table->string('last_payload_hash');
$table->integer('compacted_at');
$table->integer('updated_at');
$table->unique([
'pool_id', 'instance_id', 'service_period_id', 'allocation_kind',
'allocation_target_id', 'bucket_start',
], 'traffic_settlement_receipt_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');
});
$schema->create('cloud_traffic_scan_failures', static function (Blueprint $table): void {
$table->increments('id');
$table->string('scan_kind');
$table->integer('subject_id');
$table->integer('attempt_count')->default(1);
$table->string('last_error_code');
$table->integer('retry_at');
$table->integer('first_failed_at');
$table->integer('last_failed_at');
$table->unique(['scan_kind', 'subject_id']);
});
$base = time();
Capsule::table('cloud_traffic_pool_groups')->insert([
['id' => 1, 'code' => 'line-a', 'name' => 'Line A', 'mode' => 'shadow',
'created_at' => $base, 'updated_at' => $base],
['id' => 2, 'code' => 'line-b', 'name' => 'Line B', 'mode' => 'shadow',
'created_at' => $base, 'updated_at' => $base],
['id' => 3, 'code' => 'line-c', 'name' => 'Line C', 'mode' => 'shadow',
'created_at' => $base, 'updated_at' => $base],
['id' => 4, 'code' => 'line-d', 'name' => 'Line D', 'mode' => 'shadow',
'created_at' => $base, 'updated_at' => $base],
]);
$newInstance = static function (int $id, int $userId = 7) use ($base): CloudInstance {
$instance = new CloudInstance();
$instance->id = $id;
$instance->user_id = $userId;
$instance->current_period_id = null;
$instance->lifetime_download_bytes = 0;
$instance->lifetime_upload_bytes = 0;
$instance->traffic_synced_at = 0;
$instance->status = 'active';
$instance->created_at = $base;
$instance->updated_at = $base;
$instance->save();
return $instance;
};
$newPeriod = static function (
CloudInstance $instance,
int $id,
int $groupId,
int $quota,
int $startsAt,
int $endsAt,
string $mode = 'finite'
) use ($base): CloudServicePeriod {
$period = new CloudServicePeriod();
$period->id = $id;
$period->instance_id = (int) $instance->id;
$period->user_id = (int) $instance->user_id;
$period->traffic_pool_group_id = $groupId;
$period->traffic_quota_mode = $mode;
$period->starts_at = $startsAt;
$period->ends_at = $endsAt;
$period->traffic_quota_bytes = $quota;
$period->download_base_bytes = 0;
$period->upload_base_bytes = 0;
$period->download_used_bytes = 0;
$period->upload_used_bytes = 0;
$period->status = $startsAt <= $base ? 'active' : 'scheduled';
$period->activated_at = $startsAt <= $base ? $base : null;
$period->created_at = $base;
$period->updated_at = $base;
$period->save();
$instance->current_period_id = $id;
$instance->save();
return $period;
};
$instanceA = $newInstance(11);
$instanceB = $newInstance(12);
$instanceC = $newInstance(13);
$periodA = $newPeriod($instanceA, 101, 1, 200, $base, $base + 100);
$periodB = $newPeriod($instanceB, 102, 1, 200, $base, $base + 200);
$periodC = $newPeriod($instanceC, 103, 1, 50, $base, $base + 300);
foreach ([$periodA, $periodB, $periodC] as $period) {
$result = CloudTrafficPoolAccounting::reconcileServicePeriod(
(int) $period->id,
'test-period:' . (int) $period->id,
$base + 1
);
$assert($result['enabled'] === true, 'Active period was not materialized.');
}
Capsule::connection('default')->flushQueryLog();
Capsule::connection('default')->enableQueryLog();
$batchProjection = CloudTrafficPoolAccounting::snapshots([
['user_id' => 7, 'traffic_pool_group_id' => 1],
['user_id' => 7, 'traffic_pool_group_id' => 2],
['user_id' => 7, 'traffic_pool_group_id' => 3],
['user_id' => 9, 'traffic_pool_group_id' => 1],
], $base + 1);
$batchProjectionQueries = count(
Capsule::connection('default')->getQueryLog()
);
Capsule::connection('default')->disableQueryLog();
$assert($batchProjectionQueries <= 3
&& $batchProjection['7:1']['active_entitlement_bytes'] === 450
&& $batchProjection['7:2']['pool_id'] === null
&& $batchProjection['9:1']['pool_id'] === null,
'Batch snapshots regressed to a query loop or crossed pool identities.');
$instanceA->lifetime_download_bytes = 250;
$instanceA->lifetime_upload_bytes = 50;
$instanceA->traffic_synced_at = $base + 10;
$instanceA->save();
$settled = CloudTrafficPoolAccounting::settleInstanceUsage(11, $base + 10);
$assert($settled['available_bytes'] === 150,
'FEFO settlement produced the wrong remaining balance.');
$poolAfterFirstSettlement = CloudTrafficPool::where('user_id', 7)
->where('traffic_pool_group_id', 1)->first();
$assert($poolAfterFirstSettlement !== null
&& CloudTrafficPoolAccounting::verifyProjection(
(int) $poolAfterFirstSettlement->id,
$base + 10
)['valid'] === true,
'Realtime FEFO settlement was not immediately verifiable before sealing.');
$entitlements = CloudTrafficEntitlement::orderBy('ends_at')->get();
$assert((int) $entitlements[0]->consumed_bytes === 200
&& (int) $entitlements[1]->consumed_bytes === 100
&& (int) $entitlements[2]->consumed_bytes === 0,
'Usage was not allocated in strict FEFO order.');
$ledgerCount = CloudTrafficLedgerEvent::count();
$projectionBeforeRepeat = CloudTrafficPoolAccounting::snapshot(
7, 1, $base + 10
);
CloudTrafficPoolAccounting::settleInstanceUsage(11, $base + 10);
$projectionAfterRepeat = CloudTrafficPoolAccounting::snapshot(
7, 1, $base + 10
);
$assert(CloudTrafficLedgerEvent::count() === $ledgerCount
&& $projectionAfterRepeat['available_bytes']
=== $projectionBeforeRepeat['available_bytes']
&& $projectionAfterRepeat['overage_bytes']
=== $projectionBeforeRepeat['overage_bytes']
&& CloudTrafficPoolAccounting::verifyProjection(
(int) $poolAfterFirstSettlement->id,
$base + 10
)['valid'] === true,
'A repeated cumulative observation changed the immutable ledger.');
CloudTrafficPoolAccounting::reconcileServicePeriod(
101,
'test-expire:101',
$base + 101
);
$assert((string) CloudTrafficEntitlement::where(
'service_period_id', 101)->first()->state === 'expired'
&& CloudTrafficPoolAccounting::snapshot(7, 1, $base + 101)['available_bytes']
=== 150,
'A fully consumed expired entitlement deducted the pool twice.');
$instanceB->lifetime_download_bytes = 120;
$instanceB->traffic_synced_at = $base + 110;
$instanceB->save();
CloudTrafficPoolAccounting::settleInstanceUsage(12, $base + 110);
$assert(CloudTrafficPoolAccounting::snapshot(7, 1, $base + 110)[
'available_bytes'] === 30,
'A second instance did not consume the same shared pool.');
$instanceC->lifetime_download_bytes = 40;
$instanceC->traffic_synced_at = $base + 120;
$instanceC->save();
$exhausted = CloudTrafficPoolAccounting::settleInstanceUsage(13, $base + 120);
$assert($exhausted['state'] === 'exhausted'
&& $exhausted['available_bytes'] === 0
&& $exhausted['overage_bytes'] === 10,
'Sampling overage was not retained as explicit debt.');
$unlimited = $newInstance(14);
$unlimitedPeriod = $newPeriod(
$unlimited,
104,
1,
0,
$base,
$base + 350,
'unlimited'
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
104,
'test-unlimited:104',
$base + 1
);
$unlimited->lifetime_download_bytes = 1_000;
$unlimited->traffic_synced_at = $base + 130;
$unlimited->save();
CloudTrafficPoolAccounting::settleInstanceUsage(14, $base + 130);
$pool = CloudTrafficPool::where('user_id', 7)
->where('traffic_pool_group_id', 1)->first();
$assert((int) $pool->overage_bytes === 10,
'Unlimited-instance traffic consumed or increased the finite pool.');
$future = $newInstance(15);
$futurePeriod = $newPeriod(
$future,
105,
1,
100,
$base + 400,
$base + 800
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-future:105',
$base + 200
);
$assert(CloudTrafficPoolAccounting::snapshot(7, 1, $base + 200)[
'available_bytes'] === 0,
'A future renewal contributed quota before starts_at.');
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-activate:105',
$base + 400
);
$afterActivation = CloudTrafficPoolAccounting::snapshot(7, 1, $base + 400);
$assert($afterActivation['available_bytes'] === 90
&& $afterActivation['overage_bytes'] === 0,
'A new entitlement did not settle sampling debt before exposing balance.');
$futurePeriod = CloudServicePeriod::find(105);
$futurePeriod->traffic_quota_bytes = 150;
$futurePeriod->updated_at = $base + 410;
$futurePeriod->save();
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-upgrade:105:150',
$base + 410
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-upgrade:105:150',
$base + 410
);
$afterUpgrade = CloudTrafficPoolAccounting::snapshot(7, 1, $base + 410);
$assert($afterUpgrade['available_bytes'] === 140,
'Upgrade delta was missing or was granted more than once.');
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-close:105',
$base + 801
);
$closing = CloudTrafficEntitlement::where('service_period_id', 105)->first();
$assert((string) $closing->state === 'closing'
&& CloudTrafficPoolAccounting::snapshot(7, 1, $base + 801)[
'available_bytes'] === 0,
'Expired unused quota remained visible during bounded closing.');
CloudTrafficPoolAccounting::reconcileServicePeriod(
105,
'test-finalize:105',
$base + 801 + CloudTrafficPoolAccounting::CLOSING_SECONDS
);
$expired = CloudTrafficEntitlement::where('service_period_id', 105)->first();
$assert((string) $expired->state === 'expired'
&& (int) $expired->forfeited_bytes === 140,
'Closing did not forfeit only the entitlement unused balance.');
$other = $newInstance(21, 7);
$otherPeriod = $newPeriod($other, 201, 2, 500, $base, $base + 900);
CloudTrafficPoolAccounting::reconcileServicePeriod(
201,
'test-other-line:201',
$base + 1
);
$assert(CloudTrafficPoolAccounting::snapshot(7, 2, $base + 1)[
'available_bytes'] === 500
&& CloudTrafficPoolAccounting::snapshot(7, 1, $base + 1)[
'available_bytes'] === 0,
'Different traffic-pool groups were merged.');
$other->lifetime_download_bytes = 100;
$other->traffic_synced_at = $base + 20;
$other->save();
CloudTrafficPoolAccounting::settleInstanceUsage(21, $base + 20);
$other->lifetime_download_bytes = 90;
$other->traffic_synced_at = $base + 21;
$other->save();
$regression = CloudTrafficPoolAccounting::settleInstanceUsage(21, $base + 21);
$assert($regression['state'] === 'attention'
&& $regression['attention_code'] === 'traffic_counter_regression',
'A durable counter regression silently refunded or recharged traffic.');
$attentionLedgerCount = CloudTrafficLedgerEvent::count();
$attentionCursorVersion = (int) CloudTrafficInstanceCursor::find(21)->version;
CloudTrafficPoolAccounting::settlePendingUsage(200);
$assert(CloudTrafficLedgerEvent::count() === $attentionLedgerCount
&& (int) CloudTrafficInstanceCursor::find(21)->version
=== $attentionCursorVersion,
'The normal scanner repeatedly rewrote an attention cursor.');
try {
CloudTrafficPoolAccounting::resolveAttention(
(int) $regression['pool_id'],
(int) $regression['version'],
'not-a-resolution-uuid',
$base + 22
);
throw new RuntimeException('A non-UUID attention resolution was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 422,
'Invalid attention resolution identity returned the wrong status.');
}
try {
CloudTrafficPoolAccounting::resolveAttention(
(int) $regression['pool_id'],
(int) $regression['version'],
'00000000-0000-4000-8000-000000000001',
$base + 22
);
throw new RuntimeException('Unresolved counter regression was cleared.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Unresolved attention returned the wrong status.');
}
$other->lifetime_download_bytes = 150;
$other->traffic_synced_at = $base + 23;
$other->save();
$resolved = CloudTrafficPoolAccounting::resolveAttention(
(int) $regression['pool_id'],
(int) $regression['version'],
'00000000-0000-4000-8000-000000000002',
$base + 23
);
$assert($resolved['state'] === 'available'
&& $resolved['attention_code'] === null
&& $resolved['available_bytes'] === 350,
'Verified attention did not settle pending bytes before recovery.');
$accountingInstance = $newInstance(22, 8);
$accountingPeriod = $newPeriod(
$accountingInstance, 202, 2, 100, $base, $base + 900
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
202, 'test-accounting-attention:initial', $base + 1
);
$accountingPeriod->traffic_quota_bytes = 90;
$accountingPeriod->save();
$accountingAttention = CloudTrafficPoolAccounting::reconcileServicePeriod(
202, 'test-accounting-attention:decrease', $base + 2
);
$accountingAttentionEvents = CloudTrafficLedgerEvent::count();
$retriedAccountingAttention = CloudTrafficPoolAccounting::reconcileServicePeriod(
202, 'test-accounting-attention:decrease', $base + 3
);
$assert(CloudTrafficLedgerEvent::count() === $accountingAttentionEvents
&& $retriedAccountingAttention['state'] === 'attention',
'Retrying the same accounting fence changed its immutable event envelope.');
try {
CloudTrafficPoolAccounting::resolveAttention(
(int) $accountingAttention['pool_id'],
(int) $accountingAttention['version'],
'00000000-0000-4000-8000-000000000003',
$base + 3
);
throw new RuntimeException('Unfixed accounting conflict was cleared.');
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Unfixed accounting attention returned the wrong status.');
}
$accountingPeriod = CloudServicePeriod::find(202);
$accountingPeriod->traffic_quota_bytes = 100;
$accountingPeriod->save();
$accountingResolved = CloudTrafficPoolAccounting::resolveAttention(
(int) $accountingAttention['pool_id'],
(int) $accountingAttention['version'],
'00000000-0000-4000-8000-000000000004',
$base + 4
);
$assert($accountingResolved['state'] === 'available',
'A corrected service-period conflict had no audited recovery exit.');
$finiteToUnlimited = $newInstance(31);
$finiteOld = $newPeriod(
$finiteToUnlimited, 301, 3, 100, $base, $base + 100
);
$unlimitedNew = $newPeriod(
$finiteToUnlimited, 302, 3, 0, $base + 100, $base + 200, 'unlimited'
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
301, 'test-finite-old:301', $base + 1
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
302, 'test-unlimited-new:302', $base + 101
);
$finiteToUnlimited->lifetime_download_bytes = 60;
$finiteToUnlimited->traffic_synced_at = $base + 90;
$finiteToUnlimited->save();
CloudTrafficPoolAccounting::settleInstanceUsage(31, $base + 90);
$assert(CloudTrafficPoolAccounting::snapshot(7, 3, $base + 90)[
'available_bytes'] === 40,
'A delayed finite-period sample was ignored after switching to unlimited.');
$unlimitedToFinite = $newInstance(41);
$unlimitedOld = $newPeriod(
$unlimitedToFinite, 401, 4, 0, $base, $base + 100, 'unlimited'
);
$finiteNew = $newPeriod(
$unlimitedToFinite, 402, 4, 100, $base + 100, $base + 200
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
401, 'test-unlimited-old:401', $base + 1
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
402, 'test-finite-new:402', $base + 101
);
$unlimitedToFinite->lifetime_download_bytes = 60;
$unlimitedToFinite->traffic_synced_at = $base + 90;
$unlimitedToFinite->save();
CloudTrafficPoolAccounting::settleInstanceUsage(41, $base + 90);
$assert(CloudTrafficPoolAccounting::snapshot(7, 4, $base + 90)[
'available_bytes'] === 0,
'A delayed unlimited-period sample consumed future finite quota.');
$unlimitedToFinite->lifetime_download_bytes = 70;
$unlimitedToFinite->traffic_synced_at = $base + 110;
$unlimitedToFinite->save();
CloudTrafficPoolAccounting::settleInstanceUsage(41, $base + 110);
$assert(CloudTrafficPoolAccounting::snapshot(7, 4, $base + 110)[
'available_bytes'] === 90,
'Finite-period traffic after the boundary was not charged exactly once.');
$finishedWithTraffic = $newInstance(51, 9);
$finishedTrafficPeriod = $newPeriod(
$finishedWithTraffic,
501,
1,
100,
$base - 200,
$base - 30
);
$finishedTrafficPeriod->status = 'finished';
$finishedTrafficPeriod->save();
$finishedWithTraffic->lifetime_download_bytes = 30;
$finishedWithTraffic->traffic_synced_at = $base - 40;
$finishedWithTraffic->save();
CloudTrafficPoolAccounting::settlePendingUsage(200);
$finishedEntitlement = CloudTrafficEntitlement::where(
'service_period_id', 501
)->first();
$assert($finishedEntitlement !== null
&& (int) $finishedEntitlement->consumed_bytes === 30
&& (int) CloudTrafficInstanceCursor::find(51)->accounted_download_bytes
=== 30,
'A finished period lost its last persisted traffic sample.');
CloudTrafficPoolAccounting::reconcileDue(200, $base + 61);
$finishedEntitlement->refresh();
$assert((string) $finishedEntitlement->state === 'expired'
&& (int) $finishedEntitlement->forfeited_bytes === 70,
'A finished period did not converge after settling its final sample.');
$finishedWithoutTraffic = $newInstance(52, 10);
$finishedPeriod = $newPeriod(
$finishedWithoutTraffic,
502,
1,
80,
$base - 300,
$base - 200
);
$finishedPeriod->status = 'finished';
$finishedPeriod->save();
CloudTrafficPoolAccounting::reconcileDue(200, $base);
$finishedProjection = CloudTrafficEntitlement::where(
'service_period_id', 502
)->first();
$assert($finishedProjection !== null
&& (string) $finishedProjection->state === 'expired',
'A finished period without an entitlement was permanently skipped.');
$deletedInstance = $newInstance(53, 11);
$deletedPeriod = $newPeriod(
$deletedInstance, 503, 1, 100, $base, $base + 500
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
503, 'test-deleted-tail:initial', $base + 1
);
$deletedInstance->lifetime_download_bytes = 40;
$deletedInstance->traffic_synced_at = $base + 20;
$deletedInstance->status = 'deleted';
$deletedInstance->save();
CloudTrafficPoolAccounting::settlePendingUsage(200);
$deletedCursor = CloudTrafficInstanceCursor::find(53);
$assert($deletedCursor !== null
&& (int) $deletedCursor->accounted_download_bytes === 40,
'Deleting an instance skipped its final persisted counter delta.');
$lateClosingInstance = $newInstance(54, 12);
$lateClosingPeriod = $newPeriod(
$lateClosingInstance,
504,
1,
100,
$base - 300,
$base - 200
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
504, 'test-late-closing:activate', $base - 250
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
504, 'test-late-closing:expire', $base
);
$lateClosingEntitlement = CloudTrafficEntitlement::where(
'service_period_id', 504
)->first();
$closingEvent = CloudTrafficLedgerEvent::where(
'entitlement_id', (int) $lateClosingEntitlement->id
)->where('event_type', 'entitlement_closing')->first();
$closingPayload = json_decode((string) $closingEvent->payload_json, true);
$assert((string) $lateClosingEntitlement->state === 'expired'
&& (int) ($closingPayload['closing_deadline_at'] ?? 0)
=== $base - 200 + CloudTrafficPoolAccounting::CLOSING_SECONDS,
'Closing was incorrectly extended from the discovery time.');
$lateAttentionInstance = $newInstance(55, 13);
$lateAttentionPeriod = $newPeriod(
$lateAttentionInstance,
505,
1,
100,
$base,
$base + 30
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
505, 'test-late-attention:initial', $base + 1
);
$lateAttentionInstance->lifetime_download_bytes = 50;
$lateAttentionInstance->traffic_synced_at = $base + 10;
$lateAttentionInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(55, $base + 10);
$lateAttentionInstance->lifetime_download_bytes = 40;
$lateAttentionInstance->traffic_synced_at = $base + 20;
$lateAttentionInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(55, $base + 20);
CloudTrafficPoolAccounting::reconcileServicePeriod(
505, 'test-late-attention:closing', $base + 31
);
$lateAttentionInstance->lifetime_download_bytes = 60;
$lateAttentionInstance->traffic_synced_at = $base + 20;
$lateAttentionInstance->save();
$lateAttentionPool = CloudTrafficPool::where('user_id', 13)
->where('traffic_pool_group_id', 1)->first();
$lateAttentionResult = CloudTrafficPoolAccounting::resolveAttention(
(int) $lateAttentionPool->id,
(int) $lateAttentionPool->version,
'00000000-0000-4000-8000-000000000005',
$base + 121
);
$lateAttentionEntitlement = CloudTrafficEntitlement::where(
'service_period_id', 505
)->first();
$assert((int) $lateAttentionEntitlement->consumed_bytes === 50
&& (int) $lateAttentionEntitlement->forfeited_bytes === 50
&& $lateAttentionResult['overage_bytes'] === 10,
'Attention recovery consumed expired quota after the closing deadline.');
$snapshotInstance = $newInstance(56, 14);
$snapshotPeriod = $newPeriod(
$snapshotInstance, 506, 1, 100, $base, $base + 500
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
506, 'test-snapshot:initial', $base + 1
);
$snapshotInstance->lifetime_download_bytes = 20;
$snapshotInstance->traffic_synced_at = $base + 10;
$snapshotInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(56, $base + 5);
$snapshotBucket = Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 56)->where('state', 'open')->first();
$assert($snapshotBucket !== null
&& (int) $snapshotBucket->last_observed_at === $base + 10,
'Counters were paired with a stale observation timestamp.');
$settlementDay = intdiv($base, 86400) * 86400;
$bucketedInstance = $newInstance(72, 22);
$bucketedPeriod = $newPeriod(
$bucketedInstance,
800,
1,
1_000,
$settlementDay,
$settlementDay + 172_800
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
800, 'test-bucketed-settlement:initial', $base
);
$bucketedInstance->lifetime_download_bytes = 10;
$bucketedInstance->lifetime_upload_bytes = 5;
$bucketedInstance->traffic_synced_at = $settlementDay + 10;
$bucketedInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(72, $base);
$bucketedInstance->lifetime_download_bytes = 25;
$bucketedInstance->lifetime_upload_bytes = 8;
$bucketedInstance->traffic_synced_at = $settlementDay + 3_620;
$bucketedInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(72, $base);
$bucketedPool = CloudTrafficPool::where('user_id', 22)
->where('traffic_pool_group_id', 1)->first();
$openBucket = Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 72)->where('state', 'open')->first();
$assert($openBucket !== null
&& (int) $openBucket->bucket_start === $settlementDay
&& (int) $openBucket->seal_at === $settlementDay + 86_400
&& (int) $openBucket->amount_bytes === 33
&& (int) $openBucket->download_bytes === 25
&& (int) $openBucket->upload_bytes === 8
&& (int) $openBucket->sample_count === 2
&& CloudTrafficLedgerEvent::where('instance_id', 72)
->where('event_type', 'usage_allocated')->count() === 0
&& CloudTrafficPoolAccounting::verifyProjection(
(int) $bucketedPool->id
)['valid'] === true,
'Within-day traffic did not remain a verifiable durable accumulator.');
$assert(CloudTrafficPoolAccounting::sealDueUsageBuckets(
200, $settlementDay + 3_600
) === 0,
'A daily settlement bucket was sealed at the former hourly boundary.');
$sealedBucketCount = CloudTrafficPoolAccounting::sealDueUsageBuckets(
200, $settlementDay + 86_400
);
$assert($sealedBucketCount >= 1
&& CloudTrafficPoolAccounting::sealDueUsageBuckets(
200, $settlementDay + 86_400
) === 0
&& Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 72)->where('state', 'sealed')->count() === 1
&& CloudTrafficLedgerEvent::where('instance_id', 72)
->where('event_type', 'usage_allocated')->count() === 1,
'Completed settlement buckets were not sealed exactly once.');
$sealedBucket = Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 72)->where('state', 'sealed')->first();
$sealedEvent = $sealedBucket === null ? null
: CloudTrafficLedgerEvent::find((int) $sealedBucket->sealed_event_id);
$assert($sealedBucket !== null && $sealedEvent !== null
&& strlen((string) $sealedBucket->event_identity) === 16
&& (string) $sealedEvent->event_key === 'traffic-settlement:v2:'
. bin2hex((string) $sealedBucket->event_identity)
&& (string) $sealedEvent->source_id
=== bin2hex((string) $sealedBucket->event_identity),
'Sealed settlement fixture is incomplete.');
$compaction = CloudTrafficSettlementRetention::compact(
200,
(int) $sealedBucket->updated_at
+ CloudTrafficSettlementRetention::RETENTION_SECONDS + 1
);
$receipt = Capsule::table('cloud_traffic_settlement_receipts')
->where('instance_id', 72)->first();
$repeatCompaction = CloudTrafficSettlementRetention::compact(
200,
(int) $sealedBucket->updated_at
+ CloudTrafficSettlementRetention::RETENTION_SECONDS + 1
);
$assert($compaction['compacted'] >= 1
&& $compaction['compacted'] === $compaction['scanned']
&& $compaction['blocked'] === []
&& $receipt !== null
&& (int) $receipt->max_revision === 1
&& (int) $receipt->last_event_id === (int) $sealedEvent->id
&& $repeatCompaction['scanned'] === 0
&& !Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('id', (int) $sealedBucket->id)->exists(),
'Verified sealed settlement was not compacted atomically: '
. json_encode($compaction, JSON_THROW_ON_ERROR));
$bucketedInstance->lifetime_download_bytes = 30;
$bucketedInstance->lifetime_upload_bytes = 10;
$bucketedInstance->traffic_synced_at = $settlementDay + 7_230;
$bucketedInstance->save();
CloudTrafficPoolAccounting::settleInstanceUsage(72, $base);
$lateBucket = Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 72)->where('state', 'open')->first();
$assert($lateBucket !== null
&& (int) $lateBucket->bucket_revision === 2
&& (int) $lateBucket->amount_bytes === 7
&& CloudTrafficPoolAccounting::verifyProjection(
(int) $bucketedPool->id
)['valid'] === true,
'A late observation could not create a new immutable bucket revision.');
$legacyCollision = new CloudTrafficLedgerEvent();
$legacyCollision->pool_id = (int) $bucketedPool->id;
$legacyCollision->user_id = 22;
$legacyCollision->pool_sequence = (int) $bucketedPool->last_sequence + 100;
$legacyCollision->event_key = 'traffic-settlement-bucket:'
. (int) $lateBucket->id;
$legacyCollision->event_type = 'usage_allocated';
$legacyCollision->source_type = 'legacy_test';
$legacyCollision->source_id = (string) $lateBucket->id;
$legacyCollision->source_revision = 1;
$legacyCollision->payload_hash = str_repeat('0', 64);
$legacyCollision->instance_id = 72;
$legacyCollision->service_period_id = (int) $lateBucket->service_period_id;
$legacyCollision->entitlement_id = (int) $lateBucket->entitlement_id;
$legacyCollision->amount_bytes = 0;
$legacyCollision->effective_at = (int) $lateBucket->bucket_start;
$legacyCollision->observed_at = null;
$legacyCollision->payload_json = '{}';
$legacyCollision->created_at = $base;
$legacyCollision->save();
$assert(CloudTrafficPoolAccounting::sealDueUsageBuckets(
200, $settlementDay + 86_400
) === 1,
'A legacy row-id event key still collided with the durable v2 identity.');
$lateSealedBucket = Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('instance_id', 72)->where('bucket_revision', 2)->first();
$lateSealedEvent = $lateSealedBucket === null ? null
: CloudTrafficLedgerEvent::find((int) $lateSealedBucket->sealed_event_id);
$assert($lateSealedBucket !== null && $lateSealedEvent !== null,
'Late sealed settlement fixture is incomplete.');
$lateSealedEvent->amount_bytes = (int) $lateSealedEvent->amount_bytes + 1;
$lateSealedEvent->save();
$blockedCompaction = CloudTrafficSettlementRetention::compact(
200,
(int) $lateSealedBucket->updated_at
+ CloudTrafficSettlementRetention::RETENTION_SECONDS + 1
);
$assert(count($blockedCompaction['blocked']) === 1
&& Capsule::table('cloud_traffic_usage_settlement_buckets')
->where('id', (int) $lateSealedBucket->id)->exists(),
'Mismatched immutable evidence was deleted during compaction.');
$lateSealedEvent->amount_bytes = (int) $lateSealedEvent->amount_bytes - 1;
$lateSealedEvent->save();
$scanInstanceA = $newInstance(57, 15);
$scanPeriodA = $newPeriod(
$scanInstanceA, 507, 1, 100, $base, $base + 500
);
$scanInstanceB = $newInstance(58, 15);
$scanPeriodB = $newPeriod(
$scanInstanceB, 508, 1, 100, $base, $base + 500
);
foreach ([507, 508] as $scanPeriodId) {
CloudTrafficPoolAccounting::reconcileServicePeriod(
$scanPeriodId,
'test-scan:' . $scanPeriodId,
$base + 1
);
}
$scanInstanceA->lifetime_download_bytes = 10;
$scanInstanceA->traffic_synced_at = $base + 10;
$scanInstanceA->save();
$scanInstanceB->lifetime_download_bytes = 20;
$scanInstanceB->traffic_synced_at = $base + 10;
$scanInstanceB->save();
$firstUsageWindow = CloudTrafficPoolAccounting::settlePendingUsage(1, 0);
$secondUsageWindow = CloudTrafficPoolAccounting::settlePendingUsage(
1,
(int) $firstUsageWindow['next_cursor']
);
$usageWrap = CloudTrafficPoolAccounting::settlePendingUsage(
1,
(int) $secondUsageWindow['next_cursor']
);
$assert((int) $firstUsageWindow['next_cursor'] === 57
&& (int) $secondUsageWindow['next_cursor'] === 58
&& (int) $usageWrap['next_cursor'] === 0
&& (int) CloudTrafficInstanceCursor::find(58)->accounted_download_bytes
=== 20,
'The bounded usage scan did not advance past the first selected row.');
$scanFinishedA = $newInstance(59, 16);
$scanFinishedPeriodA = $newPeriod(
$scanFinishedA, 509, 1, 50, $base - 200, $base - 100
);
$scanFinishedPeriodA->status = 'finished';
$scanFinishedPeriodA->save();
$scanFinishedB = $newInstance(60, 17);
$scanFinishedPeriodB = $newPeriod(
$scanFinishedB, 510, 1, 50, $base - 200, $base - 100
);
$scanFinishedPeriodB->status = 'finished';
$scanFinishedPeriodB->save();
$firstPeriodWindow = CloudTrafficPoolAccounting::reconcileDue(
1,
$base,
508
);
$secondPeriodWindow = CloudTrafficPoolAccounting::reconcileDue(
1,
$base,
(int) $firstPeriodWindow['next_cursor']
);
$periodWrap = CloudTrafficPoolAccounting::reconcileDue(
1,
$base,
(int) $secondPeriodWindow['next_cursor']
);
$assert((int) $firstPeriodWindow['next_cursor'] === 509
&& (int) $secondPeriodWindow['next_cursor'] === 510
&& (int) $periodWrap['next_cursor'] === 0
&& CloudTrafficEntitlement::where('service_period_id', 510)->exists(),
'The bounded period scan did not advance past the first selected row.');
$dueBeforePoisonInstance = $newInstance(61, 18);
$dueBeforePoison = $newPeriod(
$dueBeforePoisonInstance,
511,
1,
50,
$base - 200,
$base - 100
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
511, 'test-cross-category:activate', $base - 150
);
$poisonPeriodInstance = $newInstance(62, 19);
$poisonPeriod = $newPeriod(
$poisonPeriodInstance,
600,
1,
50,
$base,
$base
);
$crossCategoryWindow = CloudTrafficPoolAccounting::reconcileDue(
1,
$base,
0
);
$assert((int) $crossCategoryWindow['next_cursor'] === 511
&& (string) CloudTrafficEntitlement::where(
'service_period_id', 511
)->first()->state === 'expired',
'A higher-ID missing period starved a lower-ID due entitlement.');
$poisonWindow = CloudTrafficPoolAccounting::reconcileDue(
1,
$base,
511
);
$poisonRetry = CloudTrafficPoolAccounting::reconcileDue(1, $base, 0);
$assert(count($poisonWindow['errors']) === 1
&& Capsule::table('cloud_traffic_scan_failures')
->where('scan_kind', 'period')->where('subject_id', 600)->exists()
&& $poisonRetry['processed'] === 0
&& $poisonRetry['errors'] === [],
'A deterministic period error was not quarantined across scan restarts.');
$poisonUsageInstance = $newInstance(63, 20);
$poisonUsagePeriod = $newPeriod(
$poisonUsageInstance,
601,
1,
50,
$base,
$base
);
$poisonUsageInstance->lifetime_download_bytes = 10;
$poisonUsageInstance->traffic_synced_at = $base + 1;
$poisonUsageInstance->save();
$healthyUsageInstance = $newInstance(64, 20);
$healthyUsagePeriod = $newPeriod(
$healthyUsageInstance,
602,
1,
50,
$base,
$base + 500
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
602, 'test-usage-quarantine:healthy', $base + 1
);
$healthyUsageInstance->lifetime_download_bytes = 10;
$healthyUsageInstance->traffic_synced_at = $base + 1;
$healthyUsageInstance->save();
$badUsageWindow = CloudTrafficPoolAccounting::settlePendingUsage(1, 0);
$goodUsageWindow = CloudTrafficPoolAccounting::settlePendingUsage(
1,
(int) $badUsageWindow['next_cursor']
);
$usageRestart = CloudTrafficPoolAccounting::settlePendingUsage(1, 0);
$assert(count($badUsageWindow['errors']) === 1
&& (int) $goodUsageWindow['next_cursor'] === 64
&& Capsule::table('cloud_traffic_scan_failures')
->where('scan_kind', 'instance')->where('subject_id', 63)->exists()
&& $usageRestart['errors'] === [],
'A deterministic instance error starved later usage after a restart.');
$idleScan = CloudTrafficPoolAccounting::settlePendingUsage(1);
$assert($idleScan['processed'] === 0,
'An instance without a trusted sample poisoned the bounded scan queue.');
$compensatedTrafficInstance = $newInstance(70, 21);
$compensatedTrafficPeriod = $newPeriod(
$compensatedTrafficInstance,
700,
1,
100,
$base,
$base + 1000
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
700,
'test-compensation-window:700',
$base + 1
);
$trafficBefore = CloudTrafficEntitlement::where(
'service_period_id', 700
)->firstOrFail();
$trafficBeforeCommercial = [
(int) $trafficBefore->granted_bytes,
(int) $trafficBefore->consumed_bytes,
(int) $trafficBefore->forfeited_bytes,
(string) $trafficBefore->state,
];
$trafficOldEnd = (int) $compensatedTrafficPeriod->ends_at;
$trafficNewEnd = $trafficOldEnd + 3600;
$extendedTraffic = CloudTrafficPoolAccounting::
extendEntitlementWindowForCompensationLocked(
700,
$trafficOldEnd,
$trafficNewEnd,
9001,
$base + 2
);
$windowEvent = CloudTrafficLedgerEvent::where(
'event_key', 'compensation-item:9001:traffic-window'
)->first();
$assert($extendedTraffic !== null
&& (int) $extendedTraffic->ends_at === $trafficNewEnd
&& [
(int) $extendedTraffic->granted_bytes,
(int) $extendedTraffic->consumed_bytes,
(int) $extendedTraffic->forfeited_bytes,
(string) $extendedTraffic->state,
] === $trafficBeforeCommercial
&& $windowEvent !== null
&& (string) $windowEvent->event_type
=== 'entitlement_window_extended'
&& (string) $windowEvent->source_type === 'service_compensation'
&& (int) $windowEvent->amount_bytes === 0,
'Traffic compensation changed quota or omitted its immutable window event.');
$compensatedTrafficPeriod->ends_at = $trafficNewEnd;
$compensatedTrafficPeriod->save();
$windowEventCount = CloudTrafficLedgerEvent::where(
'event_key', 'compensation-item:9001:traffic-window'
)->count();
CloudTrafficPoolAccounting::extendEntitlementWindowForCompensationLocked(
700,
$trafficOldEnd,
$trafficNewEnd,
9001,
$base + 3
);
$assert(CloudTrafficLedgerEvent::where(
'event_key', 'compensation-item:9001:traffic-window'
)->count() === $windowEventCount,
'Traffic compensation replay appended a second ledger event.');
$closedTrafficInstance = $newInstance(71, 21);
$closedTrafficPeriod = $newPeriod(
$closedTrafficInstance,
701,
1,
100,
$base,
$base + 1100
);
CloudTrafficPoolAccounting::reconcileServicePeriod(
701,
'test-compensation-window:701',
$base + 1
);
$closedEntitlement = CloudTrafficEntitlement::where(
'service_period_id', 701
)->firstOrFail();
$closedEntitlement->state = 'expired';
$closedEntitlement->closed_at = $base + 1;
$closedEntitlement->save();
try {
CloudTrafficPoolAccounting::extendEntitlementWindowForCompensationLocked(
701,
(int) $closedTrafficPeriod->ends_at,
(int) $closedTrafficPeriod->ends_at + 3600,
9002,
$base + 2
);
throw new RuntimeException(
'An expired traffic entitlement was revived by compensation.'
);
} catch (CloudError $error) {
$assert($error->httpStatus === 409,
'Closed traffic compensation returned the wrong conflict status.');
}
$assert((string) $closedEntitlement->fresh()->state === 'expired'
&& (int) $closedEntitlement->fresh()->ends_at
=== (int) $closedTrafficPeriod->ends_at,
'Rejected traffic compensation mutated a closed entitlement.');
$verification = CloudTrafficPoolAccounting::verifyProjection((int) $pool->id);
$assert($verification['valid'] === true,
'Immutable ledger does not reproduce entitlement or pool projections: '
. implode(',', $verification['errors']));
echo "Cloud traffic pool accounting checks passed.\n";