-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathThinClientStoreModel.cs
More file actions
191 lines (173 loc) · 8.18 KB
/
ThinClientStoreModel.cs
File metadata and controls
191 lines (173 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Documents;
using Newtonsoft.Json;
/// <summary>
/// An IStoreModelExtension implementation that routes operations through the ThinClient proxy.
/// It applies session tokens, resolves partition key ranges, and delegates requests to ThinClientStoreClient.
/// </summary>
internal class ThinClientStoreModel : GatewayStoreModel
{
private readonly GlobalPartitionEndpointManager globalPartitionEndpointManager;
private readonly bool isPartitionLevelFailoverEnabled;
private ThinClientStoreClient thinClientStoreClient;
public ThinClientStoreModel(
GlobalEndpointManager endpointManager,
GlobalPartitionEndpointManager globalPartitionEndpointManager,
ISessionContainer sessionContainer,
ConsistencyLevel defaultConsistencyLevel,
DocumentClientEventSource eventSource,
JsonSerializerSettings serializerSettings,
CosmosHttpClient httpClient,
bool isPartitionLevelFailoverEnabled = false)
: base(endpointManager,
sessionContainer,
defaultConsistencyLevel,
eventSource,
serializerSettings,
httpClient,
globalPartitionEndpointManager,
isPartitionLevelFailoverEnabled)
{
this.thinClientStoreClient = new ThinClientStoreClient(
httpClient,
eventSource,
serializerSettings,
isPartitionLevelFailoverEnabled);
this.isPartitionLevelFailoverEnabled = isPartitionLevelFailoverEnabled;
this.globalPartitionEndpointManager = globalPartitionEndpointManager;
this.globalPartitionEndpointManager.SetBackgroundConnectionPeriodicRefreshTask(
base.MarkEndpointsToHealthyAsync);
}
public override async Task<DocumentServiceResponse> ProcessMessageAsync(
DocumentServiceRequest request,
CancellationToken cancellationToken = default)
{
if (!ThinClientStoreModel.IsOperationSupportedByThinClient(request))
{
return await base.ProcessMessageAsync(request, cancellationToken);
}
await GatewayStoreModel.ApplySessionTokenAsync(
request,
base.defaultConsistencyLevel,
base.sessionContainer,
base.partitionKeyRangeCache,
base.clientCollectionCache,
base.endpointManager);
DocumentServiceResponse response;
try
{
if (request.ResourceType.Equals(ResourceType.Document) && base.endpointManager.TryGetLocationForGatewayDiagnostics(
request.RequestContext.LocationEndpointToRoute,
out string regionName))
{
request.RequestContext.RegionName = regionName;
}
// This is applicable for both per partition automatic failover and per partition circuit breaker.
if (this.isPartitionLevelFailoverEnabled
&& !ReplicatedResourceClient.IsMasterResource(request.ResourceType)
&& request.ResourceType.IsPartitioned())
{
(bool isSuccess, PartitionKeyRange partitionKeyRange) = await GatewayStoreModel.TryResolvePartitionKeyRangeAsync(
request: request,
sessionContainer: this.sessionContainer,
partitionKeyRangeCache: this.partitionKeyRangeCache,
clientCollectionCache: this.clientCollectionCache,
refreshCache: false);
request.RequestContext.ResolvedPartitionKeyRange = partitionKeyRange;
this.globalPartitionEndpointManager.TryAddPartitionLevelLocationOverride(request);
}
Uri physicalAddress = ThinClientStoreClient.IsFeedRequest(request.OperationType) ? base.GetFeedUri(request) : base.GetEntityUri(request);
AccountProperties properties = await this.GetDatabaseAccountPropertiesAsync();
response = await this.thinClientStoreClient.InvokeAsync(
request,
request.ResourceType,
physicalAddress,
this.endpointManager.ResolveThinClientEndpoint(request),
properties.Id,
base.clientCollectionCache,
cancellationToken);
}
catch (DocumentClientException exception)
{
if ((!ReplicatedResourceClient.IsMasterResource(request.ResourceType)) &&
(exception.StatusCode == HttpStatusCode.PreconditionFailed || exception.StatusCode == HttpStatusCode.Conflict
|| (exception.StatusCode == HttpStatusCode.NotFound && exception.GetSubStatus() != SubStatusCodes.ReadSessionNotAvailable)))
{
await base.CaptureSessionTokenAndHandleSplitAsync(
exception.StatusCode,
exception.GetSubStatus(),
request,
exception.Headers);
}
throw;
}
await this.CaptureSessionTokenAndHandleSplitAsync(
response.StatusCode,
response.SubStatusCode,
request,
response.Headers);
return response;
}
public static bool IsOperationSupportedByThinClient(
DocumentServiceRequest request)
{
// Thin proxy supports the following operations for Document resources.
return request.ResourceType == ResourceType.Document
&& (request.OperationType == OperationType.Batch
|| request.OperationType == OperationType.Patch
|| request.OperationType == OperationType.Create
|| request.OperationType == OperationType.Read
|| request.OperationType == OperationType.Upsert
|| request.OperationType == OperationType.Replace
|| request.OperationType == OperationType.Delete
|| request.OperationType == OperationType.Query);
}
private async Task<AccountProperties> GetDatabaseAccountPropertiesAsync()
{
try
{
AccountProperties accountProperties = await this.endpointManager.GetDatabaseAccountAsync();
if (accountProperties != null)
{
return accountProperties;
}
throw new InvalidOperationException("Failed to retrieve AccountProperties. The response was null.");
}
catch (Exception ex)
{
DefaultTrace.TraceError("Exception while retrieving database account information: {0}", ex.Message);
throw;
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (this.thinClientStoreClient != null)
{
try
{
this.thinClientStoreClient.Dispose();
}
catch (Exception exception)
{
DefaultTrace.TraceWarning("Exception {0} thrown during dispose of HttpClient, this could happen if there are inflight request during the dispose of client",
exception.Message);
}
this.thinClientStoreClient = null;
}
}
base.Dispose(disposing);
}
}
}