feat(traffic): expose storage compaction health
parent
6fa13651b1
commit
2bb9bb78b6
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
/** Read-only bounded summary for traffic-storage compaction health. */
|
||||
final class CloudTrafficStorageHealthService
|
||||
{
|
||||
private const WARNING_LAG_SECONDS = 3600;
|
||||
private const INSTANCE_SCAN_LIMIT = 2000;
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* key:string,label:string,status:string,message:string,checked_at:int,
|
||||
* metrics:array<string,int|bool>
|
||||
* }
|
||||
*/
|
||||
public static function snapshot(?int $now = null): array
|
||||
{
|
||||
$now ??= time();
|
||||
if ($now <= max(
|
||||
CloudTrafficSettlementRetention::RETENTION_SECONDS,
|
||||
CloudTrafficCheckpointRetention::RETENTION_SECONDS
|
||||
)) {
|
||||
throw new \InvalidArgumentException(
|
||||
'traffic_storage_health_clock_invalid'
|
||||
);
|
||||
}
|
||||
$main = User::getConnectionResolver()->connection();
|
||||
$log = User::getConnectionResolver()->connection('log');
|
||||
if (!$main->getSchemaBuilder()->hasTable(
|
||||
'cloud_traffic_usage_settlement_buckets'
|
||||
) || !$main->getSchemaBuilder()->hasTable(
|
||||
'cloud_traffic_instance_cursors'
|
||||
) || !$log->getSchemaBuilder()->hasTable(
|
||||
'cloud_vm_traffic_checkpoints'
|
||||
)) {
|
||||
throw new \RuntimeException('traffic_storage_health_schema_missing');
|
||||
}
|
||||
$settlementCutoff = $now
|
||||
- CloudTrafficSettlementRetention::RETENTION_SECONDS;
|
||||
$checkpointCutoff = $now
|
||||
- CloudTrafficCheckpointRetention::RETENTION_SECONDS;
|
||||
$settlementQuery = $main->table(
|
||||
'cloud_traffic_usage_settlement_buckets'
|
||||
)->where('state', 'sealed')->where('updated_at', '<=', $settlementCutoff);
|
||||
$settlementCount = (int) (clone $settlementQuery)->count();
|
||||
$oldestSettlement = $settlementCount === 0 ? null
|
||||
: (int) (clone $settlementQuery)->min('updated_at');
|
||||
$checkpointQuery = $log->table('cloud_vm_traffic_checkpoints')
|
||||
->where('instance_id', '>', 0)
|
||||
->where('first_observed_at', '<=', $checkpointCutoff);
|
||||
$checkpointCount = (int) (clone $checkpointQuery)->count();
|
||||
$oldestCheckpoint = $checkpointCount === 0 ? null
|
||||
: (int) (clone $checkpointQuery)->min('first_observed_at');
|
||||
$dueInstanceIds = (clone $checkpointQuery)
|
||||
->select('instance_id')->distinct()->orderBy('instance_id')
|
||||
->limit(self::INSTANCE_SCAN_LIMIT + 1)->pluck('instance_id')
|
||||
->map(static fn ($id): int => (int) $id)
|
||||
->filter(static fn (int $id): bool => $id > 0)
|
||||
->values()->all();
|
||||
$scanTruncated = count($dueInstanceIds) > self::INSTANCE_SCAN_LIMIT;
|
||||
if ($scanTruncated) {
|
||||
$dueInstanceIds = array_slice(
|
||||
$dueInstanceIds, 0, self::INSTANCE_SCAN_LIMIT
|
||||
);
|
||||
}
|
||||
$attentionCount = $dueInstanceIds === [] ? 0
|
||||
: (int) $main->table('cloud_traffic_instance_cursors')
|
||||
->whereIn('instance_id', $dueInstanceIds)
|
||||
->where('state', 'attention')->count();
|
||||
$oldestLag = max(
|
||||
$oldestSettlement === null
|
||||
? 0 : $settlementCutoff - $oldestSettlement,
|
||||
$oldestCheckpoint === null
|
||||
? 0 : $checkpointCutoff - $oldestCheckpoint
|
||||
);
|
||||
$metrics = [
|
||||
'settlement_buckets_due' => $settlementCount,
|
||||
'hourly_checkpoints_due' => $checkpointCount,
|
||||
'hourly_checkpoint_instances_due' => count($dueInstanceIds),
|
||||
'cursor_attention_instances' => $attentionCount,
|
||||
'oldest_due_lag_seconds' => max(0, $oldestLag),
|
||||
'instance_scan_truncated' => $scanTruncated,
|
||||
];
|
||||
$status = $attentionCount > 0 || $scanTruncated
|
||||
|| $oldestLag > self::WARNING_LAG_SECONDS
|
||||
? 'warning' : 'ok';
|
||||
if ($settlementCount === 0 && $checkpointCount === 0) {
|
||||
$message = '结算桶和小时检查点均处于既定保留窗口内。';
|
||||
} else {
|
||||
$message = '待压缩结算桶 ' . $settlementCount
|
||||
. ' 个,小时检查点 ' . $checkpointCount . ' 行(涉及 '
|
||||
. count($dueInstanceIds) . ' 个实例);最旧超出窗口 '
|
||||
. self::duration(max(0, $oldestLag)) . ',其中流量游标异常 '
|
||||
. $attentionCount . ' 个实例。';
|
||||
if ($scanTruncated) {
|
||||
$message .= ' 实例计数已达到有界扫描上限。';
|
||||
}
|
||||
}
|
||||
return [
|
||||
'key' => 'traffic_storage',
|
||||
'label' => '流量存储压缩',
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
'checked_at' => $now,
|
||||
'metrics' => $metrics,
|
||||
];
|
||||
}
|
||||
|
||||
private static function duration(int $seconds): string
|
||||
{
|
||||
if ($seconds >= 86400) {
|
||||
return intdiv($seconds, 86400) . ' 天';
|
||||
}
|
||||
if ($seconds >= 3600) {
|
||||
return intdiv($seconds, 3600) . ' 小时';
|
||||
}
|
||||
return intdiv($seconds, 60) . ' 分钟';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
<?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_usage_settlement_buckets',
|
||||
static function (Blueprint $table): void {
|
||||
$table->increments('id');
|
||||
$table->string('state');
|
||||
$table->integer('updated_at');
|
||||
});
|
||||
$main->create('cloud_traffic_instance_cursors',
|
||||
static function (Blueprint $table): void {
|
||||
$table->integer('instance_id')->primary();
|
||||
$table->string('state');
|
||||
});
|
||||
$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']['hourly_checkpoints_due'] === 0
|
||||
&& $healthy['metrics']['cursor_attention_instances'] === 0,
|
||||
'Empty compaction queues did not report a healthy bounded state.');
|
||||
|
||||
$settlementCutoff = $now
|
||||
- CloudTrafficSettlementRetention::RETENTION_SECONDS;
|
||||
$checkpointCutoff = $now
|
||||
- CloudTrafficCheckpointRetention::RETENTION_SECONDS;
|
||||
$capsule->connection('default')
|
||||
->table('cloud_traffic_usage_settlement_buckets')->insert([
|
||||
['state' => 'sealed', 'updated_at' => $settlementCutoff - 7200],
|
||||
['state' => 'open', 'updated_at' => $settlementCutoff - 86400],
|
||||
]);
|
||||
$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',
|
||||
]);
|
||||
$warning = CloudTrafficStorageHealthService::snapshot($now);
|
||||
$assert($warning['status'] === 'warning'
|
||||
&& $warning['metrics']['settlement_buckets_due'] === 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 行'),
|
||||
'Compaction backlog or attention state was hidden from operator health.');
|
||||
|
||||
echo "Traffic storage health summary contracts passed.\n";
|
||||
Loading…
Reference in New Issue