fix: guard inactive snapshot reconciliation stats

dev
admin 1 month ago
parent a578502798
commit be5efa7033

@ -2090,6 +2090,75 @@ class VMServiceTests(unittest.TestCase):
"already_absent": False,
})
def test_live_snapshot_release_evidence_skips_stopped_vm(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())
service.create_live_snapshot(
created["uuid"], "f" * 32, consistency="crash")
service.action(created["uuid"], "shutdown")
domain = self.backend.domains[0]
calls = []
def stopped_job_stats(flags):
calls.append(flags)
raise FakeLibvirtError(
"Requested operation is not valid: domain is not running")
domain.jobStats = stopped_job_stats
service.release_live_snapshot_completed_evidence(created["uuid"])
self.assertEqual(calls, [])
self.assertEqual(domain.backup_terminal, self.backend.job_completed)
def test_live_snapshot_reconcile_stopped_vm_uses_durable_marker(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())
snapshot = service.create_live_snapshot(
created["uuid"], "0" * 32, consistency="crash")
service.action(created["uuid"], "shutdown")
domain = self.backend.domains[0]
calls = []
def stopped_job_stats(flags):
calls.append(flags)
raise FakeLibvirtError(
"Requested operation is not valid: domain is not running")
domain.jobStats = stopped_job_stats
result = service.reconcile_live_snapshot_create(
created["uuid"], "0" * 32, snapshot["volume_name"],
verified_capacity=snapshot["size_bytes"],
verified_identity=snapshot["volume_identity"],
)
self.assertEqual(result["outcome"], "succeeded")
self.assertEqual(result["power_state"], "stopped")
self.assertEqual(calls, [])
def test_live_snapshot_reconcile_stopped_vm_without_marker_stays_uncertain(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())
snapshot = service.create_live_snapshot(
created["uuid"], "1" * 32, consistency="crash")
service.action(created["uuid"], "shutdown")
domain = self.backend.domains[0]
calls = []
def stopped_job_stats(flags):
calls.append(flags)
raise FakeLibvirtError(
"Requested operation is not valid: domain is not running")
domain.jobStats = stopped_job_stats
with self.assertRaises(self.service_module.VMSnapshotUncertainError):
service.reconcile_live_snapshot_create(
created["uuid"], "1" * 32, snapshot["volume_name"])
self.assertEqual(calls, [])
def test_live_snapshot_allocation_failure_never_starts_backup(self):
service = self.cloud_service()
created = self.create_cloud_vm(service, self.cloud_spec())

