Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext_redis.py support redis clusters --- Fixes #9538 #9789

Merged
merged 13 commits into from
Nov 20, 2024
Merged
15 changes: 15 additions & 0 deletions api/configs/middleware/cache/redis_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@ class RedisConfig(BaseSettings):
description="Socket timeout in seconds for Redis Sentinel connections",
default=0.1,
)

REDIS_USE_CLUSTERS: Optional[bool] = Field(
liuhaoran1212 marked this conversation as resolved.
Show resolved Hide resolved
description="Enable Redis Clusters mode for high availability",
default=False,
)

REDIS_CLUSTERS: Optional[str] = Field(
description="Comma-separated list of Redis Clusters nodes (host:port)",
default=None,
)

REDIS_CLUSTERS_PASSWORD: Optional[str] = Field(
description="Password for Redis Clusters authentication (if required)",
default=None,
)
10 changes: 9 additions & 1 deletion api/extensions/ext_redis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import redis
from redis.connection import Connection, SSLConnection
from redis.sentinel import Sentinel
from redis.cluster import RedisCluster, ClusterNode

from configs import dify_config


class RedisClientWrapper(redis.Redis):
class RedisClientWrapper():
"""
A wrapper class for the Redis client that addresses the issue where the global
`redis_client` variable cannot be updated when a new Redis instance is returned
Expand Down Expand Up @@ -71,6 +72,13 @@ def init_app(app):
)
master = sentinel.master_for(dify_config.REDIS_SENTINEL_SERVICE_NAME, **redis_params)
redis_client.initialize(master)
elif dify_config.REDIS_USE_CLUSTERS:
startup_nodes = [
{"host": node.split(":")[0], "port": int(node.split(":")[1])}
for node in dify_config.REDIS_CLUSTERS.split(",")
]
nodes = [ClusterNode(host=node["host"], port=node["port"]) for node in startup_nodes]
liuhaoran1212 marked this conversation as resolved.
Show resolved Hide resolved
redis_client.initialize(RedisCluster(startup_nodes=nodes, password=dify_config.REDIS_CLUSTERS_PASSWORD))
else:
redis_params.update(
{
Expand Down
6 changes: 6 additions & 0 deletions api/tests/unit_tests/core/test_model_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from unittest.mock import MagicMock

import pytest
import redis
from api.extensions.ext_redis import RedisClientWrapper

from core.entities.provider_entities import ModelLoadBalancingConfiguration
from core.model_manager import LBModelManager
Expand Down Expand Up @@ -55,6 +57,10 @@ def incr(key):
start_index += 1
return start_index

fake_redis_client = redis.Redis(host="localhost", port=6379, db=0)
liuhaoran1212 marked this conversation as resolved.
Show resolved Hide resolved
redis_client = RedisClientWrapper()
redis_client.initialize(fake_redis_client)

mocker.patch("redis.Redis.incr", side_effect=incr)
mocker.patch("redis.Redis.set", return_value=None)
mocker.patch("redis.Redis.expire", return_value=None)
Expand Down
3 changes: 3 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ x-shared-env: &shared-api-worker-env
REDIS_SENTINEL_PASSWORD: ${REDIS_SENTINEL_PASSWORD:-}
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-60}
REDIS_SENTINEL_SOCKET_TIMEOUT: ${REDIS_SENTINEL_SOCKET_TIMEOUT:-0.1}
REDIS_CLUSTERS: ${REDIS_CLUSTERS:-}
REDIS_USE_CLUSTERS: ${REDIS_USE_CLUSTERS:-false}
REDIS_CLUSTERS_PASSWORD: ${REDIS_CLUSTERS_PASSWORD:-}
CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://:difyai123456@redis:6379/1}
BROKER_USE_SSL: ${BROKER_USE_SSL:-false}
CELERY_USE_SENTINEL: ${CELERY_USE_SENTINEL:-false}
Expand Down