Skip to content

Commit

Permalink
Merge "Optimize resource tracker queries for instances" into stable/g…
Browse files Browse the repository at this point in the history
…rizzly
  • Loading branch information
Jenkins authored and openstack-gerrit committed Apr 12, 2013
2 parents fd66545 + e93ea66 commit eeaed90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 11 additions & 8 deletions nova/db/sqlalchemy/api.py
Expand Up @@ -1700,12 +1700,15 @@ def instance_get_active_by_window_joined(context, begin, end=None,


@require_admin_context
def _instance_get_all_query(context, project_only=False):
return model_query(context, models.Instance, project_only=project_only).\
options(joinedload('info_cache')).\
options(joinedload('security_groups')).\
options(joinedload('metadata')).\
options(joinedload('system_metadata'))
def _instance_get_all_query(context, project_only=False, joins=None):
if joins is None:
joins = ['info_cache', 'security_groups', 'metadata',
'system_metadata']

query = model_query(context, models.Instance, project_only=project_only)
for join in joins:
query = query.options(joinedload(join))
return query


@require_admin_context
Expand All @@ -1715,8 +1718,8 @@ def instance_get_all_by_host(context, host):

@require_admin_context
def instance_get_all_by_host_and_node(context, host, node):
return _instance_get_all_query(context).filter_by(host=host).\
filter_by(node=node).all()
return _instance_get_all_query(context, joins=[]).filter_by(host=host).\
filter_by(node=node).all()


@require_admin_context
Expand Down
16 changes: 15 additions & 1 deletion nova/tests/test_db_api.py
Expand Up @@ -55,7 +55,8 @@ def setUp(self):

def create_instances_with_args(self, **kwargs):
args = {'reservation_id': 'a', 'image_ref': 1, 'host': 'host1',
'project_id': self.project_id, 'vm_state': 'fake'}
'node': 'node1', 'project_id': self.project_id,
'vm_state': 'fake'}
if 'context' in kwargs:
ctxt = kwargs.pop('context')
args['project_id'] = ctxt.project_id
Expand Down Expand Up @@ -151,6 +152,19 @@ def test_instance_get_all_by_filters_deleted(self):
else:
self.assertTrue(result[1]['deleted'])

def test_instance_get_all_by_host_and_node_no_join(self):
# Test that system metadata is not joined.
sys_meta = {'foo': 'bar'}
expected = self.create_instances_with_args(system_metadata=sys_meta)

elevated = self.context.elevated()
instances = db.instance_get_all_by_host_and_node(elevated, 'host1',
'node1')
self.assertEqual(1, len(instances))
instance = instances[0]
self.assertEqual(expected['uuid'], instance['uuid'])
self.assertFalse('system_metadata' in dict(instance))

def test_migration_get_unconfirmed_by_dest_compute(self):
ctxt = context.get_admin_context()

Expand Down

0 comments on commit eeaed90

Please sign in to comment.