Skip to content

Commit 8b166fc

Browse files
Merge pull request Azure#2 from tvaron3/tvaron3/readTimeout
Fixed the timeout logic
2 parents 9a234f8 + 8859c9f commit 8b166fc

File tree

1 file changed

+5
-34
lines changed

1 file changed

+5
-34
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/_timeout_failover_retry_policy.py

+5-34
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def __init__(self, connection_policy, global_endpoint_manager, *args):
1818

1919
self.global_endpoint_manager = global_endpoint_manager
2020
self.retry_count = 0
21-
self.location_index = 0
2221
self.connection_policy = connection_policy
2322
self.request = args[0] if args else None
2423

@@ -29,14 +28,13 @@ def ShouldRetry(self, _exception):
2928
:returns: a boolean stating whether the request should be retried
3029
:rtype: bool
3130
"""
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
3534

3635
if not self.connection_policy.EnableEndpointDiscovery:
3736
return False
3837

39-
4038
# Check if the next retry about to be done is safe
4139
if _exception.status_code == http_constants.StatusCodes.SERVICE_UNAVAILABLE and \
4240
self.retry_count >= self._max_service_unavailable_retry_count:
@@ -47,46 +45,19 @@ def ShouldRetry(self, _exception):
4745
return False
4846

4947
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()
6749
self.request.route_to_location(location_endpoint)
6850
return True
6951

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-
8052
# This function prepares the request to go to the next region
8153
def resolve_next_region_service_endpoint(self):
82-
self.location_index += 1
8354
# clear previous location-based routing directive
8455
self.request.clear_route_to_location()
8556
# clear the last routed endpoint within same region since we are going to a new region now
8657
self.request.last_routed_location_endpoint_within_region = None
8758
# set location-based routing directive based on retry count
8859
# 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)
9061
# Resolve the endpoint for the request and pin the resolution to the resolved endpoint
9162
# This enables marking the endpoint unavailability on endpoint failover/unreachability
9263
return self.global_endpoint_manager.resolve_service_endpoint(self.request)

0 commit comments

Comments
 (0)