From ec3d7e4cb882eff42fa8f1e5f8f52723fb909b0e Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Wed, 16 Jan 2013 16:50:47 -0800 Subject: [PATCH] libvirt: Optimize test_connection and capabilities The getCapabilities call can be very slow so it is not a good choice for testing the libvirt connection. This patch switches to getLibVersion and also caches the result of getCapabilities so it doesn't need to be requested every time. Note that this means that nova-compute will need to be restarted if the capaabilities of the host changes. This is an acceptable risk because capabilities changes should be very rare and nova-compute should be restarted if libvirt is restarted or reinstalled. This simple change lowers boot time in my devstack install from 22 seconds down to 8 seconds! Fixes bug 1100446 Change-Id: I1b5072a906b19c6130957cf255e8d35b20990828 --- nova/tests/test_libvirt.py | 4 ++-- nova/virt/libvirt/driver.py | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 0abf16801a6..95695b469c7 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -2854,11 +2854,11 @@ def test_broken_connection(self): conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False) self.mox.StubOutWithMock(conn, "_wrapped_conn") - self.mox.StubOutWithMock(conn._wrapped_conn, "getCapabilities") + self.mox.StubOutWithMock(conn._wrapped_conn, "getLibVersion") self.mox.StubOutWithMock(libvirt.libvirtError, "get_error_code") self.mox.StubOutWithMock(libvirt.libvirtError, "get_error_domain") - conn._wrapped_conn.getCapabilities().AndRaise( + conn._wrapped_conn.getLibVersion().AndRaise( libvirt.libvirtError("fake failure")) libvirt.libvirtError.get_error_code().AndReturn(error) diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 557818a9928..c4818a56fea 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -277,6 +277,7 @@ def __init__(self, virtapi, read_only=False): self._host_state = None self._initiator = None self._wrapped_conn = None + self._caps = None self.read_only = read_only self.firewall_driver = firewall.load_driver( DEFAULT_FIREWALL_DRIVER, @@ -362,7 +363,7 @@ def _get_connection(self): def _test_connection(self): try: - self._wrapped_conn.getCapabilities() + self._wrapped_conn.getLibVersion() return True except libvirt.libvirtError as e: if (e.get_error_code() == libvirt.VIR_ERR_SYSTEM_ERROR and @@ -1422,11 +1423,11 @@ def _volume_in_mapping(mount_device, block_device_info): def get_host_capabilities(self): """Returns an instance of config.LibvirtConfigCaps representing the capabilities of the host""" - xmlstr = self._conn.getCapabilities() - - caps = vconfig.LibvirtConfigCaps() - caps.parse_str(xmlstr) - return caps + if not self._caps: + xmlstr = self._conn.getCapabilities() + self._caps = vconfig.LibvirtConfigCaps() + self._caps.parse_str(xmlstr) + return self._caps def get_host_uuid(self): """Returns a UUID representing the host."""