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

Allow replica to master promotion in nodes_cache #2549

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,8 @@ def _get_or_create_cluster_node(self, host, port, role, tmp_nodes_cache):
if target_node is None or target_node.redis_connection is None:
# create new cluster node for this cluster
target_node = ClusterNode(host, port, role)
if target_node.server_type != role:
target_node.server_type = role

return target_node

Expand Down
51 changes: 51 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,57 @@ def test_init_slots_cache(self):

assert len(n_manager.nodes_cache) == 6

def test_init_promote_server_type_for_node_in_cache(self):
"""
When replica is promoted to master, nodes_cache must change the server type
accordingly
"""
cluster_slots_before_promotion = [
[0, 16383, ["127.0.0.1", 7000], ["127.0.0.1", 7003]]
]
cluster_slots_after_promotion = [
[0, 16383, ["127.0.0.1", 7003], ["127.0.0.1", 7004]]
]

cluster_slots_results = [
cluster_slots_before_promotion,
cluster_slots_after_promotion,
]

with patch.object(Redis, "execute_command") as execute_command_mock:

def execute_command(*_args, **_kwargs):
if _args[0] == "CLUSTER SLOTS":
mock_cluster_slots = cluster_slots_results.pop(0)
return mock_cluster_slots
elif _args[0] == "COMMAND":
return {"get": [], "set": []}
elif _args[0] == "INFO":
return {"cluster_enabled": True}
elif len(_args) > 1 and _args[1] == "cluster-require-full-coverage":
return {"cluster-require-full-coverage": False}
else:
return execute_command_mock(*_args, **_kwargs)

execute_command_mock.side_effect = execute_command

nm = NodesManager(
startup_nodes=[ClusterNode(host=default_host, port=default_port)],
from_url=False,
require_full_coverage=False,
dynamic_startup_nodes=True,
)

assert nm.default_node.host == "127.0.0.1"
assert nm.default_node.port == 7000
assert nm.default_node.server_type == PRIMARY

nm.initialize()

assert nm.default_node.host == "127.0.0.1"
assert nm.default_node.port == 7003
assert nm.default_node.server_type == PRIMARY

def test_init_slots_cache_cluster_mode_disabled(self):
"""
Test that creating a RedisCluster failes if one of the startup nodes
Expand Down