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

[Internal] Per Partition Automatic Failover: Fixes Metadata Requests Retry Policy #4205

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
eb7b0a0
Code changes to retry on next preferred region for metadata reads on …
kundadebdatta Nov 9, 2023
ec125de
Code changes to add retry for PK Ranges call.
kundadebdatta Dec 6, 2023
483cc45
Code changes to mark endpoint unavailable for read when cosmos except…
kundadebdatta Dec 7, 2023
cc4657f
Code changes to fix unit tests. Added global endpoint manager in Pk R…
kundadebdatta Dec 8, 2023
dbee389
Code changes to fix unit tests.
kundadebdatta Dec 8, 2023
505ee41
Code changes to fix build break.
kundadebdatta Dec 8, 2023
77bc01d
Minor code clean-up.
kundadebdatta Dec 8, 2023
c26bbb9
Code changes to capture metadata location endpoint within on before s…
kundadebdatta Dec 19, 2023
416cb6e
Code changes to address review comments.
kundadebdatta Dec 19, 2023
ba31430
Code changes to fix build failure.
kundadebdatta Dec 19, 2023
dab70d0
Code changes to refactor metadata timeout policy.
kundadebdatta Dec 20, 2023
2e4cfc7
Code changes to add retry for request timeout. Fix emulator tests.
kundadebdatta Dec 20, 2023
697f9be
Code changes to add metadata retry policy unit tests.
kundadebdatta Dec 21, 2023
bcb2222
Code changes to add more tests.
kundadebdatta Dec 21, 2023
621bd64
Merge branch 'master' into users/kundadebdatta/4181_retry_metadata_re…
kundadebdatta Dec 21, 2023
0204173
Code changes to refactor metadata retry policy logic to increment loc…
kundadebdatta Dec 22, 2023
6724c77
Merge branch 'master' into users/kundadebdatta/4181_retry_metadata_re…
kundadebdatta Dec 22, 2023
b507ed2
Code changes to address review comments.
kundadebdatta Dec 22, 2023
2f427e3
Code changes to address review comments.
kundadebdatta Dec 29, 2023
a20af65
Code changes to add separate condition for pk range requests.
kundadebdatta Dec 29, 2023
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
22 changes: 10 additions & 12 deletions Microsoft.Azure.Cosmos/src/MetadataRequestThrottleRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
internal sealed class MetadataRequestThrottleRetryPolicy : IDocumentClientRetryPolicy
{
/// <summary>
/// A callback delegate to fetch the location endpoint at a later point of time.
/// </summary>
private readonly Func<Uri> locationEndpointCallbackUri;

/// <summary>
/// An instance of <see cref="GlobalEndpointManager"/>.
/// </summary>
Expand All @@ -32,21 +27,24 @@ internal sealed class MetadataRequestThrottleRetryPolicy : IDocumentClientRetryP
/// </summary>
private readonly IDocumentClientRetryPolicy throttlingRetryPolicy;

/// <summary>
/// An instance of <see cref="Uri"/> containing the location endpoint where the partition key
/// range http request will be sent over.
/// </summary>
private Uri metadataLocationEndpoint;

/// <summary>
/// The constructor to initialize an instance of <see cref="MetadataRequestThrottleRetryPolicy"/>.
/// </summary>
/// <param name="locationEndpointCallbackUri">A callback delegate to fetch the location endpoint at a later point of time.</param>
/// <param name="endpointManager">An instance of <see cref="GlobalEndpointManager"/></param>
/// <param name="maxRetryAttemptsOnThrottledRequests">An integer defining the maximum number
/// of attempts to retry when requests are throttled.</param>
/// <param name="maxRetryWaitTimeInSeconds">An integer defining the maximum wait time in seconds.</param>
public MetadataRequestThrottleRetryPolicy(
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
Func<Uri> locationEndpointCallbackUri,
GlobalEndpointManager endpointManager,
int maxRetryAttemptsOnThrottledRequests,
int maxRetryWaitTimeInSeconds)
{
this.locationEndpointCallbackUri = locationEndpointCallbackUri;
this.globalEndpointManager = endpointManager;
this.throttlingRetryPolicy = new ResourceThrottleRetryPolicy(
maxRetryAttemptsOnThrottledRequests,
Expand Down Expand Up @@ -105,6 +103,7 @@ public Task<ShouldRetryResult> ShouldRetryAsync(
/// <param name="request">The request being sent to the service.</param>
public void OnBeforeSendRequest(DocumentServiceRequest request)
{
this.metadataLocationEndpoint = this.globalEndpointManager.ResolveServiceEndpoint(request);
}

/// <summary>
Expand All @@ -113,11 +112,10 @@ public void OnBeforeSendRequest(DocumentServiceRequest request)
/// <returns>A boolean flag indicating if the operation was successful.</returns>
private bool MarkEndpointUnavailableForRead()
{
Uri location = this.locationEndpointCallbackUri?.Invoke();
if (location != null)
if (this.metadataLocationEndpoint != null)
{
DefaultTrace.TraceWarning("MetadataRequestThrottleRetryPolicy: Marking the following endpoint unavailable for reads: {0}.", location);
this.globalEndpointManager.MarkEndpointUnavailableForRead(location);
DefaultTrace.TraceWarning("MetadataRequestThrottleRetryPolicy: Marking the following endpoint unavailable for reads: {0}.", this.metadataLocationEndpoint);
this.globalEndpointManager.MarkEndpointUnavailableForRead(this.metadataLocationEndpoint);
return true;
}
else
Expand Down
18 changes: 9 additions & 9 deletions Microsoft.Azure.Cosmos/src/Routing/PartitionKeyRangeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ internal class PartitionKeyRangeCache : IRoutingMapProvider, ICollectionRoutingM
private readonly CollectionCache collectionCache;
private readonly GlobalEndpointManager endpointManager;

private Uri pkRangeLocationEndpoint;

public PartitionKeyRangeCache(
ICosmosAuthorizationTokenProvider authorizationTokenProvider,
IStoreModel storeModel,
Expand Down Expand Up @@ -201,13 +199,14 @@ private async Task<CollectionRoutingMap> GetRoutingMapForCollectionAsync(
}

RetryOptions retryOptions = new RetryOptions();
using (DocumentServiceResponse response = await BackoffRetryUtility<DocumentServiceResponse>.ExecuteAsync(
() => this.ExecutePartitionKeyRangeReadChangeFeedAsync(collectionRid, headers, trace, clientSideRequestStatistics),
new MetadataRequestThrottleRetryPolicy(
locationEndpointCallbackUri: () => this.pkRangeLocationEndpoint,
MetadataRequestThrottleRetryPolicy metadataRetryPolicy = new (
endpointManager: this.endpointManager,
maxRetryAttemptsOnThrottledRequests: retryOptions.MaxRetryAttemptsOnThrottledRequests,
maxRetryWaitTimeInSeconds: retryOptions.MaxRetryWaitTimeInSeconds)))
maxRetryWaitTimeInSeconds: retryOptions.MaxRetryWaitTimeInSeconds);

using (DocumentServiceResponse response = await BackoffRetryUtility<DocumentServiceResponse>.ExecuteAsync(
() => this.ExecutePartitionKeyRangeReadChangeFeedAsync(collectionRid, headers, trace, clientSideRequestStatistics, metadataRetryPolicy),
retryPolicy: metadataRetryPolicy))
{
lastStatusCode = response.StatusCode;
changeFeedNextIfNoneMatch = response.Headers[HttpConstants.HttpHeaders.ETag];
Expand Down Expand Up @@ -254,7 +253,8 @@ private async Task<CollectionRoutingMap> GetRoutingMapForCollectionAsync(
private async Task<DocumentServiceResponse> ExecutePartitionKeyRangeReadChangeFeedAsync(string collectionRid,
INameValueCollection headers,
ITrace trace,
IClientSideRequestStatistics clientSideRequestStatistics)
IClientSideRequestStatistics clientSideRequestStatistics,
IDocumentClientRetryPolicy retryPolicy)
{
using (ITrace childTrace = trace.StartChild("Read PartitionKeyRange Change Feed", TraceComponent.Transport, Tracing.TraceLevel.Info))
{
Expand All @@ -265,7 +265,7 @@ private async Task<DocumentServiceResponse> ExecutePartitionKeyRangeReadChangeFe
AuthorizationTokenType.PrimaryMasterKey,
headers))
{
this.pkRangeLocationEndpoint = this.endpointManager.ResolveServiceEndpoint(request);
retryPolicy?.OnBeforeSendRequest(request);
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
string authorizationToken = null;
try
{
Expand Down
Loading