Skip to content

Commit e0d3ba5

Browse files
authored
Fixed MovedError, and stopped iterating through startup nodes when slots are fully covered (#1819)
1 parent 01fedaf commit e0d3ba5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

redis/cluster.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -996,9 +996,11 @@ def _execute_command(self, target_node, *args, **kwargs):
996996
self.reinitialize_counter += 1
997997
if self._should_reinitialized():
998998
self.nodes_manager.initialize()
999+
# Reset the counter
1000+
self.reinitialize_counter = 0
9991001
else:
10001002
self.nodes_manager.update_moved_exception(e)
1001-
moved = True
1003+
moved = True
10021004
except TryAgainError:
10031005
log.exception("TryAgainError")
10041006

@@ -1320,6 +1322,7 @@ def initialize(self):
13201322
tmp_slots = {}
13211323
disagreements = []
13221324
startup_nodes_reachable = False
1325+
fully_covered = False
13231326
kwargs = self.connection_kwargs
13241327
for startup_node in self.startup_nodes.values():
13251328
try:
@@ -1431,6 +1434,12 @@ def initialize(self):
14311434
f'slots cache: {", ".join(disagreements)}'
14321435
)
14331436

1437+
fully_covered = self.check_slots_coverage(tmp_slots)
1438+
if fully_covered:
1439+
# Don't need to continue to the next startup node if all
1440+
# slots are covered
1441+
break
1442+
14341443
if not startup_nodes_reachable:
14351444
raise RedisClusterException(
14361445
"Redis Cluster cannot be connected. Please provide at least "
@@ -1440,7 +1449,6 @@ def initialize(self):
14401449
# Create Redis connections to all nodes
14411450
self.create_redis_connections(list(tmp_nodes_cache.values()))
14421451

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

14821492
def close(self):
14831493
self.default_node = None

0 commit comments

Comments
 (0)