Fetch all db resources in one query

Instead of calling resource_get_by_name_and_stack once for every
resource in the stack, db_api.resource_get_all_by_stack is called
only once for all resources.

This reduces the number of sql queries during a describe_stack_resource
call to 3:
1. load the stack
2. load the template
3. load the resources

This is a big improvement over the start of this patch series, which
is 2 queries plus:
* 1 per resource in the stack
* 1 per access to a resource metadata attribute
* 1 per access to a resource data value

There is probably still potential to reduce queries from 3, but this
may well be a fix for
Partial-Bug: 1306743

Change-Id: I80be5d3de8744813d974f2e9860c148ad258f385
This commit is contained in:
Steve Baker 2014-04-24 16:25:55 +12:00
parent 029992e62f
commit 7ed7031466
1 changed files with 11 additions and 2 deletions

View File

@ -88,6 +88,7 @@ class Stack(collections.Mapping):
self._resources = None
self._dependencies = None
self._access_allowed_handlers = {}
self._db_resources = None
self.adopt_stack_data = adopt_stack_data
self.stack_user_project_id = stack_user_project_id
self.created_time = created_time
@ -119,13 +120,21 @@ class Stack(collections.Mapping):
self._resources = dict((name, resource.Resource(name, data, self))
for (name, data) in
template_resources.items())
# There is no need to continue storing the db resources
# after resource creation
self._db_resources = None
return self._resources
def db_resource_get(self, name):
if not self.id:
return None
return db_api.resource_get_by_name_and_stack(self.context,
name, self.id)
if self._db_resources is None:
try:
self._db_resources = db_api.resource_get_all_by_stack(
self.context, self.id)
except exception.NotFound:
return None
return self._db_resources.get(name)
@property
def dependencies(self):