1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00

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.
This commit is contained in:
catborise 2026-11-13 16:15:25 +03:00
parent 81d183f802
commit be4616ee85
2 changed files with 21 additions and 2 deletions

View file

@ -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

View file

@ -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