Expose instance_get_all_uuids_by_host() from DB API and use it

We've had a long-standing optimization point in the
InstanceList.get_uuids_by_host() method, where we pull the entire
list of instance objects only to strip and return the uuids. We
already had a query method for generating just the uuids in the
DB API, but it was not exposed as it was just a helper for another
method. This exposes it publicly and uses it in that InstanceList
method.

Change-Id: I35c05e78d59ec9b95c2a443979b8d66cd42ea043
This commit is contained in:
Dan Smith 2018-05-17 14:25:21 -07:00
parent 7dc4286b12
commit 6cfcce0559
5 changed files with 24 additions and 14 deletions

View File

@ -769,6 +769,11 @@ def instance_get_all(context, columns_to_join=None):
return IMPL.instance_get_all(context, columns_to_join=columns_to_join)
def instance_get_all_uuids_by_host(context, host):
"""Get a list of instance uuids on host."""
return IMPL.instance_get_all_uuids_by_host(context, host)
def instance_get_all_by_filters(context, filters, sort_key='created_at',
sort_dir='desc', limit=None, marker=None,
columns_to_join=None):

View File

@ -2607,6 +2607,11 @@ def _instance_get_all_uuids_by_host(context, host):
return uuids
@pick_context_manager_reader
def instance_get_all_uuids_by_host(context, host):
return _instance_get_all_uuids_by_host(context, host)
@pick_context_manager_reader
def instance_get_all_by_host_and_node(context, host, node,
columns_to_join=None):

View File

@ -1453,11 +1453,7 @@ class InstanceList(base.ObjectListBase, base.NovaObject):
@base.remotable_classmethod
def get_uuids_by_host(cls, context, host):
# NOTE(danms): We could potentially do this a little more efficiently
# but for now just pull all the instances and scrape the uuids.
db_instances = db.instance_get_all_by_host(context, host,
columns_to_join=[])
return [inst['uuid'] for inst in db_instances]
return db.instance_get_all_uuids_by_host(context, host)
@staticmethod
@db_api.pick_context_manager_reader

View File

@ -306,6 +306,15 @@ class UnsupportedDbRegexpTestCase(DbTestCase):
self.context, {'display_name': '%test%'},
marker=uuidsentinel.uuid1)
def test_instance_get_all_uuids_by_host(self, mock_get_regexp):
test1 = self.create_instance_with_args(display_name='test1')
test2 = self.create_instance_with_args(display_name='test2')
test3 = self.create_instance_with_args(display_name='test3')
uuids = [i.uuid for i in (test1, test2, test3)]
found_uuids = db.instance_get_all_uuids_by_host(self.context,
test1.host)
self.assertEqual(sorted(uuids), sorted(found_uuids))
def _assert_equals_inst_order(self, correct_order, filters,
sort_keys=None, sort_dirs=None,
limit=None, marker=None,

View File

@ -1968,19 +1968,14 @@ class _TestInstanceListObject(object):
self.assertEqual(2, len(instances))
self.assertEqual([1, 2], [x.id for x in instances])
@mock.patch('nova.db.instance_get_all_by_host')
@mock.patch('nova.db.instance_get_all_uuids_by_host')
def test_get_uuids_by_host(self, mock_get_all):
fake_instances = [
fake_instance.fake_db_instance(id=1),
fake_instance.fake_db_instance(id=2),
]
fake_instances = [uuids.inst1, uuids.inst2]
mock_get_all.return_value = fake_instances
expected_uuids = [inst['uuid'] for inst in fake_instances]
actual_uuids = objects.InstanceList.get_uuids_by_host(
self.context, 'b')
self.assertEqual(expected_uuids, actual_uuids)
mock_get_all.assert_called_once_with(self.context, 'b',
columns_to_join=[])
self.assertEqual(fake_instances, actual_uuids)
mock_get_all.assert_called_once_with(self.context, 'b')
class TestInstanceListObject(test_objects._LocalTest,