A context cache for Resource objects

A context cache which memoizes the resources fetched by calls to
Resource.get_all_by_root_stack(..., cache=True)
which are recalled by subsequent calls to Resource.get_all_by_stack.

Because get_all_by_stack returns a collection instead of a single
resource, there is no way of taking advantage of the SQLAlchemy
identity map [1].

[1] http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#is-the-session-a-cache

Change-Id: Ia5aae0c86a586041020e9798566c9e0af48c180d
Partial-Bug: #1578854
This commit is contained in:
Steve Baker 2016-08-13 15:23:08 +12:00
parent 3ab0ede98c
commit bc3b84fb60
2 changed files with 29 additions and 2 deletions

View File

@ -1879,6 +1879,11 @@ class EngineService(service.Service):
# so sqlalchemy filters can't be used.
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):
for res in res_iter:
if res_type not in res.type():

View File

@ -15,6 +15,8 @@
"""Resource object."""
import collections
from oslo_config import cfg
from oslo_serialization import jsonutils
from oslo_versionedobjects import base
@ -42,6 +44,19 @@ def retry_on_conflict(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(
heat_base.HeatObject,
base.VersionedObjectDictCompat,
@ -136,6 +151,10 @@ class Resource(
@classmethod
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,
filters)
return cls._resources_to_dict(context, resources_db)
@ -165,12 +184,15 @@ class Resource(
return dict(resources)
@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(
context,
stack_id,
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
def purge_deleted(cls, context, stack_id):