From 683fc0a8b015f96f624c75f096a53b9cb71ff92c Mon Sep 17 00:00:00 2001 From: Casey Schneider-Mizell Date: Tue, 16 Jan 2024 13:08:35 -0800 Subject: [PATCH 1/4] add table map endpoint and l2 cache check --- caveclient/endpoints.py | 1 + caveclient/l2cache.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/caveclient/endpoints.py b/caveclient/endpoints.py index 50850127..d31c85ba 100644 --- a/caveclient/endpoints.py +++ b/caveclient/endpoints.py @@ -262,6 +262,7 @@ l2cache_endpoints_v1 = { "l2cache_data": l2cache_v1 + "/table/{table_id}/attributes", "l2cache_meta": l2cache_v1 + "/attribute_metadata", + "l2cache_table_mapping": l2cache_v1 + "/table_mapping", } l2cache_api_versions = {1: l2cache_endpoints_v1} diff --git a/caveclient/l2cache.py b/caveclient/l2cache.py index 10a74703..1d511c3f 100644 --- a/caveclient/l2cache.py +++ b/caveclient/l2cache.py @@ -5,6 +5,7 @@ ) from .auth import AuthClient import json +from urllib.parse import urlparse server_key = "l2cache_server_address" @@ -135,6 +136,41 @@ def attributes(self): self._available_attributes = list(self.cache_metadata().keys()) return self._available_attributes + def table_mapping(self): + """Retrieves table mappings for l2 cache. + Parameters + ---------- + + Returns + ------- + dict + keys are pcg table names, values are dicts with fields `l2cache_id` and `cv_path`. + """ + endpoint_mapping = self.default_url_mapping + url = self._endpoints["l2cache_table_mapping"].format_map(endpoint_mapping) + response = self.session.get(url) + return handle_response(response) + + def has_cache(self, datastack_name=None): + """Checks if the l2 cache is available for the dataset + + Parameters + ---------- + datastack_name : str, optional + The name of the datastack to check, by default None (if None, uses the client's datastack) + + Returns + ------- + bool + True if the l2 cache is available, False otherwise + """ + seg_source = self.fc.info.segmentation_source(datastack_name=datastack_name) + if urlparse(seg_source).scheme != "graphene": + return False + table_name = self.fc.chunkedgraph.table_name + table_mapping = self.table_mapping() + return table_name in table_mapping + client_mapping = { 1: L2CacheClientLegacy, From 6780b363c0e792cdac0ff55cba1a8845477d9ddb Mon Sep 17 00:00:00 2001 From: Casey Schneider-Mizell Date: Tue, 16 Jan 2024 16:04:25 -0800 Subject: [PATCH 2/4] Add 404 robustness to l2cache table_mapping endpoint --- caveclient/l2cache.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/caveclient/l2cache.py b/caveclient/l2cache.py index 1d511c3f..540f251b 100644 --- a/caveclient/l2cache.py +++ b/caveclient/l2cache.py @@ -4,6 +4,8 @@ l2cache_endpoints_common, ) from .auth import AuthClient +from requests.exceptions import HTTPError +from warnings import warn import json from urllib.parse import urlparse @@ -168,7 +170,14 @@ def has_cache(self, datastack_name=None): if urlparse(seg_source).scheme != "graphene": return False table_name = self.fc.chunkedgraph.table_name - table_mapping = self.table_mapping() + try: + table_mapping = self.table_mapping() + except HTTPError as e: + if e.response.status_code == 404: + warn(f"Table '{table_name}' does not have a l2 cache table mapping. Assuming no cache.") + return {} + else: + raise e return table_name in table_mapping From 65e7e72d2f311aa524f562c3ffa635b188e3e7b8 Mon Sep 17 00:00:00 2001 From: Casey Schneider-Mizell Date: Wed, 17 Jan 2024 09:16:03 -0800 Subject: [PATCH 3/4] Correct return in 404 --- caveclient/l2cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caveclient/l2cache.py b/caveclient/l2cache.py index 540f251b..58a44e06 100644 --- a/caveclient/l2cache.py +++ b/caveclient/l2cache.py @@ -175,7 +175,7 @@ def has_cache(self, datastack_name=None): except HTTPError as e: if e.response.status_code == 404: warn(f"Table '{table_name}' does not have a l2 cache table mapping. Assuming no cache.") - return {} + return False else: raise e return table_name in table_mapping From 836dbf962da09ecba632fd31a286310a04cf57bf Mon Sep 17 00:00:00 2001 From: Casey Schneider-Mizell Date: Wed, 17 Jan 2024 11:20:09 -0800 Subject: [PATCH 4/4] Better error for old deployments --- caveclient/l2cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caveclient/l2cache.py b/caveclient/l2cache.py index 58a44e06..7f48ead5 100644 --- a/caveclient/l2cache.py +++ b/caveclient/l2cache.py @@ -174,7 +174,7 @@ def has_cache(self, datastack_name=None): table_mapping = self.table_mapping() except HTTPError as e: if e.response.status_code == 404: - warn(f"Table '{table_name}' does not have a l2 cache table mapping. Assuming no cache.") + warn(f"L2cache deployment '{self.server_address}/l2cache' does not have a l2 cache table mapping. Assuming no cache.") return False else: raise e