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/CloudAbuseRetentionService.php

234 lines
8.7 KiB
PHP

<?php
namespace App\Services\Cloud;
use App\Models\User;
/** Bounded retention for the compact instance-centric abuse ledger. */
final class CloudAbuseRetentionService
{
public const DAILY_SUMMARY_RETENTION_SECONDS = 180 * 86400;
public const ALERT_HISTORY_RETENTION_SECONDS = 180 * 86400;
public const ORPHAN_HOST_RETENTION_SECONDS = 7 * 86400;
public const DEFAULT_BATCH_SIZE = 2000;
/** @var \Closure():int */
private readonly \Closure $clock;
public function __construct(?callable $clock = null)
{
$this->clock = \Closure::fromCallable(
$clock ?? static fn (): int => time()
);
}
/**
* Run one bounded, restart-safe main-database retention pass.
*
* @return array{
* daily_summaries:int,
* alert_events:int,
* resolved_findings:int,
* orphan_hosts:int
* }
*/
public function prune(int $limit = self::DEFAULT_BATCH_SIZE): array
{
if ($limit < 1 || $limit > self::DEFAULT_BATCH_SIZE) {
throw new \InvalidArgumentException(
'Abuse retention batch size must be between 1 and '
. self::DEFAULT_BATCH_SIZE
);
}
$now = ($this->clock)();
if (!is_int($now) || $now < self::ALERT_HISTORY_RETENTION_SECONDS) {
throw new \LogicException('Abuse retention clock is invalid');
}
$historyCutoff = $now - self::ALERT_HISTORY_RETENTION_SECONDS;
return [
'daily_summaries' => $this->pruneDailySummaries(
$now - self::DAILY_SUMMARY_RETENTION_SECONDS,
$limit
),
'alert_events' => $this->pruneAlertEvents(
$historyCutoff,
$limit
),
'resolved_findings' => $this->pruneResolvedFindings(
$historyCutoff,
$limit
),
'orphan_hosts' => $this->pruneOrphanHosts(
$now - self::ORPHAN_HOST_RETENTION_SECONDS,
$now,
$limit
),
];
}
private function pruneDailySummaries(int $cutoff, int $limit): int
{
$main = self::connection();
return $main->transaction(static function () use (
$main,
$cutoff,
$limit
): int {
$rows = $main->table('cloud_network_daily_summaries')
->where('day_start', '<=', $cutoff)
->orderBy('day_start')->orderBy('instance_id')
->limit($limit)->lockForUpdate()
->get(['instance_id', 'day_start']);
$deleted = 0;
foreach ($rows as $row) {
$deleted += (int) $main
->table('cloud_network_daily_summaries')
->where('instance_id', (int) $row->instance_id)
->where('day_start', (int) $row->day_start)
->where('day_start', '<=', $cutoff)
->delete();
}
return $deleted;
}, 3);
}
/**
* Pending, dispatching, unknown and not-yet-reserved events are permanent
* safety fences. Only terminal known-effect deliveries release an event.
*/
private function pruneAlertEvents(int $cutoff, int $limit): int
{
$main = self::connection();
return $main->transaction(static function () use (
$main,
$cutoff,
$limit
): int {
$events = $main->table('cloud_network_alert_events as event')
->join(
'cloud_network_alert_deliveries as delivery',
'delivery.event_id',
'=',
'event.id'
)->where('event.created_at', '<=', $cutoff)
->whereIn('delivery.state', ['sent', 'failed', 'skipped'])
->whereNotNull('delivery.terminal_at')
->where('delivery.terminal_at', '<=', $cutoff)
->orderBy('event.created_at')->orderBy('event.id')
->limit($limit)->lockForUpdate()
->get(['event.id']);
$ids = $events->map(
static fn (object $row): int => (int) $row->id
)->all();
if ($ids === []) {
return 0;
}
return (int) $main->table('cloud_network_alert_events')
->whereIn('id', $ids)
->where('created_at', '<=', $cutoff)
->whereExists(static function ($query) use ($cutoff): void {
$query->selectRaw('1')
->from('cloud_network_alert_deliveries as delivery')
->whereColumn(
'delivery.event_id',
'cloud_network_alert_events.id'
)->whereIn('delivery.state', [
'sent', 'failed', 'skipped',
])->whereNotNull('delivery.terminal_at')
->where('delivery.terminal_at', '<=', $cutoff);
})->delete();
}, 3);
}
private function pruneResolvedFindings(int $cutoff, int $limit): int
{
$main = self::connection();
return $main->transaction(static function () use (
$main,
$cutoff,
$limit
): int {
$ids = $main->table('cloud_network_findings')
->where('state', 'resolved')
->whereNotNull('resolved_at')
->where('resolved_at', '<=', $cutoff)
->whereNotExists(static function ($query): void {
$query->selectRaw('1')
->from('cloud_network_alert_events as event')
->whereColumn(
'event.finding_id',
'cloud_network_findings.id'
);
})->orderBy('resolved_at')->orderBy('id')
->limit($limit)->lockForUpdate()->pluck('id')
->map(static fn ($id): int => (int) $id)->all();
if ($ids === []) {
return 0;
}
return (int) $main->table('cloud_network_findings')
->whereIn('id', $ids)
->where('state', 'resolved')
->where('resolved_at', '<=', $cutoff)
->whereNotExists(static function ($query): void {
$query->selectRaw('1')
->from('cloud_network_alert_events as event')
->whereColumn(
'event.finding_id',
'cloud_network_findings.id'
);
})->delete();
}, 3);
}
private function pruneOrphanHosts(
int $cutoff,
int $now,
int $limit
): int {
$main = self::connection();
return $main->transaction(static function () use (
$main,
$cutoff,
$now,
$limit
): int {
$ids = $main->table('cloud_network_monitor_hosts')
->where('updated_at', '<=', $cutoff)
->where(static function ($lease) use ($now): void {
$lease->whereNull('poll_lease_token')
->orWhere('poll_lease_until', '<=', $now);
})->whereNotExists(static function ($query): void {
$query->selectRaw('1')->from('cloud_hosts as host')
->whereColumn(
'host.id',
'cloud_network_monitor_hosts.host_id'
);
})->orderBy('updated_at')->orderBy('host_id')
->limit($limit)->lockForUpdate()->pluck('host_id')
->map(static fn ($id): int => (int) $id)->all();
if ($ids === []) {
return 0;
}
return (int) $main->table('cloud_network_monitor_hosts')
->whereIn('host_id', $ids)
->where('updated_at', '<=', $cutoff)
->where(static function ($lease) use ($now): void {
$lease->whereNull('poll_lease_token')
->orWhere('poll_lease_until', '<=', $now);
})->whereNotExists(static function ($query): void {
$query->selectRaw('1')->from('cloud_hosts as host')
->whereColumn(
'host.id',
'cloud_network_monitor_hosts.host_id'
);
})->delete();
}, 3);
}
private static function connection(): object
{
return User::getConnectionResolver()->connection('default');
}
}