From 98ccb70ced51b9aa5217b190f78fbc06ad6b7a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Fredrik=20Ki=C3=A6r?= <31612826+anders-kiaer@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:29:59 +0200 Subject: [PATCH] Combine into one Redis instance (#415) --- .../src/backend/primary/user_session_proxy.py | 31 +++++++++++++++---- backend/src/backend/shared_middleware.py | 2 +- backend/src/config.py | 2 +- docker-compose-prod.yml | 2 +- docker-compose.yml | 10 +----- radixconfig.yml | 10 +----- 6 files changed, 30 insertions(+), 27 deletions(-) diff --git a/backend/src/backend/primary/user_session_proxy.py b/backend/src/backend/primary/user_session_proxy.py index e2f48a843..1f62a9746 100644 --- a/backend/src/backend/primary/user_session_proxy.py +++ b/backend/src/backend/primary/user_session_proxy.py @@ -1,6 +1,6 @@ import os import asyncio -from typing import Any +from typing import Any, Optional import httpx import redis @@ -8,11 +8,24 @@ from starlette.responses import StreamingResponse from starlette.background import BackgroundTask +from src import config from src.services.utils.authenticated_user import AuthenticatedUser LOCALHOST_DEVELOPMENT = os.environ.get("UVICORN_RELOAD") == "true" +class _RedisUserJobs: + def __init__(self) -> None: + # redis.Redis does not yet have namespace support - https://github.com/redis/redis-py/issues/12 - need to prefix manually. + self._redis_client = redis.Redis.from_url(config.REDIS_URL, decode_responses=True) + + def get_job_name(self, user_id: str) -> Optional[str]: + return self._redis_client.get("user-job-name:" + user_id) + + def set_job_name(self, user_id: str, job_name: str) -> None: + self._redis_client.set("user-job-name:" + user_id, job_name) + + class RadixJobScheduler: """Utility class to help with spawning Radix jobs on demand, and provide correct URL to communicate with running Radix jobs""" @@ -20,12 +33,18 @@ class RadixJobScheduler: def __init__(self, name: str, port: int) -> None: self._name = name self._port = port - self._existing_job_names = redis.Redis(host="redis-user-session-state", port=6380, decode_responses=True) + self._redis_user_jobs = _RedisUserJobs() + + def _get_job_name(self, user_id: str) -> Optional[str]: + return self._redis_user_jobs.get_job_name(user_id) + + def _set_job_name(self, user_id: str, job_name: str) -> None: + self._redis_user_jobs.set_job_name(user_id, job_name) async def _active_running_job(self, user_id: str) -> bool: """Returns true if there already is a running job for logged in user.""" - existing_job_name = self._existing_job_names.get(user_id) + existing_job_name = self._get_job_name(user_id) if not existing_job_name: return False if LOCALHOST_DEVELOPMENT: @@ -53,7 +72,7 @@ async def _create_new_job(self, user_id: str) -> None: same name.""" if LOCALHOST_DEVELOPMENT: - self._existing_job_names.set(user_id, self._name) + self._set_job_name(user_id, self._name) else: print(f"Requesting new user session container for user {user_id}.") async with httpx.AsyncClient() as client: @@ -74,7 +93,7 @@ async def _create_new_job(self, user_id: str) -> None: } }, ) - self._existing_job_names.set(user_id, res.json()["name"]) + self._set_job_name(user_id, res.json()["name"]) while not await self._active_running_job(user_id): # It takes a couple of seconds before Radix job uvicorn process has @@ -87,7 +106,7 @@ async def get_base_url(self, user_id: str) -> str: if not await self._active_running_job(user_id): await self._create_new_job(user_id) - job_name = self._existing_job_names.get(user_id) + job_name = self._get_job_name(user_id) return f"http://{job_name}:{self._port}" diff --git a/backend/src/backend/shared_middleware.py b/backend/src/backend/shared_middleware.py index 1877c2c85..573334a08 100644 --- a/backend/src/backend/shared_middleware.py +++ b/backend/src/backend/shared_middleware.py @@ -17,5 +17,5 @@ def add_shared_middlewares(app: FastAPI) -> None: paths_redirected_to_login=paths_redirected_to_login, ) - session_store = RedisStore(config.REDIS_URL) + session_store = RedisStore(config.REDIS_URL, prefix="user-auth:") app.add_middleware(SessionMiddleware, store=session_store) diff --git a/backend/src/config.py b/backend/src/config.py index 4efefc787..ccc43d3e6 100644 --- a/backend/src/config.py +++ b/backend/src/config.py @@ -25,5 +25,5 @@ print(f"{RESOURCE_SCOPES_DICT=}") -REDIS_URL = "redis://redis-login-state:6379" +REDIS_URL = "redis://redis-user-session:6379" SESSION_STORAGE = "redis" diff --git a/docker-compose-prod.yml b/docker-compose-prod.yml index f4a8a854a..e6bbcfba6 100644 --- a/docker-compose-prod.yml +++ b/docker-compose-prod.yml @@ -43,7 +43,7 @@ services: volumes: - ./backend/src:/home/appuser/backend/src - redis: + redis-user-session: image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e" expose: - "6379" diff --git a/docker-compose.yml b/docker-compose.yml index 20f8c8871..2520900e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -52,15 +52,7 @@ services: volumes: - ./backend/src:/home/appuser/backend/src - redis-user-session-state: - image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e" - expose: - - "6380" - environment: - - ALLOW_EMPTY_PASSWORD=yes - - REDIS_PORT_NUMBER=6380 - - redis-login-state: + redis-user-session: image: "bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e" expose: - "6379" diff --git a/radixconfig.yml b/radixconfig.yml index 053ba08a3..f6152510a 100644 --- a/radixconfig.yml +++ b/radixconfig.yml @@ -35,21 +35,13 @@ spec: variables: UVICORN_PORT: 5000 UVICORN_ENTRYPOINT: src.backend.primary.main:app - - name: redis-login-state + - name: redis-user-session image: bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e ports: - name: http port: 6379 variables: ALLOW_EMPTY_PASSWORD: yes - - name: redis-user-session-state - image: bitnami/redis:6.2.10@sha256:bd42fcdab5959ce2b21b6ea8410d4b3ee87ecb2e320260326ec731ecfcffbd0e - ports: - - name: http - port: 6380 - variables: - ALLOW_EMPTY_PASSWORD: yes - REDIS_PORT_NUMBER: 6380 jobs: - name: backend-user-session