Compare commits

...

13 Commits

Author SHA1 Message Date
liuhaoran
499dee02f5
Merge 0a53be946d into 1f87676d52 2024-11-15 18:08:28 +08:00
root
0a53be946d fix test_model_manager.py import
Signed-off-by: root <root@localhost.localdomain>
2024-11-12 09:30:25 +08:00
root
de3c43c924 remove ext_celery.py
Signed-off-by: root <root@localhost.localdomain>
2024-11-11 18:26:42 +08:00
root
ec0c4a25fc fixed test_model_manager.py
Signed-off-by: root <root@localhost.localdomain>
2024-11-11 18:23:10 +08:00
liuhaoran
6327d72bc8
fix ext_redis.py 2024-11-04 09:42:39 +08:00
liuhaoran
c6d4e50e3f
fix test_model_manager.py 2024-11-04 09:17:58 +08:00
liuhaoran
aaee1824d9
Merge branch 'main' into tag_redis 2024-10-29 17:42:59 +08:00
root
10445688bd Merge branch 'tag_redis' of https://bgithub.xyz/liuhaoran1212/dify into tag_redis 2024-10-29 17:25:44 +08:00
root
ac817303ce Fix3 the issues with langgenius#9538
Signed-off-by: root <root@localhost.localdomain>
2024-10-29 17:21:08 +08:00
liuhaoran
ec5229f79c
Update api/configs/middleware/cache/redis_config.py
Okay

Co-authored-by: Bowen Liang <bowenliang@apache.org>
2024-10-29 17:18:56 +08:00
root
d66ae4ae12 Fix2 the issues with #9538
Signed-off-by: root <root@localhost.localdomain>
2024-10-28 08:54:23 +08:00
root
198f2be1a5 Fix the issues with #9538
Signed-off-by: root <root@localhost.localdomain>
2024-10-24 20:13:02 +08:00
root
808862a55f ext_redis.py support redis cluster
Signed-off-by: root <root@localhost.localdomain>
2024-10-24 15:29:46 +08:00
4 changed files with 41 additions and 10 deletions

View File

@ -68,3 +68,18 @@ class RedisConfig(BaseSettings):
description="Socket timeout in seconds for Redis Sentinel connections",
default=0.1,
)
REDIS_USE_CLUSTERS: bool = Field(
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,
)

View File

@ -1,11 +1,12 @@
import redis
from redis.cluster import ClusterNode, RedisCluster
from redis.connection import Connection, SSLConnection
from redis.sentinel import Sentinel
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
@ -71,6 +72,12 @@ 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:
nodes = [
ClusterNode(host=node.split(":")[0], port=int(node.split.split(":")[1]))
for node in dify_config.REDIS_CLUSTERS.split(",")
]
redis_client.initialize(RedisCluster(startup_nodes=nodes, password=dify_config.REDIS_CLUSTERS_PASSWORD))
else:
redis_params.update(
{

View File

@ -1,10 +1,12 @@
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
import pytest
import redis
from core.entities.provider_entities import ModelLoadBalancingConfiguration
from core.model_manager import LBModelManager
from core.model_runtime.entities.model_entities import ModelType
from extensions.ext_redis import redis_client
@pytest.fixture
@ -38,6 +40,9 @@ def lb_model_manager():
def test_lb_model_manager_fetch_next(mocker, lb_model_manager):
# initialize redis client
redis_client.initialize(redis.Redis())
assert len(lb_model_manager._load_balancing_configs) == 3
config1 = lb_model_manager._load_balancing_configs[0]
@ -55,12 +60,13 @@ def test_lb_model_manager_fetch_next(mocker, lb_model_manager):
start_index += 1
return start_index
mocker.patch("redis.Redis.incr", side_effect=incr)
mocker.patch("redis.Redis.set", return_value=None)
mocker.patch("redis.Redis.expire", return_value=None)
with (
patch.object(redis_client, "incr", side_effect=incr),
patch.object(redis_client, "set", return_value=None),
patch.object(redis_client, "expire", return_value=None),
):
config = lb_model_manager.fetch_next()
assert config == config2
config = lb_model_manager.fetch_next()
assert config == config2
config = lb_model_manager.fetch_next()
assert config == config3
config = lb_model_manager.fetch_next()
assert config == config3

View File

@ -55,6 +55,9 @@ x-shared-env: &shared-api-worker-env
REDIS_SENTINEL_USERNAME: ${REDIS_SENTINEL_USERNAME:-}
REDIS_SENTINEL_PASSWORD: ${REDIS_SENTINEL_PASSWORD:-}
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:-}
ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES:-60}
CELERY_BROKER_URL: ${CELERY_BROKER_URL:-redis://:difyai123456@redis:6379/1}
BROKER_USE_SSL: ${BROKER_USE_SSL:-false}