Fix joins in instance_get_all_by_host

Some callers of instance_get_all_by_host are passing
in columns_to_join=[], like the _sync_scheduler_instance_info
periodic task in the compute manager, to avoid unnecessary
joins with other tables.

The problem was columns_to_join wasn't being passed through
to _instance_get_all_query which builds the actual query
method, and defaults to join on info_cache and security_groups.

This fixes the problem by passing through columns_to_join and
provides tests to show it working both with and without the joins.

Change-Id: I69f2ddca8fb0935e03b0f426891d01360940a85a
Closes-Bug: #1680616
(cherry picked from commit d52bcc616f)
This commit is contained in:
Matt Riedemann 2017-04-06 17:59:34 -04:00
parent a8ec586ff7
commit e2c2b1d586
2 changed files with 25 additions and 2 deletions

View File

@ -2582,9 +2582,10 @@ def _instance_get_all_query(context, project_only=False, joins=None):
@pick_context_manager_reader_allow_async
def instance_get_all_by_host(context, host, columns_to_join=None):
query = _instance_get_all_query(context, joins=columns_to_join)
return _instances_fill_metadata(context,
_instance_get_all_query(context).filter_by(host=host).all(),
manual_joins=columns_to_join)
query.filter_by(host=host).all(),
manual_joins=columns_to_join)
def _instance_get_all_uuids_by_host(context, host):

View File

@ -1182,6 +1182,28 @@ class SqlAlchemyDbApiTestCase(DbTestCase):
result = test(ctxt)
self.assertEqual(2, len(result))
# make sure info_cache and security_groups were auto-joined
instance = result[0]
self.assertIn('info_cache', instance)
self.assertIn('security_groups', instance)
def test_instance_get_all_by_host_no_joins(self):
"""Tests that we don't join on the info_cache and security_groups
tables if columns_to_join is an empty list.
"""
self.create_instance_with_args()
@sqlalchemy_api.pick_context_manager_reader
def test(ctxt):
return sqlalchemy_api.instance_get_all_by_host(
ctxt, 'host1', columns_to_join=[])
result = test(context.get_admin_context())
self.assertEqual(1, len(result))
# make sure info_cache and security_groups were not auto-joined
instance = result[0]
self.assertNotIn('info_cache', instance)
self.assertNotIn('security_groups', instance)
def test_instance_get_all_uuids_by_host(self):
ctxt = context.get_admin_context()