@@ -3023,15 +3023,15 @@ class TransactionStrategy(AbstractStrategy):
3023
3023
NO_SLOTS_COMMANDS = {"UNWATCH" }
3024
3024
IMMEDIATE_EXECUTE_COMMANDS = {"WATCH" , "UNWATCH" }
3025
3025
UNWATCH_COMMANDS = {"DISCARD" , "EXEC" , "UNWATCH" }
3026
+ SLOT_REDIRECT_ERRORS = (AskError , MovedError )
3027
+ CONNECTION_ERRORS = (ConnectionError ,OSError ,ClusterDownError )
3026
3028
3027
3029
def __init__ (self , pipe : ClusterPipeline ):
3028
3030
super ().__init__ (pipe )
3029
3031
self ._explicit_transaction = False
3030
3032
self ._watching = False
3031
3033
self ._pipeline_slots : Set [int ] = set ()
3032
3034
self ._transaction_connection : Optional [Connection ] = None
3033
- self ._cluster_error = False
3034
- self ._slot_migrating = False
3035
3035
self ._executing = False
3036
3036
3037
3037
def _get_client_and_connection_for_transaction (self ) -> Tuple [Redis , Connection ]:
@@ -3125,28 +3125,19 @@ def _send_command_parse_response(
3125
3125
Send a command and parse the response
3126
3126
"""
3127
3127
3128
- self .slot_migrating = False
3129
- try :
3130
- conn .send_command (* args )
3131
- output = redis_node .parse_response (conn , command_name , ** options )
3132
-
3133
- except (AskError , MovedError ) as slot_error :
3134
- self .slot_migrating = True
3135
- raise slot_error
3136
- except ConnectionError as conn_error :
3137
- self ._cluster_error = True
3138
- raise conn_error
3128
+ conn .send_command (* args )
3129
+ output = redis_node .parse_response (conn , command_name , ** options )
3139
3130
3140
3131
if command_name in self .UNWATCH_COMMANDS :
3141
3132
self ._watching = False
3142
3133
return output
3143
3134
3144
3135
def _reinitialize_on_error (self , error ):
3145
3136
if self ._watching :
3146
- if self .slot_migrating and self ._executing :
3137
+ if type ( error ) in self .SLOT_REDIRECT_ERRORS and self ._executing :
3147
3138
raise WatchError ("Slot rebalancing occurred while watching keys" )
3148
3139
3149
- if self .slot_migrating or self ._cluster_error :
3140
+ if type ( error ) in self .SLOT_REDIRECT_ERRORS or type ( error ) in self .CONNECTION_ERRORS :
3150
3141
if self ._transaction_connection :
3151
3142
self ._transaction_connection = None
3152
3143
@@ -3157,8 +3148,6 @@ def _reinitialize_on_error(self, error):
3157
3148
else :
3158
3149
self ._nodes_manager .update_moved_exception (error )
3159
3150
3160
- self .slot_migrating = False
3161
- self ._cluster_error = False
3162
3151
self ._executing = False
3163
3152
3164
3153
def _raise_first_error (self , responses , stack ):
@@ -3196,8 +3185,6 @@ def _execute_transaction(
3196
3185
)
3197
3186
3198
3187
self ._executing = True
3199
- self .slot_migrating = False
3200
- self ._cluster_error = False
3201
3188
3202
3189
redis_node , connection = self ._get_client_and_connection_for_transaction ()
3203
3190
@@ -3220,8 +3207,7 @@ def _execute_transaction(
3220
3207
except ResponseError as e :
3221
3208
self .annotate_exception (e , 0 , "MULTI" )
3222
3209
errors .append (e )
3223
- except (ClusterDownError , ConnectionError ) as cluster_error :
3224
- self ._cluster_error = True
3210
+ except self .CONNECTION_ERRORS as cluster_error :
3225
3211
self .annotate_exception (cluster_error , 0 , "MULTI" )
3226
3212
raise
3227
3213
@@ -3232,12 +3218,10 @@ def _execute_transaction(
3232
3218
else :
3233
3219
try :
3234
3220
_ = redis_node .parse_response (connection , "_" )
3235
- except (AskError , MovedError ) as slot_error :
3236
- self .slot_migrating = True
3221
+ except self .SLOT_REDIRECT_ERRORS as slot_error :
3237
3222
self .annotate_exception (slot_error , i + 1 , command .args )
3238
3223
errors .append (slot_error )
3239
- except (ClusterDownError , ConnectionError ) as cluster_error :
3240
- self ._cluster_error = True
3224
+ except self .CONNECTION_ERRORS as cluster_error :
3241
3225
self .annotate_exception (cluster_error , i + 1 , command .args )
3242
3226
raise
3243
3227
except ResponseError as e :
@@ -3274,7 +3258,7 @@ def _execute_transaction(
3274
3258
)
3275
3259
3276
3260
# find any errors in the response and raise if necessary
3277
- if raise_on_error or self . slot_migrating :
3261
+ if raise_on_error or len ( errors ) > 0 :
3278
3262
self ._raise_first_error (
3279
3263
response ,
3280
3264
self ._command_queue ,
@@ -3312,7 +3296,7 @@ def reset(self):
3312
3296
self ._transaction_connection
3313
3297
)
3314
3298
self ._transaction_connection = None
3315
- except ConnectionError :
3299
+ except self . CONNECTION_ERRORS :
3316
3300
# disconnect will also remove any previous WATCHes
3317
3301
if self ._transaction_connection :
3318
3302
self ._transaction_connection .disconnect ()
@@ -3321,8 +3305,6 @@ def reset(self):
3321
3305
self ._watching = False
3322
3306
self ._explicit_transaction = False
3323
3307
self ._pipeline_slots = set ()
3324
- self ._slot_migrating = False
3325
- self ._cluster_error = False
3326
3308
self ._executing = False
3327
3309
3328
3310
def send_cluster_commands (
0 commit comments