diff options
Diffstat (limited to 'nova/availability_zones.py')
-rw-r--r-- | nova/availability_zones.py | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/nova/availability_zones.py b/nova/availability_zones.py index 6d111c120..216c46041 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -23,7 +23,7 @@ from nova.openstack.common import memorycache # NOTE(vish): azs don't change that often, so cache them for an hour to # avoid hitting the db multiple times on every request. AZ_CACHE_SECONDS = 60 * 60 -MC = memorycache.get_client() +MC = None availability_zone_opts = [ cfg.StrOpt('internal_service_availability_zone', @@ -38,6 +38,27 @@ CONF = cfg.CONF CONF.register_opts(availability_zone_opts) +def _get_cache(): + global MC + + if MC is None: + MC = memorycache.get_client() + + return MC + + +def _reset_cache(): + """Reset the cache, mainly for testing purposes.""" + + global MC + + MC = None + + +def _make_cache_key(host): + return "azcache-%s" % host + + def set_availability_zones(context, services): # Makes sure services isn't a sqlalchemy object services = [dict(service.iteritems()) for service in services] @@ -50,6 +71,11 @@ def set_availability_zones(context, services): az = u','.join(list(metadata[service['host']])) else: az = CONF.default_availability_zone + # update the cache + cache = _get_cache() + cache_key = _make_cache_key(service['host']) + cache.delete(cache_key) + cache.set(cache_key, az, AZ_CACHE_SECONDS) service['availability_zone'] = az return services @@ -62,9 +88,10 @@ def get_host_availability_zone(context, host, conductor_api=None): metadata = db.aggregate_metadata_get_by_host( context, host, key='availability_zone') if 'availability_zone' in metadata: - return list(metadata['availability_zone'])[0] + az = list(metadata['availability_zone'])[0] else: - return CONF.default_availability_zone + az = CONF.default_availability_zone + return az def get_availability_zones(context): @@ -95,10 +122,11 @@ def get_instance_availability_zone(context, instance): if not host: return None - cache_key = "azcache-%s" % host - az = MC.get(cache_key) + cache_key = _make_cache_key(host) + cache = _get_cache() + az = cache.get(cache_key) if not az: elevated = context.elevated() az = get_host_availability_zone(elevated, host) - MC.set(cache_key, az, AZ_CACHE_SECONDS) + cache.set(cache_key, az, AZ_CACHE_SECONDS) return az |