diff --git a/web/app/Services/Cloud/CloudStorageReplicaRepairService.php b/web/app/Services/Cloud/CloudStorageReplicaRepairService.php index 472e7b22..5d3c61e3 100644 --- a/web/app/Services/Cloud/CloudStorageReplicaRepairService.php +++ b/web/app/Services/Cloud/CloudStorageReplicaRepairService.php @@ -17,6 +17,14 @@ use App\Models\CloudStorageReplica; */ final class CloudStorageReplicaRepairService { + private const REPAIRABLE_PUBLICATION_STATES = [ + 'waiting_primary', + 'waiting_protection', + 'ready_to_finalize', + 'finalizing', + 'completed', + ]; + /** Reserve at most one repair so each worker cycle remains bounded. */ public function tick(int $now): bool { @@ -30,7 +38,10 @@ final class CloudStorageReplicaRepairService ->whereIn('id', static function ($query): void { $query->select('artifact_id') ->from('cloud_storage_publications') - ->where('state', 'completed'); + ->whereIn( + 'state', + self::REPAIRABLE_PUBLICATION_STATES + ); }) ->whereRaw( "(SELECT COUNT(*) FROM cloud_storage_replicas repair_coverage " @@ -96,7 +107,11 @@ final class CloudStorageReplicaRepairService ->where('artifact_id', $artifactId) ->lockForUpdate()->first(); if (!$publication instanceof CloudStoragePublication - || (string) $publication->state !== 'completed') { + || !in_array( + (string) $publication->state, + self::REPAIRABLE_PUBLICATION_STATES, + true + )) { return false; } $backends = CloudStorageBackend::query()->orderBy('id') diff --git a/web/tests/CloudStorageReplicaRepairServiceTest.php b/web/tests/CloudStorageReplicaRepairServiceTest.php index 3e62cff7..5e6bd395 100644 --- a/web/tests/CloudStorageReplicaRepairServiceTest.php +++ b/web/tests/CloudStorageReplicaRepairServiceTest.php @@ -197,7 +197,7 @@ $artifactId = Capsule::table('cloud_storage_artifacts')->insertGetId([ 'sha256' => str_repeat('e', 64), 'durability_policy' => 'mirror_required_2', 'desired_copies' => 2, 'required_copies' => 2, - 'state' => 'protected', 'last_operation_id' => str_repeat('1', 32), + 'state' => 'available', 'last_operation_id' => str_repeat('1', 32), 'verified_at' => $now, 'created_at' => $now, 'updated_at' => $now, ]); $primaryReplicaId = Capsule::table('cloud_storage_replicas')->insertGetId([ @@ -250,9 +250,9 @@ Capsule::table('cloud_storage_operations')->insert([ Capsule::table('cloud_storage_publications')->insert([ 'publication_key' => str_repeat('8', 32), 'artifact_id' => $artifactId, 'template_import_intent_id' => str_repeat('9', 32), - 'state' => 'completed', 'attempts' => 1, 'available_at' => 0, + 'state' => 'waiting_protection', 'attempts' => 1, 'available_at' => 0, 'lease_until' => 0, 'last_error_code' => '', - 'coverage_ready_at' => $now, 'finalized_at' => $now, + 'coverage_ready_at' => null, 'finalized_at' => null, 'created_at' => $now, 'updated_at' => $now, ]); Capsule::table('cloud_storage_backends')->where('id', 2)->update([ @@ -262,7 +262,8 @@ Capsule::table('cloud_storage_backends')->where('id', 2)->update([ ]); $assert($repair->tick($now + 3), - 'Replacement backend did not repair unreachable required-two coverage.'); + 'Replacement backend did not repair required-two coverage while the ' + . 'owner publication was still waiting for protection.'); $replacement = CloudStorageReplica::where('artifact_id', $artifactId) ->where('state', 'reserved')->firstOrFail(); $assert((int) $replacement->backend_id === 4