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

Add support for cluster myshardid #2704

Merged
merged 11 commits into from
May 8, 2023
8 changes: 8 additions & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def parse_cluster_shards(resp, **options):
return shards


def parse_cluster_myshardid(resp, **options):
"""
Parse CLUSTER MYSHARDID response.
"""
return resp.decode("utf-8")


PRIMARY = "primary"
REPLICA = "replica"
SLOT_ID = "slot-id"
Expand Down Expand Up @@ -341,6 +348,7 @@ class AbstractRedisCluster:
CLUSTER_COMMANDS_RESPONSE_CALLBACKS = {
"CLUSTER SLOTS": parse_cluster_slots,
"CLUSTER SHARDS": parse_cluster_shards,
"CLUSTER MYSHARDID": parse_cluster_myshardid,
}

RESULT_CALLBACKS = dict_merge(
Expand Down
9 changes: 8 additions & 1 deletion redis/commands/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
if TYPE_CHECKING:
from redis.asyncio.cluster import TargetNodesT


# Not complete, but covers the major ones
# https://redis.io/commands
READ_COMMANDS = frozenset(
Expand Down Expand Up @@ -634,6 +633,14 @@ def cluster_shards(self, target_nodes=None):
"""
return self.execute_command("CLUSTER SHARDS", target_nodes=target_nodes)

def cluster_myshardid(self, target_nodes=None):
"""
SoulPancake marked this conversation as resolved.
Show resolved Hide resolved
Returns the shard ID of the node.
For more information see https://redis.io/commands/cluster-myshardid/
"""
return self.execute_command("CLUSTER MYSHARDID", target_nodes=target_nodes)

def cluster_links(self, target_node: "TargetNodesT") -> ResponseT:
"""
Each node in a Redis Cluster maintains a pair of long-lived TCP link with each
Expand Down
7 changes: 7 additions & 0 deletions tests/test_asyncio/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,13 @@ async def test_cluster_myid(self, r: RedisCluster) -> None:
myid = await r.cluster_myid(node)
assert len(myid) == 40

@skip_if_server_version_lt("7.2.0")
@skip_if_redis_enterprise()
async def test_cluster_myshardid(self, r: RedisCluster) -> None:
node = r.get_random_node()
myshardid = await r.cluster_myshardid(node)
assert len(myshardid) == 40

@skip_if_redis_enterprise()
async def test_cluster_slots(self, r: RedisCluster) -> None:
mock_all_nodes_resp(r, default_cluster_slots)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,13 @@ def test_cluster_shards(self, r):
for attribute in node.keys():
assert attribute in attributes

@skip_if_server_version_lt("7.2.0")
@skip_if_redis_enterprise()
def test_cluster_myshardid(self, r):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an async version of the test to tests/test_asyncio/test_cluster.py

myshardid = r.cluster_myshardid()
assert isinstance(myshardid, str)
assert len(myshardid) > 0

@skip_if_redis_enterprise()
def test_cluster_addslots(self, r):
node = r.get_random_node()
Expand Down