feat(storage): report replica protection coverage
parent
62e51bd752
commit
369c47d52d
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Cloud;
|
||||
|
||||
use App\Models\CloudStorageArtifact;
|
||||
|
||||
/**
|
||||
* Read-only projection of immutable-object replica coverage.
|
||||
*
|
||||
* Coverage is derived from verified ready replicas instead of operation rows.
|
||||
* This keeps the administrator overview accurate while a copy operation is
|
||||
* retrying, reconciling or waiting for a replacement backend.
|
||||
*/
|
||||
final class CloudStorageProtectionSummaryService
|
||||
{
|
||||
/** @return array<string,int> */
|
||||
public static function summary(?int $now = null): array
|
||||
{
|
||||
$now ??= time();
|
||||
$rows = CloudStorageArtifact::query()
|
||||
->from('cloud_storage_artifacts as artifacts')
|
||||
->leftJoin('cloud_storage_replicas as replicas', static function ($join): void {
|
||||
$join->on('replicas.artifact_id', '=', 'artifacts.id')
|
||||
->where('replicas.state', '=', 'ready');
|
||||
})
|
||||
->whereNotIn('artifacts.state', [
|
||||
'retiring', 'delete_attention', 'deleted',
|
||||
])
|
||||
->groupBy(
|
||||
'artifacts.id',
|
||||
'artifacts.desired_copies',
|
||||
'artifacts.required_copies',
|
||||
'artifacts.created_at'
|
||||
)
|
||||
->get([
|
||||
'artifacts.id',
|
||||
'artifacts.desired_copies',
|
||||
'artifacts.required_copies',
|
||||
'artifacts.created_at',
|
||||
CloudStorageArtifact::query()->getQuery()->raw(
|
||||
'COUNT(replicas.id) AS ready_copies'
|
||||
),
|
||||
]);
|
||||
|
||||
$protected = 0;
|
||||
$degraded = 0;
|
||||
$backlog = 0;
|
||||
$oldestCreatedAt = null;
|
||||
foreach ($rows as $row) {
|
||||
$ready = (int) ($row->ready_copies ?? 0);
|
||||
$desired = (int) $row->desired_copies;
|
||||
$required = (int) $row->required_copies;
|
||||
if ($ready >= $desired) {
|
||||
$protected++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$backlog++;
|
||||
if ($ready >= $required) {
|
||||
$degraded++;
|
||||
}
|
||||
$createdAt = (int) $row->created_at;
|
||||
if ($oldestCreatedAt === null || $createdAt < $oldestCreatedAt) {
|
||||
$oldestCreatedAt = $createdAt;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'protected_artifact_count' => $protected,
|
||||
'degraded_artifact_count' => $degraded,
|
||||
'replication_backlog_count' => $backlog,
|
||||
'oldest_replication_age_seconds' => $oldestCreatedAt === null
|
||||
? 0
|
||||
: max(0, $now - $oldestCreatedAt),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Services\Cloud\CloudStorageProtectionSummaryService;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
$db = new Capsule();
|
||||
$db->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
||||
$db->setAsGlobal();
|
||||
$db->bootEloquent();
|
||||
$db->schema()->create('cloud_storage_artifacts', static function (Blueprint $t): void {
|
||||
$t->increments('id');
|
||||
$t->unsignedTinyInteger('desired_copies');
|
||||
$t->unsignedTinyInteger('required_copies');
|
||||
$t->string('state');
|
||||
$t->unsignedBigInteger('created_at');
|
||||
});
|
||||
$db->schema()->create('cloud_storage_replicas', static function (Blueprint $t): void {
|
||||
$t->increments('id');
|
||||
$t->unsignedBigInteger('artifact_id');
|
||||
$t->string('state');
|
||||
});
|
||||
|
||||
$now = 2_000;
|
||||
$artifacts = [
|
||||
['id' => 1, 'desired_copies' => 2, 'required_copies' => 1,
|
||||
'state' => 'protected', 'created_at' => 1_800],
|
||||
['id' => 2, 'desired_copies' => 2, 'required_copies' => 1,
|
||||
'state' => 'available', 'created_at' => 1_700],
|
||||
['id' => 3, 'desired_copies' => 2, 'required_copies' => 2,
|
||||
'state' => 'publishing', 'created_at' => 1_600],
|
||||
['id' => 4, 'desired_copies' => 2, 'required_copies' => 1,
|
||||
'state' => 'retiring', 'created_at' => 1_000],
|
||||
];
|
||||
Capsule::table('cloud_storage_artifacts')->insert($artifacts);
|
||||
Capsule::table('cloud_storage_replicas')->insert([
|
||||
['artifact_id' => 1, 'state' => 'ready'],
|
||||
['artifact_id' => 1, 'state' => 'ready'],
|
||||
['artifact_id' => 2, 'state' => 'ready'],
|
||||
['artifact_id' => 2, 'state' => 'copying'],
|
||||
['artifact_id' => 3, 'state' => 'copying'],
|
||||
['artifact_id' => 4, 'state' => 'ready'],
|
||||
]);
|
||||
|
||||
$summary = CloudStorageProtectionSummaryService::summary($now);
|
||||
$assert($summary === [
|
||||
'protected_artifact_count' => 1,
|
||||
'degraded_artifact_count' => 1,
|
||||
'replication_backlog_count' => 2,
|
||||
'oldest_replication_age_seconds' => 400,
|
||||
], 'Replica coverage summary does not match verified ready replicas.');
|
||||
|
||||
Capsule::table('cloud_storage_replicas')->where('artifact_id', 2)
|
||||
->where('state', 'copying')->update(['state' => 'ready']);
|
||||
Capsule::table('cloud_storage_replicas')->where('artifact_id', 3)
|
||||
->where('state', 'copying')->update(['state' => 'ready']);
|
||||
$recovered = CloudStorageProtectionSummaryService::summary($now);
|
||||
$assert($recovered['protected_artifact_count'] === 2
|
||||
&& $recovered['degraded_artifact_count'] === 0
|
||||
&& $recovered['replication_backlog_count'] === 1
|
||||
&& $recovered['oldest_replication_age_seconds'] === 400,
|
||||
'Summary did not converge when replicas became ready.');
|
||||
|
||||
Capsule::table('cloud_storage_artifacts')->whereIn('id', [1, 2, 3])
|
||||
->update(['state' => 'deleted']);
|
||||
$empty = CloudStorageProtectionSummaryService::summary($now);
|
||||
$assert($empty === [
|
||||
'protected_artifact_count' => 0,
|
||||
'degraded_artifact_count' => 0,
|
||||
'replication_backlog_count' => 0,
|
||||
'oldest_replication_age_seconds' => 0,
|
||||
], 'Deleted artifacts leaked into the protection overview.');
|
||||
|
||||
echo "Object-storage replica protection summary passed.\n";
|
||||
Loading…
Reference in New Issue