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

ClientRetryPolicy: Fixes behavior to Meta-data write operations in multimaster accounts #3466

Merged
merged 14 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 18 additions & 3 deletions Microsoft.Azure.Cosmos/src/ClientRetryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ private async Task<ShouldRetryResult> ShouldRetryInternalAsync(
this.documentServiceRequest?.RequestContext?.LocationEndpointToRoute?.ToString() ?? string.Empty,
this.documentServiceRequest?.ResourceAddress ?? string.Empty);

if (this.globalEndpointManager.IsMetadataWriteRequestMultimaster(this.documentServiceRequest))
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
{
Task<ShouldRetryResult> retryResult = this.ShouldRetryOnEndpointFailureAsync(
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
isReadRequest: false,
markBothReadAndWriteAsUnavailable: false,
forceRefresh: true,
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
retryOnPreferredLocations: false,
overwriteEndpointDiscovery: true);

this.retryContext.RetryLocationIndex = 0;
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved

return await retryResult;
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
}

return await this.ShouldRetryOnEndpointFailureAsync(
isReadRequest: false,
markBothReadAndWriteAsUnavailable: false,
Expand Down Expand Up @@ -237,9 +251,10 @@ private async Task<ShouldRetryResult> ShouldRetryOnEndpointFailureAsync(
bool isReadRequest,
bool markBothReadAndWriteAsUnavailable,
bool forceRefresh,
bool retryOnPreferredLocations)
bool retryOnPreferredLocations,
bool overwriteEndpointDiscovery = false)
{
if (!this.enableEndpointDiscovery || this.failoverRetryCount > MaxRetryCount)
if (this.failoverRetryCount > MaxRetryCount || (!this.enableEndpointDiscovery && !overwriteEndpointDiscovery))
{
DefaultTrace.TraceInformation("ClientRetryPolicy: ShouldRetryOnEndpointFailureAsync() Not retrying. Retry count = {0}, Endpoint = {1}",
this.failoverRetryCount,
Expand All @@ -249,7 +264,7 @@ private async Task<ShouldRetryResult> ShouldRetryOnEndpointFailureAsync(

this.failoverRetryCount++;

if (this.locationEndpoint != null)
if (this.locationEndpoint != null && !overwriteEndpointDiscovery)
{
if (isReadRequest || markBothReadAndWriteAsUnavailable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public GlobalEndpointManager(IDocumentClientInternal owner, ConnectionPolicy con

public int PreferredLocationCount => this.connectionPolicy.PreferredLocations != null ? this.connectionPolicy.PreferredLocations.Count : 0;

public bool IsMetadataWriteRequestMultimaster(DocumentServiceRequest request)
{
return this.locationCache.IsMetadataWriteRequestOnMultimasterAccount(request);
}

/// <summary>
/// This will get the account information.
/// It will try the global endpoint first.
Expand Down Expand Up @@ -541,7 +546,6 @@ private async Task RefreshDatabaseAccountInternalAsync(bool forceRefresh)
}
}
}

internal async Task<AccountProperties> GetDatabaseAccountAsync(bool forceRefresh = false)
{
#nullable disable // Needed because AsyncCache does not have nullable enabled
Expand Down
12 changes: 10 additions & 2 deletions Microsoft.Azure.Cosmos/src/Routing/LocationCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public LocationCache(
#endif
#endif
}

/// <summary>
/// Gets total availible write locations from the current location information for a Cosmos account
/// </summary>
public int TotalAvailableWriteLocations => this.locationInfo.AvailableWriteLocations.Count;
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Gets list of read endpoints ordered by
/// 1. Preferred location
Expand Down Expand Up @@ -206,6 +209,11 @@ public void OnLocationPreferenceChanged(ReadOnlyCollection<string> preferredLoca
preferenceList: preferredLocations);
}

public bool IsMetadataWriteRequestOnMultimasterAccount(DocumentServiceRequest request)
{
return !request.IsReadOnlyRequest && this.TotalAvailableWriteLocations > 1 && !this.CanUseMultipleWriteLocations(request);
NaluTripician marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Resolves request to service endpoint.
/// 1. If this is a write request
Expand Down Expand Up @@ -243,7 +251,7 @@ public Uri ResolveServiceEndpoint(DocumentServiceRequest request)
// first and the second writable region in DatabaseAccount (for manual failover)
DatabaseAccountLocationsInfo currentLocationInfo = this.locationInfo;

if (this.enableEndpointDiscovery && currentLocationInfo.AvailableWriteLocations.Count > 0)
if ((this.enableEndpointDiscovery && currentLocationInfo.AvailableWriteLocations.Count > 0) || this.IsMetadataWriteRequestOnMultimasterAccount(request))
{
locationIndex = Math.Min(locationIndex % 2, currentLocationInfo.AvailableWriteLocations.Count - 1);
string writeLocation = currentLocationInfo.AvailableWriteLocations[locationIndex];
Expand Down