From 34c5c84a17dee7a821eb1f3b18eadb0598150cdf Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Tue, 17 Dec 2019 03:06:39 +0000 Subject: Bypass cache for LCM of cloud region Cache only the resource uri without query string. Bypass the cache for LCM of a cloud region,which enhance the resilience in case mismatch between AAI and cache Change-Id: I57fd7981753d5959757401cea69f6fabd1874e25 Issue-ID: MULTICLOUD-968 Signed-off-by: Bin Yang --- share/common/utils/aai_cache.py | 19 +++++++++++++------ share/common/utils/restcall.py | 14 +++++++++----- share/newton_base/registration/registration.py | 6 +++--- share/starlingx_base/registration/registration.py | 4 ++-- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/share/common/utils/aai_cache.py b/share/common/utils/aai_cache.py index 62545df6..c2e8e034 100644 --- a/share/common/utils/aai_cache.py +++ b/share/common/utils/aai_cache.py @@ -17,11 +17,14 @@ logger = logging.getLogger(__name__) # note: memcached key length should be < 250, the value < 1MB + def flush_cache_by_url(resource_url): try: - cache.delete("AAI_" + resource_url) + # this is to invalidate similar cache data blindly + resource_wo_query = resource_url.split("?", 1)[0] + cache.delete("AAI_" + resource_wo_query) except: - pass # silently drop any exception + pass # silently drop any exception def get_cache_by_url(resource_url): @@ -43,14 +46,18 @@ def set_cache_by_url(resource_url, resource_in_json): if filter_cache_by_url(resource_url): # cache the resource for 24 hours # logger.debug("Cache the resource: "+ resource_url) - cache.set("AAI_" + resource_url, json.dumps(resource_in_json), 3600 * 24) + cache.set("AAI_" + resource_url, + json.dumps(resource_in_json), 3600 * 24) except Exception as e: logger.error("get_cache_by_url exception: %s" % str(e)) - pass + def filter_cache_by_url(resource_url): + # cannot cache url with query string, e.g. depth=all + if resource_url.find(r"?") >= 0: + return False # hardcoded filter: cloud region only - if resource_url.find(r"cloud-infrastructure/cloud-regions/cloud-region") > 0: + if resource_url.find(r"cloud-infrastructure/cloud-regions/cloud-region") >= 0: return True else: - return False \ No newline at end of file + return False diff --git a/share/common/utils/restcall.py b/share/common/utils/restcall.py index 464dd65f..9e204564 100644 --- a/share/common/utils/restcall.py +++ b/share/common/utils/restcall.py @@ -139,11 +139,15 @@ def req_to_aai(resource, method, content='', appid=settings.MULTICLOUD_APP_ID, n # hook to flush cache if method.upper() in ["PUT", "POST", "PATCH", "DELETE"]: aai_cache.flush_cache_by_url(resource) - elif method.upper() in ["GET"] and not nocache: - content = aai_cache.get_cache_by_url(resource) - # logger.debug("cached resource: %s, %s" % (resource, content)) - if content: - return content + elif method.upper() in ["GET"]: + if not nocache: + content = aai_cache.get_cache_by_url(resource) + # logger.debug("cached resource: %s, %s" % (resource, content)) + if content: + return content + else: + # flush possible cached data blindly + aai_cache.flush_cache_by_url(resource) ret, resp_body, resp_status = _call_req( settings.AAI_BASE_URL, settings.AAI_USERNAME, settings.AAI_PASSWORD, rest_no_auth, diff --git a/share/newton_base/registration/registration.py b/share/newton_base/registration/registration.py index 9bcf90de..cdeebbc4 100644 --- a/share/newton_base/registration/registration.py +++ b/share/newton_base/registration/registration.py @@ -282,7 +282,7 @@ class RegistryHelper(MultiCloudAAIHelper): # get cloud-region retcode, content, status_code = \ - restcall.req_to_aai(resource_url, "GET") + restcall.req_to_aai(resource_url, "GET", nocache=True) # add resource-version cloudregiondata = {} @@ -1412,7 +1412,7 @@ class RegistryHelper(MultiCloudAAIHelper): # get cloud-region retcode, content, status_code = \ - restcall.req_to_aai(resource_url, "GET") + restcall.req_to_aai(resource_url, "GET", nocache=True) # add resource-version to url if retcode == 0 and content: @@ -1515,7 +1515,7 @@ class RegistryHelper(MultiCloudAAIHelper): # get cloud-region retcode, content, status_code = \ - restcall.req_to_aai(resource_url, "GET") + restcall.req_to_aai(resource_url, "GET", nocache=True) # add resource-version to url if retcode == 0 and content: diff --git a/share/starlingx_base/registration/registration.py b/share/starlingx_base/registration/registration.py index c2006114..565799a0 100644 --- a/share/starlingx_base/registration/registration.py +++ b/share/starlingx_base/registration/registration.py @@ -343,7 +343,7 @@ class RegistryHelper(newton_registration.RegistryHelper): # get cloud-region retcode, content, status_code = \ - restcall.req_to_aai(resource_url, "GET") + restcall.req_to_aai(resource_url, "GET", nocache=True) # add resource-version if retcode == 0 and content: @@ -372,7 +372,7 @@ class RegistryHelper(newton_registration.RegistryHelper): while True: # get cloud-region retcode2, content2, status_code2 = \ - restcall.req_to_aai(resource_url, "GET") + restcall.req_to_aai(resource_url, "GET", nocache=True) if retcode2 == 0 and content2: content2 = json.JSONDecoder().decode(content2) if content2.get("identity-url", None)\ -- cgit 1.2.3-korg