Limit InstanceList join to system_metadata in os-simple-tenant-usage

The simple-tenant-usage API is calling get_active_by_window_joined with
all of the default instance attribute fields when really only
system_metadata is needed (to get the related flavor). That adds
unnecessary load times on the DB query and data processing when
constructing the instance list, especially given this is a query over a
time range of instances, so as you add more instances to scale it's
going to slow down.

This simply limits the expected_attrs in the get_active_by_window_joined
call to system_metadata so we don't load the other unnecessary
attributes.

Closes-Bug: #1383469

Change-Id: Ic77d51cd45730ca664e68cfe0cb1671b5442d4a9
This commit is contained in:
Matt Riedemann 2014-10-20 13:59:07 -07:00
parent 203079caba
commit 466ddb115e
3 changed files with 23 additions and 8 deletions

View File

@ -27,7 +27,6 @@ from nova.api.openstack import xmlutil
from nova import exception
from nova.i18n import _
from nova import objects
from nova.objects import instance as instance_obj
authorize_show = extensions.extension_authorizer('compute',
'simple_tenant_usage:show')
@ -142,7 +141,7 @@ class SimpleTenantUsageController(object):
instances = objects.InstanceList.get_active_by_window_joined(
context, period_start, period_stop, tenant_id,
expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS)
expected_attrs=['system_metadata'])
rval = {}
flavors = {}

View File

@ -25,7 +25,6 @@ from nova.api.openstack import extensions
from nova import exception
from nova.i18n import _
from nova import objects
from nova.objects import instance as instance_obj
ALIAS = "os-simple-tenant-usage"
authorize_show = extensions.extension_authorizer('compute',
@ -108,7 +107,7 @@ class SimpleTenantUsageController(object):
instances = objects.InstanceList.get_active_by_window_joined(
context, period_start, period_stop, tenant_id,
expected_attrs=instance_obj.INSTANCE_DEFAULT_FIELDS)
expected_attrs=['system_metadata'])
rval = {}
flavors = {}

View File

@ -171,10 +171,27 @@ class SimpleTenantUsageTestV21(test.TestCase):
req.method = "GET"
req.headers["content-type"] = "application/json"
res = req.get_response(self._get_wsgi_app(self.admin_context))
self.assertEqual(res.status_int, 200)
res_dict = jsonutils.loads(res.body)
return res_dict['tenant_usages']
# Make sure that get_active_by_window_joined is only called with
# expected_attrs=['system_metadata'].
orig_get_active_by_window_joined = (
objects.InstanceList.get_active_by_window_joined)
def fake_get_active_by_window_joined(context, begin, end=None,
project_id=None, host=None,
expected_attrs=None,
use_slave=False):
self.assertEqual(['system_metadata'], expected_attrs)
return orig_get_active_by_window_joined(context, begin, end,
project_id, host,
expected_attrs, use_slave)
with mock.patch.object(objects.InstanceList,
'get_active_by_window_joined',
side_effect=fake_get_active_by_window_joined):
res = req.get_response(self._get_wsgi_app(self.admin_context))
self.assertEqual(res.status_int, 200)
res_dict = jsonutils.loads(res.body)
return res_dict['tenant_usages']
def test_verify_detailed_index(self):
usages = self._get_tenant_usages('1')