@ -4649,6 +4649,36 @@ class VMService:
"宿主机返回的在线快照任务类型无效,必须人工核查")
return job_type, result
@classmethod
def _snapshot_job_type_if_active(
cls, domain: Any, flags: int,
) -> tuple[int, dict[str, Any]] | None:
"""Query backup stats only while libvirt reports an active domain.
``virDomainGetJobStats`` rejects an inactive domain. A VM can also
shut down between the activity check and the query, so re-check the
state when libvirt reports that race and let callers decide whether
the missing runtime evidence is safe to defer.
"""
try:
if not domain.isActive():
return None
except libvirt.libvirtError as exc:
raise VMSnapshotUncertainError(
"无法确认虚拟机运行状态,不能安全查询在线备份任务"
) from exc
try:
return cls._snapshot_job_type(domain, flags)
except libvirt.libvirtError as exc:
try:
if not domain.isActive():
return None
except libvirt.libvirtError:
pass
raise VMSnapshotUncertainError(
"宿主机无法查询在线备份任务状态,必须人工核查"
) from exc
@classmethod
def _assert_no_active_snapshot_backup(cls, domain: Any) -> None:
"""Keep expiry target states from interrupting an active disk copy."""
@ -5194,8 +5224,14 @@ class VMService:
return
with self._connect() as conn:
domain = self._domain(conn, vm_uuid)
job_type, stats = self._snapshot_job_type(
retained = self._snapshot_job_type_if_active(
domain, completed_flag | keep_completed_flag)
# A completed result is only cleanup evidence. If the VM has
# already been powered off, leave that evidence for a later
# active-domain pass instead of asking libvirt for runtime stats.
if retained is None:
return
job_type, stats = retained
none = getattr(libvirt, "VIR_DOMAIN_JOB_NONE", 0)
if job_type == none:
return
@ -5203,8 +5239,11 @@ class VMService:
libvirt, "VIR_DOMAIN_JOB_OPERATION_BACKUP", None):
raise VMSnapshotUncertainError(
"拒绝清除非备份 libvirt 作业的最终证据")
consumed_type, consumed_stats = self._snapshot_job_type(
consumed = self._snapshot_job_type_if_active(
domain, completed_flag)
if consumed is None:
return
consumed_type, consumed_stats = consumed
if (consumed_type != job_type
or consumed_stats.get("operation") != getattr(
libvirt, "VIR_DOMAIN_JOB_OPERATION_BACKUP", None)):
@ -5253,11 +5292,25 @@ class VMService:
domain = self._domain(conn, vm_uuid)
_root, _managed, _disk, _pool, source = (
self._snapshot_disk_context(conn, domain))
none = getattr(libvirt, "VIR_DOMAIN_JOB_NONE", 0)
current, _stats = self._snapshot_job_type(domain, 0)
if current != none:
durable_marker = (
isinstance(verified_capacity, int)
and not isinstance(verified_capacity, bool)
and verified_capacity > 0
and isinstance(verified_identity, str)
and re.fullmatch(r"[0-9a-f]{64}", verified_identity) is not None
)
marker_supplied = (
verified_capacity is not None or verified_identity is not None)
if marker_supplied and not durable_marker:
raise VMSnapshotUncertainError(
"在线快照备份任务仍存在,暂不能完成自动核对")
"在线快照的持久化验证标记不完整,不能自动核对")
none = getattr(libvirt, "VIR_DOMAIN_JOB_NONE", 0)
current_result = self._snapshot_job_type_if_active(domain, 0)
if current_result is not None:
current, _stats = current_result
if current != none:
raise VMSnapshotUncertainError(
"在线快照备份任务仍存在,暂不能完成自动核对")
pool = self._active_pool(conn, self.storage_pool)
self._refresh_directory_pool(pool)
if snapshot_volume not in set(pool.listVolumes()):
@ -5278,18 +5331,6 @@ class VMService:
conn, "", pool.name(), volume.name(), volume.path())):
raise VMSnapshotUncertainError(
"在线快照最终卷未通过独立 qcow2 验证")
durable_marker = (
isinstance(verified_capacity, int)
and not isinstance(verified_capacity, bool)
and verified_capacity > 0
and isinstance(verified_identity, str)
and re.fullmatch(r"[0-9a-f]{64}", verified_identity) is not None
)
marker_supplied = (
verified_capacity is not None or verified_identity is not None)
if marker_supplied and not durable_marker:
raise VMSnapshotUncertainError(
"在线快照的持久化验证标记不完整,不能自动核对")
if durable_marker:
if capacity != verified_capacity or identity != verified_identity:
raise VMSnapshotUncertainError(
@ -5305,8 +5346,12 @@ class VMService:
or backup_operation is None):
raise VMSnapshotUncertainError(
"宿主机不能提供在线备份最终状态,无法自动核对")
terminal, terminal_stats = self._snapshot_job_type(
terminal_result = self._snapshot_job_type_if_active(
domain, completed_flag | keep_completed_flag)
if terminal_result is None:
raise VMSnapshotUncertainError(
"虚拟机已关机且没有可验证的在线备份最终状态")
terminal, terminal_stats = terminal_result
operation = terminal_stats.get("operation")
failed = getattr(libvirt, "VIR_DOMAIN_JOB_FAILED", 4)
cancelled = getattr(libvirt, "VIR_DOMAIN_JOB_CANCELLED", 5)

Loading…
Cancel
Save