@@ -18,7 +18,6 @@ def __init__(self, connection_policy, global_endpoint_manager, *args):
18
18
19
19
self .global_endpoint_manager = global_endpoint_manager
20
20
self .retry_count = 0
21
- self .location_index = 0
22
21
self .connection_policy = connection_policy
23
22
self .request = args [0 ] if args else None
24
23
@@ -29,14 +28,13 @@ def ShouldRetry(self, _exception):
29
28
:returns: a boolean stating whether the request should be retried
30
29
:rtype: bool
31
30
"""
32
- if self . request :
33
- if not _OperationType .IsReadOnlyOperation (self .request .operation_type ):
34
- return False
31
+ # we don't retry on write operations for timeouts or service unavailable
32
+ if self . request and ( not _OperationType .IsReadOnlyOperation (self .request .operation_type ) ):
33
+ return False
35
34
36
35
if not self .connection_policy .EnableEndpointDiscovery :
37
36
return False
38
37
39
-
40
38
# Check if the next retry about to be done is safe
41
39
if _exception .status_code == http_constants .StatusCodes .SERVICE_UNAVAILABLE and \
42
40
self .retry_count >= self ._max_service_unavailable_retry_count :
@@ -47,46 +45,19 @@ def ShouldRetry(self, _exception):
47
45
return False
48
46
49
47
if self .request :
50
- # Update the last routed location to where this request was routed previously.
51
- # So that we can check in location cache if we need to return the current or previous
52
- # based on where the request was routed previously.
53
- self .request .last_routed_location_endpoint_within_region = self .request .location_endpoint_to_route
54
-
55
- if _OperationType .IsReadOnlyOperation (self .request .operation_type ):
56
- # We just directly got to the next location in case of read requests
57
- # We don't retry again on the same region for regional endpoint
58
- location_endpoint = self .resolve_next_region_service_endpoint ()
59
- else :
60
- self .global_endpoint_manager .swap_regional_endpoint_values (self .request )
61
- location_endpoint = self .resolve_current_region_service_endpoint ()
62
- # This is the case where both current and previous point to the same writable endpoint
63
- # In this case we don't want to retry again, rather failover to the next region
64
- if self .request .last_routed_location_endpoint_within_region == location_endpoint :
65
- location_endpoint = self .resolve_next_region_service_endpoint ()
66
-
48
+ location_endpoint = self .resolve_next_region_service_endpoint ()
67
49
self .request .route_to_location (location_endpoint )
68
50
return True
69
51
70
-
71
- # This function prepares the request to go to the second endpoint in the same region
72
- def resolve_current_region_service_endpoint (self ):
73
- # clear previous location-based routing directive
74
- self .request .clear_route_to_location ()
75
- # resolve the next service endpoint in the same region
76
- # since we maintain 2 endpoints per region for write operations
77
- self .request .route_to_location_with_preferred_location_flag (self .location_index , True )
78
- return self .global_endpoint_manager .resolve_service_endpoint (self .request )
79
-
80
52
# This function prepares the request to go to the next region
81
53
def resolve_next_region_service_endpoint (self ):
82
- self .location_index += 1
83
54
# clear previous location-based routing directive
84
55
self .request .clear_route_to_location ()
85
56
# clear the last routed endpoint within same region since we are going to a new region now
86
57
self .request .last_routed_location_endpoint_within_region = None
87
58
# set location-based routing directive based on retry count
88
59
# ensuring usePreferredLocations is set to True for retry
89
- self .request .route_to_location_with_preferred_location_flag (self .location_index , True )
60
+ self .request .route_to_location_with_preferred_location_flag (self .retry_count , True )
90
61
# Resolve the endpoint for the request and pin the resolution to the resolved endpoint
91
62
# This enables marking the endpoint unavailability on endpoint failover/unreachability
92
63
return self .global_endpoint_manager .resolve_service_endpoint (self .request )
0 commit comments