Skip to content

Commit

Permalink
libvirt: Optimize test_connection and capabilities
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vishvananda committed Jan 17, 2013
1 parent 65d7543 commit ec3d7e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions nova/tests/test_libvirt.py
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions nova/virt/libvirt/driver.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit ec3d7e4

Please sign in to comment.