-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathThinClientStoreClient.cs
More file actions
185 lines (163 loc) · 7.71 KB
/
ThinClientStoreClient.cs
File metadata and controls
185 lines (163 loc) · 7.71 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net.Http;
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;
using static Microsoft.Azure.Cosmos.ThinClientTransportSerializer;
/// <summary>
/// A TransportClient that sends requests to proxy endpoint.
/// And then processes the response back into DocumentServiceResponse objects.
/// </summary>
internal class ThinClientStoreClient : GatewayStoreClient
{
private readonly bool isPartitionLevelFailoverEnabled;
private readonly ObjectPool<BufferProviderWrapper> bufferProviderWrapperPool;
public ThinClientStoreClient(
CosmosHttpClient httpClient,
ICommunicationEventSource eventSource,
JsonSerializerSettings serializerSettings = null,
bool isPartitionLevelFailoverEnabled = false)
: base(httpClient,
eventSource,
serializerSettings,
isPartitionLevelFailoverEnabled)
{
this.bufferProviderWrapperPool = new ObjectPool<BufferProviderWrapper>(() => new BufferProviderWrapper());
this.isPartitionLevelFailoverEnabled = isPartitionLevelFailoverEnabled;
}
public override async Task<DocumentServiceResponse> InvokeAsync(
DocumentServiceRequest request,
ResourceType resourceType,
Uri physicalAddress,
Uri thinClientEndpoint,
string globalDatabaseAccountName,
ClientCollectionCache clientCollectionCache,
CancellationToken cancellationToken)
{
using (HttpResponseMessage responseMessage = await this.InvokeClientAsync(
request,
resourceType,
physicalAddress,
thinClientEndpoint,
globalDatabaseAccountName,
clientCollectionCache,
cancellationToken))
{
HttpResponseMessage proxyResponse = await ThinClientTransportSerializer.ConvertProxyResponseAsync(responseMessage);
return await ThinClientStoreClient.ParseResponseAsync(proxyResponse, request.SerializerSettings ?? base.SerializerSettings, request);
}
}
internal override async Task<StoreResponse> InvokeStoreAsync(Uri baseAddress, ResourceOperation resourceOperation, DocumentServiceRequest request)
{
Uri physicalAddress = ThinClientStoreClient.IsFeedRequest(request.OperationType) ?
HttpTransportClient.GetResourceFeedUri(resourceOperation.resourceType, baseAddress, request) :
HttpTransportClient.GetResourceEntryUri(resourceOperation.resourceType, baseAddress, request);
using (HttpResponseMessage responseMessage = await this.InvokeClientAsync(
request,
resourceOperation.resourceType,
physicalAddress,
default,
default,
default,
default))
{
return await HttpTransportClient.ProcessHttpResponse(request.ResourceAddress, string.Empty, responseMessage, physicalAddress, request);
}
}
private async ValueTask<HttpRequestMessage> PrepareRequestForProxyAsync(
DocumentServiceRequest request,
Uri physicalAddress,
Uri thinClientEndpoint,
string globalDatabaseAccountName,
ClientCollectionCache clientCollectionCache)
{
HttpRequestMessage requestMessage = base.PrepareRequestMessageAsync(request, physicalAddress).Result;
requestMessage.Version = new Version(2, 0);
BufferProviderWrapper bufferProviderWrapper = this.bufferProviderWrapperPool.Get();
try
{
PartitionKeyRange partitionKeyRange = request.RequestContext?.ResolvedPartitionKeyRange;
if (partitionKeyRange != null)
{
requestMessage.Headers.TryAddWithoutValidation(
ThinClientConstants.ProxyStartEpk,
partitionKeyRange?.MinInclusive);
requestMessage.Headers.TryAddWithoutValidation(
ThinClientConstants.ProxyEndEpk,
partitionKeyRange?.MaxExclusive);
}
requestMessage.Headers.TryAddWithoutValidation(
ThinClientConstants.ProxyOperationType,
request.OperationType.ToOperationTypeString());
requestMessage.Headers.TryAddWithoutValidation(
ThinClientConstants.ProxyResourceType,
request.ResourceType.ToResourceTypeString());
Stream contentStream = await ThinClientTransportSerializer.SerializeProxyRequestAsync(
bufferProviderWrapper,
globalDatabaseAccountName,
clientCollectionCache,
requestMessage);
if (!contentStream.CanSeek)
{
throw new InvalidOperationException(
$"The serializer returned a non-seekable stream ({contentStream.GetType().FullName}).");
}
requestMessage.Content = new StreamContent(contentStream);
requestMessage.Content.Headers.ContentLength = contentStream.Length;
requestMessage.RequestUri = thinClientEndpoint;
requestMessage.Method = HttpMethod.Post;
return requestMessage;
}
finally
{
this.bufferProviderWrapperPool.Return(bufferProviderWrapper);
}
}
private Task<HttpResponseMessage> InvokeClientAsync(
DocumentServiceRequest request,
ResourceType resourceType,
Uri physicalAddress,
Uri thinClientEndpoint,
string globalDatabaseAccountName,
ClientCollectionCache clientCollectionCache,
CancellationToken cancellationToken)
{
DefaultTrace.TraceInformation("In {0}, OperationType: {1}, ResourceType: {2}", nameof(ThinClientStoreClient), request.OperationType, request.ResourceType);
return base.httpClient.SendHttpAsync(
() => this.PrepareRequestForProxyAsync(request, physicalAddress, thinClientEndpoint, globalDatabaseAccountName, clientCollectionCache),
resourceType,
HttpTimeoutPolicy.GetTimeoutPolicy(request, isThinClientEnabled: true),
request.RequestContext.ClientRequestStatistics,
cancellationToken,
request);
}
internal class ObjectPool<T>
{
private readonly ConcurrentBag<T> Objects;
private readonly Func<T> ObjectGenerator;
public ObjectPool(Func<T> objectGenerator)
{
this.ObjectGenerator = objectGenerator ?? throw new ArgumentNullException(nameof(objectGenerator));
this.Objects = new ConcurrentBag<T>();
}
public T Get()
{
return this.Objects.TryTake(out T item) ? item : this.ObjectGenerator();
}
public void Return(T item)
{
this.Objects.Add(item);
}
}
}
}