Grokzen / redis-py-cluster

Python cluster client for the official redis cluster. Redis 3.0+.
https://redis-py-cluster.readthedocs.io/
MIT License
1.1k stars 316 forks source link

Logging error message in RedisClusterException #421

Closed aaksarin closed 3 years ago

aaksarin commented 3 years ago

Hi. Today I got RedisClusterException

image

and problem there is that error handling of ResponseError loosing original error message

image

It would be easier to debug my problem if original error message (e.str()) was logged.

Grokzen commented 3 years ago

@aaksarin This depends on what python version you are using. When i run this code parts inside python 3.9.0+ for example the exception is propegated and includes the original exception message

(redis-py-cluster) ➜  redis-py-cluster git:(master) ✗ python test.py
Traceback (most recent call last):
  File "/home/grok/code/github/redis-py-cluster/rediscluster/nodemanager.py", line 227, in initialize
    cluster_slots = r.execute_command("cluster", "slots")
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 901, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 915, in parse_response
    response = connection.read_response()
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/connection.py", line 756, in read_response
    raise response
redis.exceptions.ResponseError: This instance has cluster support disabled

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/grok/code/github/redis-py-cluster/test.py", line 2, in <module>
    rc = RedisCluster(startup_nodes=[{'host': '127.0.0.1', 'port': '6379'}])
  File "/home/grok/code/github/redis-py-cluster/rediscluster/client.py", line 368, in __init__
    pool = connection_pool_cls(
  File "/home/grok/code/github/redis-py-cluster/rediscluster/connection.py", line 160, in __init__
    self.nodes.initialize()
  File "/home/grok/code/github/redis-py-cluster/rediscluster/nodemanager.py", line 239, in initialize
    raise RedisClusterException("ERROR sending 'cluster slots' command to redis server: {0}".format(node))
rediscluster.exceptions.RedisClusterException: ERROR sending 'cluster slots' command to redis server: {'host': '127.0.0.1', 'port': '6379'}

So i have added the following logging

log.exception("ReseponseError sending 'cluster slots' to redis server")

and hopefully that should be enough to solve your issue above where it logs to exception log level

I ran the code above on python 3.9.0+ with this simple test script to simulate the error

from rediscluster import RedisCluster
rc = RedisCluster(startup_nodes=[{'host': '127.0.0.1', 'port': '6379'}])
rc.cluster_slots()

and i got the following output with a very simple logging config

(redis-py-cluster) ➜  redis-py-cluster git:(master) ✗ python test.py  
ReseponseError sending 'cluster slots' to redis server
Traceback (most recent call last):
  File "/home/grok/code/github/redis-py-cluster/rediscluster/nodemanager.py", line 227, in initialize
    cluster_slots = r.execute_command("cluster", "slots")
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 901, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 915, in parse_response
    response = connection.read_response()
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/connection.py", line 756, in read_response
    raise response
redis.exceptions.ResponseError: This instance has cluster support disabled
Traceback (most recent call last):
  File "/home/grok/code/github/redis-py-cluster/rediscluster/nodemanager.py", line 227, in initialize
    cluster_slots = r.execute_command("cluster", "slots")
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 901, in execute_command
    return self.parse_response(conn, command_name, **options)
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/client.py", line 915, in parse_response
    response = connection.read_response()
  File "/home/grok/.virtualenvs/redis-py-cluster/lib/python3.9/site-packages/redis/connection.py", line 756, in read_response
    raise response
redis.exceptions.ResponseError: This instance has cluster support disabled

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/grok/code/github/redis-py-cluster/test.py", line 2, in <module>
    rc = RedisCluster(startup_nodes=[{'host': '127.0.0.1', 'port': '6379'}])
  File "/home/grok/code/github/redis-py-cluster/rediscluster/client.py", line 368, in __init__
    pool = connection_pool_cls(
  File "/home/grok/code/github/redis-py-cluster/rediscluster/connection.py", line 160, in __init__
    self.nodes.initialize()
  File "/home/grok/code/github/redis-py-cluster/rediscluster/nodemanager.py", line 239, in initialize
    raise RedisClusterException("ERROR sending 'cluster slots' command to redis server: {0}".format(node))
rediscluster.exceptions.RedisClusterException: ERROR sending 'cluster slots' command to redis server: {'host': '127.0.0.1', 'port': '6379'}

https://github.com/Grokzen/redis-py-cluster/commit/3756bf116d6f047e4445c4a9ef84885d5c612024