From e710897a50f4daac6337e9db114e8a73bf025dda Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 10 Jan 2013 14:39:00 -0500 Subject: Skip domains on libvirt errors in get_vcpu_used() This commit is to avoid a race condition between deletes and creates. It is possible for a delete to remove a domain from the list returned by _conn.listDomainsID() while get_vcpu_used() is looping over the list. If this happens libVirt raises a LibVirtError and sets the instance to the ERROR state. To avoid this LibVirtError is caught and the loop continues instead of raising the exception. Change-Id: I3206142e36726936013bb27ccd4359de4f94b3a3 --- nova/virt/libvirt/driver.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index ea6e0e6a0..b2cc39e4f 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -2043,15 +2043,26 @@ class LibvirtDriver(driver.ComputeDriver): """ total = 0 - for dom_id in self.list_instance_ids(): - dom = self._conn.lookupByID(dom_id) - vcpus = dom.vcpus() - if vcpus is None: - # dom.vcpus is not implemented for lxc, but returning 0 for - # a used count is hardly useful for something measuring usage - total += 1 - else: - total += len(vcpus[1]) + dom_ids = self.list_instance_ids() + for dom_id in dom_ids: + try: + dom = self._conn.lookupByID(dom_id) + vcpus = dom.vcpus() + if vcpus is None: + # dom.vcpus is not implemented for lxc, but returning 0 for + # a used count is hardly useful for something measuring + # usage + total += 1 + else: + total += len(vcpus[1]) + except libvirt.libvirtError as err: + if err.get_error_code() == libvirt.VIR_ERR_NO_DOMAIN: + LOG.debug(_("List of domains returned by libVirt: %s") + % dom_ids) + LOG.warn(_("libVirt can't find a domain with id: %s") + % dom_id) + continue + raise # NOTE(gtt116): give change to do other task. greenthread.sleep(0) return total -- cgit