clearml-server/apiserver/redis_manager.py

97 lines
2.9 KiB
Python
Raw Normal View History

2019-10-26 21:10:08 +00:00
from os import getenv
from boltons.iterutils import first
2019-10-26 21:10:08 +00:00
from redis import StrictRedis
2023-05-25 16:22:39 +00:00
from redis.cluster import RedisCluster
2021-01-05 14:28:49 +00:00
from apiserver.apierrors.errors.server_error import ConfigError, GeneralError
2021-01-05 14:44:31 +00:00
from apiserver.config_repo import config
log = config.logger(__file__)
2021-05-03 14:26:44 +00:00
OVERRIDE_HOST_ENV_KEY = (
"CLEARML_REDIS_SERVICE_HOST",
"TRAINS_REDIS_SERVICE_HOST",
"REDIS_SERVICE_HOST",
)
OVERRIDE_PORT_ENV_KEY = (
"CLEARML_REDIS_SERVICE_PORT",
"TRAINS_REDIS_SERVICE_PORT",
"REDIS_SERVICE_PORT",
)
2022-02-13 17:37:52 +00:00
OVERRIDE_PASSWORD_ENV_KEY = (
"CLEARML_REDIS_SERVICE_PASSWORD",
"TRAINS_REDIS_SERVICE_PASSWORD",
"REDIS_SERVICE_PASSWORD",
)
2019-10-26 21:10:08 +00:00
OVERRIDE_HOST = first(filter(None, map(getenv, OVERRIDE_HOST_ENV_KEY)))
2019-10-26 21:10:08 +00:00
if OVERRIDE_HOST:
log.info(f"Using override redis host {OVERRIDE_HOST}")
OVERRIDE_PORT = first(filter(None, map(getenv, OVERRIDE_PORT_ENV_KEY)))
2019-10-26 21:10:08 +00:00
if OVERRIDE_PORT:
log.info(f"Using override redis port {OVERRIDE_PORT}")
2022-02-13 17:37:52 +00:00
OVERRIDE_PASSWORD = first(filter(None, map(getenv, OVERRIDE_PASSWORD_ENV_KEY)))
class RedisManager(object):
def __init__(self, redis_config_dict):
self.aliases = {}
for alias, alias_config in redis_config_dict.items():
2019-10-26 21:10:08 +00:00
alias_config = alias_config.as_plain_ordered_dict()
alias_config["password"] = config.get(
f"secure.redis.{alias}.password", None
)
2019-10-26 21:10:08 +00:00
is_cluster = alias_config.get("cluster", False)
2019-10-26 21:10:08 +00:00
host = OVERRIDE_HOST or alias_config.get("host", None)
if host:
alias_config["host"] = host
port = OVERRIDE_PORT or alias_config.get("port", None)
if port:
alias_config["port"] = port
2022-02-13 17:37:52 +00:00
password = OVERRIDE_PASSWORD or alias_config.get("password", None)
if password:
alias_config["password"] = password
if not port or not host:
raise ConfigError(
"Redis configuration is invalid. missing port or host", alias=alias
)
if is_cluster:
2019-10-26 21:10:08 +00:00
del alias_config["cluster"]
del alias_config["db"]
self.aliases[alias] = RedisCluster(**alias_config)
else:
self.aliases[alias] = StrictRedis(**alias_config)
2021-01-05 16:10:32 +00:00
def connection(self, alias) -> StrictRedis:
obj = self.aliases.get(alias)
if not obj:
raise GeneralError(f"Invalid Redis alias {alias}")
obj.get("health")
return obj
def host(self, alias):
r = self.connection(alias)
if isinstance(r, RedisCluster):
2023-05-25 16:22:39 +00:00
connections = r.get_default_node().redis_connection.connection_pool._available_connections
else:
connections = r.connection_pool._available_connections
if not connections:
return None
return connections[0].host
redman = RedisManager(config.get("hosts.redis"))