Skip to content

Fixed MovedError, and stopped iterating through startup nodes when slots are fully covered #1819

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

Merged
merged 2 commits into from
Dec 22, 2021
Merged
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
14 changes: 12 additions & 2 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,11 @@ def _execute_command(self, target_node, *args, **kwargs):
self.reinitialize_counter += 1
if self._should_reinitialized():
self.nodes_manager.initialize()
# Reset the counter
self.reinitialize_counter = 0
else:
self.nodes_manager.update_moved_exception(e)
moved = True
moved = True
except TryAgainError:
log.exception("TryAgainError")

Expand Down Expand Up @@ -1320,6 +1322,7 @@ def initialize(self):
tmp_slots = {}
disagreements = []
startup_nodes_reachable = False
fully_covered = False
kwargs = self.connection_kwargs
for startup_node in self.startup_nodes.values():
try:
Expand Down Expand Up @@ -1431,6 +1434,12 @@ def initialize(self):
f'slots cache: {", ".join(disagreements)}'
)

fully_covered = self.check_slots_coverage(tmp_slots)
if fully_covered:
# Don't need to continue to the next startup node if all
# slots are covered
break

if not startup_nodes_reachable:
raise RedisClusterException(
"Redis Cluster cannot be connected. Please provide at least "
Expand All @@ -1440,7 +1449,6 @@ def initialize(self):
# Create Redis connections to all nodes
self.create_redis_connections(list(tmp_nodes_cache.values()))

fully_covered = self.check_slots_coverage(tmp_slots)
# Check if the slots are not fully covered
if not fully_covered and self._require_full_coverage:
# Despite the requirement that the slots be covered, there
Expand Down Expand Up @@ -1478,6 +1486,8 @@ def initialize(self):
self.default_node = self.get_nodes_by_server_type(PRIMARY)[0]
# Populate the startup nodes with all discovered nodes
self.populate_startup_nodes(self.nodes_cache.values())
# If initialize was called after a MovedError, clear it
self._moved_exception = None

def close(self):
self.default_node = None
Expand Down