Skip to content

Commit

Permalink
Remedies LP Bug #928910 - Use libvirt lookupByName() to check existence
Browse files Browse the repository at this point in the history
Make determining if an instance exists on a host
more efficient by adding an instance_exists() method to the
base virt driver that can be overridden by drivers that
have a more efficient mechanism of looking up an instance
by its ID / name. Modifies the _check_instance_already_created
method of the compute manager to use this new instance_exists() method.

Someone from Citrix should look into how to make the instance_exists()
method in the Xen and VMWare virt drivers more efficient than the
base "loop over all domains and see if the instance ID exists" method
now in the base driver class.

Change-Id: Ibf219788f9c104698057367da89300a060945778
  • Loading branch information
jaypipes committed Feb 10, 2012
1 parent d808ce1 commit 27c11c4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nova/compute/manager.py
Expand Up @@ -440,7 +440,7 @@ def _run_instance(self, context, instance_uuid,

def _check_instance_not_already_created(self, context, instance):
"""Ensure an instance with the same name is not already present."""
if instance['name'] in self.driver.list_instances():
if self.driver.instance_exists(instance['name']):
raise exception.Error(_("Instance has already been created"))

def _check_image_size(self, context, instance):
Expand Down
15 changes: 15 additions & 0 deletions nova/virt/driver.py
Expand Up @@ -114,6 +114,21 @@ def get_info(self, instance_name):
# TODO(Vek): Need to pass context in for access to auth_token
raise NotImplementedError()

def instance_exists(self, instance_id):
"""Checks existence of an instance on the host.
Returns True if an instance with the supplied ID exists on
the host, False otherwise.
:note This implementation works for all drivers, but it is
not particularly efficient. Maintainers of the virt drivers are
encouraged to override this method with something more
efficient.
:param instance_id: The ID / name of the instance to lookup
"""
return instance_id in self.list_instances()

def list_instances(self):
"""
Return the names of all the instances known to the virtualization
Expand Down
8 changes: 8 additions & 0 deletions nova/virt/libvirt/connection.py
Expand Up @@ -281,6 +281,14 @@ def _connect(uri, read_only):
else:
return libvirt.openAuth(uri, auth, 0)

def instance_exists(self, instance_id):
"""Efficient override of base instance_exists method."""
try:
_ignored = self._conn.lookupByName(instance_id)
return True
except libvirt.libvirtError:
return False

def list_instances(self):
return [self._conn.lookupByID(x).name()
for x in self._conn.listDomainsID()
Expand Down

0 comments on commit 27c11c4

Please sign in to comment.