diff --git a/web/tests/CloudObjectStorageOperationsContractTest.php b/web/tests/CloudObjectStorageOperationsContractTest.php index 0679f864..8a8253ec 100644 --- a/web/tests/CloudObjectStorageOperationsContractTest.php +++ b/web/tests/CloudObjectStorageOperationsContractTest.php @@ -27,6 +27,9 @@ $method = static function (string $source, string $name, string $next = '') use $routes = $read('config/routes.php'); $registry = $read('app/Services/Cloud/CloudObjectStorageBackendRegistry.php'); $backups = $read('app/Services/Cloud/CloudBackupService.php'); +$artifactDeletion = $read( + 'app/Services/Cloud/CloudStorageArtifactDeletionService.php' +); foreach ([ 'createBackend', 'updateBackend', 'testBackend', 'activateBackend', @@ -63,9 +66,23 @@ $assert(str_contains($cleanupDebt, "'next_check_at'") && !str_contains($cleanupDebt, "'object_key'") && !str_contains($cleanupDebt, "'object_version_id'"), 'Cleanup-debt DTO exposed provider object identity.'); -$assert(str_contains($retryCleanup, 'inspectObject(') - && str_contains($retryCleanup, "'delete_uncertain'") - && !str_contains($retryCleanup, 'deleteObject('), - 'Manual cleanup retry must inspect exact evidence without replaying DELETE.'); +$assert(str_contains($retryCleanup, 'CloudStorageArtifactDeletionService') + && str_contains($retryCleanup, 'reopenAttention(') + && str_contains($retryCleanup, '->process(') + && !str_contains($retryCleanup, 'deleteObject(') + && str_contains($artifactDeletion, "=== 'reconcile'") + && str_contains($artifactDeletion, 'recordPresence(') + && str_contains($artifactDeletion, 'reopenDeleteReconciliation(') + && str_contains($artifactDeletion, + "['publish', 'copy'], true"), + 'Manual cleanup retry must resume the fenced replica-deletion ledger.'); +$reconcileGuard = strpos( + $artifactDeletion, + "if ((string) \$work['mode'] === 'reconcile')" +); +$deleteCall = strpos($artifactDeletion, '$this->s3()->deleteObject('); +$assert($reconcileGuard !== false && $deleteCall !== false + && $reconcileGuard < $deleteCall, + 'Unknown-effect reconciliation must return before issuing another DELETE.'); echo "Object-storage lifecycle and cleanup contracts passed.\n"; diff --git a/web/tests/CloudPortableBackupAdminUiContractTest.php b/web/tests/CloudPortableBackupAdminUiContractTest.php index e68d31db..5fa7d531 100644 --- a/web/tests/CloudPortableBackupAdminUiContractTest.php +++ b/web/tests/CloudPortableBackupAdminUiContractTest.php @@ -20,6 +20,9 @@ $runtime = $read('public/cloud/admin-instances.js') . "\n" . $read('public/cloud/admin-instance-backups.js'); $assets = $read('resources/views/venix/layout_assets.tpl'); $service = $read('app/Services/Cloud/CloudBackupService.php'); +$publicationPreparation = $read( + 'app/Services/Cloud/CloudBackupPublicationPreparation.php' +); $placement = $read('app/Services/Cloud/CloudPlacementService.php'); $jobService = $read('app/Services/Cloud/CloudJobService.php'); $lifecycle = $read('app/Services/Cloud/CloudLifecycleService.php'); @@ -196,11 +199,22 @@ $assert(str_contains($service, && str_contains($service, '实例缺少创建时冻结的网络兼容快照'), 'Backup listing must reject missing network compatibility metadata before submission.'); -$assert(str_contains($service, - 'private const CREATE_TRANSACTION_ATTEMPTS = 1;') - && str_contains($service, - '}, self::CREATE_TRANSACTION_ATTEMPTS);'), - 'CreateMultipartUpload must not be replayed by automatic database transaction retries.'); +$multipartDispatch = strpos( + $publicationPreparation, + '$this->s3()->beginMultipartPlan(' +); +$jobTransaction = strpos( + $publicationPreparation, + "return (new CloudInstance())->getConnection()->transaction(" +); +$assert($multipartDispatch !== false && $jobTransaction !== false + && $multipartDispatch < $jobTransaction + && !str_contains($service, 'beginMultipartPlan(') + && str_contains($publicationPreparation, + 'Provider I/O always happens after the business intent commits') + && str_contains($publicationPreparation, + 'markPrimaryPreparationUnknown('), + 'CreateMultipartUpload must run outside retried business transactions and preserve unknown effects.'); $assert(!str_contains($runtime, 'object_key') && !str_contains($runtime, 'part_url') diff --git a/web/tests/CloudPortableBackupSourceContractFinalizerTest.php b/web/tests/CloudPortableBackupSourceContractFinalizerTest.php index 68e12012..edf18cb1 100644 --- a/web/tests/CloudPortableBackupSourceContractFinalizerTest.php +++ b/web/tests/CloudPortableBackupSourceContractFinalizerTest.php @@ -25,6 +25,7 @@ $schema = $capsule->schema('default'); $schema->create('cloud_instance_backups', static function (Blueprint $table): void { $table->increments('id'); $table->string('backup_key')->unique(); + $table->integer('storage_artifact_id')->nullable(); $table->integer('source_instance_id'); $table->integer('source_host_id'); $table->integer('user_id'); @@ -41,9 +42,7 @@ $schema->create('cloud_instance_backups', static function (Blueprint $table): vo $table->text('source_portable_restore_contract_json')->default('{}'); $table->string('source_portable_restore_contract_sha256', 64)->default(''); $table->text('spec_json'); - $table->text('sealed_storage_runtime'); $table->text('sealed_credential')->nullable(); - $table->integer('storage_revision')->default(1); $table->integer('active_job_id')->nullable(); $table->string('last_operation_id')->nullable(); $table->string('error_code')->default(''); @@ -60,6 +59,93 @@ $schema->create('cloud_jobs', static function (Blueprint $table): void { $table->integer('user_id')->nullable(); $table->string('operation_id')->unique(); }); +$schema->create('cloud_storage_artifacts', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('artifact_key')->unique(); + $table->string('kind'); + $table->unsignedBigInteger('maximum_size_bytes'); + $table->unsignedBigInteger('size_bytes')->default(0); + $table->string('sha256')->default(''); + $table->string('durability_policy'); + $table->unsignedTinyInteger('desired_copies'); + $table->unsignedTinyInteger('required_copies'); + $table->string('state'); + $table->string('last_operation_id')->nullable(); + $table->unsignedBigInteger('verified_at')->default(0); + $table->unsignedBigInteger('created_at'); + $table->unsignedBigInteger('updated_at'); +}); +$schema->create('cloud_storage_replicas', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('replica_key')->unique(); + $table->unsignedBigInteger('artifact_id'); + $table->unsignedBigInteger('backend_id'); + $table->string('role'); + $table->string('object_key'); + $table->string('object_version_id')->default(''); + $table->string('object_etag')->default(''); + $table->unsignedBigInteger('size_bytes')->default(0); + $table->string('sha256')->default(''); + $table->string('state'); + $table->string('publish_operation_id')->nullable(); + $table->string('delete_operation_id')->nullable(); + $table->unsignedInteger('delete_generation')->default(0); + $table->unsignedInteger('delete_cycle_attempts')->default(0); + $table->unsignedInteger('delete_presence_confirmations')->default(0); + $table->unsignedBigInteger('delete_last_checked_at')->default(0); + $table->unsignedBigInteger('delete_next_check_at')->default(0); + $table->unsignedBigInteger('delete_attention_at')->nullable(); + $table->string('lease_token')->nullable(); + $table->unsignedBigInteger('lease_until')->default(0); + $table->string('error_code')->default(''); + $table->unsignedBigInteger('verified_at')->default(0); + $table->unsignedBigInteger('created_at'); + $table->unsignedBigInteger('updated_at'); +}); +$schema->create('cloud_storage_operations', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('operation_id')->unique(); + $table->string('request_idempotency_key')->unique(); + $table->string('request_fingerprint'); + $table->unsignedBigInteger('artifact_id'); + $table->unsignedBigInteger('replica_id')->nullable(); + $table->unsignedBigInteger('source_backend_id')->nullable(); + $table->unsignedBigInteger('target_backend_id')->nullable(); + $table->string('kind'); + $table->string('phase'); + $table->string('outcome')->nullable(); + $table->string('effect_resolution'); + $table->boolean('effect_may_have_applied')->nullable(); + $table->boolean('safe_to_resubmit')->nullable(); + $table->unsignedInteger('attempts')->default(0); + $table->string('lease_token')->nullable(); + $table->unsignedBigInteger('lease_until')->default(0); + $table->unsignedBigInteger('available_at')->default(0); + $table->unsignedBigInteger('next_check_at')->default(0); + $table->string('error_code')->default(''); + $table->text('result_json')->nullable(); + $table->unsignedBigInteger('terminal_at')->nullable(); + $table->unsignedBigInteger('created_at'); + $table->unsignedBigInteger('updated_at'); +}); +$schema->create('cloud_storage_publications', static function (Blueprint $table): void { + $table->increments('id'); + $table->string('publication_key')->unique(); + $table->unsignedBigInteger('artifact_id')->unique(); + $table->unsignedBigInteger('backup_id')->nullable()->unique(); + $table->string('template_import_intent_id')->nullable()->unique(); + $table->string('rescue_template_id')->nullable()->unique(); + $table->string('state'); + $table->unsignedInteger('attempts')->default(0); + $table->unsignedBigInteger('available_at')->default(0); + $table->string('lease_token')->nullable(); + $table->unsignedBigInteger('lease_until')->default(0); + $table->string('last_error_code')->default(''); + $table->unsignedBigInteger('coverage_ready_at')->nullable(); + $table->unsignedBigInteger('finalized_at')->nullable(); + $table->unsignedBigInteger('created_at'); + $table->unsignedBigInteger('updated_at'); +}); $payload = [ 'contract' => 1, 'architecture' => 'x86_64', 'machine' => 'q35', @@ -72,18 +158,61 @@ $now = time(); $create = static function (int $id, string $key, string $operation) use ( $capsule, $now ): CloudJob { + $storageOperation = substr(hash('sha256', 'source-contract-' . $id), 0, 32); + $objectKey = 'vm-backups/' . $key . '/system.qcow2'; $capsule->table('cloud_instance_backups')->insert([ 'id' => $id, 'backup_key' => $key, + 'storage_artifact_id' => $id, 'source_instance_id' => 100 + $id, 'source_host_id' => 3, 'user_id' => 9, 'source_uuid' => sprintf('00000000-0000-4000-8000-%012d', $id), 'name' => 'portable-' . $id, 'state' => 'creating', 'consistency' => 'crash', - 'object_key' => 'vm-backups/' . $key . '/system.qcow2', - 'spec_json' => '{}', 'sealed_storage_runtime' => 'sealed', + 'object_key' => $objectKey, + 'spec_json' => '{}', 'active_job_id' => $id, 'last_operation_id' => $operation, 'created_at' => $now, 'updated_at' => $now, ]); + $capsule->table('cloud_storage_artifacts')->insert([ + 'id' => $id, + 'artifact_key' => substr(hash('sha256', 'artifact-' . $id), 0, 32), + 'kind' => 'backup', + 'maximum_size_bytes' => 21 * 1073741824, + 'size_bytes' => 0, 'sha256' => '', + 'durability_policy' => 'backup_primary_async_replica', + 'desired_copies' => 2, 'required_copies' => 1, + 'state' => 'publishing', + 'last_operation_id' => $storageOperation, + 'verified_at' => 0, 'created_at' => $now, 'updated_at' => $now, + ]); + $capsule->table('cloud_storage_replicas')->insert([ + 'id' => $id, + 'replica_key' => substr(hash('sha256', 'replica-' . $id), 0, 32), + 'artifact_id' => $id, 'backend_id' => 1, 'role' => 'primary', + 'object_key' => $objectKey, 'state' => 'publishing', + 'publish_operation_id' => $storageOperation, + 'created_at' => $now, 'updated_at' => $now, + ]); + $capsule->table('cloud_storage_operations')->insert([ + 'id' => $id, 'operation_id' => $storageOperation, + 'request_idempotency_key' => 'source-contract-' . $id, + 'request_fingerprint' => hash('sha256', 'source-contract-' . $id), + 'artifact_id' => $id, 'replica_id' => $id, + 'target_backend_id' => 1, 'kind' => 'publish', + 'phase' => 'awaiting_receipt', 'effect_resolution' => 'unknown', + 'attempts' => 1, 'created_at' => $now, 'updated_at' => $now, + ]); + $capsule->table('cloud_storage_publications')->insert([ + 'id' => $id, + 'publication_key' => substr( + hash('sha256', 'publication-' . $id), + 0, + 32 + ), + 'artifact_id' => $id, 'backup_id' => $id, + 'state' => 'waiting_primary', + 'created_at' => $now, 'updated_at' => $now, + ]); $capsule->table('cloud_jobs')->insert([ 'id' => $id, 'kind' => 'backup_create', 'host_id' => 3, 'instance_id' => 100 + $id, 'backup_id' => $id, @@ -100,10 +229,16 @@ $result = static function (string $key) use ($payload): array { 'cleanup_pending' => false, 'source_contract' => $payload, ]; }; -$finalizerPayload = static function (string $key) use ($contract): array { +$finalizerPayload = static function (int $id, string $key) use ($contract): array { return [ 'owner_id' => 9, 'backup_key' => $key, 'maximum_size_bytes' => 21 * 1073741824, + 'storage_artifact_id' => $id, + 'storage_publication_operation_id' => substr( + hash('sha256', 'source-contract-' . $id), + 0, + 32 + ), 'source_portable_restore_contract_sha256' => $contract->sha256(), ]; }; @@ -111,7 +246,7 @@ $finalizerPayload = static function (string $key) use ($contract): array { $key = str_repeat('1', 32); CloudBackupService::finishJobLocked( $create(1, $key, str_repeat('2', 32)), - 'succeeded', $result($key), $finalizerPayload($key) + 'succeeded', $result($key), $finalizerPayload(1, $key) ); $ready = CloudInstanceBackup::findOrFail(1); $assert((string) $ready->state === 'ready' @@ -128,7 +263,7 @@ unset($missing['source_contract']); try { CloudBackupService::finishJobLocked( $create(2, $missingKey, str_repeat('4', 32)), - 'succeeded', $missing, $finalizerPayload($missingKey) + 'succeeded', $missing, $finalizerPayload(2, $missingKey) ); throw new RuntimeException('Missing source contract was accepted.'); } catch (CloudError $error) {