Skip to content

Commit e8bc02e

Browse files
Merge pull request Azure#3 from tvaron3/readtimeout
Fixed the timeout retry policy
2 parents 8b166fc + ac78da9 commit e8bc02e

File tree

5 files changed

+13
-15
lines changed

5 files changed

+13
-15
lines changed

sdk/cosmos/azure-cosmos/azure/cosmos/_retry_utility.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def Execute(client, global_endpoint_manager, function, *args, **kwargs):
131131
sub_status_code=SubStatusCodes.THROUGHPUT_OFFER_NOT_FOUND)
132132
return result
133133
except exceptions.CosmosHttpResponseError as e:
134-
retry_policy = defaultRetry_policy
135134
if request and _has_database_account_header(request.headers):
136135
retry_policy = database_account_retry_policy
137136
# Re-assign retry policy based on error code
@@ -173,8 +172,12 @@ def Execute(client, global_endpoint_manager, function, *args, **kwargs):
173172

174173
retry_policy.container_rid = cached_container["_rid"]
175174
request.headers[retry_policy._intended_headers] = retry_policy.container_rid
176-
elif e.status_code in [StatusCodes.REQUEST_TIMEOUT, StatusCodes.SERVICE_UNAVAILABLE]:
175+
elif e.status_code == StatusCodes.REQUEST_TIMEOUT:
177176
retry_policy = timeout_failover_retry_policy
177+
elif e.status_code >= StatusCodes.INTERNAL_SERVER_ERROR:
178+
retry_policy = timeout_failover_retry_policy
179+
else:
180+
retry_policy = defaultRetry_policy
178181

179182
# If none of the retry policies applies or there is no retry needed, set the
180183
# throttle related response headers and re-throw the exception back arg[0]

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
Cosmos database service.
66
"""
77
from azure.cosmos.documents import _OperationType
8-
from . import http_constants
98

109

1110
class _TimeoutFailoverRetryPolicy(object):
1211

1312
def __init__(self, connection_policy, global_endpoint_manager, *args):
14-
self._max_retry_attempt_count = 120
15-
self._max_service_unavailable_retry_count = 1
16-
self.retry_after_in_milliseconds = 0
13+
self.retry_after_in_milliseconds = 500
1714
self.args = args
1815

1916
self.global_endpoint_manager = global_endpoint_manager
17+
# If an account only has 1 region, then we still want to retry once on the same region
18+
self._max_retry_attempt_count = len(self.global_endpoint_manager.location_cache.read_regional_endpoints) + 1
2019
self.retry_count = 0
2120
self.connection_policy = connection_policy
2221
self.request = args[0] if args else None
@@ -28,17 +27,13 @@ def ShouldRetry(self, _exception):
2827
:returns: a boolean stating whether the request should be retried
2928
:rtype: bool
3029
"""
31-
# we don't retry on write operations for timeouts or service unavailable
30+
# we don't retry on write operations for timeouts or any internal server errors
3231
if self.request and (not _OperationType.IsReadOnlyOperation(self.request.operation_type)):
3332
return False
3433

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

38-
# Check if the next retry about to be done is safe
39-
if _exception.status_code == http_constants.StatusCodes.SERVICE_UNAVAILABLE and \
40-
self.retry_count >= self._max_service_unavailable_retry_count:
41-
return False
4237
self.retry_count += 1
4338
# Check if the next retry about to be done is safe
4439
if self.retry_count >= self._max_retry_attempt_count:

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_retry_utility_async.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ async def ExecuteAsync(client, global_endpoint_manager, function, *args, **kwarg
130130

131131
return result
132132
except exceptions.CosmosHttpResponseError as e:
133-
retry_policy = None
134133
if request and _has_database_account_header(request.headers):
135134
retry_policy = database_account_retry_policy
136135
elif e.status_code == StatusCodes.FORBIDDEN and e.sub_status in \
@@ -171,7 +170,9 @@ async def ExecuteAsync(client, global_endpoint_manager, function, *args, **kwarg
171170

172171
retry_policy.container_rid = cached_container["_rid"]
173172
request.headers[retry_policy._intended_headers] = retry_policy.container_rid
174-
elif e.status_code in [StatusCodes.REQUEST_TIMEOUT, StatusCodes.SERVICE_UNAVAILABLE]:
173+
elif e.status_code == StatusCodes.REQUEST_TIMEOUT:
174+
retry_policy = timeout_failover_retry_policy
175+
elif e.status_code >= StatusCodes.INTERNAL_SERVER_ERROR:
175176
retry_policy = timeout_failover_retry_policy
176177
else:
177178
retry_policy = defaultRetry_policy

sdk/cosmos/azure-cosmos/azure/cosmos/http_constants.py

-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ class StatusCodes:
400400
RETRY_WITH = 449
401401

402402
INTERNAL_SERVER_ERROR = 500
403-
SERVICE_UNAVAILABLE = 503
404403

405404
# Operation pause and cancel. These are FAKE status codes for QOS logging purpose only.
406405
OPERATION_PAUSED = 1200

sdk/cosmos/azure-cosmos/test/test_globaldb_mock.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def MockExecuteFunction(self, function, *args, **kwargs):
166166

167167
def MockGetDatabaseAccountStub(self, endpoint):
168168
raise exceptions.CosmosHttpResponseError(
169-
status_code=StatusCodes.SERVICE_UNAVAILABLE, message="Service unavailable")
169+
status_code=StatusCodes.INTERNAL_SERVER_ERROR, message="Internal Server Error")
170170

171171
def test_global_db_endpoint_discovery_retry_policy(self):
172172
connection_policy = documents.ConnectionPolicy()

0 commit comments

Comments
 (0)