redis-py
redis-py copied to clipboard
redis sentinel throw "Timeout connecting to server" error
Version: redis-py version 4.3.4 redis server version 6.2.7 redis sentinel version 6.2.7
redis server Platform: Operating System: Debian GNU/Linux 10 (buster) Kernel: Linux 4.19.0-21-cloud-amd64 Architecture: x86-64
docker: Docker version 20.10.17, build 100c701
redis image: docker official image
redis client Platform: Python 3.9.13 on Windows 10 virtual environment
Description: 1 master, 1 replica, 3 sentinel all in one host. discover_master and discover_slaves can get right result, but master.set throw error "timed out"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6d04dbb25a24 sentinel_redis-sentinel "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 0.0.0.0:26381->26379/tcp, :::26381->26379/tcp sentinel-redis-sentinel-2 9280b646008b sentinel_redis-sentinel "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 0.0.0.0:26380->26379/tcp, :::26380->26379/tcp sentinel-redis-sentinel-3 1d0df59ff350 sentinel_redis-sentinel "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 0.0.0.0:26379->26379/tcp, :::26379->26379/tcp sentinel-redis-sentinel-1 886361c99913 sentinel_redis-slave "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 6379/tcp, 0.0.0.0:6381->6381/tcp, :::6381->6381/tcp sentinel-redis-slave-1 d6827153fdd6 sentinel_redis-master "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 6379/tcp, 0.0.0.0:6380->6380/tcp, :::6380->6380/tcp sentinel-redis-master-1
redis-cli -p 26379
127.0.0.1:26379> SENTINEL master mymaster
- "name"
- "mymaster"
- "ip"
- "172.25.0.2"
- "port"
- "6380"
- "runid"
- "78823b9e95971e48fc161987bc9db927db95f4a7"
- "flags"
- "master"
- "link-pending-commands"
- "0"
- "link-refcount"
- "1"
- "last-ping-sent"
- "0"
- "last-ok-ping-reply"
- "96"
- "last-ping-reply"
- "96"
- "down-after-milliseconds"
- "5000"
- "info-refresh"
- "8505"
- "role-reported"
- "master"
- "role-reported-time"
- "2267051"
- "config-epoch"
- "0"
- "num-slaves"
- "1"
- "num-other-sentinels"
- "2"
- "quorum"
- "2"
- "failover-timeout"
- "60000"
- "parallel-syncs"
- "1"
127.0.0.1:26379> SENTINEL replicas mymaster
-
- "name"
- "172.25.0.3:6381"
- "ip"
- "172.25.0.3"
- "port"
- "6381"
- "runid"
- "d1fb8b420957cd5c5172b2559d2b3c81310bed50"
- "flags"
- "slave"
- "link-pending-commands"
- "0"
- "link-refcount"
- "1"
- "last-ping-sent"
- "0"
- "last-ok-ping-reply"
- "97"
- "last-ping-reply"
- "97"
- "down-after-milliseconds"
- "5000"
- "info-refresh"
- "3327"
- "role-reported"
- "slave"
- "role-reported-time"
- "2352117"
- "master-link-down-time"
- "0"
- "master-link-status"
- "ok"
- "master-host"
- "redis-master"
- "master-port"
- "6380"
- "slave-priority"
- "100"
- "slave-repl-offset"
- "469845"
- "replica-announced"
- "1"
127.0.0.1:26379> SENTINEL sentinels mymaster
-
- "name"
- "a41ea1840b91accbb79f848bbedba9a53dfe2a9c"
- "ip"
- "172.25.0.5"
- "port"
- "26379"
- "runid"
- "a41ea1840b91accbb79f848bbedba9a53dfe2a9c"
- "flags"
- "sentinel"
- "link-pending-commands"
- "0"
- "link-refcount"
- "1"
- "last-ping-sent"
- "0"
- "last-ok-ping-reply"
- "1011"
- "last-ping-reply"
- "1011"
- "down-after-milliseconds"
- "5000"
- "last-hello-message"
- "876"
- "voted-leader"
- "?"
- "voted-leader-epoch"
- "0"
-
- "name"
- "88c6d217e0c959d7b67ff4af3b784d2a28cabb39"
- "ip"
- "172.25.0.4"
- "port"
- "26379"
- "runid"
- "88c6d217e0c959d7b67ff4af3b784d2a28cabb39"
- "flags"
- "sentinel"
- "link-pending-commands"
- "0"
- "link-refcount"
- "1"
- "last-ping-sent"
- "0"
- "last-ok-ping-reply"
- "1011"
- "last-ping-reply"
- "1011"
- "down-after-milliseconds"
- "5000"
- "last-hello-message"
- "790"
- "voted-leader"
- "?"
- "voted-leader-epoch"
- "0"
from redis.sentinel import Sentinel
sentinel = Sentinel([('host ip', 26379), ('host ip', 26380), ('host ip', 26381)], socket_timeout=0.1)
# sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
master_result = sentinel.discover_master('mymaster')
slaves_result = sentinel.discover_slaves('mymaster')
print(master_result)
print(slaves_result)
('172.25.0.2', 6380)
[('172.25.0.3', 6381)]
master = sentinel.master_for('mymaster', socket_timeout=0.1)
slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
master.set('foo', 'bar')
result = slave.get('foo')
print(result)
Traceback (most recent call last): File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 609, in connect sock = self.retry.call_with_retry( File "F:\projects\mystuff\demo\lib\site-packages\redis\retry.py", line 51, in call_with_retry
raise error File "F:\projects\mystuff\demo\lib\site-packages\redis\retry.py", line 46, in call_with_retry
return do() File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 610, in
lambda: self._connect(), lambda error: self.disconnect(error) File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 675, in _connect
raise err File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 663, in _connect
sock.connect(socket_address) socket.timeout: timed outDuring handling of the above exception, another exception occurred:
Traceback (most recent call last): File "F:\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "F:\Python\Python39\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "F:\projects\mystuff\demo\demo\prodramatiq\redis_op.py", line 36, in
master.set('foo', 'bar') File "F:\projects\mystuff\demo\lib\site-packages\redis\commands\core.py", line 2214, in set return self.execute_command("SET", *pieces, **options) File "F:\projects\mystuff\demo\lib\site-packages\redis\client.py", line 1232, in execute_command
conn = self.connection or pool.get_connection(command_name, **options) File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 1383, in get_connection connection.connect() File "F:\projects\mystuff\demo\lib\site-packages\redis\sentinel.py", line 54, in connect return self.retry.call_with_retry(self._connect_retry, lambda error: None) File "F:\projects\mystuff\demo\lib\site-packages\redis\retry.py", line 51, in call_with_retry
raise error File "F:\projects\mystuff\demo\lib\site-packages\redis\retry.py", line 46, in call_with_retry
return do() File "F:\projects\mystuff\demo\lib\site-packages\redis\sentinel.py", line 44, in _connect_retry
self.connect_to(self.connection_pool.get_master_address()) File "F:\projects\mystuff\demo\lib\site-packages\redis\sentinel.py", line 34, in connect_to super().connect() File "F:\projects\mystuff\demo\lib\site-packages\redis\connection.py", line 613, in connect raise TimeoutError("Timeout connecting to server") redis.exceptions.TimeoutError: Timeout connecting to server
seems i have the same issue!
I get the same error
Same issue here, has anybody found a workaround?
Got same issue, need help
seems i have the same issue!
Same issue here, has anybody found a workaround?
hi bro, did you got a solution?
Have you tried to increase the value of socket_timeout? For example, using sentinel.master_for('mymaster') instead of sentinel.master_for('mymaster', socket_timeout=0.1).
Thanks @nerg4l the same issue with or without 'timeout' setting
I'm also experiencing this problem. Has anyone found a solution?
edit:
Sending a giant timeout works for me:
from django.conf import settings
from redis import Sentinel
def get_redis_client():
sentinel = Sentinel(settings.REDIS_SENTINELS, socket_timeout=1000)
client = sentinel.master_for(settings.REDIS_MASTER_NAME, socket_timeout=1000)
return client
edit:
turns out this param units are milliseconds, not seconds