@@ -414,14 +414,25 @@ def __init__(
414
414
stale data.
415
415
When set to true, read commands will be assigned between the
416
416
primary and its replications in a Round-Robin manner.
417
- :cluster_error_retry_attempts: 'int'
417
+ :cluster_error_retry_attempts: 'int'
418
418
Retry command execution attempts when encountering ClusterDownError
419
419
or ConnectionError
420
- :retry_on_timeout: 'bool'
420
+ :retry_on_timeout: 'bool'
421
421
To specify a retry policy, first set `retry_on_timeout` to `True`
422
422
then set `retry` to a valid `Retry` object
423
- :retry: 'Retry'
423
+ :retry: 'Retry'
424
424
a `Retry` object
425
+ :reinitialize_steps: 'int'
426
+ Specifies the number of MOVED errors that need to occur before
427
+ reinitializing the whole cluster topology. If a MOVED error occurs
428
+ and the cluster does not need to be reinitialized on this current
429
+ error handling, only the MOVED slot will be patched with the
430
+ redirected node.
431
+ To reinitialize the cluster on every MOVED error, set
432
+ reinitialize_steps to 1.
433
+ To avoid reinitializing the cluster on moved errors, set
434
+ reinitialize_steps to 0.
435
+
425
436
:**kwargs:
426
437
Extra arguments that will be sent into Redis instance when created
427
438
(See Official redis-py doc for supported kwargs
@@ -727,7 +738,9 @@ def _determine_nodes(self, *args, **kwargs):
727
738
return [node ]
728
739
729
740
def _should_reinitialized (self ):
730
- # In order not to reinitialize the cluster, the user can set
741
+ # To reinitialize the cluster on every MOVED error,
742
+ # set reinitialize_steps to 1.
743
+ # To avoid reinitializing the cluster on moved errors, set
731
744
# reinitialize_steps to 0.
732
745
if self .reinitialize_steps == 0 :
733
746
return False
@@ -958,8 +971,8 @@ def _execute_command(self, target_node, *args, **kwargs):
958
971
# redirected node output and try again. If MovedError exceeds
959
972
# 'reinitialize_steps' number of times, we will force
960
973
# reinitializing the tables, and then try again.
961
- # 'reinitialize_steps' counter will increase faster when the
962
- # same client object is shared between multiple threads. To
974
+ # 'reinitialize_steps' counter will increase faster when
975
+ # the same client object is shared between multiple threads. To
963
976
# reduce the frequency you can set this variable in the
964
977
# RedisCluster constructor.
965
978
log .exception ("MovedError" )
@@ -1055,6 +1068,10 @@ def __repr__(self):
1055
1068
def __eq__ (self , obj ):
1056
1069
return isinstance (obj , ClusterNode ) and obj .name == self .name
1057
1070
1071
+ def __del__ (self ):
1072
+ if self .redis_connection is not None :
1073
+ self .redis_connection .close ()
1074
+
1058
1075
1059
1076
class LoadBalancer :
1060
1077
"""
@@ -1300,6 +1317,11 @@ def initialize(self):
1300
1317
startup_node .host , startup_node .port , ** copy_kwargs
1301
1318
)
1302
1319
self .startup_nodes [startup_node .name ].redis_connection = r
1320
+ # Make sure cluster mode is enabled on this node
1321
+ if bool (r .info ().get ("cluster_enabled" )) is False :
1322
+ raise RedisClusterException (
1323
+ "Cluster mode is not enabled on this node"
1324
+ )
1303
1325
cluster_slots = r .execute_command ("CLUSTER SLOTS" )
1304
1326
startup_nodes_reachable = True
1305
1327
except (ConnectionError , TimeoutError ) as e :
@@ -1327,7 +1349,7 @@ def initialize(self):
1327
1349
message = e .__str__ ()
1328
1350
raise RedisClusterException (
1329
1351
'ERROR sending "cluster slots" command to redis '
1330
- f"server: { startup_node } . error: { message } "
1352
+ f"server { startup_node . name } . error: { message } "
1331
1353
)
1332
1354
1333
1355
# CLUSTER SLOTS command results in the following output:
0 commit comments