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/app/Services/Cloud/CloudTrafficCheckpointReten...

280 lines
11 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
use App\Models\CloudServicePeriod;
use App\Models\CloudTrafficInstanceCursor;
use App\Models\User;
/**
* Removes hot hourly traffic checkpoints only after compact evidence exists.
*
* Daily checkpoints preserve the cumulative history shape. A service period
* that still needs an intra-day boundary must already have either persisted
* base counters or an immutable boundary anchor before its hourly row can be
* removed. Any ambiguous state retains data and has no billing side effect.
*/
final class CloudTrafficCheckpointRetention
{
public const RETENTION_SECONDS = 72 * 3600;
public const MAX_INSTANCE_BATCH = 2000;
private const MAX_ROWS_PER_INSTANCE = 2000;
/**
* @return array{
* scanned_instances:int,
* scanned_rows:int,
* deleted:int,
* skipped:int,
* next_cursor:int,
* blocked:list<array{instance_id:int,code:string}>
* }
*/
public static function prune(
int $limit = 200,
?int $now = null,
int $afterInstanceId = 0
): array {
$now ??= time();
if ($now < self::RETENTION_SECONDS) {
throw new \InvalidArgumentException(
'traffic_checkpoint_retention_clock_invalid'
);
}
$limit = max(1, min(self::MAX_INSTANCE_BATCH, $limit));
$afterInstanceId = max(0, $afterInstanceId);
$cutoff = $now - self::RETENTION_SECONDS;
$log = User::getConnectionResolver()->connection('log');
foreach ([
'cloud_vm_traffic_checkpoints',
'cloud_vm_traffic_daily_checkpoints',
'cloud_vm_traffic_boundary_anchors',
] as $table) {
if (!$log->getSchemaBuilder()->hasTable($table)) {
throw new \RuntimeException(
'traffic_checkpoint_retention_schema_missing'
);
}
}
$instanceIds = self::candidateInstanceIds(
$log, $cutoff, $afterInstanceId, $limit
);
if ($instanceIds === [] && $afterInstanceId > 0) {
$instanceIds = self::candidateInstanceIds($log, $cutoff, 0, $limit);
}
$deleted = 0;
$skipped = 0;
$scannedRows = 0;
$blocked = [];
foreach ($instanceIds as $instanceId) {
try {
$result = self::pruneInstance(
$log, $instanceId, $cutoff
);
$deleted += $result['deleted'];
$skipped += $result['skipped'];
$scannedRows += $result['scanned'];
foreach ($result['blocked'] as $code) {
$blocked[] = [
'instance_id' => $instanceId,
'code' => $code,
];
}
} catch (\Throwable $error) {
$code = str_starts_with($error->getMessage(), 'traffic_')
? $error->getMessage()
: 'traffic_checkpoint_retention_failed';
$blocked[] = [
'instance_id' => $instanceId,
'code' => $code,
];
}
}
return [
'scanned_instances' => count($instanceIds),
'scanned_rows' => $scannedRows,
'deleted' => $deleted,
'skipped' => $skipped,
'next_cursor' => count($instanceIds) < $limit
? 0 : max($instanceIds),
'blocked' => $blocked,
];
}
/** @return list<int> */
private static function candidateInstanceIds($log, int $cutoff,
int $afterInstanceId, int $limit): array
{
return $log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', '>', $afterInstanceId)
->where('first_observed_at', '<=', $cutoff)
->distinct()->orderBy('instance_id')->limit($limit)
->pluck('instance_id')
->map(static fn ($id): int => (int) $id)
->filter(static fn (int $id): bool => $id > 0)
->values()->all();
}
/**
* @return array{scanned:int,deleted:int,skipped:int,blocked:list<string>}
*/
private static function pruneInstance($log, int $instanceId,
int $cutoff): array
{
$connection = CloudInstance::query()->getConnection();
return $connection->transaction(static function () use (
$log, $instanceId, $cutoff
): array {
$instance = CloudInstance::where('id', $instanceId)
->lockForUpdate()->first([
'id', 'user_id', 'lifetime_download_bytes',
'lifetime_upload_bytes',
]);
if ($instance === null) {
return self::blockedResult(
'traffic_checkpoint_instance_missing'
);
}
$cursor = CloudTrafficInstanceCursor::where(
'instance_id', $instanceId
)->lockForUpdate()->first(['instance_id', 'user_id', 'state']);
if ($cursor !== null
&& ((int) $cursor->user_id !== (int) $instance->user_id
|| (string) $cursor->state === 'attention')) {
return self::blockedResult(
'traffic_checkpoint_cursor_attention'
);
}
$periods = CloudServicePeriod::where('instance_id', $instanceId)
->whereIn('status', ['scheduled', 'active', 'exhausted'])
->where('starts_at', '<', $cutoff)
->lockForUpdate()->get([
'id', 'instance_id', 'user_id', 'starts_at',
'download_base_bytes', 'upload_base_bytes', 'status',
]);
return $log->transaction(static function () use (
$log, $instance, $instanceId, $cutoff, $periods
): array {
$rows = $log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', $instanceId)
->where('first_observed_at', '<=', $cutoff)
->orderBy('first_observed_at')
->limit(self::MAX_ROWS_PER_INSTANCE)
->lockForUpdate()->get();
if ($rows->isEmpty()) {
return [
'scanned' => 0, 'deleted' => 0, 'skipped' => 0,
'blocked' => [],
];
}
$days = $rows->map(static fn ($row): int =>
intdiv((int) $row->first_observed_at, 86400) * 86400
)->unique()->values()->all();
$dailyRows = $log->table(
'cloud_vm_traffic_daily_checkpoints'
)->where('instance_id', $instanceId)
->whereIn('checkpoint_day', $days)
->lockForUpdate()->get()->keyBy('checkpoint_day');
$periodIds = $periods->pluck('id')->map(
static fn ($id): int => (int) $id
)->all();
$anchors = $periodIds === [] ? collect() : $log->table(
'cloud_vm_traffic_boundary_anchors'
)->whereIn('service_period_id', $periodIds)
->lockForUpdate()->get()->keyBy('service_period_id');
$deleted = 0;
$skipped = 0;
$blocked = [];
foreach ($rows as $row) {
$code = self::rowBlocker(
$row, $instance, $dailyRows, $periods, $anchors
);
if ($code !== null) {
$skipped++;
$blocked[$code] = true;
continue;
}
$removed = $log->table('cloud_vm_traffic_checkpoints')
->where('instance_id', $instanceId)
->where('host_id', (int) $row->host_id)
->where('vm_uuid', $row->vm_uuid)
->where('checkpoint_hour',
(int) $row->checkpoint_hour)
->delete();
if ($removed !== 1) {
throw new \RuntimeException(
'traffic_checkpoint_delete_conflict'
);
}
$deleted++;
}
return [
'scanned' => $rows->count(),
'deleted' => $deleted,
'skipped' => $skipped,
'blocked' => array_keys($blocked),
];
}, 3);
}, 3);
}
private static function rowBlocker($row, CloudInstance $instance,
$dailyRows, $periods, $anchors): ?string
{
if ((int) $row->user_id !== (int) $instance->user_id) {
return 'traffic_checkpoint_owner_mismatch';
}
$observedAt = (int) $row->first_observed_at;
$download = (int) $row->download_rx_bytes;
$upload = (int) $row->upload_tx_bytes;
$day = intdiv($observedAt, 86400) * 86400;
$daily = $dailyRows->get($day);
if ($daily === null
|| (int) $daily->user_id !== (int) $instance->user_id
|| (int) $daily->first_observed_at > $observedAt
|| (int) $daily->last_observed_at < $observedAt
|| (int) $daily->first_download_rx_bytes > $download
|| (int) $daily->last_download_rx_bytes < $download
|| (int) $daily->first_upload_tx_bytes > $upload
|| (int) $daily->last_upload_tx_bytes < $upload) {
return 'traffic_checkpoint_daily_evidence_missing';
}
foreach ($periods as $period) {
if ((int) $period->starts_at >= $observedAt
|| ($period->download_base_bytes !== null
&& $period->upload_base_bytes !== null)) {
continue;
}
$anchor = $anchors->get((int) $period->id);
if ($anchor === null
|| (int) $anchor->instance_id !== (int) $instance->id
|| (int) $anchor->user_id !== (int) $instance->user_id
|| (int) $anchor->boundary_at !== (int) $period->starts_at
|| (int) $anchor->observed_at <= (int) $period->starts_at
|| (int) $anchor->download_rx_bytes < 0
|| (int) $anchor->upload_tx_bytes < 0
|| (int) $anchor->download_rx_bytes
> (int) $instance->lifetime_download_bytes
|| (int) $anchor->upload_tx_bytes
> (int) $instance->lifetime_upload_bytes
|| !in_array((string) $anchor->source_kind,
['hourly_checkpoint', 'metric_sample'], true)) {
return 'traffic_checkpoint_period_boundary_unfrozen';
}
}
return null;
}
/** @return array{scanned:int,deleted:int,skipped:int,blocked:list<string>} */
private static function blockedResult(string $code): array
{
return [
'scanned' => 0,
'deleted' => 0,
'skipped' => 1,
'blocked' => [$code],
];
}
}