-
Notifications
You must be signed in to change notification settings - Fork 494
/
CosmosQueryClientCore.cs
500 lines (448 loc) · 22.2 KB
/
CosmosQueryClientCore.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.CosmosElements;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.Azure.Cosmos.Query.Core.Metrics;
using Microsoft.Azure.Cosmos.Query.Core.Monads;
using Microsoft.Azure.Cosmos.Query.Core.Pipeline.Pagination;
using Microsoft.Azure.Cosmos.Query.Core.QueryClient;
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Routing;
using Newtonsoft.Json;
using static Microsoft.Azure.Documents.RuntimeConstants;
internal class CosmosQueryClientCore : CosmosQueryClient
{
private const string QueryExecutionInfoHeader = "x-ms-cosmos-query-execution-info";
private readonly CosmosClientContext clientContext;
private readonly ContainerInternal cosmosContainerCore;
private readonly DocumentClient documentClient;
private readonly SemaphoreSlim semaphore;
public CosmosQueryClientCore(
CosmosClientContext clientContext,
ContainerInternal cosmosContainerCore)
{
this.clientContext = clientContext ?? throw new ArgumentException(nameof(clientContext));
this.cosmosContainerCore = cosmosContainerCore;
this.documentClient = this.clientContext.DocumentClient;
this.semaphore = new SemaphoreSlim(1, 1);
}
public override Action<IQueryable> OnExecuteScalarQueryCallback => this.documentClient.OnExecuteScalarQueryCallback;
public override async Task<ContainerQueryProperties> GetCachedContainerQueryPropertiesAsync(
string containerLink,
PartitionKey? partitionKey,
ITrace trace,
CancellationToken cancellationToken)
{
ContainerProperties containerProperties = await this.clientContext.GetCachedContainerPropertiesAsync(
containerLink,
trace,
cancellationToken);
string effectivePartitionKeyString = null;
if (partitionKey != null)
{
// Dis-ambiguate the NonePK if used
PartitionKeyInternal partitionKeyInternal = partitionKey.Value.IsNone ? containerProperties.GetNoneValue() : partitionKey.Value.InternalKey;
effectivePartitionKeyString = partitionKeyInternal.GetEffectivePartitionKeyString(containerProperties.PartitionKey);
}
return new ContainerQueryProperties(
containerProperties.ResourceId,
effectivePartitionKeyString,
containerProperties.PartitionKey);
}
public override async Task<TryCatch<PartitionedQueryExecutionInfo>> TryGetPartitionedQueryExecutionInfoAsync(
SqlQuerySpec sqlQuerySpec,
PartitionKeyDefinition partitionKeyDefinition,
bool requireFormattableOrderByQuery,
bool isContinuationExpected,
bool allowNonValueAggregateQuery,
bool hasLogicalPartitionKey,
bool allowDCount,
CancellationToken cancellationToken)
{
return (await this.documentClient.QueryPartitionProvider).TryGetPartitionedQueryExecutionInfo(
querySpec: sqlQuerySpec,
partitionKeyDefinition: partitionKeyDefinition,
requireFormattableOrderByQuery: requireFormattableOrderByQuery,
isContinuationExpected: isContinuationExpected,
allowNonValueAggregateQuery: allowNonValueAggregateQuery,
hasLogicalPartitionKey: hasLogicalPartitionKey,
allowDCount: allowDCount);
}
public override async Task<TryCatch<QueryPage>> ExecuteItemQueryAsync(
string resourceUri,
ResourceType resourceType,
OperationType operationType,
Guid clientQueryCorrelationId,
FeedRange feedRange,
QueryRequestOptions requestOptions,
SqlQuerySpec sqlQuerySpec,
string continuationToken,
bool isContinuationExpected,
int pageSize,
ITrace trace,
CancellationToken cancellationToken)
{
requestOptions.MaxItemCount = pageSize;
ResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
resourceUri: resourceUri,
resourceType: resourceType,
operationType: operationType,
requestOptions: requestOptions,
feedRange: feedRange,
cosmosContainerCore: this.cosmosContainerCore,
streamPayload: this.clientContext.SerializerCore.ToStreamSqlQuerySpec(sqlQuerySpec, resourceType),
requestEnricher: (cosmosRequestMessage) =>
{
cosmosRequestMessage.Headers.Add(
HttpConstants.HttpHeaders.IsContinuationExpected,
isContinuationExpected.ToString());
QueryRequestOptions.FillContinuationToken(
cosmosRequestMessage,
continuationToken);
cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.ContentType, MediaTypes.QueryJson);
cosmosRequestMessage.Headers.Add(HttpConstants.HttpHeaders.IsQuery, bool.TrueString);
},
trace: trace,
cancellationToken: cancellationToken);
return CosmosQueryClientCore.GetCosmosElementResponse(
requestOptions,
resourceType,
message,
trace);
}
public override async Task<PartitionedQueryExecutionInfo> ExecuteQueryPlanRequestAsync(
string resourceUri,
ResourceType resourceType,
OperationType operationType,
SqlQuerySpec sqlQuerySpec,
PartitionKey? partitionKey,
string supportedQueryFeatures,
ITrace trace,
CancellationToken cancellationToken)
{
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo;
using (ResponseMessage message = await this.clientContext.ProcessResourceOperationStreamAsync(
resourceUri: resourceUri,
resourceType: resourceType,
operationType: operationType,
requestOptions: null,
feedRange: partitionKey.HasValue ? new FeedRangePartitionKey(partitionKey.Value) : null,
cosmosContainerCore: this.cosmosContainerCore,
streamPayload: this.clientContext.SerializerCore.ToStreamSqlQuerySpec(sqlQuerySpec, resourceType),
requestEnricher: (requestMessage) =>
{
requestMessage.Headers.Add(HttpConstants.HttpHeaders.ContentType, RuntimeConstants.MediaTypes.QueryJson);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.IsQueryPlanRequest, bool.TrueString);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.SupportedQueryFeatures, supportedQueryFeatures);
requestMessage.Headers.Add(HttpConstants.HttpHeaders.QueryVersion, new Version(major: 1, minor: 0).ToString());
requestMessage.UseGatewayMode = true;
},
trace: trace,
cancellationToken: cancellationToken))
{
// Syntax exception are argument exceptions and thrown to the user.
message.EnsureSuccessStatusCode();
partitionedQueryExecutionInfo = this.clientContext.SerializerCore.FromStream<PartitionedQueryExecutionInfo>(message.Content);
}
return partitionedQueryExecutionInfo;
}
public override Task<List<PartitionKeyRange>> GetTargetPartitionKeyRangesByEpkStringAsync(
string resourceLink,
string collectionResourceId,
string effectivePartitionKeyString,
bool forceRefresh,
ITrace trace)
{
return this.GetTargetPartitionKeyRangesAsync(
resourceLink,
collectionResourceId,
new List<Range<string>>
{
Range<string>.GetPointRange(effectivePartitionKeyString)
},
forceRefresh,
trace);
}
public override async Task<List<PartitionKeyRange>> GetTargetPartitionKeyRangeByFeedRangeAsync(
string resourceLink,
string collectionResourceId,
PartitionKeyDefinition partitionKeyDefinition,
FeedRangeInternal feedRangeInternal,
bool forceRefresh,
ITrace trace)
{
using (ITrace childTrace = trace.StartChild("Get Overlapping Feed Ranges", TraceComponent.Routing, Tracing.TraceLevel.Info))
{
IRoutingMapProvider routingMapProvider = await this.GetRoutingMapProviderAsync();
List<Range<string>> ranges = await feedRangeInternal.GetEffectiveRangesAsync(routingMapProvider, collectionResourceId, partitionKeyDefinition, trace);
return await this.GetTargetPartitionKeyRangesAsync(
resourceLink,
collectionResourceId,
ranges,
forceRefresh,
childTrace);
}
}
public override async Task<List<PartitionKeyRange>> GetTargetPartitionKeyRangesAsync(
string resourceLink,
string collectionResourceId,
List<Range<string>> providedRanges,
bool forceRefresh,
ITrace trace)
{
if (string.IsNullOrEmpty(collectionResourceId))
{
throw new ArgumentNullException(nameof(collectionResourceId));
}
if (providedRanges == null ||
!providedRanges.Any() ||
providedRanges.Any(x => x == null))
{
throw new ArgumentNullException(nameof(providedRanges));
}
using (ITrace getPKRangesTrace = trace.StartChild("Get Partition Key Ranges", TraceComponent.Routing, Tracing.TraceLevel.Info))
{
IRoutingMapProvider routingMapProvider = await this.GetRoutingMapProviderAsync();
List<PartitionKeyRange> ranges = await routingMapProvider.TryGetOverlappingRangesAsync(collectionResourceId, providedRanges, getPKRangesTrace);
if (ranges == null && PathsHelper.IsNameBased(resourceLink))
{
// Refresh the cache and don't try to re-resolve collection as it is not clear what already
// happened based on previously resolved collection rid.
// Return NotFoundException this time. Next query will succeed.
// This can only happen if collection is deleted/created with same name and client was not restarted
// in between.
CollectionCache collectionCache = await this.documentClient.GetCollectionCacheAsync(getPKRangesTrace);
collectionCache.Refresh(resourceLink);
}
if (ranges == null)
{
throw new NotFoundException($"{DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)}: GetTargetPartitionKeyRanges(collectionResourceId:{collectionResourceId}, providedRanges: {string.Join(",", providedRanges)} failed due to stale cache");
}
return ranges;
}
}
public override bool ByPassQueryParsing()
{
return CustomTypeExtensions.ByPassQueryParsing();
}
public override void ClearSessionTokenCache(string collectionFullName)
{
ISessionContainer sessionContainer = this.clientContext.DocumentClient.sessionContainer;
sessionContainer.ClearTokenByCollectionFullname(collectionFullName);
}
private static TryCatch<QueryPage> GetCosmosElementResponse(
QueryRequestOptions requestOptions,
ResourceType resourceType,
ResponseMessage cosmosResponseMessage,
ITrace trace)
{
using (ITrace getCosmosElementResponse = trace.StartChild("Get Cosmos Element Response", TraceComponent.Json, Tracing.TraceLevel.Info))
{
using (cosmosResponseMessage)
{
if (
cosmosResponseMessage.Headers.QueryMetricsText != null &&
BackendMetricsParser.TryParse(cosmosResponseMessage.Headers.QueryMetricsText, out BackendMetrics backendMetrics))
{
QueryMetricsTraceDatum datum = new QueryMetricsTraceDatum(
new QueryMetrics(backendMetrics, IndexUtilizationInfo.Empty, ClientSideMetrics.Empty));
trace.AddDatum("Query Metrics", datum);
}
if (!cosmosResponseMessage.IsSuccessStatusCode)
{
CosmosException exception = cosmosResponseMessage.CosmosException ?? new CosmosException(
cosmosResponseMessage.ErrorMessage,
cosmosResponseMessage.StatusCode,
(int)cosmosResponseMessage.Headers.SubStatusCode,
cosmosResponseMessage.Headers.ActivityId,
cosmosResponseMessage.Headers.RequestCharge);
return TryCatch<QueryPage>.FromException(exception);
}
if (!(cosmosResponseMessage.Content is MemoryStream memoryStream))
{
memoryStream = new MemoryStream();
cosmosResponseMessage.Content.CopyTo(memoryStream);
}
long responseLengthBytes = memoryStream.Length;
CosmosArray documents = CosmosQueryClientCore.ParseElementsFromRestStream(
memoryStream,
resourceType,
requestOptions.CosmosSerializationFormatOptions);
CosmosQueryExecutionInfo cosmosQueryExecutionInfo;
if (cosmosResponseMessage.Headers.TryGetValue(QueryExecutionInfoHeader, out string queryExecutionInfoString))
{
cosmosQueryExecutionInfo = JsonConvert.DeserializeObject<CosmosQueryExecutionInfo>(queryExecutionInfoString);
}
else
{
cosmosQueryExecutionInfo = default;
}
QueryState queryState;
if (cosmosResponseMessage.Headers.ContinuationToken != null)
{
queryState = new QueryState(CosmosString.Create(cosmosResponseMessage.Headers.ContinuationToken));
}
else
{
queryState = default;
}
Dictionary<string, string> additionalHeaders = new Dictionary<string, string>();
foreach (string key in cosmosResponseMessage.Headers)
{
if (!QueryPage.BannedHeaders.Contains(key))
{
additionalHeaders[key] = cosmosResponseMessage.Headers[key];
}
}
QueryPage response = new QueryPage(
documents,
cosmosResponseMessage.Headers.RequestCharge,
cosmosResponseMessage.Headers.ActivityId,
responseLengthBytes,
cosmosQueryExecutionInfo,
disallowContinuationTokenMessage: null,
additionalHeaders,
queryState);
return TryCatch<QueryPage>.FromResult(response);
}
}
}
private void PopulatePartitionKeyRangeInfo(
RequestMessage request,
PartitionKeyRangeIdentity partitionKeyRangeIdentity)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.ResourceType.IsPartitioned())
{
// If the request already has the logical partition key,
// then we shouldn't add the physical partition key range id.
bool hasPartitionKey = request.Headers.PartitionKey != null;
if (!hasPartitionKey)
{
request
.ToDocumentServiceRequest()
.RouteTo(partitionKeyRangeIdentity);
}
}
}
public override async Task ForceRefreshCollectionCacheAsync(string collectionLink, CancellationToken cancellationToken)
{
this.ClearSessionTokenCache(collectionLink);
CollectionCache collectionCache = await this.documentClient.GetCollectionCacheAsync(NoOpTrace.Singleton);
using (Documents.DocumentServiceRequest request = Documents.DocumentServiceRequest.Create(
Documents.OperationType.Query,
Documents.ResourceType.Collection,
collectionLink,
Documents.AuthorizationTokenType.Invalid)) //this request doesn't actually go to server
{
request.ForceNameCacheRefresh = true;
await collectionCache.ResolveCollectionAsync(request, cancellationToken, NoOpTrace.Singleton);
}
}
public override async Task<IReadOnlyList<PartitionKeyRange>> TryGetOverlappingRangesAsync(
string collectionResourceId,
Range<string> range,
bool forceRefresh = false)
{
PartitionKeyRangeCache partitionKeyRangeCache = await this.GetRoutingMapProviderAsync();
return await partitionKeyRangeCache.TryGetOverlappingRangesAsync(
collectionResourceId,
range,
NoOpTrace.Singleton,
forceRefresh);
}
private Task<PartitionKeyRangeCache> GetRoutingMapProviderAsync()
{
return this.documentClient.GetPartitionKeyRangeCacheAsync(NoOpTrace.Singleton);
}
/// <summary>
/// Converts a list of CosmosElements into a memory stream.
/// </summary>
/// <param name="stream">The memory stream response for the query REST response Azure Cosmos</param>
/// <param name="resourceType">The resource type</param>
/// <param name="cosmosSerializationOptions">The custom serialization options. This allows custom serialization types like BSON, JSON, or other formats</param>
/// <returns>An array of CosmosElements parsed from the response body.</returns>
public static CosmosArray ParseElementsFromRestStream(
Stream stream,
ResourceType resourceType,
CosmosSerializationFormatOptions cosmosSerializationOptions)
{
if (!(stream is MemoryStream memoryStream))
{
memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
}
if (!memoryStream.CanRead)
{
throw new InvalidDataException("Stream can not be read");
}
// Parse out the document from the REST response like this:
// {
// "_rid": "qHVdAImeKAQ=",
// "Documents": [{
// "id": "03230",
// "_rid": "qHVdAImeKAQBAAAAAAAAAA==",
// "_self": "dbs\/qHVdAA==\/colls\/qHVdAImeKAQ=\/docs\/qHVdAImeKAQBAAAAAAAAAA==\/",
// "_etag": "\"410000b0-0000-0000-0000-597916b00000\"",
// "_attachments": "attachments\/",
// "_ts": 1501107886
// }],
// "_count": 1
// }
// You want to create a CosmosElement for each document in "Documents".
ReadOnlyMemory<byte> content = memoryStream.TryGetBuffer(out ArraySegment<byte> buffer) ? buffer : (ReadOnlyMemory<byte>)memoryStream.ToArray();
IJsonNavigator jsonNavigator;
if (cosmosSerializationOptions != null)
{
// Use the users custom navigator
jsonNavigator = cosmosSerializationOptions.CreateCustomNavigatorCallback(content);
if (jsonNavigator == null)
{
throw new InvalidOperationException("The CosmosSerializationOptions did not return a JSON navigator.");
}
}
else
{
jsonNavigator = JsonNavigator.Create(content);
}
string resourceName = resourceType switch
{
ResourceType.Collection => "DocumentCollections",
_ => resourceType.ToResourceTypeString() + "s",
};
if (!jsonNavigator.TryGetObjectProperty(
jsonNavigator.GetRootNode(),
resourceName,
out ObjectProperty objectProperty))
{
throw new InvalidOperationException($"Response Body Contract was violated. QueryResponse did not have property: {resourceName}");
}
if (!(CosmosElement.Dispatch(
jsonNavigator,
objectProperty.ValueNode) is CosmosArray cosmosArray))
{
throw new InvalidOperationException($"QueryResponse did not have an array of : {resourceName}");
}
return cosmosArray;
}
}
}