Skip to content

Commit 33e76d8

Browse files
willianmrsWillian Moreira
authored andcommitted
Optimizing cluster initialization changing the checks for cluster-enabled flag (#3158)
* change if the cluster-mode is enabled by trying run CLUSTER SLOT insted of INFO * fix typo * fixing cluster mode is not enabled on this node tests * remove changes on asyncio * rename mock flag to be more consistent * optimizing async cluster creation using CLUSTER SLOT command instead of INFO command * fixing test. Before INFO and CLUSTER_SLOT was used for performing the connection, now only the CLUSTER_SLOT, so the total commands is minus 1 * remove dot at the end of string * remove unecessary print from test * fix lint problems --------- Co-authored-by: Willian Moreira <[email protected]>
1 parent 49cbc44 commit 33e76d8

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

redis/asyncio/cluster.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,13 +1226,12 @@ async def initialize(self) -> None:
12261226
for startup_node in self.startup_nodes.values():
12271227
try:
12281228
# Make sure cluster mode is enabled on this node
1229-
if not (await startup_node.execute_command("INFO")).get(
1230-
"cluster_enabled"
1231-
):
1229+
try:
1230+
cluster_slots = await startup_node.execute_command("CLUSTER SLOTS")
1231+
except ResponseError:
12321232
raise RedisClusterException(
12331233
"Cluster mode is not enabled on this node"
12341234
)
1235-
cluster_slots = await startup_node.execute_command("CLUSTER SLOTS")
12361235
startup_nodes_reachable = True
12371236
except Exception as e:
12381237
# Try the next startup node.

redis/cluster.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,11 +1513,12 @@ def initialize(self):
15131513
)
15141514
self.startup_nodes[startup_node.name].redis_connection = r
15151515
# Make sure cluster mode is enabled on this node
1516-
if bool(r.info().get("cluster_enabled")) is False:
1516+
try:
1517+
cluster_slots = str_if_bytes(r.execute_command("CLUSTER SLOTS"))
1518+
except ResponseError:
15171519
raise RedisClusterException(
15181520
"Cluster mode is not enabled on this node"
15191521
)
1520-
cluster_slots = str_if_bytes(r.execute_command("CLUSTER SLOTS"))
15211522
startup_nodes_reachable = True
15221523
except Exception as e:
15231524
# Try the next startup node.

tests/test_asyncio/test_cluster.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ async def slowlog(r: RedisCluster) -> None:
128128
await r.config_set("slowlog-max-len", old_max_length_value)
129129

130130

131-
async def get_mocked_redis_client(*args, **kwargs) -> RedisCluster:
131+
async def get_mocked_redis_client(
132+
cluster_slots_raise_error=False, *args, **kwargs
133+
) -> RedisCluster:
132134
"""
133135
Return a stable RedisCluster object that have deterministic
134136
nodes and slots setup to remove the problem of different IP addresses
@@ -140,9 +142,13 @@ async def get_mocked_redis_client(*args, **kwargs) -> RedisCluster:
140142
with mock.patch.object(ClusterNode, "execute_command") as execute_command_mock:
141143

142144
async def execute_command(*_args, **_kwargs):
145+
143146
if _args[0] == "CLUSTER SLOTS":
144-
mock_cluster_slots = cluster_slots
145-
return mock_cluster_slots
147+
if cluster_slots_raise_error:
148+
raise ResponseError()
149+
else:
150+
mock_cluster_slots = cluster_slots
151+
return mock_cluster_slots
146152
elif _args[0] == "COMMAND":
147153
return {"get": [], "set": []}
148154
elif _args[0] == "INFO":
@@ -2457,7 +2463,10 @@ async def test_init_slots_cache_cluster_mode_disabled(self) -> None:
24572463
"""
24582464
with pytest.raises(RedisClusterException) as e:
24592465
rc = await get_mocked_redis_client(
2460-
host=default_host, port=default_port, cluster_enabled=False
2466+
cluster_slots_raise_error=True,
2467+
host=default_host,
2468+
port=default_port,
2469+
cluster_enabled=False,
24612470
)
24622471
await rc.aclose()
24632472
assert "Cluster mode is not enabled on this node" in str(e.value)
@@ -2718,10 +2727,9 @@ async def parse_response(
27182727
async with r.pipeline() as pipe:
27192728
with pytest.raises(ClusterDownError):
27202729
await pipe.get(key).execute()
2721-
27222730
assert (
27232731
node.parse_response.await_count
2724-
== 4 * r.cluster_error_retry_attempts - 3
2732+
== 3 * r.cluster_error_retry_attempts - 2
27252733
)
27262734

27272735
async def test_connection_error_not_raised(self, r: RedisCluster) -> None:

tests/test_cluster.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ def cleanup():
151151
r.config_set("slowlog-max-len", 128)
152152

153153

154-
def get_mocked_redis_client(func=None, *args, **kwargs):
154+
def get_mocked_redis_client(
155+
func=None, cluster_slots_raise_error=False, *args, **kwargs
156+
):
155157
"""
156158
Return a stable RedisCluster object that have deterministic
157159
nodes and slots setup to remove the problem of different IP addresses
@@ -164,8 +166,11 @@ def get_mocked_redis_client(func=None, *args, **kwargs):
164166

165167
def execute_command(*_args, **_kwargs):
166168
if _args[0] == "CLUSTER SLOTS":
167-
mock_cluster_slots = cluster_slots
168-
return mock_cluster_slots
169+
if cluster_slots_raise_error:
170+
raise ResponseError()
171+
else:
172+
mock_cluster_slots = cluster_slots
173+
return mock_cluster_slots
169174
elif _args[0] == "COMMAND":
170175
return {"get": [], "set": []}
171176
elif _args[0] == "INFO":
@@ -2651,7 +2656,10 @@ def test_init_slots_cache_cluster_mode_disabled(self):
26512656
"""
26522657
with pytest.raises(RedisClusterException) as e:
26532658
get_mocked_redis_client(
2654-
host=default_host, port=default_port, cluster_enabled=False
2659+
cluster_slots_raise_error=True,
2660+
host=default_host,
2661+
port=default_port,
2662+
cluster_enabled=False,
26552663
)
26562664
assert "Cluster mode is not enabled on this node" in str(e.value)
26572665

0 commit comments

Comments
 (0)