From 9eb795364de388eb5e4e6e95bb8ec7cc2ee11204 Mon Sep 17 00:00:00 2001 From: codex Date: Fri, 6 Nov 2026 20:37:34 +0800 Subject: [PATCH] fix(storage): count only exact ready replicas --- .../CloudStorageProtectionSummaryService.php | 13 +++++- ...oudStorageProtectionSummaryServiceTest.php | 43 +++++++++++++++---- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php b/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php index 327db7f9..cc08df37 100644 --- a/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php +++ b/web/app/Services/Cloud/CloudStorageProtectionSummaryService.php @@ -21,7 +21,18 @@ final class CloudStorageProtectionSummaryService ->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'); + ->where('replicas.state', '=', 'ready') + ->whereColumn( + 'replicas.size_bytes', + '=', + 'artifacts.size_bytes' + ) + ->whereColumn( + 'replicas.sha256', + '=', + 'artifacts.sha256' + ) + ->where('replicas.verified_at', '>', 0); }) ->whereNotIn('artifacts.state', [ 'retiring', 'delete_attention', 'deleted', diff --git a/web/tests/CloudStorageProtectionSummaryServiceTest.php b/web/tests/CloudStorageProtectionSummaryServiceTest.php index ad66b4db..7f23d319 100644 --- a/web/tests/CloudStorageProtectionSummaryServiceTest.php +++ b/web/tests/CloudStorageProtectionSummaryServiceTest.php @@ -20,34 +20,55 @@ $db->schema()->create('cloud_storage_artifacts', static function (Blueprint $t): $t->increments('id'); $t->unsignedTinyInteger('desired_copies'); $t->unsignedTinyInteger('required_copies'); + $t->unsignedBigInteger('size_bytes'); + $t->string('sha256'); $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->unsignedBigInteger('size_bytes'); + $t->string('sha256'); + $t->unsignedBigInteger('verified_at'); $t->string('state'); }); $now = 2_000; $artifacts = [ ['id' => 1, 'desired_copies' => 2, 'required_copies' => 1, + 'size_bytes' => 100, 'sha256' => str_repeat('a', 64), 'state' => 'protected', 'created_at' => 1_800], ['id' => 2, 'desired_copies' => 2, 'required_copies' => 1, + 'size_bytes' => 200, 'sha256' => str_repeat('b', 64), 'state' => 'available', 'created_at' => 1_700], ['id' => 3, 'desired_copies' => 2, 'required_copies' => 2, + 'size_bytes' => 300, 'sha256' => str_repeat('c', 64), 'state' => 'publishing', 'created_at' => 1_600], ['id' => 4, 'desired_copies' => 2, 'required_copies' => 1, + 'size_bytes' => 400, 'sha256' => str_repeat('d', 64), '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'], + ['artifact_id' => 1, 'size_bytes' => 100, + 'sha256' => str_repeat('a', 64), 'verified_at' => 1, + 'state' => 'ready'], + ['artifact_id' => 1, 'size_bytes' => 100, + 'sha256' => str_repeat('a', 64), 'verified_at' => 2, + 'state' => 'ready'], + ['artifact_id' => 2, 'size_bytes' => 200, + 'sha256' => str_repeat('b', 64), 'verified_at' => 3, + 'state' => 'ready'], + ['artifact_id' => 2, 'size_bytes' => 0, 'sha256' => '', + 'verified_at' => 0, 'state' => 'copying'], + // State alone must not count a corrupted or stale identity as coverage. + ['artifact_id' => 3, 'size_bytes' => 301, + 'sha256' => str_repeat('c', 64), 'verified_at' => 4, + 'state' => 'ready'], + ['artifact_id' => 4, 'size_bytes' => 400, + 'sha256' => str_repeat('d', 64), 'verified_at' => 5, + 'state' => 'ready'], ]); $summary = CloudStorageProtectionSummaryService::summary($now); @@ -59,9 +80,15 @@ $assert($summary === [ ], 'Replica coverage summary does not match verified ready replicas.'); Capsule::table('cloud_storage_replicas')->where('artifact_id', 2) - ->where('state', 'copying')->update(['state' => 'ready']); + ->where('state', 'copying')->update([ + 'state' => 'ready', 'size_bytes' => 200, + 'sha256' => str_repeat('b', 64), 'verified_at' => 6, + ]); Capsule::table('cloud_storage_replicas')->where('artifact_id', 3) - ->where('state', 'copying')->update(['state' => 'ready']); + ->where('state', 'ready')->update([ + 'size_bytes' => 300, 'sha256' => str_repeat('c', 64), + 'verified_at' => 7, + ]); $recovered = CloudStorageProtectionSummaryService::summary($now); $assert($recovered['protected_artifact_count'] === 2 && $recovered['degraded_artifact_count'] === 0