2019-10-26 21:10:08 +00:00
|
|
|
from os import getenv
|
2019-10-25 12:36:58 +00:00
|
|
|
|
2019-11-09 19:29:23 +00:00
|
|
|
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
|
2019-10-25 12:36:58 +00:00
|
|
|
|
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
|
2019-11-09 19:29:23 +00:00
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
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
|
|
|
|
2019-11-09 19:29:23 +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}")
|
|
|
|
|
2019-11-09 19:29:23 +00:00
|
|
|
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)))
|
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
|
|
|
|
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()
|
2022-02-13 17:48:26 +00:00
|
|
|
alias_config["password"] = config.get(
|
|
|
|
f"secure.redis.{alias}.password", None
|
|
|
|
)
|
2019-10-26 21:10:08 +00:00
|
|
|
|
2019-10-25 12:36:58 +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
|
|
|
|
|
2022-02-13 17:48:26 +00:00
|
|
|
if not port or not host:
|
2019-10-25 12:36:58 +00:00
|
|
|
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"]
|
2022-02-13 17:48:26 +00:00
|
|
|
del alias_config["db"]
|
|
|
|
self.aliases[alias] = RedisCluster(**alias_config)
|
2019-10-25 12:36:58 +00:00
|
|
|
else:
|
|
|
|
self.aliases[alias] = StrictRedis(**alias_config)
|
|
|
|
|
2021-01-05 16:10:32 +00:00
|
|
|
def connection(self, alias) -> StrictRedis:
|
2019-10-25 12:36:58 +00:00
|
|
|
obj = self.aliases.get(alias)
|
|
|
|
if not obj:
|
|
|
|
raise GeneralError(f"Invalid Redis alias {alias}")
|
2022-02-13 17:48:26 +00:00
|
|
|
|
|
|
|
obj.get("health")
|
|
|
|
return obj
|
2019-10-25 12:36:58 +00:00
|
|
|
|
|
|
|
def host(self, alias):
|
|
|
|
r = self.connection(alias)
|
2022-02-13 17:48:26 +00:00
|
|
|
if isinstance(r, RedisCluster):
|
2023-05-25 16:22:39 +00:00
|
|
|
connections = r.get_default_node().redis_connection.connection_pool._available_connections
|
2019-10-25 12:36:58 +00:00
|
|
|
else:
|
2022-02-13 17:48:26 +00:00
|
|
|
connections = r.connection_pool._available_connections
|
2019-10-25 12:36:58 +00:00
|
|
|
|
2022-02-13 17:48:26 +00:00
|
|
|
if not connections:
|
2019-10-25 12:36:58 +00:00
|
|
|
return None
|
|
|
|
|
2022-02-13 17:48:26 +00:00
|
|
|
return connections[0].host
|
|
|
|
|
2019-10-25 12:36:58 +00:00
|
|
|
|
|
|
|
redman = RedisManager(config.get("hosts.redis"))
|