Skip to content

Commit

Permalink
Add support for cluster myshardid (#2704)
Browse files Browse the repository at this point in the history
* feat: adding support for cluster myshardid

* lint fix

* fix: comment fix and async test

* fix: adding version check

* fix lint:

* linters

---------

Co-authored-by: Anuragkillswitch <[email protected]>
Co-authored-by: dvora-h <[email protected]>
Co-authored-by: dvora-h <[email protected]>
  • Loading branch information
4 people authored May 8, 2023
1 parent 906e413 commit cfdcfd8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
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):
"""
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):
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

0 comments on commit cfdcfd8

Please sign in to comment.