Merge "A context cache for Resource objects"

This commit is contained in:
Jenkins 2016-08-24 17:12:01 +00:00 committed by Gerrit Code Review
commit d86ea51bbd
2 changed files with 29 additions and 2 deletions

View File

@ -1859,6 +1859,11 @@ class EngineService(service.Service):
# so sqlalchemy filters can't be used. # so sqlalchemy filters can't be used.
res_type = filters.pop('type', None) res_type = filters.pop('type', None)
if depth > 0:
# populate context with resources from all nested depths
resource_objects.Resource.get_all_by_root_stack(
cnxt, stack.id, filters, cache=True)
def filter_type(res_iter): def filter_type(res_iter):
for res in res_iter: for res in res_iter:
if res_type not in res.type(): if res_type not in res.type():

View File

@ -15,6 +15,8 @@
"""Resource object.""" """Resource object."""
import collections
from oslo_config import cfg from oslo_config import cfg
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_versionedobjects import base from oslo_versionedobjects import base
@ -42,6 +44,19 @@ def retry_on_conflict(func):
return wrapper(func) return wrapper(func)
class ResourceCache(object):
def __init__(self):
self.delete_all()
def delete_all(self):
self.by_stack_id_name = collections.defaultdict(dict)
def set_by_stack_id(self, resources):
for res in six.itervalues(resources):
self.by_stack_id_name[res.stack_id][res.name] = res
class Resource( class Resource(
heat_base.HeatObject, heat_base.HeatObject,
base.VersionedObjectDictCompat, base.VersionedObjectDictCompat,
@ -136,6 +151,10 @@ class Resource(
@classmethod @classmethod
def get_all_by_stack(cls, context, stack_id, filters=None): def get_all_by_stack(cls, context, stack_id, filters=None):
cache = context.cache(ResourceCache)
resources = cache.by_stack_id_name.get(stack_id)
if resources:
return dict(resources)
resources_db = db_api.resource_get_all_by_stack(context, stack_id, resources_db = db_api.resource_get_all_by_stack(context, stack_id,
filters) filters)
return cls._resources_to_dict(context, resources_db) return cls._resources_to_dict(context, resources_db)
@ -165,12 +184,15 @@ class Resource(
return dict(resources) return dict(resources)
@classmethod @classmethod
def get_all_by_root_stack(cls, context, stack_id, filters): def get_all_by_root_stack(cls, context, stack_id, filters, cache=False):
resources_db = db_api.resource_get_all_by_root_stack( resources_db = db_api.resource_get_all_by_root_stack(
context, context,
stack_id, stack_id,
filters) filters)
return cls._resources_to_dict(context, resources_db) all = cls._resources_to_dict(context, resources_db)
if cache:
context.cache(ResourceCache).set_by_stack_id(all)
return all
@classmethod @classmethod
def purge_deleted(cls, context, stack_id): def purge_deleted(cls, context, stack_id):