Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Availability: Fixes retry behavior on HttpException #2077

Merged
merged 4 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions Microsoft.Azure.Cosmos/src/ClientRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public async Task<ShouldRetryResult> ShouldRetryAsync(

this.retryContext = null;
// Received Connection error (HttpRequestException), initiate the endpoint rediscovery
if (exception is HttpRequestException httpException)
if (exception is HttpRequestException _)
{
DefaultTrace.TraceWarning("Endpoint not reachable. Refresh cache and retry");
return await this.ShouldRetryOnEndpointFailureAsync(this.isReadRequest, false);
return await this.ShouldRetryOnEndpointFailureAsync(
isReadRequest: this.isReadRequest,
forceRefresh: false,
retryOnPreferredLocations: true);
}

DocumentClientException clientException = exception as DocumentClientException;
Expand Down Expand Up @@ -146,7 +149,7 @@ public void OnBeforeSendRequest(DocumentServiceRequest request)
if (this.retryContext != null)
{
// set location-based routing directive based on request retry context
request.RequestContext.RouteToLocation(this.retryContext.RetryCount, this.retryContext.RetryRequestOnPreferredLocations);
request.RequestContext.RouteToLocation(this.retryContext.RetryLocationIndex, this.retryContext.RetryRequestOnPreferredLocations);
}

// Resolve the endpoint for the request and pin the resolution to the resolved endpoint
Expand All @@ -171,7 +174,10 @@ private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
&& subStatusCode == SubStatusCodes.WriteForbidden)
{
DefaultTrace.TraceWarning("Endpoint not writable. Refresh cache and retry");
return await this.ShouldRetryOnEndpointFailureAsync(false, true);
return await this.ShouldRetryOnEndpointFailureAsync(
isReadRequest: false,
forceRefresh: true,
retryOnPreferredLocations: false);
}

// Regional endpoint is not available yet for reads (e.g. add/ online of region is in progress)
Expand All @@ -180,7 +186,10 @@ private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
&& (this.isReadRequest || this.canUseMultipleWriteLocations))
{
DefaultTrace.TraceWarning("Endpoint not available for reads. Refresh cache and retry");
return await this.ShouldRetryOnEndpointFailureAsync(true, false);
return await this.ShouldRetryOnEndpointFailureAsync(
isReadRequest: this.isReadRequest,
forceRefresh: false,
retryOnPreferredLocations: false);
}

if (statusCode == HttpStatusCode.NotFound
Expand All @@ -199,7 +208,10 @@ private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
return null;
}

private async Task<ShouldRetryResult> ShouldRetryOnEndpointFailureAsync(bool isReadRequest, bool forceRefresh)
private async Task<ShouldRetryResult> ShouldRetryOnEndpointFailureAsync(
bool isReadRequest,
bool forceRefresh,
bool retryOnPreferredLocations)
{
if (!this.enableEndpointDiscovery || this.failoverRetryCount > MaxRetryCount)
{
Expand Down Expand Up @@ -239,10 +251,16 @@ private async Task<ShouldRetryResult> ShouldRetryOnEndpointFailureAsync(bool isR

await this.globalEndpointManager.RefreshLocationAsync(null, forceRefresh);

int retryLocationIndex = this.failoverRetryCount; // Used to generate a round-robin effect
if (retryOnPreferredLocations)
{
retryLocationIndex = 0; // When the endpoint is marked as unavailable, it is moved to the bottom of the preferrence list
}

this.retryContext = new RetryContext
{
RetryCount = this.failoverRetryCount,
RetryRequestOnPreferredLocations = false
RetryLocationIndex = retryLocationIndex,
RetryRequestOnPreferredLocations = retryOnPreferredLocations,
};

return ShouldRetryResult.RetryAfter(retryDelay);
Expand Down Expand Up @@ -273,7 +291,7 @@ private ShouldRetryResult ShouldRetryOnSessionNotAvailable()
{
this.retryContext = new RetryContext()
{
RetryCount = this.sessionTokenRetryCount - 1,
RetryLocationIndex = this.sessionTokenRetryCount - 1,
RetryRequestOnPreferredLocations = this.sessionTokenRetryCount > 1
};

Expand All @@ -292,7 +310,7 @@ private ShouldRetryResult ShouldRetryOnSessionNotAvailable()
{
this.retryContext = new RetryContext
{
RetryCount = this.sessionTokenRetryCount - 1,
RetryLocationIndex = this.sessionTokenRetryCount - 1,
RetryRequestOnPreferredLocations = false
};

Expand Down Expand Up @@ -336,7 +354,7 @@ private ShouldRetryResult ShouldRetryOnServiceUnavailable()
// RetryCount is used as zero-based index
this.retryContext = new RetryContext()
{
RetryCount = this.serviceUnavailableRetryCount,
RetryLocationIndex = this.serviceUnavailableRetryCount,
RetryRequestOnPreferredLocations = true
};

Expand All @@ -345,7 +363,7 @@ private ShouldRetryResult ShouldRetryOnServiceUnavailable()

private sealed class RetryContext
{
public int RetryCount { get; set; }
public int RetryLocationIndex { get; set; }
public bool RetryRequestOnPreferredLocations { get; set; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task EndpointFailureMockTest()
Assert.AreEqual(globalEndpointManager.ReadEndpoints[0], globalEndpointManager.WriteEndpoints[0]);

//Sleep a second for the unavailable endpoint entry to expire and background refresh timer to kick in
Thread.Sleep(2000);
Thread.Sleep(3000);
await globalEndpointManager.RefreshLocationAsync(null);
Assert.AreEqual(globalEndpointManager.ReadEndpoints[0], new Uri(readLocation1.Endpoint));
}
Expand Down
Loading