From be4616ee8529bac39b1f7d1220920327420f5334 Mon Sep 17 00:00:00 2001 From: catborise Date: Fri, 13 Nov 2026 16:15:25 +0300 Subject: [PATCH] fix(computes): a VM that cannot be read leaves a host's capacity unknown A host's capacity sums its VMs' vCPUs and memory; a VM busy with a job is read from its XML. A VM whose XML could not be read for any reason was skipped as if gone, so a refused read or a dropped connection made the host look as if it had given less than it had, even nothing. Only a VM gone meanwhile is skipped now; another error leaves the capacity unknown, which the pages show as not read. --- vrtManager/hostdetails.py | 7 +++++-- vrtManager/tests/test_host_reads.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/vrtManager/hostdetails.py b/vrtManager/hostdetails.py index 56107f9b..b358e574 100644 --- a/vrtManager/hostdetails.py +++ b/vrtManager/hostdetails.py @@ -3,6 +3,7 @@ import time from libvirt import ( libvirtError, VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT, + VIR_ERR_NO_DOMAIN, VIR_DOMAIN_BLOCKED, VIR_DOMAIN_PAUSED, VIR_DOMAIN_RUNNING, @@ -135,8 +136,10 @@ class wvmHostDetails(wvmConnect): if memory is None or vcpu is None: try: doc = etree.fromstring(dom.XMLDesc(0)) - except libvirtError: - continue # gone meanwhile + except libvirtError as err: + if err.get_error_code() == VIR_ERR_NO_DOMAIN: + continue # gone meanwhile + raise # unread, its share would be missing: the capacity is not known memory, vcpu = int(doc.findtext("currentMemory")), doc.find("vcpu") vcpu = int(vcpu.get("current", vcpu.text)) yield stats["state.state"], memory, vcpu diff --git a/vrtManager/tests/test_host_reads.py b/vrtManager/tests/test_host_reads.py index c4f3ce59..5f9175a6 100644 --- a/vrtManager/tests/test_host_reads.py +++ b/vrtManager/tests/test_host_reads.py @@ -190,6 +190,22 @@ class CapacityTestCase(unittest.TestCase): self.assertEqual(conn.wvm.getAllDomainStats.call_count, 1) idle.XMLDesc.assert_not_called() + def test_a_vm_that_cannot_be_read_leaves_the_capacity_unknown(self): + # a VM gone meanwhile is left out; one whose XML is refused would be missing from the sums + from libvirt import VIR_ERR_NO_DOMAIN, VIR_ERR_SYSTEM_ERROR, libvirtError + + def unread(code): + err = libvirtError("refused") + err.get_error_code = lambda: code + return MagicMock(**{"XMLDesc.side_effect": err}) + + conn = connection(wvmHostDetails) + conn.wvm.getAllDomainStats = MagicMock(return_value=[(unread(VIR_ERR_NO_DOMAIN), {"state.state": VIR_DOMAIN_RUNNING})]) + self.assertEqual(conn.get_capacity()["vms"]["running"], 0) + conn.wvm.getAllDomainStats = MagicMock(return_value=[(unread(VIR_ERR_SYSTEM_ERROR), {"state.state": VIR_DOMAIN_RUNNING})]) + with self.assertRaises(libvirtError): + conn.get_capacity() + class CpuTopologyTestCase(unittest.TestCase): """The topology comes from the capabilities' NUMA cells: getInfo makes