-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathLocationCache.cs
More file actions
1067 lines (933 loc) · 51.1 KB
/
LocationCache.cs
File metadata and controls
1067 lines (933 loc) · 51.1 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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Routing
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
/// <summary>
/// Implements the abstraction to resolve target location for geo-replicated DatabaseAccount
/// with multiple writable and readable locations.
/// </summary>
internal sealed class LocationCache
{
private const string UnavailableLocationsExpirationTimeInSeconds = "UnavailableLocationsExpirationTimeInSeconds";
private static int DefaultUnavailableLocationsExpirationTimeInSeconds = 5 * 60;
private readonly bool enableEndpointDiscovery;
private readonly Uri defaultEndpoint;
private readonly bool useMultipleWriteLocations;
private readonly object lockObject;
private readonly TimeSpan unavailableLocationsExpirationTime;
private readonly int connectionLimit;
private readonly ConcurrentDictionary<Uri, LocationUnavailabilityInfo> locationUnavailablityInfoByEndpoint;
private readonly RegionNameMapper regionNameMapper;
private readonly Func<bool> isPartitionLevelFailoverEnabled;
private DatabaseAccountLocationsInfo locationInfo;
private DateTime lastCacheUpdateTimestamp;
private bool enableMultipleWriteLocations;
public LocationCache(
ReadOnlyCollection<string> preferredLocations,
Uri defaultEndpoint,
bool enableEndpointDiscovery,
int connectionLimit,
bool useMultipleWriteLocations,
Func<bool> isPartitionLevelFailoverEnabled = null)
{
this.locationInfo = new DatabaseAccountLocationsInfo(preferredLocations, defaultEndpoint);
this.defaultEndpoint = defaultEndpoint;
this.enableEndpointDiscovery = enableEndpointDiscovery;
this.useMultipleWriteLocations = useMultipleWriteLocations;
this.connectionLimit = connectionLimit;
this.isPartitionLevelFailoverEnabled = isPartitionLevelFailoverEnabled;
this.lockObject = new object();
this.locationUnavailablityInfoByEndpoint = new ConcurrentDictionary<Uri, LocationUnavailabilityInfo>();
this.lastCacheUpdateTimestamp = DateTime.MinValue;
this.enableMultipleWriteLocations = false;
this.unavailableLocationsExpirationTime = TimeSpan.FromSeconds(LocationCache.DefaultUnavailableLocationsExpirationTimeInSeconds);
this.regionNameMapper = new RegionNameMapper();
#if !(NETSTANDARD15 || NETSTANDARD16)
#if NETSTANDARD20
// GetEntryAssembly returns null when loaded from native netstandard2.0
if (System.Reflection.Assembly.GetEntryAssembly() != null)
{
#endif
string unavailableLocationsExpirationTimeInSecondsConfig = System.Configuration.ConfigurationManager.AppSettings[LocationCache.UnavailableLocationsExpirationTimeInSeconds];
if (!string.IsNullOrEmpty(unavailableLocationsExpirationTimeInSecondsConfig))
{
int unavailableLocationsExpirationTimeinSecondsConfigValue;
if (!int.TryParse(unavailableLocationsExpirationTimeInSecondsConfig, out unavailableLocationsExpirationTimeinSecondsConfigValue))
{
this.unavailableLocationsExpirationTime = TimeSpan.FromSeconds(LocationCache.DefaultUnavailableLocationsExpirationTimeInSeconds);
}
else
{
this.unavailableLocationsExpirationTime = TimeSpan.FromSeconds(unavailableLocationsExpirationTimeinSecondsConfigValue);
}
}
#if NETSTANDARD20
}
#endif
#endif
}
/// <summary>
/// Gets list of read endpoints ordered by
/// 1. Preferred location
/// 2. Endpoint availablity
/// </summary>
public ReadOnlyCollection<Uri> ReadEndpoints
{
get
{
// Hot-path: avoid ConcurrentDictionary methods which acquire locks
if (DateTime.UtcNow - this.lastCacheUpdateTimestamp > this.unavailableLocationsExpirationTime
&& this.locationUnavailablityInfoByEndpoint.Any())
{
this.UpdateLocationCache();
}
return this.locationInfo.ReadEndpoints;
}
}
/// <summary>
/// Gets list of account level read endpoints.
/// </summary>
public ReadOnlyCollection<Uri> AccountReadEndpoints => this.locationInfo.AccountReadEndpoints;
/// <summary>
/// Gets list of write endpoints ordered by
/// 1. Preferred location
/// 2. Endpoint availablity
/// </summary>
public ReadOnlyCollection<Uri> WriteEndpoints
{
get
{
// Hot-path: avoid ConcurrentDictionary methods which acquire locks
if (DateTime.UtcNow - this.lastCacheUpdateTimestamp > this.unavailableLocationsExpirationTime
&& this.locationUnavailablityInfoByEndpoint.Any())
{
this.UpdateLocationCache();
}
return this.locationInfo.WriteEndpoints;
}
}
/// <summary>
/// Gets the list of thin client read endpoints.
/// </summary>
public ReadOnlyCollection<Uri> ThinClientReadEndpoints
{
get
{
// Hot-path: avoid ConcurrentDictionary methods which acquire locks
if (DateTime.UtcNow - this.lastCacheUpdateTimestamp > this.unavailableLocationsExpirationTime
&& this.locationUnavailablityInfoByEndpoint.Any())
{
this.UpdateLocationCache();
}
return this.locationInfo.ThinClientReadEndpoints;
}
}
/// <summary>
/// Gets the list of thin client write endpoints.
/// </summary>
public ReadOnlyCollection<Uri> ThinClientWriteEndpoints
{
get
{
// Hot-path: avoid ConcurrentDictionary methods which acquire locks
if (DateTime.UtcNow - this.lastCacheUpdateTimestamp > this.unavailableLocationsExpirationTime
&& this.locationUnavailablityInfoByEndpoint.Any())
{
this.UpdateLocationCache();
}
return this.locationInfo.ThinClientWriteEndpoints;
}
}
public ReadOnlyCollection<string> EffectivePreferredLocations => this.locationInfo.EffectivePreferredLocations;
/// <summary>
/// Returns the location corresponding to the endpoint if location specific endpoint is provided.
/// For the defaultEndPoint, we will return the first available write location.
/// Returns null, in other cases.
/// </summary>
/// <remarks>
/// Today we return null for defaultEndPoint if multiple write locations can be used.
/// This needs to be modifed to figure out proper location in such case.
/// </remarks>
public string GetLocation(Uri endpoint)
{
string location = this.locationInfo.AvailableWriteEndpointByLocation.FirstOrDefault(uri => uri.Value == endpoint).Key ?? this.locationInfo.AvailableReadEndpointByLocation.FirstOrDefault(uri => uri.Value == endpoint).Key;
if (location == null && endpoint == this.defaultEndpoint && !this.CanUseMultipleWriteLocations())
{
if (this.locationInfo.AvailableWriteEndpointByLocation.Any())
{
return this.locationInfo.AvailableWriteEndpointByLocation.First().Key;
}
}
return location;
}
/// <summary>
/// Set region name for a location if present in the locationcache otherwise set region name as null.
/// If endpoint's hostname is same as default endpoint hostname, set regionName as null.
/// </summary>
/// <param name="endpoint"></param>
/// <param name="regionName"></param>
/// <returns>true if region found else false</returns>
public bool TryGetLocationForGatewayDiagnostics(Uri endpoint, out string regionName)
{
if (Uri.Compare(
endpoint,
this.defaultEndpoint,
UriComponents.Host,
UriFormat.SafeUnescaped,
StringComparison.OrdinalIgnoreCase) == 0)
{
regionName = null;
return false;
}
regionName = this.GetLocation(endpoint);
return true;
}
/// <summary>
/// Marks the current location unavailable for read
/// </summary>
public void MarkEndpointUnavailableForRead(Uri endpoint)
{
this.MarkEndpointUnavailable(endpoint, OperationType.Read);
}
/// <summary>
/// Marks the current location unavailable for write
/// </summary>
public void MarkEndpointUnavailableForWrite(Uri endpoint)
{
this.MarkEndpointUnavailable(endpoint, OperationType.Write);
}
/// <summary>
/// Invoked when <see cref="AccountProperties"/> is read
/// </summary>
/// <param name="databaseAccount">Read DatabaseAccoaunt </param>
public void OnDatabaseAccountRead(AccountProperties databaseAccount)
{
this.UpdateLocationCache(
databaseAccount.WritableRegions,
databaseAccount.ReadableRegions,
thinClientWriteLocations: databaseAccount.ThinClientWritableLocationsInternal,
thinClientReadLocations: databaseAccount.ThinClientReadableLocationsInternal,
preferenceList: null,
enableMultipleWriteLocations: databaseAccount.EnableMultipleWriteLocations);
}
/// <summary>
/// Invoked when <see cref="ConnectionPolicy.PreferredLocations"/> changes
/// </summary>
/// <param name="preferredLocations"></param>
public void OnLocationPreferenceChanged(ReadOnlyCollection<string> preferredLocations)
{
this.UpdateLocationCache(
preferenceList: preferredLocations);
}
public static bool IsMetaData(DocumentServiceRequest request)
{
return (request.OperationType != Documents.OperationType.ExecuteJavaScript && request.ResourceType == ResourceType.StoredProcedure) ||
request.ResourceType != ResourceType.Document;
}
public bool IsMultimasterMetadataWriteRequest(DocumentServiceRequest request)
{
return !request.IsReadOnlyRequest && this.locationInfo.AvailableWriteLocations.Count > 1
&& LocationCache.IsMetaData(request)
&& this.CanUseMultipleWriteLocations();
}
/// <summary>
/// Gets the default endpoint of the account
/// </summary>
/// <returns>the default endpoint.</returns>
public Uri GetDefaultEndpoint()
{
return this.defaultEndpoint;
}
/// <summary>
/// Gets the mapping of available write region names to the respective endpoints
/// </summary>
public ReadOnlyDictionary<string, Uri> GetAvailableWriteEndpointsByLocation()
{
return this.locationInfo.AvailableWriteEndpointByLocation;
}
/// <summary>
/// Gets the mapping of available read region names to the respective endpoints
/// </summary>
public ReadOnlyDictionary<string, Uri> GetAvailableReadEndpointsByLocation()
{
return this.locationInfo.AvailableReadEndpointByLocation;
}
public Uri GetHubUri()
{
DatabaseAccountLocationsInfo currentLocationInfo = this.locationInfo;
string writeLocation = currentLocationInfo.AvailableWriteLocations[0];
Uri locationEndpointToRoute = currentLocationInfo.AvailableWriteEndpointByLocation[writeLocation];
return locationEndpointToRoute;
}
/// <summary>
/// Gets available (account-level) read locations.
/// </summary>
public ReadOnlyCollection<string> GetAvailableAccountLevelReadLocations()
{
return this.locationInfo.AvailableReadLocations;
}
/// <summary>
/// Gets available (account-level) write locations.
/// </summary>
public ReadOnlyCollection<string> GetAvailableAccountLevelWriteLocations()
{
return this.locationInfo.AvailableWriteLocations;
}
/// <summary>
/// Resolves request to service endpoint.
/// 1. If this is a write request
/// (a) If UseMultipleWriteLocations = true
/// (i) For document writes, resolve to most preferred and available write endpoint.
/// Once the endpoint is marked unavailable, it is moved to the end of available write endpoint. Current request will
/// be retried on next preferred available write endpoint.
/// (ii) For all other resources, always resolve to first/second (regardless of preferred locations)
/// write endpoint in <see cref="AccountProperties.WritableRegions"/>.
/// Endpoint of first write location in <see cref="AccountProperties.WritableRegions"/> is the only endpoint that supports
/// write operation on all resource types (except during that region's failover).
/// Only during manual failover, client would retry write on second write location in <see cref="AccountProperties.WritableRegions"/>.
/// (b) Else resolve the request to first write endpoint in <see cref="AccountProperties.writeRegions"/> OR
/// second write endpoint in <see cref="AccountProperties.WritableRegions"/> in case of manual failover of that location.
/// 2. Else resolve the request to most preferred available read endpoint (automatic failover for read requests)
/// </summary>
/// <param name="request">Request for which endpoint is to be resolved</param>
/// <returns>Resolved endpoint</returns>
public Uri ResolveServiceEndpoint(DocumentServiceRequest request)
{
if (request.RequestContext != null && request.RequestContext.LocationEndpointToRoute != null)
{
return request.RequestContext.LocationEndpointToRoute;
}
int locationIndex = request.RequestContext.LocationIndexToRoute.GetValueOrDefault(0);
Uri locationEndpointToRoute = this.defaultEndpoint;
if (!request.RequestContext.UsePreferredLocations.GetValueOrDefault(true) // Should not use preferred location ?
|| (request.OperationType.IsWriteOperation() && !this.CanUseMultipleWriteLocations(request)))
{
// For non-document resource types in case of client can use multiple write locations
// or when client cannot use multiple write locations, flip-flop between the
// first and the second writable region in DatabaseAccount (for manual failover)
DatabaseAccountLocationsInfo currentLocationInfo = this.locationInfo;
if (this.enableEndpointDiscovery && currentLocationInfo.AvailableWriteLocations.Count > 0)
{
locationIndex = Math.Min(locationIndex % 2, currentLocationInfo.AvailableWriteLocations.Count - 1);
string writeLocation = currentLocationInfo.AvailableWriteLocations[locationIndex];
locationEndpointToRoute = currentLocationInfo.AvailableWriteEndpointByLocation[writeLocation];
}
}
else
{
ReadOnlyCollection<Uri> endpoints = this.GetApplicableEndpoints(request, !request.OperationType.IsWriteOperation());
locationEndpointToRoute = endpoints[locationIndex % endpoints.Count];
}
request.RequestContext.RouteToLocation(locationEndpointToRoute);
return locationEndpointToRoute;
}
public ReadOnlyCollection<Uri> GetApplicableEndpoints(DocumentServiceRequest request, bool isReadRequest)
{
if (request.RequestContext.ExcludeRegions == null || request.RequestContext.ExcludeRegions.Count == 0)
{
return isReadRequest ? this.ReadEndpoints : this.WriteEndpoints;
}
DatabaseAccountLocationsInfo databaseAccountLocationsInfoSnapshot = this.locationInfo;
ReadOnlyCollection<string> effectivePreferredLocations = databaseAccountLocationsInfoSnapshot.EffectivePreferredLocations;
// For reads when PPAF is enabled, use WriteEndpoints[0] as fallback (dynamic,
// tracks current write region) instead of this.defaultEndpoint (static, region-agnostic,
// never updated after init). This aligns with UpdateLocationCache which already uses
// WriteEndpoints[0] as the ReadEndpoints fallback, and matches Java/Python SDK behavior.
Uri fallbackEndpoint = (isReadRequest && this.isPartitionLevelFailoverEnabled?.Invoke() == true)
? databaseAccountLocationsInfoSnapshot.WriteEndpoints[0]
: this.defaultEndpoint;
return GetApplicableEndpoints(
isReadRequest ? databaseAccountLocationsInfoSnapshot.AvailableReadEndpointByLocation : databaseAccountLocationsInfoSnapshot.AvailableWriteEndpointByLocation,
effectivePreferredLocations,
fallbackEndpoint,
request.RequestContext.ExcludeRegions);
}
public ReadOnlyCollection<string> GetApplicableRegions(IEnumerable<string> excludeRegions, bool isReadRequest)
{
DatabaseAccountLocationsInfo databaseAccountLocationsInfoSnapshot = this.locationInfo;
ReadOnlyCollection<string> effectivePreferredLocations = this.locationInfo.EffectivePreferredLocations;
if (effectivePreferredLocations == null || effectivePreferredLocations.Count == 0)
{
throw new ArgumentException("effectivePreferredLocations cannot be null or empty!");
}
return GetApplicableRegions(
isReadRequest ? databaseAccountLocationsInfoSnapshot.AvailableReadLocations : databaseAccountLocationsInfoSnapshot.AvailableWriteLocations,
effectivePreferredLocations,
effectivePreferredLocations[0],
excludeRegions);
}
/// <summary>
/// Gets applicable endpoints for a request, if there are no applicable endpoints, returns the fallback endpoint
/// </summary>
/// <param name="regionNameByEndpoint"></param>
/// <param name="effectivePreferredLocations"></param>
/// <param name="fallbackEndpoint"></param>
/// <param name="excludeRegions"></param>
/// <returns>a list of applicable endpoints for a request</returns>
private static ReadOnlyCollection<Uri> GetApplicableEndpoints(
ReadOnlyDictionary<string, Uri> regionNameByEndpoint,
ReadOnlyCollection<string> effectivePreferredLocations,
Uri fallbackEndpoint,
IEnumerable<string> excludeRegions)
{
List<Uri> applicableEndpoints = new List<Uri>(regionNameByEndpoint.Count);
HashSet<string> excludeRegionsHash = excludeRegions == null ? new HashSet<string>() : new HashSet<string>(excludeRegions);
foreach (string region in effectivePreferredLocations)
{
if (excludeRegionsHash.Count > 0)
{
if (!excludeRegionsHash.Contains(region) && regionNameByEndpoint.TryGetValue(region, out Uri endpoint))
{
applicableEndpoints.Add(endpoint);
}
}
else
{
if (regionNameByEndpoint.TryGetValue(region, out Uri endpoint))
{
applicableEndpoints.Add(endpoint);
}
}
}
if (applicableEndpoints.Count == 0)
{
applicableEndpoints.Add(fallbackEndpoint);
}
return new ReadOnlyCollection<Uri>(applicableEndpoints);
}
/// <summary>
/// Gets applicable endpoints for a request, if there are no applicable endpoints, returns the fallback endpoint
/// </summary>
/// <param name="availableLocations"></param>
/// <param name="effectivePreferredLocations"></param>
/// <param name="fallbackRegion"></param>
/// <param name="excludeRegions"></param>
/// <returns>a list of applicable endpoints for a request</returns>
private static ReadOnlyCollection<string> GetApplicableRegions(
ReadOnlyCollection<string> availableLocations,
ReadOnlyCollection<string> effectivePreferredLocations,
string fallbackRegion,
IEnumerable<string> excludeRegions)
{
List<string> applicableRegions = new List<string>(availableLocations.Count);
HashSet<string> excludeRegionsHash = excludeRegions == null ? null : new HashSet<string>(excludeRegions);
if (excludeRegions != null)
{
foreach (string region in effectivePreferredLocations)
{
if (availableLocations.Contains(region)
&& !excludeRegionsHash.Contains(region))
{
applicableRegions.Add(region);
}
}
}
else
{
foreach (string region in effectivePreferredLocations)
{
if (availableLocations.Contains(region))
{
applicableRegions.Add(region);
}
}
}
if (applicableRegions.Count == 0)
{
applicableRegions.Add(fallbackRegion);
}
return new ReadOnlyCollection<string>(applicableRegions);
}
public bool ShouldRefreshEndpoints(out bool canRefreshInBackground)
{
canRefreshInBackground = true;
DatabaseAccountLocationsInfo currentLocationInfo = this.locationInfo;
string mostPreferredLocation = currentLocationInfo.EffectivePreferredLocations.FirstOrDefault();
// we should schedule refresh in background if we are unable to target the user's most preferredLocation.
if (this.enableEndpointDiscovery)
{
// Refresh if client opts-in to useMultipleWriteLocations but server-side setting is disabled
bool shouldRefresh = this.useMultipleWriteLocations && !this.enableMultipleWriteLocations;
ReadOnlyCollection<Uri> readLocationEndpoints = currentLocationInfo.ReadEndpoints;
if (this.IsEndpointUnavailable(readLocationEndpoints[0], OperationType.Read))
{
canRefreshInBackground = readLocationEndpoints.Count > 1;
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = true since the first read endpoint {0} is not available for read. canRefreshInBackground = {1}",
readLocationEndpoints[0],
canRefreshInBackground);
return true;
}
if (!string.IsNullOrEmpty(mostPreferredLocation))
{
Uri mostPreferredReadEndpoint;
if (currentLocationInfo.AvailableReadEndpointByLocation.TryGetValue(mostPreferredLocation, out mostPreferredReadEndpoint))
{
if (mostPreferredReadEndpoint != readLocationEndpoints[0])
{
// For reads, we can always refresh in background as we can alternate to
// other available read endpoints
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = true since most preferred location {0} is not available for read.", mostPreferredLocation);
return true;
}
}
else
{
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = true since most preferred location {0} is not in available read locations.", mostPreferredLocation);
return true;
}
}
Uri mostPreferredWriteEndpoint;
ReadOnlyCollection<Uri> writeLocationEndpoints = currentLocationInfo.WriteEndpoints;
if (!this.CanUseMultipleWriteLocations())
{
if (this.IsEndpointUnavailable(writeLocationEndpoints[0], OperationType.Write))
{
// Since most preferred write endpoint is unavailable, we can only refresh in background if
// we have an alternate write endpoint
canRefreshInBackground = writeLocationEndpoints.Count > 1;
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = true since most preferred location {0} endpoint {1} is not available for write. canRefreshInBackground = {2}",
mostPreferredLocation,
writeLocationEndpoints[0],
canRefreshInBackground);
return true;
}
else
{
return shouldRefresh;
}
}
else if (!string.IsNullOrEmpty(mostPreferredLocation))
{
if (currentLocationInfo.AvailableWriteEndpointByLocation.TryGetValue(mostPreferredLocation, out mostPreferredWriteEndpoint))
{
shouldRefresh |= mostPreferredWriteEndpoint != writeLocationEndpoints[0];
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = {0} since most preferred location {1} is not available for write.", shouldRefresh, mostPreferredLocation);
return shouldRefresh;
}
else
{
DefaultTrace.TraceInformation("ShouldRefreshEndpoints = true since most preferred location {0} is not in available write locations", mostPreferredLocation);
return true;
}
}
else
{
return shouldRefresh;
}
}
else
{
return false;
}
}
public bool CanUseMultipleWriteLocations(DocumentServiceRequest request)
{
return this.CanUseMultipleWriteLocations() &&
(request.ResourceType == ResourceType.Document ||
(request.ResourceType == ResourceType.StoredProcedure && request.OperationType == Documents.OperationType.ExecuteJavaScript));
}
private void ClearStaleEndpointUnavailabilityInfo()
{
if (this.locationUnavailablityInfoByEndpoint.Any())
{
List<Uri> unavailableEndpoints = this.locationUnavailablityInfoByEndpoint.Keys.ToList();
foreach (Uri unavailableEndpoint in unavailableEndpoints)
{
LocationUnavailabilityInfo unavailabilityInfo;
LocationUnavailabilityInfo removed;
if (this.locationUnavailablityInfoByEndpoint.TryGetValue(unavailableEndpoint, out unavailabilityInfo)
&& DateTime.UtcNow - unavailabilityInfo.LastUnavailabilityCheckTimeStamp > this.unavailableLocationsExpirationTime
&& this.locationUnavailablityInfoByEndpoint.TryRemove(unavailableEndpoint, out removed))
{
DefaultTrace.TraceInformation(
"Removed endpoint {0} unavailable for operations {1} from unavailableEndpoints",
unavailableEndpoint,
unavailabilityInfo.UnavailableOperations);
}
}
}
}
private bool IsEndpointUnavailable(Uri endpoint, OperationType expectedAvailableOperations)
{
LocationUnavailabilityInfo unavailabilityInfo;
if (expectedAvailableOperations == OperationType.None
|| !this.locationUnavailablityInfoByEndpoint.TryGetValue(endpoint, out unavailabilityInfo)
|| !unavailabilityInfo.UnavailableOperations.HasFlag(expectedAvailableOperations))
{
return false;
}
else
{
if (DateTime.UtcNow - unavailabilityInfo.LastUnavailabilityCheckTimeStamp > this.unavailableLocationsExpirationTime)
{
return false;
}
else
{
DefaultTrace.TraceInformation(
"Endpoint {0} unavailable for operations {1} present in unavailableEndpoints",
endpoint,
unavailabilityInfo.UnavailableOperations);
// Unexpired entry present. Endpoint is unavailable
return true;
}
}
}
private void MarkEndpointUnavailable(
Uri unavailableEndpoint,
OperationType unavailableOperationType)
{
DateTime currentTime = DateTime.UtcNow;
LocationUnavailabilityInfo updatedInfo = this.locationUnavailablityInfoByEndpoint.AddOrUpdate(
unavailableEndpoint,
(Uri endpoint) =>
{
return new LocationUnavailabilityInfo()
{
LastUnavailabilityCheckTimeStamp = currentTime,
UnavailableOperations = unavailableOperationType,
};
},
(Uri endpoint, LocationUnavailabilityInfo info) =>
{
info.LastUnavailabilityCheckTimeStamp = currentTime;
info.UnavailableOperations |= unavailableOperationType;
return info;
});
this.UpdateLocationCache();
DefaultTrace.TraceInformation(
"Endpoint {0} unavailable for {1} added/updated to unavailableEndpoints with timestamp {2}",
unavailableEndpoint,
unavailableOperationType,
updatedInfo.LastUnavailabilityCheckTimeStamp);
}
private void UpdateLocationCache(
IEnumerable<AccountRegion> writeLocations = null,
IEnumerable<AccountRegion> readLocations = null,
IEnumerable<AccountRegion> thinClientWriteLocations = null,
IEnumerable<AccountRegion> thinClientReadLocations = null,
ReadOnlyCollection<string> preferenceList = null,
bool? enableMultipleWriteLocations = null)
{
lock (this.lockObject)
{
DatabaseAccountLocationsInfo nextLocationInfo = new DatabaseAccountLocationsInfo(this.locationInfo);
if (preferenceList != null)
{
nextLocationInfo.PreferredLocations = preferenceList;
}
if (enableMultipleWriteLocations.HasValue)
{
this.enableMultipleWriteLocations = enableMultipleWriteLocations.Value;
}
this.ClearStaleEndpointUnavailabilityInfo();
if (readLocations != null)
{
nextLocationInfo.AvailableReadEndpointByLocation = this.GetEndpointByLocation(
readLocations,
out ReadOnlyCollection<string> availableReadLocations,
out ReadOnlyDictionary<Uri, string> availableReadLocationsByEndpoint);
nextLocationInfo.AvailableReadLocations = availableReadLocations;
nextLocationInfo.AccountReadEndpoints = nextLocationInfo.AvailableReadEndpointByLocation.Select(x => x.Value).ToList().AsReadOnly();
nextLocationInfo.AvailableReadLocationByEndpoint = availableReadLocationsByEndpoint;
}
if (writeLocations != null)
{
nextLocationInfo.AvailableWriteEndpointByLocation = this.GetEndpointByLocation(
writeLocations,
out ReadOnlyCollection<string> availableWriteLocations,
out ReadOnlyDictionary<Uri, string> availableWriteLocationsByEndpoint);
nextLocationInfo.AvailableWriteLocations = availableWriteLocations;
nextLocationInfo.AvailableWriteLocationByEndpoint = availableWriteLocationsByEndpoint;
}
if (thinClientReadLocations != null && thinClientReadLocations.Count() > 0)
{
nextLocationInfo.ThinClientReadEndpointByLocation = this.GetEndpointByLocation(
thinClientReadLocations,
out ReadOnlyCollection<string> thinClientAvailableReadLocations,
out ReadOnlyDictionary<Uri, string> thinClientAvailableReadLocationsByEndpoint);
nextLocationInfo.ThinClientReadLocations = thinClientAvailableReadLocations;
nextLocationInfo.ThinClientReadLocationByEndpoint = thinClientAvailableReadLocationsByEndpoint;
}
if (thinClientWriteLocations != null && thinClientWriteLocations.Count() > 0)
{
nextLocationInfo.ThinClientWriteEndpointByLocation = this.GetEndpointByLocation(
thinClientWriteLocations,
out ReadOnlyCollection<string> thinClientAvailableWriteLocations,
out ReadOnlyDictionary<Uri, string> thinClientAvailableWriteLocationsByEndpoint);
nextLocationInfo.ThinClientWriteLocations = thinClientAvailableWriteLocations;
nextLocationInfo.ThinClientWriteLocationByEndpoint = thinClientAvailableWriteLocationsByEndpoint;
}
nextLocationInfo.WriteEndpoints = this.GetPreferredAvailableEndpoints(
endpointsByLocation: nextLocationInfo.AvailableWriteEndpointByLocation,
orderedLocations: nextLocationInfo.AvailableWriteLocations,
expectedAvailableOperation: OperationType.Write,
fallbackEndpoint: this.defaultEndpoint);
nextLocationInfo.ReadEndpoints = this.GetPreferredAvailableEndpoints(
endpointsByLocation: nextLocationInfo.AvailableReadEndpointByLocation,
orderedLocations: nextLocationInfo.AvailableReadLocations,
expectedAvailableOperation: OperationType.Read,
fallbackEndpoint: nextLocationInfo.WriteEndpoints[0]);
nextLocationInfo.EffectivePreferredLocations = nextLocationInfo.PreferredLocations;
nextLocationInfo.ThinClientWriteEndpoints = this.GetPreferredAvailableEndpoints(
endpointsByLocation: nextLocationInfo.ThinClientWriteEndpointByLocation,
orderedLocations: nextLocationInfo.ThinClientWriteLocations,
expectedAvailableOperation: OperationType.Write,
fallbackEndpoint: this.defaultEndpoint);
nextLocationInfo.ThinClientReadEndpoints = this.GetPreferredAvailableEndpoints(
endpointsByLocation: nextLocationInfo.ThinClientReadEndpointByLocation,
orderedLocations: nextLocationInfo.ThinClientReadLocations,
expectedAvailableOperation: OperationType.Read,
fallbackEndpoint: nextLocationInfo.ThinClientWriteEndpoints[0]);
if (nextLocationInfo.PreferredLocations == null || nextLocationInfo.PreferredLocations.Count == 0)
{
if (!nextLocationInfo.AvailableReadLocationByEndpoint.TryGetValue(this.defaultEndpoint, out string regionForDefaultEndpoint))
{
nextLocationInfo.EffectivePreferredLocations = nextLocationInfo.AvailableReadLocations;
}
else
{
// if defaultEndpoint equals a regional endpoint - do not use account-level regions,
// stick to defaultEndpoint configured for the CosmosClient instance
List<string> locations = new ()
{
regionForDefaultEndpoint
};
nextLocationInfo.EffectivePreferredLocations = new ReadOnlyCollection<string>(locations);
}
}
this.lastCacheUpdateTimestamp = DateTime.UtcNow;
DefaultTrace.TraceInformation("Current WriteEndpoints = ({0}) ReadEndpoints = ({1})",
string.Join(", ", nextLocationInfo.WriteEndpoints.Select(endpoint => endpoint.ToString())),
string.Join(", ", nextLocationInfo.ReadEndpoints.Select(endpoint => endpoint.ToString())));
this.locationInfo = nextLocationInfo;
}
}
private ReadOnlyCollection<Uri> GetPreferredAvailableEndpoints(ReadOnlyDictionary<string, Uri> endpointsByLocation, ReadOnlyCollection<string> orderedLocations, OperationType expectedAvailableOperation, Uri fallbackEndpoint)
{
List<Uri> endpoints = new List<Uri>();
DatabaseAccountLocationsInfo currentLocationInfo = this.locationInfo;
// if enableEndpointDiscovery is false, we always use the defaultEndpoint that user passed in during documentClient init
if (this.enableEndpointDiscovery)
{
if (this.CanUseMultipleWriteLocations() || expectedAvailableOperation.HasFlag(OperationType.Read))
{
List<Uri> unavailableEndpoints = new List<Uri>();
// When client can not use multiple write locations, preferred locations list should only be used
// determining read endpoints order.
// If client can use multiple write locations, preferred locations list should be used for determining
// both read and write endpoints order.
if (currentLocationInfo.PreferredLocations != null && currentLocationInfo.PreferredLocations.Count >= 1)
{
foreach (string location in currentLocationInfo.PreferredLocations)
{
if (endpointsByLocation.TryGetValue(location, out Uri endpoint))
{
if (this.IsEndpointUnavailable(endpoint, expectedAvailableOperation))
{
unavailableEndpoints.Add(endpoint);
}
else
{
endpoints.Add(endpoint);
}
}
}
}
else
{
foreach (string location in orderedLocations)
{
if (endpointsByLocation.TryGetValue(location, out Uri endpoint))
{
// if defaultEndpoint equals a regional endpoint - do not use account-level regions,
// stick to defaultEndpoint configured for the CosmosClient instance
if (this.defaultEndpoint.Equals(endpoint))
{
endpoints = new List<Uri>();
break;
}
if (this.IsEndpointUnavailable(endpoint, expectedAvailableOperation))
{
unavailableEndpoints.Add(endpoint);
}
else
{
endpoints.Add(endpoint);
}
}
}
}
if (endpoints.Count == 0)
{
endpoints.Add(fallbackEndpoint);
unavailableEndpoints.Remove(fallbackEndpoint);
}
endpoints.AddRange(unavailableEndpoints);
}
else
{
foreach (string location in orderedLocations)
{
if (!string.IsNullOrEmpty(location) && // location is empty during manual failover
endpointsByLocation.TryGetValue(location, out Uri endpoint))
{
endpoints.Add(endpoint);
}
}
}
}
if (endpoints.Count == 0)
{
endpoints.Add(fallbackEndpoint);
}
return endpoints.AsReadOnly();
}
private ReadOnlyDictionary<string, Uri> GetEndpointByLocation(IEnumerable<AccountRegion> locations, out ReadOnlyCollection<string> orderedLocations, out ReadOnlyDictionary<Uri, string> availableLocationsByEndpoint)
{
Dictionary<string, Uri> endpointsByLocation = new Dictionary<string, Uri>(StringComparer.OrdinalIgnoreCase);
Dictionary<Uri, string> mutableAvailableLocationsByEndpoint = new Dictionary<Uri, string>();
List<string> parsedLocations = new List<string>();
foreach (AccountRegion location in locations)
{
Uri endpoint;
if (!string.IsNullOrEmpty(location.Name)
&& Uri.TryCreate(location.Endpoint, UriKind.Absolute, out endpoint))
{
endpointsByLocation[location.Name] = endpoint;
parsedLocations.Add(location.Name);
mutableAvailableLocationsByEndpoint[endpoint] = location.Name;
this.SetServicePointConnectionLimit(endpoint);
}
else
{
DefaultTrace.TraceInformation("GetAvailableEndpointsByLocation() - skipping add for location = {0} as it is location name is either empty or endpoint is malformed {1}",
location.Name,
location.Endpoint);
}
}
orderedLocations = parsedLocations.AsReadOnly();
availableLocationsByEndpoint = new ReadOnlyDictionary<Uri, string>(mutableAvailableLocationsByEndpoint);
return new ReadOnlyDictionary<string, Uri>(endpointsByLocation);
}
internal bool CanUseMultipleWriteLocations()
{
return this.useMultipleWriteLocations && this.enableMultipleWriteLocations;
}
internal Uri ResolveThinClientEndpoint(DocumentServiceRequest request, bool isReadRequest)
{
if (request.RequestContext != null && request.RequestContext.LocationEndpointToRoute != null)
{
return request.RequestContext.LocationEndpointToRoute;
}
DatabaseAccountLocationsInfo snapshot = this.locationInfo;
ReadOnlyCollection<Uri> endpoints = isReadRequest
? snapshot.ThinClientReadEndpoints
: snapshot.ThinClientWriteEndpoints;
int locationIndex = request.RequestContext.LocationIndexToRoute.GetValueOrDefault(0);
Uri chosenEndpoint = endpoints[locationIndex % endpoints.Count];
request.RequestContext.RouteToLocation(chosenEndpoint);
return chosenEndpoint;
}
private void SetServicePointConnectionLimit(Uri endpoint)
{
#if !NETSTANDARD16
if (ServicePointAccessor.IsSupported)
{
ServicePointAccessor servicePoint = ServicePointAccessor.FindServicePoint(endpoint);
servicePoint.ConnectionLimit = this.connectionLimit;
}
#endif
}
private sealed class LocationUnavailabilityInfo
{
public DateTime LastUnavailabilityCheckTimeStamp { get; set; }
public OperationType UnavailableOperations { get; set; }
}
private sealed class DatabaseAccountLocationsInfo
{
public DatabaseAccountLocationsInfo(ReadOnlyCollection<string> preferredLocations, Uri defaultEndpoint)
{
this.PreferredLocations = preferredLocations;
this.AvailableWriteLocations = new List<string>().AsReadOnly();
this.AvailableReadLocations = new List<string>().AsReadOnly();
this.AvailableWriteEndpointByLocation = new ReadOnlyDictionary<string, Uri>(new Dictionary<string, Uri>(StringComparer.OrdinalIgnoreCase));
this.AvailableReadEndpointByLocation = new ReadOnlyDictionary<string, Uri>(new Dictionary<string, Uri>(StringComparer.OrdinalIgnoreCase));
this.AvailableWriteLocationByEndpoint = new ReadOnlyDictionary<Uri, string>(new Dictionary<Uri, string>());
this.AvailableReadLocationByEndpoint = new ReadOnlyDictionary<Uri, string>(new Dictionary<Uri, string>());
this.WriteEndpoints = new List<Uri>() { defaultEndpoint }.AsReadOnly();
this.AccountReadEndpoints = new List<Uri>() { defaultEndpoint }.AsReadOnly();
this.ReadEndpoints = new List<Uri>() { defaultEndpoint }.AsReadOnly();
this.EffectivePreferredLocations = new List<string>().AsReadOnly();
this.ThinClientWriteLocations = new List<string>().AsReadOnly();
this.ThinClientReadLocations = new List<string>().AsReadOnly();
this.ThinClientWriteEndpointByLocation =
new ReadOnlyDictionary<string, Uri>(new Dictionary<string, Uri>());
this.ThinClientReadEndpointByLocation =