Skip to content

Commit 04c6565

Browse files
committed
Fixed 'MovedError' bug in RedisCluster, changed the initialize function of the nodes manager to stop iterating through startup nodes when the coverage is full
1 parent 4831034 commit 04c6565

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

redis/cluster.py

+11-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,11 @@ 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 slots are covered
1440+
break
1441+
14341442
if not startup_nodes_reachable:
14351443
raise RedisClusterException(
14361444
"Redis Cluster cannot be connected. Please provide at least "
@@ -1440,7 +1448,6 @@ def initialize(self):
14401448
# Create Redis connections to all nodes
14411449
self.create_redis_connections(list(tmp_nodes_cache.values()))
14421450

1443-
fully_covered = self.check_slots_coverage(tmp_slots)
14441451
# Check if the slots are not fully covered
14451452
if not fully_covered and self._require_full_coverage:
14461453
# Despite the requirement that the slots be covered, there
@@ -1478,6 +1485,8 @@ def initialize(self):
14781485
self.default_node = self.get_nodes_by_server_type(PRIMARY)[0]
14791486
# Populate the startup nodes with all discovered nodes
14801487
self.populate_startup_nodes(self.nodes_cache.values())
1488+
# If initialize was called after a MovedError, clear it
1489+
self._moved_exception = None
14811490

14821491
def close(self):
14831492
self.default_node = None

0 commit comments

Comments
 (0)