From 0e00865d94374af296890345d71c6229927af0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20P=C4=99czek?= Date: Wed, 27 Dec 2023 14:32:29 +0100 Subject: [PATCH 1/4] Emable SSL on Redis connection based on env config (to enable AWS lambda connectivity) --- development/stream_interface/inference_pipeline_demo.py | 2 +- docker/dockerfiles/Dockerfile.onnx.lambda | 2 ++ docker/dockerfiles/Dockerfile.onnx.lambda.slim | 2 ++ inference/core/cache/__init__.py | 4 ++-- inference/core/cache/redis.py | 4 ++-- inference/core/env.py | 1 + 6 files changed, 10 insertions(+), 5 deletions(-) diff --git a/development/stream_interface/inference_pipeline_demo.py b/development/stream_interface/inference_pipeline_demo.py index aca78c5a5..a592a513b 100644 --- a/development/stream_interface/inference_pipeline_demo.py +++ b/development/stream_interface/inference_pipeline_demo.py @@ -149,7 +149,7 @@ def command_thread(pipeline: InferencePipeline, watchdog: PipelineWatchDog) -> N help=f"Flag to decide if output to be streamed or displayed on screen", required=False, type=str, - default="screen", + default="display", ) args = parser.parse_args() main( diff --git a/docker/dockerfiles/Dockerfile.onnx.lambda b/docker/dockerfiles/Dockerfile.onnx.lambda index a8ba71cf6..887ebd578 100644 --- a/docker/dockerfiles/Dockerfile.onnx.lambda +++ b/docker/dockerfiles/Dockerfile.onnx.lambda @@ -49,6 +49,8 @@ ENV ALLOW_NUMPY_INPUT=False ENV INFERENCE_SERVER_ID=HostedInferenceLambda ENV DISABLE_VERSION_CHECK=true ENV DOCTR_MULTIPROCESSING_DISABLE=TRUE +ENV REDIS_SSL=true + WORKDIR ${LAMBDA_TASK_ROOT} CMD [ "lambda.handler" ] \ No newline at end of file diff --git a/docker/dockerfiles/Dockerfile.onnx.lambda.slim b/docker/dockerfiles/Dockerfile.onnx.lambda.slim index c0966f5b8..0932237f9 100644 --- a/docker/dockerfiles/Dockerfile.onnx.lambda.slim +++ b/docker/dockerfiles/Dockerfile.onnx.lambda.slim @@ -47,6 +47,8 @@ ENV LAMBDA=True ENV ALLOW_NUMPY_INPUT=False ENV INFERENCE_SERVER_ID=HostedInferenceLambda ENV DISABLE_VERSION_CHECK=true +ENV REDIS_SSL=true + WORKDIR ${LAMBDA_TASK_ROOT} CMD [ "lambda.handler" ] \ No newline at end of file diff --git a/inference/core/cache/__init__.py b/inference/core/cache/__init__.py index 7c958884a..5651afc7d 100644 --- a/inference/core/cache/__init__.py +++ b/inference/core/cache/__init__.py @@ -1,8 +1,8 @@ from inference.core.cache.memory import MemoryCache from inference.core.cache.redis import RedisCache -from inference.core.env import REDIS_HOST, REDIS_PORT +from inference.core.env import REDIS_HOST, REDIS_PORT, REDIS_SSL if REDIS_HOST is not None: - cache = RedisCache(host=REDIS_HOST, port=REDIS_PORT) + cache = RedisCache(host=REDIS_HOST, port=REDIS_PORT, ssl=REDIS_SSL) else: cache = MemoryCache() diff --git a/inference/core/cache/redis.py b/inference/core/cache/redis.py index feb6a86df..716b2be43 100644 --- a/inference/core/cache/redis.py +++ b/inference/core/cache/redis.py @@ -25,11 +25,11 @@ class RedisCache(BaseCache): _expire_thread (threading.Thread): A thread that runs the _expire method. """ - def __init__(self, host: str = "localhost", port: int = 6379, db: int = 0) -> None: + def __init__(self, host: str = "localhost", port: int = 6379, db: int = 0, ssl: bool = False) -> None: """ Initializes a new instance of the MemoryCache class. """ - self.client = redis.Redis(host=host, port=port, db=db, decode_responses=True) + self.client = redis.Redis(host=host, port=port, db=db, decode_responses=True, ssl=ssl) self.zexpires = dict() diff --git a/inference/core/env.py b/inference/core/env.py index c6a829663..9726bd970 100644 --- a/inference/core/env.py +++ b/inference/core/env.py @@ -216,6 +216,7 @@ # Redis port, default is 6379 REDIS_PORT = int(os.getenv("REDIS_PORT", 6379)) +REDIS_SSL = str2bool(os.getenv("REDIS_SSL", False)) # Required ONNX providers, default is None REQUIRED_ONNX_PROVIDERS = safe_split_value(os.getenv("REQUIRED_ONNX_PROVIDERS", None)) From 138c18f68f81846b196d4bb938d8c45c4738b7d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20P=C4=99czek?= Date: Wed, 27 Dec 2023 14:37:32 +0100 Subject: [PATCH 2/4] Make linters happy --- inference/core/cache/redis.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/inference/core/cache/redis.py b/inference/core/cache/redis.py index 716b2be43..b9fb970aa 100644 --- a/inference/core/cache/redis.py +++ b/inference/core/cache/redis.py @@ -25,11 +25,15 @@ class RedisCache(BaseCache): _expire_thread (threading.Thread): A thread that runs the _expire method. """ - def __init__(self, host: str = "localhost", port: int = 6379, db: int = 0, ssl: bool = False) -> None: + def __init__( + self, host: str = "localhost", port: int = 6379, db: int = 0, ssl: bool = False + ) -> None: """ Initializes a new instance of the MemoryCache class. """ - self.client = redis.Redis(host=host, port=port, db=db, decode_responses=True, ssl=ssl) + self.client = redis.Redis( + host=host, port=port, db=db, decode_responses=True, ssl=ssl + ) self.zexpires = dict() From 06e746fafcf8df098ba1b194da7362edee29aa83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20P=C4=99czek?= Date: Wed, 27 Dec 2023 16:18:56 +0100 Subject: [PATCH 3/4] Probe redis at start --- inference/core/cache/__init__.py | 13 +++++++++++-- inference/core/cache/redis.py | 16 ++++++++++++++-- inference/core/env.py | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/inference/core/cache/__init__.py b/inference/core/cache/__init__.py index 5651afc7d..146bcae72 100644 --- a/inference/core/cache/__init__.py +++ b/inference/core/cache/__init__.py @@ -1,8 +1,17 @@ +from inference.core import logger from inference.core.cache.memory import MemoryCache from inference.core.cache.redis import RedisCache -from inference.core.env import REDIS_HOST, REDIS_PORT, REDIS_SSL +from inference.core.env import REDIS_HOST, REDIS_PORT, REDIS_SSL, REDIS_TIMEOUT if REDIS_HOST is not None: - cache = RedisCache(host=REDIS_HOST, port=REDIS_PORT, ssl=REDIS_SSL) + try: + cache = RedisCache( + host=REDIS_HOST, port=REDIS_PORT, ssl=REDIS_SSL, timeout=REDIS_TIMEOUT + ) + except TimeoutError: + logger.error( + f"Could not connect to Redis under {REDIS_HOST}:{REDIS_PORT}. MemoryCache to be used." + ) + cache = MemoryCache() else: cache = MemoryCache() diff --git a/inference/core/cache/redis.py b/inference/core/cache/redis.py index b9fb970aa..52b6213d3 100644 --- a/inference/core/cache/redis.py +++ b/inference/core/cache/redis.py @@ -26,14 +26,26 @@ class RedisCache(BaseCache): """ def __init__( - self, host: str = "localhost", port: int = 6379, db: int = 0, ssl: bool = False + self, + host: str = "localhost", + port: int = 6379, + db: int = 0, + ssl: bool = False, + timeout: float = 2.0, ) -> None: """ Initializes a new instance of the MemoryCache class. """ self.client = redis.Redis( - host=host, port=port, db=db, decode_responses=True, ssl=ssl + host=host, + port=port, + db=db, + decode_responses=True, + ssl=ssl, + socket_timeout=timeout, + socket_connect_timeout=timeout, ) + self.client.ping() self.zexpires = dict() diff --git a/inference/core/env.py b/inference/core/env.py index 9726bd970..6a344ede9 100644 --- a/inference/core/env.py +++ b/inference/core/env.py @@ -217,6 +217,7 @@ # Redis port, default is 6379 REDIS_PORT = int(os.getenv("REDIS_PORT", 6379)) REDIS_SSL = str2bool(os.getenv("REDIS_SSL", False)) +REDIS_TIMEOUT = float(os.getenv("REDIS_TIMEOUT", 2.0)) # Required ONNX providers, default is None REQUIRED_ONNX_PROVIDERS = safe_split_value(os.getenv("REQUIRED_ONNX_PROVIDERS", None)) From 0956ffdd829c5e389d88f7e0681ba0cfc94793a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20P=C4=99czek?= Date: Wed, 27 Dec 2023 16:22:57 +0100 Subject: [PATCH 4/4] Make Redis init fallback works as expected --- inference/core/cache/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inference/core/cache/__init__.py b/inference/core/cache/__init__.py index 146bcae72..f159343e2 100644 --- a/inference/core/cache/__init__.py +++ b/inference/core/cache/__init__.py @@ -1,3 +1,5 @@ +from redis.exceptions import ConnectionError, TimeoutError + from inference.core import logger from inference.core.cache.memory import MemoryCache from inference.core.cache.redis import RedisCache @@ -8,7 +10,7 @@ cache = RedisCache( host=REDIS_HOST, port=REDIS_PORT, ssl=REDIS_SSL, timeout=REDIS_TIMEOUT ) - except TimeoutError: + except (TimeoutError, ConnectionError): logger.error( f"Could not connect to Redis under {REDIS_HOST}:{REDIS_PORT}. MemoryCache to be used." )