Skip to content

Commit 2046eac

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 7a369f2 commit 2046eac

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
@@ -1253,13 +1253,12 @@ async def initialize(self) -> None:
12531253
for startup_node in self.startup_nodes.values():
12541254
try:
12551255
# Make sure cluster mode is enabled on this node
1256-
if not (await startup_node.execute_command("INFO")).get(
1257-
"cluster_enabled"
1258-
):
1256+
try:
1257+
cluster_slots = await startup_node.execute_command("CLUSTER SLOTS")
1258+
except ResponseError:
12591259
raise RedisClusterException(
12601260
"Cluster mode is not enabled on this node"
12611261
)
1262-
cluster_slots = await startup_node.execute_command("CLUSTER SLOTS")
12631262
startup_nodes_reachable = True
12641263
except Exception as e:
12651264
# Try the next startup node.

redis/cluster.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,11 +1525,12 @@ def initialize(self):
15251525
)
15261526
self.startup_nodes[startup_node.name].redis_connection = r
15271527
# Make sure cluster mode is enabled on this node
1528-
if bool(r.info().get("cluster_enabled")) is False:
1528+
try:
1529+
cluster_slots = str_if_bytes(r.execute_command("CLUSTER SLOTS"))
1530+
except ResponseError:
15291531
raise RedisClusterException(
15301532
"Cluster mode is not enabled on this node"
15311533
)
1532-
cluster_slots = str_if_bytes(r.execute_command("CLUSTER SLOTS"))
15331534
startup_nodes_reachable = True
15341535
except Exception as e:
15351536
# 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
@@ -127,7 +127,9 @@ async def slowlog(r: RedisCluster) -> None:
127127
await r.config_set("slowlog-max-len", old_max_length_value)
128128

129129

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

141143
async def execute_command(*_args, **_kwargs):
144+
142145
if _args[0] == "CLUSTER SLOTS":
143-
mock_cluster_slots = cluster_slots
144-
return mock_cluster_slots
146+
if cluster_slots_raise_error:
147+
raise ResponseError()
148+
else:
149+
mock_cluster_slots = cluster_slots
150+
return mock_cluster_slots
145151
elif _args[0] == "COMMAND":
146152
return {"get": [], "set": []}
147153
elif _args[0] == "INFO":
@@ -2458,7 +2464,10 @@ async def test_init_slots_cache_cluster_mode_disabled(self) -> None:
24582464
"""
24592465
with pytest.raises(RedisClusterException) as e:
24602466
rc = await get_mocked_redis_client(
2461-
host=default_host, port=default_port, cluster_enabled=False
2467+
cluster_slots_raise_error=True,
2468+
host=default_host,
2469+
port=default_port,
2470+
cluster_enabled=False,
24622471
)
24632472
await rc.aclose()
24642473
assert "Cluster mode is not enabled on this node" in str(e.value)
@@ -2719,10 +2728,9 @@ async def parse_response(
27192728
async with r.pipeline() as pipe:
27202729
with pytest.raises(ClusterDownError):
27212730
await pipe.get(key).execute()
2722-
27232731
assert (
27242732
node.parse_response.await_count
2725-
== 4 * r.cluster_error_retry_attempts - 3
2733+
== 3 * r.cluster_error_retry_attempts - 2
27262734
)
27272735

27282736
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":
@@ -2654,7 +2659,10 @@ def test_init_slots_cache_cluster_mode_disabled(self):
26542659
"""
26552660
with pytest.raises(RedisClusterException) as e:
26562661
get_mocked_redis_client(
2657-
host=default_host, port=default_port, cluster_enabled=False
2662+
cluster_slots_raise_error=True,
2663+
host=default_host,
2664+
port=default_port,
2665+
cluster_enabled=False,
26582666
)
26592667
assert "Cluster mode is not enabled on this node" in str(e.value)
26602668

0 commit comments

Comments
 (0)