From 54aa3ea5e168b1572f2bb0b7c3c43d0d8112c8e7 Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Wed, 17 Feb 2016 12:24:55 +0530 Subject: [PATCH] Use caching for resource name/id finders Use caching to reduce the number of client calls. Change-Id: Ic22dfac0a5ea8c0f1ae7b83d37492e935f259db8 --- heat/common/cache.py | 18 ++++++++++++++++++ heat/engine/clients/os/__init__.py | 12 +++++++++--- heat/engine/clients/os/glance.py | 8 ++++++++ heat/engine/clients/os/neutron/__init__.py | 11 ++++++++++- heat/engine/clients/os/nova.py | 9 ++++++++- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/heat/common/cache.py b/heat/common/cache.py index 7e669fbcef..759549a4c1 100644 --- a/heat/common/cache.py +++ b/heat/common/cache.py @@ -73,6 +73,24 @@ def register_cache_configurations(conf): conf.register_group(extension_cache_group) conf.register_opts(extension_cache_opts, group=extension_cache_group) + find_cache_group = cfg.OptGroup('resource_finder_cache') + find_cache_opts = [ + cfg.IntOpt('expiration_time', default=3600, + help=_( + 'TTL, in seconds, for any cached item in the ' + 'dogpile.cache region used for caching of OpenStack ' + 'service finder functions.')), + cfg.BoolOpt('caching', default=True, + help=_( + 'Toggle to enable/disable caching when Orchestration ' + 'Engine looks for other OpenStack service resources ' + 'using name or id. Please note that the global ' + 'toggle for oslo.cache(enabled=True in [cache] group) ' + 'must be enabled to use this feature.')) + ] + conf.register_group(find_cache_group) + conf.register_opts(find_cache_opts, group=find_cache_group) + return conf diff --git a/heat/engine/clients/os/__init__.py b/heat/engine/clients/os/__init__.py index edfc090242..8047b236a0 100644 --- a/heat/engine/clients/os/__init__.py +++ b/heat/engine/clients/os/__init__.py @@ -16,6 +16,12 @@ from oslo_config import cfg from heat.common import cache -MEMOIZE = core.get_memoization_decorator(conf=cfg.CONF, - region=cache.get_cache_region(), - group="service_extension_cache") +MEMOIZE_EXTENSIONS = core.get_memoization_decorator( + conf=cfg.CONF, + region=cache.get_cache_region(), + group="service_extension_cache") + +MEMOIZE_FINDER = core.get_memoization_decorator( + conf=cfg.CONF, + region=cache.get_cache_region(), + group="resource_finder_cache") diff --git a/heat/engine/clients/os/glance.py b/heat/engine/clients/os/glance.py index e21bad6ec5..25ad020f39 100644 --- a/heat/engine/clients/os/glance.py +++ b/heat/engine/clients/os/glance.py @@ -16,6 +16,7 @@ from glanceclient import exc from glanceclient.openstack.common.apiclient import exceptions from heat.engine.clients import client_plugin +from heat.engine.clients import os as os_client from heat.engine import constraints CLIENT_NAME = 'glance' @@ -83,6 +84,13 @@ class GlanceClientPlugin(client_plugin.ClientPlugin): :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: """ + return self._find_image_id(self.context.tenant_id, + image_identifier) + + @os_client.MEMOIZE_FINDER + def _find_image_id(self, tenant_id, image_identifier): + # tenant id in the signature is used for the memoization key, + # that would differentiate similar resource names across tenants. return self.get_image(image_identifier).id def get_image(self, image_identifier): diff --git a/heat/engine/clients/os/neutron/__init__.py b/heat/engine/clients/os/neutron/__init__.py index 563b44aa42..c52366abeb 100644 --- a/heat/engine/clients/os/neutron/__init__.py +++ b/heat/engine/clients/os/neutron/__init__.py @@ -74,10 +74,19 @@ class NeutronClientPlugin(client_plugin.ClientPlugin): def find_resourceid_by_name_or_id(self, resource, name_or_id, cmd_resource=None): + return self._find_resource_id(self.context.tenant_id, + resource, name_or_id, + cmd_resource) + + @os_client.MEMOIZE_FINDER + def _find_resource_id(self, tenant_id, + resource, name_or_id, cmd_resource): + # tenant id in the signature is used for the memoization key, + # that would differentiate similar resource names across tenants. return neutronV20.find_resourceid_by_name_or_id( self.client(), resource, name_or_id, cmd_resource=cmd_resource) - @os_client.MEMOIZE + @os_client.MEMOIZE_EXTENSIONS def _list_extensions(self): extensions = self.client().list_extensions().get('extensions') return set(extension.get('alias') for extension in extensions) diff --git a/heat/engine/clients/os/nova.py b/heat/engine/clients/os/nova.py index 62f7c69014..fb9c7848c2 100644 --- a/heat/engine/clients/os/nova.py +++ b/heat/engine/clients/os/nova.py @@ -230,6 +230,13 @@ class NovaClientPlugin(client_plugin.ClientPlugin): :param flavor: the name of the flavor to find :returns: the id of :flavor: """ + return self._find_flavor_id(self.context.tenant_id, + flavor) + + @os_client.MEMOIZE_FINDER + def _find_flavor_id(self, tenant_id, flavor): + # tenant id in the signature is used for the memoization key, + # that would differentiate similar resource names across tenants. return self.get_flavor(flavor).id def get_flavor(self, flavor_identifier): @@ -672,7 +679,7 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers else: return False - @os_client.MEMOIZE + @os_client.MEMOIZE_EXTENSIONS def _list_extensions(self): extensions = self.client().list_extensions.show_all() return set(extension.alias for extension in extensions)