diff --git a/engine/tests/test_engine_boundary.py b/engine/tests/test_engine_boundary.py index 4d910b5f..4f348e6f 100644 --- a/engine/tests/test_engine_boundary.py +++ b/engine/tests/test_engine_boundary.py @@ -2053,7 +2053,6 @@ def test_interrupted_async_job_becomes_terminal_without_replay(boundary): @pytest.mark.parametrize("resource", [ f"vms/{VM_ID}/actions", - f"vms/{VM_ID}", f"vms/{VM_ID}/reinstall", f"vms/{VM_ID}/rescue", ]) @@ -2071,6 +2070,38 @@ def test_orphaned_mutation_becomes_terminal_without_replay(boundary, resource): assert "不会自动重放" in recovered["error"] +def test_orphaned_delete_remains_uncertain_without_replay(boundary): + client, _engine, store, _logs, services = boundary + call(client, "GET", "/internal/v1/hosts/hk/vms") + operation_id = "orphan-destructive-delete" + store.set("operation", operation_id, json.dumps({ + "operation_id": operation_id, + "host_id": "hk", + "user_id": 41, + "kind": "mutation", + "resource": f"vms/{VM_ID}", + "method": "DELETE", + "vm_uuid": VM_ID, + "status": "running", + "result": None, + "safe_to_refund": False, + "delete_ip_claim_keys": [], + "created_at": 1, + })) + + before = list(services["hk"].calls) + first = call( + client, "GET", f"/internal/v1/operations/{operation_id}", USER, + ).json() + second = call( + client, "GET", f"/internal/v1/operations/{operation_id}", USER, + ).json() + + assert first["status"] == second["status"] == "uncertain" + assert "原操作" in second["error"] + assert services["hk"].calls == before + + def test_network_change_lost_response_keeps_both_addresses_uncertain(boundary): client, engine, store, _, services = boundary call(client, "GET", "/internal/v1/hosts/hk/vms") diff --git a/engine/virsh_panel/internal.py b/engine/virsh_panel/internal.py index 1be8e1c8..d130844a 100644 --- a/engine/virsh_panel/internal.py +++ b/engine/virsh_panel/internal.py @@ -1637,9 +1637,11 @@ class EngineApplication: await asyncio.to_thread(self.store.set, "operation", record["operation_id"], json.dumps(record)) return record + if self._is_vm_delete_operation(record): + return await self._uncertain_delete_operation(record) kind = "reinstall_job" if record["resource"].endswith("/reinstall") else "rescue_job" if "/rescue" in record["resource"] else None if kind is None: - # A reset/shutdown/delete cannot be safely replayed merely because + # A reset/shutdown action cannot be safely replayed merely because # its response disappeared. Once no executor can still own the # operation, terminate it visibly instead of allowing a permanent # running/uncertain row to defeat expiry enforcement. @@ -1751,6 +1753,28 @@ class EngineApplication: json.dumps(record)) return record + async def _uncertain_delete_operation(self, record): + """Keep a lost destructive delete bound to its original operation. + + A missing executor does not prove whether the domain definition and + every panel-managed volume were removed. Converting this state to a + terminal failure would let PHP issue a second DELETE with a new + operation identifier. Only the explicit claim-cleanup path above may + finish automatically after the child endpoint durably proved that VM + deletion itself completed. + """ + if record["operation_id"] in self.active_operations: + return record + record.pop("finished_at", None) + record.update( + status="uncertain", + error="删除结果不确定,必须继续核对原操作,不能重复删除", + ) + await asyncio.to_thread( + self.store.set, "operation", record["operation_id"], + json.dumps(record)) + return record + async def _finish_orphan_mutation(self, record): if (record["operation_id"] in self.active_operations or int(time.time()) - int(record.get("created_at", 0)) < 120): diff --git a/web/app/Services/Cloud/CloudLifecycleService.php b/web/app/Services/Cloud/CloudLifecycleService.php index da47e5e1..8a6e92d1 100644 --- a/web/app/Services/Cloud/CloudLifecycleService.php +++ b/web/app/Services/Cloud/CloudLifecycleService.php @@ -320,14 +320,13 @@ final class CloudLifecycleService ->where(function ($query) { $query->whereIn('status', ['queued', 'running']) ->orWhere(function ($uncertain) { - // Reset/shutdown/delete style mutations cannot be - // replayed after a lost response and may legitimately - // remain uncertain forever. They must not permanently - // disable expiry and traffic policy. Reinstall, - // rescue and target-state policy operations can still - // converge, so their uncertainty remains a conflict. + // A reset/shutdown action does not change resource + // ownership, so expiry policy may still converge around + // an old uncertain action. Delete is different: until + // the original destructive operation is reconciled it + // must serialize every later lifecycle mutation. $uncertain->where('status', 'uncertain') - ->whereNotIn('kind', ['action', 'delete']); + ->where('kind', '<>', 'action'); }); })->exists(); } diff --git a/web/tests/CloudBillingLifecycleTest.php b/web/tests/CloudBillingLifecycleTest.php index 99c406d5..3c33d80b 100644 --- a/web/tests/CloudBillingLifecycleTest.php +++ b/web/tests/CloudBillingLifecycleTest.php @@ -766,14 +766,16 @@ namespace { && CloudJob::where('instance_id', $backlog->id)->where('kind', 'network_resume')->count() === 1, 'Current-period activation did not produce exactly one network restore.'); - // Lost-response action/delete jobs can remain uncertain forever because - // they are not safely replayable. They must not grant free networking by - // blocking expiry link-down. Replayable and target-state policy - // uncertainty must still serialize against a duplicate operation. + // A lost-response action may not block expiry policy because it changes no + // resource ownership. A lost destructive delete must block every later + // mutation until that original operation identifier is reconciled. $nonReplayable = $makeInstance('77777777-7777-4777-8777-777777777777', 'non-replayable-uncertain', $now - 1); $makeJob($nonReplayable, 'action'); - $makeJob($nonReplayable, 'delete'); + + $uncertainDelete = $makeInstance('7a7a7a7a-7a7a-4a7a-8a7a-7a7a7a7a7a7a', + 'uncertain-delete', $now - 1); + $makeJob($uncertainDelete, 'delete'); $replayable = $makeInstance('88888888-8888-4888-8888-888888888888', 'replayable-uncertain', $now - 1); @@ -794,6 +796,7 @@ namespace { $lifecycle->tick(100); $nonReplayable->refresh(); + $uncertainDelete->refresh(); $replayable->refresh(); $policyPending->refresh(); $legacyStoppedRunning->refresh(); @@ -804,7 +807,11 @@ namespace { ->where('kind', 'network_suspend')->count() === 1 && CloudJob::where('instance_id', $nonReplayable->id) ->where('kind', 'service_suspend')->count() === 0, - 'Permanent uncertain action/delete jobs blocked expiry network suspension.'); + 'Permanent uncertain action job blocked expiry network suspension.'); + $assert($uncertainDelete->service_status === 'active' + && $uncertainDelete->network_status === 'active' + && CloudJob::where('instance_id', $uncertainDelete->id)->count() === 1, + 'An uncertain destructive delete did not serialize lifecycle policy.'); $assert($replayable->service_status === 'expired_grace' && $replayable->network_status === 'blocking_expired' && CloudJob::where('instance_id', $replayable->id)