From 369c47d52d9baea84249baf80069e983c8ea4dbc Mon Sep 17 00:00:00 2001 From: codex Date: Fri, 6 Nov 2026 20:30:11 +0800 Subject: [PATCH] feat(storage): report replica protection coverage --- .../Admin/ObjectStorageController.php | 5 +- .../CloudStorageProtectionSummaryService.php | 77 +++++++++++++++++ .../CloudObjectStorageAdminContractTest.php | 1 + ...oudStorageProtectionSummaryServiceTest.php | 82 +++++++++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 web/app/Services/Cloud/CloudStorageProtectionSummaryService.php create mode 100644 web/tests/CloudStorageProtectionSummaryServiceTest.php diff --git a/web/app/Controllers/Admin/ObjectStorageController.php b/web/app/Controllers/Admin/ObjectStorageController.php index 1aac2cd3..433eede7 100644 --- a/web/app/Controllers/Admin/ObjectStorageController.php +++ b/web/app/Controllers/Admin/ObjectStorageController.php @@ -9,6 +9,7 @@ use App\Services\Cloud\CloudBackupService; use App\Services\Cloud\CloudError; use App\Services\Cloud\CloudInput; use App\Services\Cloud\CloudObjectStorageBackendRegistry; +use App\Services\Cloud\CloudStorageProtectionSummaryService; use App\Services\Cloud\CloudStorageReplicaAttentionService; final class ObjectStorageController extends AdminController @@ -34,7 +35,9 @@ final class ObjectStorageController extends AdminController try { $backends = CloudObjectStorageBackendRegistry::listView(); $registrySummary = CloudObjectStorageBackendRegistry::summary(); - $summary = array_replace($summary, $registrySummary, [ + $protectionSummary = CloudStorageProtectionSummaryService::summary(); + $summary = array_replace($summary, $registrySummary, + $protectionSummary, [ 'backend_count' => (int) ($registrySummary['configured_backends'] ?? 0), 'write_ready_count' => (int) ($registrySummary['writable_backends'] ?? 0), 'healthy_count' => (int) ($registrySummary['healthy_backends'] ?? 0), diff --git a/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php b/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php new file mode 100644 index 00000000..327db7f9 --- /dev/null +++ b/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php @@ -0,0 +1,77 @@ + */ + 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), + ]; + } +} diff --git a/web/tests/CloudObjectStorageAdminContractTest.php b/web/tests/CloudObjectStorageAdminContractTest.php index 4ad93119..24109013 100644 --- a/web/tests/CloudObjectStorageAdminContractTest.php +++ b/web/tests/CloudObjectStorageAdminContractTest.php @@ -47,6 +47,7 @@ $assert(str_contains($sidebar, 'href="/admin/object-storage"') $assert(str_contains($controller, 'CloudObjectStorageBackendRegistry::listView()') && str_contains($controller, 'CloudObjectStorageBackendRegistry::summary()') + && str_contains($controller, 'CloudStorageProtectionSummaryService::summary()') && str_contains($controller, 'CloudObjectStorageBackendRegistry::test(') && str_contains($controller, "'连接验证成功;尚未改变该后端的写入状态。'") && str_contains($controller, "'Cache-Control', 'private, no-store'") diff --git a/web/tests/CloudStorageProtectionSummaryServiceTest.php b/web/tests/CloudStorageProtectionSummaryServiceTest.php new file mode 100644 index 00000000..ad66b4db --- /dev/null +++ b/web/tests/CloudStorageProtectionSummaryServiceTest.php @@ -0,0 +1,82 @@ +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";