-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathClientSideRequestStatisticsTraceDatum.cs
More file actions
632 lines (553 loc) · 26.5 KB
/
ClientSideRequestStatisticsTraceDatum.cs
File metadata and controls
632 lines (553 loc) · 26.5 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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Tracing.TraceData
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using Microsoft.Azure.Cosmos.Handler;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Rntbd;
internal sealed class ClientSideRequestStatisticsTraceDatum : TraceDatum, IClientSideRequestStatistics
{
private static readonly IReadOnlyDictionary<string, AddressResolutionStatistics> EmptyEndpointToAddressResolutionStatistics = new Dictionary<string, AddressResolutionStatistics>();
private static readonly IReadOnlyList<StoreResponseStatistics> EmptyStoreResponseStatistics = new List<StoreResponseStatistics>();
private static readonly IReadOnlyList<HttpResponseStatistics> EmptyHttpResponseStatistics = new List<HttpResponseStatistics>();
private readonly List<(PartitionAddressInformation existing, PartitionAddressInformation newInfo)> partitionAddressInformationRefreshes = new List<(PartitionAddressInformation existing, PartitionAddressInformation newInfo)>();
internal static readonly string HttpRequestRegionNameProperty = "regionName";
private readonly object requestEndTimeLock = new object();
private readonly Dictionary<string, AddressResolutionStatistics> endpointToAddressResolutionStats;
private readonly List<StoreResponseStatistics> storeResponseStatistics;
private readonly List<HttpResponseStatistics> httpResponseStatistics;
private readonly List<HttpResponseStatistics> thinclientResponseStatistics;
private IReadOnlyDictionary<string, AddressResolutionStatistics> shallowCopyOfEndpointToAddressResolutionStatistics = null;
private IReadOnlyList<StoreResponseStatistics> shallowCopyOfStoreResponseStatistics = null;
private IReadOnlyList<HttpResponseStatistics> shallowCopyOfHttpResponseStatistics = null;
private IReadOnlyList<HttpResponseStatistics> shallowCopyOfThinclientResponseStatistics = null;
private SystemUsageHistory systemUsageHistory = null;
public ClientSideRequestStatisticsTraceDatum(DateTime startTime, ITrace trace)
{
this.RequestStartTimeUtc = startTime;
this.RequestEndTimeUtc = null;
this.endpointToAddressResolutionStats = new Dictionary<string, AddressResolutionStatistics>();
this.ContactedReplicas = new List<TransportAddressUri>();
this.storeResponseStatistics = new List<StoreResponseStatistics>();
this.FailedReplicas = new HashSet<TransportAddressUri>();
this.RegionsContacted = new HashSet<(string, Uri)>();
this.httpResponseStatistics = new List<HttpResponseStatistics>();
this.thinclientResponseStatistics = new List<HttpResponseStatistics>();
this.Trace = trace;
}
public DateTime RequestStartTimeUtc { get; }
public DateTime? RequestEndTimeUtc { get; private set; }
public IReadOnlyDictionary<string, AddressResolutionStatistics> EndpointToAddressResolutionStatistics
{
get
{
if (this.endpointToAddressResolutionStats.Count == 0)
{
return ClientSideRequestStatisticsTraceDatum.EmptyEndpointToAddressResolutionStatistics;
}
lock (this.endpointToAddressResolutionStats)
{
this.shallowCopyOfEndpointToAddressResolutionStatistics ??= new Dictionary<string, AddressResolutionStatistics>(this.endpointToAddressResolutionStats);
return this.shallowCopyOfEndpointToAddressResolutionStatistics;
}
}
}
public List<TransportAddressUri> ContactedReplicas { get; set; }
public HashSet<TransportAddressUri> FailedReplicas { get; }
public HashSet<(string, Uri)> RegionsContacted { get; }
public ITrace Trace { get; private set; }
public TraceSummary TraceSummary => this.Trace?.Summary;
public IReadOnlyList<StoreResponseStatistics> StoreResponseStatisticsList
{
get
{
if (this.storeResponseStatistics.Count == 0)
{
return ClientSideRequestStatisticsTraceDatum.EmptyStoreResponseStatistics;
}
lock (this.storeResponseStatistics)
{
this.shallowCopyOfStoreResponseStatistics ??= new List<StoreResponseStatistics>(this.storeResponseStatistics);
return this.shallowCopyOfStoreResponseStatistics;
}
}
}
public IReadOnlyList<HttpResponseStatistics> HttpResponseStatisticsList
{
get
{
if (this.httpResponseStatistics.Count == 0)
{
return ClientSideRequestStatisticsTraceDatum.EmptyHttpResponseStatistics;
}
lock (this.httpResponseStatistics)
{
this.shallowCopyOfHttpResponseStatistics ??= new List<HttpResponseStatistics>(this.httpResponseStatistics);
return this.shallowCopyOfHttpResponseStatistics;
}
}
}
public IReadOnlyList<HttpResponseStatistics> ThinclientResponseStatisticsList
{
get
{
if (this.thinclientResponseStatistics.Count == 0)
{
return ClientSideRequestStatisticsTraceDatum.EmptyHttpResponseStatistics;
}
lock (this.thinclientResponseStatistics)
{
this.shallowCopyOfThinclientResponseStatistics ??= new List<HttpResponseStatistics>(this.thinclientResponseStatistics);
return this.shallowCopyOfThinclientResponseStatistics;
}
}
}
public TimeSpan? RequestLatency
{
get
{
if (this.RequestEndTimeUtc.HasValue)
{
return this.RequestEndTimeUtc.Value - this.RequestStartTimeUtc;
}
return null;
}
}
public bool? IsCpuHigh => this.systemUsageHistory?.IsCpuHigh;
public bool? IsCpuThreadStarvation => this.systemUsageHistory?.IsCpuThreadStarvation;
public void RecordRequest(DocumentServiceRequest request)
{
}
public void RecordAddressCachRefreshContent(
PartitionAddressInformation existingInfo,
PartitionAddressInformation newInfo)
{
lock (this.partitionAddressInformationRefreshes)
{
this.partitionAddressInformationRefreshes.Add((existingInfo, newInfo));
}
}
public void WriteAddressCachRefreshContent(IJsonWriter jsonWriter)
{
if (this.partitionAddressInformationRefreshes.Count == 0)
{
return;
}
lock (this.partitionAddressInformationRefreshes)
{
if (this.partitionAddressInformationRefreshes.Count == 0)
{
return;
}
jsonWriter.WriteFieldName("ForceAddressRefresh");
jsonWriter.WriteArrayStart();
foreach ((PartitionAddressInformation existing, PartitionAddressInformation newInfo) in this.partitionAddressInformationRefreshes)
{
// Avoid printing the same list twice if the cache update did not change any values
if (ClientSideRequestStatisticsTraceDatum.IsSamePartitionAddressInformation(existing, newInfo))
{
jsonWriter.WriteObjectStart();
jsonWriter.WriteFieldName("No change to cache");
jsonWriter.WriteArrayStart();
foreach (AddressInformation addressInformation in existing.AllAddresses)
{
jsonWriter.WriteStringValue(addressInformation.PhysicalUri);
}
jsonWriter.WriteArrayEnd();
jsonWriter.WriteObjectEnd();
}
else
{
jsonWriter.WriteObjectStart();
jsonWriter.WriteFieldName("Original");
jsonWriter.WriteArrayStart();
foreach (AddressInformation addressInformation in existing.AllAddresses)
{
jsonWriter.WriteStringValue(addressInformation.PhysicalUri);
}
jsonWriter.WriteArrayEnd();
jsonWriter.WriteFieldName("New");
jsonWriter.WriteArrayStart();
foreach (AddressInformation addressInformation in newInfo.AllAddresses)
{
jsonWriter.WriteStringValue(addressInformation.PhysicalUri);
}
jsonWriter.WriteArrayEnd();
jsonWriter.WriteObjectEnd();
}
}
jsonWriter.WriteArrayEnd();
}
}
private static bool IsSamePartitionAddressInformation(
PartitionAddressInformation info1,
PartitionAddressInformation info2)
{
if (info1 == null && info2 == null)
{
return true;
}
if (info1 != null && info2 == null)
{
return false;
}
if (info1 == null && info2 != null)
{
return false;
}
if (info1.AllAddresses.Count != info2.AllAddresses.Count)
{
return false;
}
// Assumes both lists are in the same order.
for (int i = 0; i < info1.AllAddresses.Count; i++)
{
AddressInformation info1AddressInfo = info1.AllAddresses[i];
AddressInformation info2AddressInfo = info2.AllAddresses[i];
if (info1AddressInfo.Protocol != info2AddressInfo.Protocol
|| info1AddressInfo.IsPrimary != info2AddressInfo.IsPrimary
|| info1AddressInfo.IsPublic != info2AddressInfo.IsPublic
|| info1AddressInfo.PhysicalUri != info2AddressInfo.PhysicalUri)
{
return false;
}
}
return true;
}
public void RecordResponse(
DocumentServiceRequest request,
StoreResult storeResult,
DateTime startTimeUtc,
DateTime endTimeUtc)
{
this.UpdateRequestEndTime(endTimeUtc);
Uri locationEndpoint = request.RequestContext.LocationEndpointToRoute;
string regionName = request.RequestContext.RegionName;
StoreResponseStatistics responseStatistics = new StoreResponseStatistics(
startTimeUtc,
endTimeUtc,
storeResult,
request.ResourceType,
request.OperationType,
request.Headers[HttpConstants.HttpHeaders.SessionToken],
locationEndpoint,
regionName);
lock (this.storeResponseStatistics)
{
if (locationEndpoint != null)
{
this.TraceSummary?.AddRegionContacted(regionName, locationEndpoint);
}
if (responseStatistics.StoreResult != null && !((HttpStatusCode)responseStatistics.StoreResult.StatusCode).IsSuccess()
&& !(responseStatistics.StoreResult.StatusCode == StatusCodes.NotFound && responseStatistics.StoreResult.SubStatusCode == SubStatusCodes.Unknown)
&& !(responseStatistics.StoreResult.StatusCode == StatusCodes.Conflict && responseStatistics.StoreResult.SubStatusCode == SubStatusCodes.Unknown)
&& !(responseStatistics.StoreResult.StatusCode == StatusCodes.PreconditionFailed && responseStatistics.StoreResult.SubStatusCode == SubStatusCodes.Unknown))
{
if (this.TraceSummary != null)
{
this.TraceSummary.IncrementFailedCount();
}
}
// Reset the shallow copy
this.shallowCopyOfStoreResponseStatistics = null;
this.storeResponseStatistics.Add(responseStatistics);
}
}
public void RecordException(
DocumentServiceRequest request,
Exception exception,
DateTime startTimeUtc,
DateTime endTimeUtc)
{
this.UpdateRequestEndTime(endTimeUtc);
}
public string RecordAddressResolutionStart(Uri targetEndpoint)
{
string identifier = Guid.NewGuid().ToString();
AddressResolutionStatistics resolutionStats = new AddressResolutionStatistics(
startTime: DateTime.UtcNow,
endTime: DateTime.MaxValue,
targetEndpoint: targetEndpoint == null ? "<NULL>" : targetEndpoint.ToString());
lock (this.endpointToAddressResolutionStats)
{
// Reset the shallow copy
this.shallowCopyOfEndpointToAddressResolutionStatistics = null;
this.endpointToAddressResolutionStats.Add(identifier, resolutionStats);
}
return identifier;
}
public void RecordAddressResolutionEnd(string identifier)
{
if (string.IsNullOrEmpty(identifier))
{
return;
}
DateTime responseTime = DateTime.UtcNow;
this.UpdateRequestEndTime(responseTime);
lock (this.endpointToAddressResolutionStats)
{
if (!this.endpointToAddressResolutionStats.ContainsKey(identifier))
{
throw new ArgumentException("Identifier {0} does not exist. Please call start before calling end.", identifier);
}
AddressResolutionStatistics start = this.endpointToAddressResolutionStats[identifier];
// Reset the shallow copy
this.shallowCopyOfEndpointToAddressResolutionStatistics = null;
this.endpointToAddressResolutionStats[identifier] = new AddressResolutionStatistics(
start.StartTime,
responseTime,
start.TargetEndpoint);
}
}
public void RecordHttpResponse(HttpRequestMessage request,
HttpResponseMessage response,
ResourceType resourceType,
DateTime requestStartTimeUtc)
{
if (request.Version == new Version(2, 0))
{
this.RecordThinclientHttpResponse(request, response, resourceType, requestStartTimeUtc);
return;
}
DateTime requestEndTimeUtc = DateTime.UtcNow;
this.UpdateRequestEndTime(requestEndTimeUtc);
lock (this.httpResponseStatistics)
{
Uri locationEndpoint = request.RequestUri;
object regionName = null;
if (request.Properties != null &&
request.Properties.TryGetValue(HttpRequestRegionNameProperty, out regionName))
{
this.TraceSummary.AddRegionContacted(Convert.ToString(regionName), locationEndpoint);
}
this.shallowCopyOfHttpResponseStatistics = null;
this.httpResponseStatistics.Add(new HttpResponseStatistics(requestStartTimeUtc,
requestEndTimeUtc,
request.RequestUri,
request.Method,
resourceType,
response,
exception: null,
region: Convert.ToString(regionName)));
}
}
public void RecordHttpException(HttpRequestMessage request,
Exception exception,
ResourceType resourceType,
DateTime requestStartTimeUtc)
{
if (request.Version == new Version(2, 0))
{
this.RecordThinclientHttpException(request, exception, resourceType, requestStartTimeUtc);
return;
}
DateTime requestEndTimeUtc = DateTime.UtcNow;
this.UpdateRequestEndTime(requestEndTimeUtc);
lock (this.httpResponseStatistics)
{
Uri locationEndpoint = request.RequestUri;
object regionName = null;
if (request.Properties != null &&
request.Properties.TryGetValue(HttpRequestRegionNameProperty, out regionName))
{
this.TraceSummary.AddRegionContacted(Convert.ToString(regionName), locationEndpoint);
}
this.shallowCopyOfHttpResponseStatistics = null;
this.httpResponseStatistics.Add(new HttpResponseStatistics(requestStartTimeUtc,
requestEndTimeUtc,
request.RequestUri,
request.Method,
resourceType,
responseMessage: null,
exception: exception,
region: Convert.ToString(regionName)));
}
}
public void RecordThinclientHttpResponse(
HttpRequestMessage request,
HttpResponseMessage response,
ResourceType resourceType,
DateTime requestStartTimeUtc)
{
DateTime requestEndTimeUtc = DateTime.UtcNow;
this.UpdateRequestEndTime(requestEndTimeUtc);
lock (this.thinclientResponseStatistics)
{
Uri locationEndpoint = request.RequestUri;
object regionName = null;
if (request.Properties != null &&
request.Properties.TryGetValue(HttpRequestRegionNameProperty, out regionName))
{
this.TraceSummary?.AddRegionContacted(Convert.ToString(regionName), locationEndpoint);
}
this.shallowCopyOfThinclientResponseStatistics = null;
this.thinclientResponseStatistics.Add(new HttpResponseStatistics(
requestStartTimeUtc,
requestEndTimeUtc,
request.RequestUri,
request.Method,
resourceType,
response,
exception: null,
region: Convert.ToString(regionName)));
}
}
public void RecordThinclientHttpException(
HttpRequestMessage request,
Exception exception,
ResourceType resourceType,
DateTime requestStartTimeUtc)
{
DateTime requestEndTimeUtc = DateTime.UtcNow;
this.UpdateRequestEndTime(requestEndTimeUtc);
lock (this.thinclientResponseStatistics)
{
Uri locationEndpoint = request.RequestUri;
object regionName = null;
if (request.Properties != null &&
request.Properties.TryGetValue(HttpRequestRegionNameProperty, out regionName))
{
this.TraceSummary?.AddRegionContacted(Convert.ToString(regionName), locationEndpoint);
}
this.shallowCopyOfThinclientResponseStatistics = null;
this.thinclientResponseStatistics.Add(new HttpResponseStatistics(
requestStartTimeUtc,
requestEndTimeUtc,
request.RequestUri,
request.Method,
resourceType,
responseMessage: null,
exception: exception,
region: Convert.ToString(regionName)));
}
}
private DateTime UpdateRequestEndTime(DateTime requestEndTimeUtc)
{
lock (this.requestEndTimeLock)
{
if (!this.RequestEndTimeUtc.HasValue || requestEndTimeUtc > this.RequestEndTimeUtc)
{
this.RequestEndTimeUtc = requestEndTimeUtc;
}
this.UpdateSystemUsage();
}
return requestEndTimeUtc;
}
public void UpdateSystemUsage()
{
#if !INTERNAL
if (this.systemUsageHistory == null ||
this.systemUsageHistory.Values.Count == 0 ||
this.systemUsageHistory.LastTimestamp + DiagnosticsHandlerHelper.DiagnosticsRefreshInterval < DateTime.UtcNow)
{
this.systemUsageHistory = DiagnosticsHandlerHelper.GetInstance().GetDiagnosticsSystemHistory();
}
#endif
}
internal override void Accept(ITraceDatumVisitor traceDatumVisitor)
{
traceDatumVisitor.Visit(this);
}
public void AppendToBuilder(StringBuilder stringBuilder)
{
throw new NotImplementedException();
}
public readonly struct AddressResolutionStatistics
{
public AddressResolutionStatistics(
DateTime startTime,
DateTime endTime,
string targetEndpoint)
{
this.StartTime = startTime;
this.EndTime = endTime;
this.TargetEndpoint = targetEndpoint ?? throw new ArgumentNullException(nameof(startTime));
}
public DateTime StartTime { get; }
public DateTime? EndTime { get; }
public string TargetEndpoint { get; }
}
public sealed class StoreResponseStatistics
{
public StoreResponseStatistics(
DateTime? requestStartTime,
DateTime requestResponseTime,
StoreResult storeResult,
ResourceType resourceType,
OperationType operationType,
string requestSessionToken,
Uri locationEndpoint,
string region)
{
this.RequestStartTime = requestStartTime;
this.RequestResponseTime = requestResponseTime;
this.StoreResult = storeResult;
this.RequestResourceType = resourceType;
this.RequestOperationType = operationType;
this.RequestSessionToken = requestSessionToken;
this.LocationEndpoint = locationEndpoint;
this.IsSupplementalResponse = operationType == OperationType.Head || operationType == OperationType.HeadFeed;
this.Region = region;
}
public string Region { get; }
public DateTime? RequestStartTime { get; }
public DateTime RequestResponseTime { get; }
public StoreResult StoreResult { get; }
public ResourceType RequestResourceType { get; }
public OperationType RequestOperationType { get; }
public string RequestSessionToken { get; }
public Uri LocationEndpoint { get; }
public bool IsSupplementalResponse { get; }
public TimeSpan RequestLatency => this.RequestResponseTime - this.RequestStartTime.GetValueOrDefault();
}
public readonly struct HttpResponseStatistics
{
public HttpResponseStatistics(
DateTime requestStartTime,
DateTime requestEndTime,
Uri requestUri,
HttpMethod httpMethod,
ResourceType resourceType,
HttpResponseMessage responseMessage,
Exception exception,
string region)
{
this.RequestStartTime = requestStartTime;
this.Duration = requestEndTime - requestStartTime;
this.HttpResponseMessage = responseMessage;
this.Exception = exception;
this.ResourceType = resourceType;
this.HttpMethod = httpMethod;
this.RequestUri = requestUri;
this.Region = region;
this.ResponseContentLength = responseMessage?.Content?.Headers?.ContentLength;
if (responseMessage != null)
{
Headers headers = new Headers(GatewayStoreClient.ExtractResponseHeaders(responseMessage));
this.ActivityId = headers.ActivityId ?? System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString();
}
else
{
this.ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString();
}
}
public string Region { get; }
public DateTime RequestStartTime { get; }
public TimeSpan Duration { get; }
public HttpResponseMessage HttpResponseMessage { get; }
public Exception Exception { get; }
public ResourceType ResourceType { get; }
public HttpMethod HttpMethod { get; }
public Uri RequestUri { get; }
public string ActivityId { get; }
public long? ResponseContentLength { get; }
}
}
}