Skip to content

Commit 56c50f8

Browse files
committed
Code changes to introduce a new aggressive timeout http policy for PPAF.
1 parent 7839eb3 commit 56c50f8

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

Microsoft.Azure.Cosmos/src/GatewayStoreClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Microsoft.Azure.Cosmos
2222

2323
internal class GatewayStoreClient : TransportClient
2424
{
25+
private readonly bool isPartitionLevelFailoverEnabled;
2526
private readonly ICommunicationEventSource eventSource;
2627
protected readonly CosmosHttpClient httpClient;
2728
protected readonly JsonSerializerSettings SerializerSettings;
@@ -31,11 +32,13 @@ internal class GatewayStoreClient : TransportClient
3132
public GatewayStoreClient(
3233
CosmosHttpClient httpClient,
3334
ICommunicationEventSource eventSource,
34-
JsonSerializerSettings serializerSettings = null)
35+
JsonSerializerSettings serializerSettings = null,
36+
bool isPartitionLevelFailoverEnabled = false)
3537
{
3638
this.httpClient = httpClient;
3739
this.SerializerSettings = serializerSettings;
3840
this.eventSource = eventSource;
41+
this.isPartitionLevelFailoverEnabled = isPartitionLevelFailoverEnabled;
3942
}
4043

4144
public async Task<DocumentServiceResponse> InvokeAsync(
@@ -375,7 +378,9 @@ private Task<HttpResponseMessage> InvokeClientAsync(
375378
return this.httpClient.SendHttpAsync(
376379
() => this.PrepareRequestMessageAsync(request, physicalAddress),
377380
resourceType,
378-
HttpTimeoutPolicy.GetTimeoutPolicy(request),
381+
HttpTimeoutPolicy.GetTimeoutPolicy(
382+
request,
383+
this.isPartitionLevelFailoverEnabled),
379384
request.RequestContext.ClientRequestStatistics,
380385
cancellationToken,
381386
request);

Microsoft.Azure.Cosmos/src/GatewayStoreModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public GatewayStoreModel(
5858
this.gatewayStoreClient = new GatewayStoreClient(
5959
httpClient,
6060
this.eventSource,
61-
serializerSettings);
61+
serializerSettings,
62+
isPartitionLevelFailoverEnabled);
6263

6364
this.globalPartitionEndpointManager.SetBackgroundConnectionPeriodicRefreshTask(
6465
this.MarkEndpointsToHealthyAsync);

Microsoft.Azure.Cosmos/src/HttpClient/HttpTimeoutPolicy.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ internal abstract class HttpTimeoutPolicy
2020
public virtual bool ShouldThrow503OnTimeout => false;
2121

2222
public static HttpTimeoutPolicy GetTimeoutPolicy(
23-
DocumentServiceRequest documentServiceRequest)
23+
DocumentServiceRequest documentServiceRequest,
24+
bool isPartitionLevelFailoverEnabled = false)
2425
{
2526
//Query Plan Requests
2627
if (documentServiceRequest.ResourceType == ResourceType.Document
@@ -45,7 +46,9 @@ public static HttpTimeoutPolicy GetTimeoutPolicy(
4546
//Data Plane Read
4647
if (!HttpTimeoutPolicy.IsMetaData(documentServiceRequest) && documentServiceRequest.IsReadOnlyRequest)
4748
{
48-
return HttpTimeoutPolicyDefault.InstanceShouldThrow503OnTimeout;
49+
return isPartitionLevelFailoverEnabled
50+
? HttpTimeoutPolicyForPartitionFailover.InstanceShouldThrow503OnTimeout
51+
: HttpTimeoutPolicyDefault.InstanceShouldThrow503OnTimeout;
4952
}
5053

5154
//Meta Data Read
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
//------------------------------------------------------------
4+
namespace Microsoft.Azure.Cosmos
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Net.Http;
9+
10+
internal sealed class HttpTimeoutPolicyForPartitionFailover : HttpTimeoutPolicy
11+
{
12+
public static readonly HttpTimeoutPolicy Instance = new HttpTimeoutPolicyForPartitionFailover(false);
13+
public static readonly HttpTimeoutPolicy InstanceShouldThrow503OnTimeout = new HttpTimeoutPolicyForPartitionFailover(true);
14+
public bool shouldThrow503OnTimeout;
15+
private static readonly string Name = nameof(HttpTimeoutPolicyDefault);
16+
17+
private HttpTimeoutPolicyForPartitionFailover(bool shouldThrow503OnTimeout)
18+
{
19+
this.shouldThrow503OnTimeout = shouldThrow503OnTimeout;
20+
}
21+
22+
private readonly IReadOnlyList<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> TimeoutsAndDelays = new List<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)>()
23+
{
24+
(TimeSpan.FromSeconds(.5), TimeSpan.Zero),
25+
(TimeSpan.FromSeconds(.5), TimeSpan.Zero),
26+
(TimeSpan.FromSeconds(1), TimeSpan.Zero),
27+
};
28+
29+
public override string TimeoutPolicyName => HttpTimeoutPolicyForPartitionFailover.Name;
30+
31+
public override int TotalRetryCount => this.TimeoutsAndDelays.Count;
32+
33+
public override IEnumerator<(TimeSpan requestTimeout, TimeSpan delayForNextRequest)> GetTimeoutEnumerator()
34+
{
35+
return this.TimeoutsAndDelays.GetEnumerator();
36+
}
37+
38+
// Assume that it is not safe to retry unless it is a get method.
39+
// Create and other operations could have succeeded even though a timeout occurred.
40+
public override bool IsSafeToRetry(HttpMethod httpMethod)
41+
{
42+
return httpMethod == HttpMethod.Get;
43+
}
44+
45+
public override bool ShouldRetryBasedOnResponse(HttpMethod requestHttpMethod, HttpResponseMessage responseMessage)
46+
{
47+
return false;
48+
}
49+
50+
public override bool ShouldThrow503OnTimeout => this.shouldThrow503OnTimeout;
51+
}
52+
}

0 commit comments

Comments
 (0)