From cde9f72198ddb43007ae27ff8c4921b6607ef3fc Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Tue, 15 Apr 2014 14:29:38 -0700 Subject: [PATCH] Use cached db object in volume_mig_status ext There is no need to rerequest the volume object from the db in the extension because we already have a cached copy in the request. This can potentially save hundreds of db queries in list. Change-Id: I46da8c7b6c2af2435d246ebd8e95d4712065f1e7 Resolves-bug: 1308255 --- .../contrib/volume_mig_status_attribute.py | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cinder/api/contrib/volume_mig_status_attribute.py b/cinder/api/contrib/volume_mig_status_attribute.py index b0a52e593..a424f70a0 100644 --- a/cinder/api/contrib/volume_mig_status_attribute.py +++ b/cinder/api/contrib/volume_mig_status_attribute.py @@ -28,23 +28,19 @@ class VolumeMigStatusAttributeController(wsgi.Controller): **kwargs) self.volume_api = volume.API() - def _add_volume_mig_status_attribute(self, context, resp_volume): - try: - db_volume = self.volume_api.get(context, resp_volume['id']) - except Exception: - return - else: - key = "%s:migstat" % Volume_mig_status_attribute.alias - resp_volume[key] = db_volume['migration_status'] - key = "%s:name_id" % Volume_mig_status_attribute.alias - resp_volume[key] = db_volume['_name_id'] + def _add_volume_mig_status_attribute(self, req, context, resp_volume): + db_volume = req.cached_resource_by_id(resp_volume['id']) + key = "%s:migstat" % Volume_mig_status_attribute.alias + resp_volume[key] = db_volume['migration_status'] + key = "%s:name_id" % Volume_mig_status_attribute.alias + resp_volume[key] = db_volume['_name_id'] @wsgi.extends def show(self, req, resp_obj, id): context = req.environ['cinder.context'] if authorize(context): resp_obj.attach(xml=VolumeMigStatusAttributeTemplate()) - self._add_volume_mig_status_attribute(context, + self._add_volume_mig_status_attribute(req, context, resp_obj.obj['volume']) @wsgi.extends @@ -53,7 +49,7 @@ class VolumeMigStatusAttributeController(wsgi.Controller): if authorize(context): resp_obj.attach(xml=VolumeListMigStatusAttributeTemplate()) for volume in list(resp_obj.obj['volumes']): - self._add_volume_mig_status_attribute(context, volume) + self._add_volume_mig_status_attribute(req, context, volume) class Volume_mig_status_attribute(extensions.ExtensionDescriptor): -- 2.45.2