Skip to content

Commit 32554e8

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Avoid unnecessary joins in InstanceGroup.get_hosts" into stable/stein
2 parents b3a26d6 + 74e66fe commit 32554e8

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

nova/objects/instance_group.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,15 @@ def get_hosts(self, exclude=None):
497497
if exclude:
498498
filter_uuids = set(filter_uuids) - set(exclude)
499499
filters = {'uuid': filter_uuids, 'deleted': False}
500+
# Pass expected_attrs=[] to avoid unnecessary joins.
501+
# TODO(mriedem): This is pretty inefficient since all we care about
502+
# are the hosts. We could optimize this with a single-purpose SQL query
503+
# like:
504+
# SELECT host FROM instances WHERE deleted=0 AND host IS NOT NULL
505+
# AND uuid IN ($filter_uuids) GROUP BY host;
500506
instances = objects.InstanceList.get_by_filters(self._context,
501-
filters=filters)
507+
filters=filters,
508+
expected_attrs=[])
502509
return list(set([instance.host for instance in instances
503510
if instance.host]))
504511

nova/tests/unit/objects/test_instance_group.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ def test_get_hosts(self, mock_get_db, mock_il_get):
280280
'deleted': False
281281
}
282282
mock_il_get.assert_called_once_with(self.context,
283-
filters=expected_filters)
283+
filters=expected_filters,
284+
expected_attrs=[])
284285
self.assertEqual(2, len(hosts))
285286
self.assertIn('host1', hosts)
286287
self.assertIn('host2', hosts)
@@ -293,7 +294,8 @@ def test_get_hosts(self, mock_get_db, mock_il_get):
293294
'deleted': False
294295
}
295296
mock_il_get.assert_called_once_with(self.context,
296-
filters=expected_filters)
297+
filters=expected_filters,
298+
expected_attrs=[])
297299

298300
def test_obj_make_compatible(self):
299301
obj = objects.InstanceGroup(self.context, **_INST_GROUP_OBJ_VALS)

0 commit comments

Comments
 (0)