Skip to content

Commit bae6385

Browse files
zakafzach-iee
andauthored
allow replica to master promotion in nodes_cache (#2549)
Co-authored-by: zach.lee <zach.lee@sendbird.com>
1 parent 4a825bc commit bae6385

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

redis/cluster.py

+2
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,8 @@ def _get_or_create_cluster_node(self, host, port, role, tmp_nodes_cache):
14371437
if target_node is None or target_node.redis_connection is None:
14381438
# create new cluster node for this cluster
14391439
target_node = ClusterNode(host, port, role)
1440+
if target_node.server_type != role:
1441+
target_node.server_type = role
14401442

14411443
return target_node
14421444

tests/test_cluster.py

+51
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,57 @@ def test_init_slots_cache(self):
22622262

22632263
assert len(n_manager.nodes_cache) == 6
22642264

2265+
def test_init_promote_server_type_for_node_in_cache(self):
2266+
"""
2267+
When replica is promoted to master, nodes_cache must change the server type
2268+
accordingly
2269+
"""
2270+
cluster_slots_before_promotion = [
2271+
[0, 16383, ["127.0.0.1", 7000], ["127.0.0.1", 7003]]
2272+
]
2273+
cluster_slots_after_promotion = [
2274+
[0, 16383, ["127.0.0.1", 7003], ["127.0.0.1", 7004]]
2275+
]
2276+
2277+
cluster_slots_results = [
2278+
cluster_slots_before_promotion,
2279+
cluster_slots_after_promotion,
2280+
]
2281+
2282+
with patch.object(Redis, "execute_command") as execute_command_mock:
2283+
2284+
def execute_command(*_args, **_kwargs):
2285+
if _args[0] == "CLUSTER SLOTS":
2286+
mock_cluster_slots = cluster_slots_results.pop(0)
2287+
return mock_cluster_slots
2288+
elif _args[0] == "COMMAND":
2289+
return {"get": [], "set": []}
2290+
elif _args[0] == "INFO":
2291+
return {"cluster_enabled": True}
2292+
elif len(_args) > 1 and _args[1] == "cluster-require-full-coverage":
2293+
return {"cluster-require-full-coverage": False}
2294+
else:
2295+
return execute_command_mock(*_args, **_kwargs)
2296+
2297+
execute_command_mock.side_effect = execute_command
2298+
2299+
nm = NodesManager(
2300+
startup_nodes=[ClusterNode(host=default_host, port=default_port)],
2301+
from_url=False,
2302+
require_full_coverage=False,
2303+
dynamic_startup_nodes=True,
2304+
)
2305+
2306+
assert nm.default_node.host == "127.0.0.1"
2307+
assert nm.default_node.port == 7000
2308+
assert nm.default_node.server_type == PRIMARY
2309+
2310+
nm.initialize()
2311+
2312+
assert nm.default_node.host == "127.0.0.1"
2313+
assert nm.default_node.port == 7003
2314+
assert nm.default_node.server_type == PRIMARY
2315+
22652316
def test_init_slots_cache_cluster_mode_disabled(self):
22662317
"""
22672318
Test that creating a RedisCluster failes if one of the startup nodes

0 commit comments

Comments
 (0)