question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

the callback for `CLUSTER GETKEYSINSLOT` does not work in redis 3+

See original GitHub issue

Hiya,

Here’s some code to illustrate the issue:

>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{'host': '127.0.0.1', 'port': 2951}]
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes)
>>> rc.CLUSTER_COMMANDS_RESPONSE_CALLBACKS['CLUSTER GETKEYSINSLOT']
int
>>> rc.execute_command('CLUSTER GETKEYSINSLOT', 601, 2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-2ad54da2b333> in <module>()
----> 1 rc.execute_command('CLUSTER GETKEYSINSLOT', 601, 2)
...
...
...
venv/lib/python3.6/site-packages/redis/client.py in parse_response(self, connection, command_name, **options)
    585         response = connection.read_response()
    586         if command_name in self.response_callbacks:
--> 587             return self.response_callbacks[command_name](response, **options)
    588         return response
    589

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

After applying this diff:

(venv) lcarvalh-ld ~/src/redis-py-cluster git:unstable ± ❱❱❱ git diff
diff --git a/rediscluster/client.py b/rediscluster/client.py
index d0258c3..8f77925 100644
--- a/rediscluster/client.py
+++ b/rediscluster/client.py
@@ -108,7 +108,7 @@ class StrictRedisCluster(StrictRedis):
         'CLUSTER DELSLOTS': bool_ok,
         'CLUSTER FAILOVER': bool_ok,
         'CLUSTER FORGET': bool_ok,
-        'CLUSTER GETKEYSINSLOT': int,
+        'CLUSTER GETKEYSINSLOT': list,
         'CLUSTER INFO': parse_info,
         'CLUSTER KEYSLOT': int,
         'CLUSTER MEET': bool_ok,

The command now returns a list of bytestring keys:

>>> from rediscluster import StrictRedisCluster
>>> startup_nodes = [{'host': '10.136.208.61', 'port': 2951}]
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes)
>>> rc.CLUSTER_COMMANDS_RESPONSE_CALLBACKS['CLUSTER GETKEYSINSLOT']
list
>>> rc.execute_command('CLUSTER GETKEYSINSLOT', 601, 1)
[b'prod.tag_hosts:ROUTER_MT-LD-2']

Happy to submit a PR, but I presume that just a list of raw bytes it not ideal, based on the existence of parse_cluster_nodes and parse_cluster_slots. What would you prefer? A simple function that parses each key would satisfy my personal needs (and I’ve subclassed StrictRedisCluster to do this:

class RedisCluster(StrictRedisCluster):
    def __init__(self, *args, **kwargs) -> None:
        """Create a RedisCluster."""
        # fix getkeysinslot callback
        self.CLUSTER_COMMANDS_RESPONSE_CALLBACKS['CLUSTER GETKEYSINSLOT'] = lambda x: [nativestr(i) for i in x]
        super().__init__(*args, **kwargs)

Thanks in advance for looking!

reference: https://redis.io/commands/cluster-getkeysinslot

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Grokzencommented, Mar 6, 2018

Have a look at this commit https://github.com/Grokzen/redis-py-cluster/commit/2cbe4a5ba859571e1acb86f33f202904a4d18cb1 it should clear up everything for your problem. Also there is a nice new method that you can use instead of the execute_command solution.

0reactions
lorencarvalhocommented, Mar 6, 2018

awesome!! thanks so much

Read more comments on GitHub >

github_iconTop Results From Across the Web

redis-py-cluster/release-notes.rst at master - GitHub
Python cluster client for the official redis cluster. Redis 3.0+. ... Fixed bug where "from rediscluster import *" would not work correct ...
Read more >
redis-py-cluster Documentation - Read the Docs
A working Redis cluster based on version >=3.0.0 is required. ... This host_port_remap feature will not work on the startup_nodes so you ...
Read more >
CLUSTER GETKEYSINSLOT - Redis
The command returns an array of keys names stored in the contacted node and hashing to the specified hash slot. The maximum number...
Read more >
Redis Cluster for (node redis) not emitting events
I am using node redis library to connect to redis cluster ,followed ... Currently (v4.0.2) - no. github.com/redis/node-redis/issues/1855.
Read more >
ioredis - npm
Medis is an open-sourced, beautiful, easy-to-use Redis GUI ... ioredis supports the node.js callback style redis.get("mykey", (err, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found