-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathConnectionPolicy.cs
More file actions
611 lines (564 loc) · 24.3 KB
/
ConnectionPolicy.cs
File metadata and controls
611 lines (564 loc) · 24.3 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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
/// <summary>
/// Represents the connection policy associated with a DocumentClient to connect to the Azure Cosmos DB service.
/// </summary>
internal sealed class ConnectionPolicy
{
private const int defaultRequestTimeout = 6;
// defaultMediaRequestTimeout is based upon the blob client timeout and the retry policy.
private const int defaultMediaRequestTimeout = 300;
private const int defaultMaxConcurrentFanoutRequests = 32;
private const int defaultMaxConcurrentConnectionLimit = 50;
internal UserAgentContainer UserAgentContainer;
private static ConnectionPolicy defaultPolicy;
private Protocol connectionProtocol;
private ObservableCollection<string> preferredLocations;
private ObservableCollection<Uri> accountInitializationCustomEndpoints;
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPolicy"/> class to connect to the Azure Cosmos DB service.
/// </summary>
public ConnectionPolicy()
{
this.connectionProtocol = Protocol.Https;
this.RequestTimeout = TimeSpan.FromSeconds(ConnectionPolicy.defaultRequestTimeout);
this.MediaRequestTimeout = TimeSpan.FromSeconds(ConnectionPolicy.defaultMediaRequestTimeout);
this.ConnectionMode = ConnectionMode.Gateway;
this.MaxConcurrentFanoutRequests = defaultMaxConcurrentFanoutRequests;
this.MediaReadMode = MediaReadMode.Buffered;
this.UserAgentContainer = new UserAgentContainer(clientId: 0);
this.preferredLocations = new ObservableCollection<string>();
this.accountInitializationCustomEndpoints = new ObservableCollection<Uri>();
this.EnableEndpointDiscovery = true;
this.MaxConnectionLimit = defaultMaxConcurrentConnectionLimit;
this.RetryOptions = new RetryOptions();
this.EnableReadRequestsFallback = null;
this.ServerCertificateCustomValidationCallback = null;
this.AvailabilityStrategy = null;
this.CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions();
}
/// <summary>
/// Automatically populates the <see cref="PreferredLocations"/> for geo-replicated database accounts in the Azure Cosmos DB service,
/// based on the current region that the client is running in.
/// </summary>
/// <param name="location">The current region that this client is running in. E.g. "East US" </param>
public void SetCurrentLocation(string location)
{
if (!RegionProximityUtil.SourceRegionToTargetRegionsRTTInMs.ContainsKey(location))
{
throw new ArgumentException($"ApplicationRegion configuration '{location}' is not a valid Azure region or the current SDK version does not recognize it. If the value represents a valid region, make sure you are using the latest SDK version.");
}
List<string> proximityBasedPreferredLocations = RegionProximityUtil.GeneratePreferredRegionList(location);
if (proximityBasedPreferredLocations != null)
{
this.preferredLocations.Clear();
foreach (string preferredLocation in proximityBasedPreferredLocations)
{
this.preferredLocations.Add(preferredLocation);
}
}
}
public void SetPreferredLocations(IReadOnlyList<string> regions)
{
if (regions == null)
{
throw new ArgumentNullException(nameof(regions));
}
this.preferredLocations.Clear();
foreach (string preferredLocation in regions)
{
this.preferredLocations.Add(preferredLocation);
}
}
/// <summary>
/// Sets the custom private endpoints required to fetch account information from
/// private domain names.
/// </summary>
/// <param name="customEndpoints">An instance of <see cref="IEnumerable{T}"/> containing the custom DNS endpoints
/// provided by the customer.</param>
public void SetAccountInitializationCustomEndpoints(
IEnumerable<Uri> customEndpoints)
{
if (customEndpoints == null)
{
throw new ArgumentNullException(nameof(customEndpoints));
}
this.accountInitializationCustomEndpoints.Clear();
foreach (Uri endpoint in customEndpoints)
{
this.accountInitializationCustomEndpoints.Add(endpoint);
}
}
/// <summary>
/// Gets or sets the maximum number of concurrent fanout requests sent to the Azure Cosmos DB service.
/// </summary>
/// <value>Default value is 32.</value>
internal int MaxConcurrentFanoutRequests
{
get;
set;
}
/// <summary>
/// Gets or sets the request timeout in seconds when connecting to the Azure Cosmos DB service.
/// The number specifies the time to wait for response to come back from network peer.
/// </summary>
/// <value>Default value is 10 seconds.</value>
public TimeSpan RequestTimeout
{
get;
set;
}
/// <summary>
/// Gets or sets the media request timeout in seconds when connecting to the Azure Cosmos DB service.
/// The number specifies the time to wait for response to come back from network peer for attachment content (a.k.a. media) operations.
/// </summary>
/// <value>
/// Default value is 300 seconds.
/// </value>
public TimeSpan MediaRequestTimeout
{
get;
set;
}
/// <summary>
/// Gets or sets the connection mode used by the client when connecting to the Azure Cosmos DB service.
/// </summary>
/// <value>
/// Default value is <see cref="Cosmos.ConnectionMode.Gateway"/>
/// </value>
/// <remarks>
/// For more information, see <see href="https://learn.microsoft.com/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3#direct-connection">Connection policy: Use direct connection mode</see>.
/// </remarks>
public ConnectionMode ConnectionMode
{
get;
set;
}
/// <summary>
/// Gets or sets the attachment content (a.k.a. media) download mode when connecting to the Azure Cosmos DB service.
/// </summary>
/// <value>
/// Default value is <see cref="Cosmos.MediaReadMode.Buffered"/>.
/// </value>
public MediaReadMode MediaReadMode
{
get;
set;
}
/// <summary>
/// Gets or sets the connection protocol when connecting to the Azure Cosmos DB service.
/// </summary>
/// <value>
/// Default value is <see cref="Protocol.Https"/>.
/// </value>
/// <remarks>
/// This setting is not used when <see cref="ConnectionMode"/> is set to <see cref="Cosmos.ConnectionMode.Gateway"/>.
/// Gateway mode only supports HTTPS.
/// For more information, see <see href="https://learn.microsoft.com/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3#networking">Connection policy: Use the HTTPS protocol</see>.
/// </remarks>
public Protocol ConnectionProtocol
{
get
{
return this.connectionProtocol;
}
set
{
if (value != Protocol.Https && value != Protocol.Tcp)
{
throw new ArgumentOutOfRangeException("value");
}
this.connectionProtocol = value;
}
}
/// <summary>
/// Gets or sets whether to allow for reads to go to multiple regions configured on an account of Azure Cosmos DB service.
/// </summary>
/// <value>
/// Default value is null.
/// </value>
/// <remarks>
/// If this property is not set, the default is true for all Consistency Levels other than Bounded Staleness,
/// The default is false for Bounded Staleness.
/// This property only has effect if the following conditions are satisifed:
/// 1. <see cref="EnableEndpointDiscovery"/> is true
/// 2. the Azure Cosmos DB account has more than one region
/// </remarks>
public bool? EnableReadRequestsFallback
{
get;
set;
}
/// <summary>
/// Gets or sets the flag to enable address cache refresh on connection reset notification.
/// </summary>
/// <value>
/// The default value is false
/// </value>
public bool EnableTcpConnectionEndpointRediscovery
{
get;
set;
}
/// <summary>
/// Gets the default connection policy used to connect to the Azure Cosmos DB service.
/// </summary>
/// <value>
/// Refer to the default values for the individual properties of <see cref="ConnectionPolicy"/> that determine the default connection policy.
/// </value>
public static ConnectionPolicy Default
{
get
{
if (ConnectionPolicy.defaultPolicy == null)
{
ConnectionPolicy.defaultPolicy = new ConnectionPolicy();
}
return ConnectionPolicy.defaultPolicy;
}
}
/// <summary>
/// A suffix to be added to the default user-agent for the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// Setting this property after sending any request won't have any effect.
/// </remarks>
public string UserAgentSuffix
{
get
{
return this.UserAgentContainer.Suffix;
}
set
{
this.UserAgentContainer.Suffix = value;
}
}
/// <summary>
/// Gets and sets the preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service.
/// For example, "East US" as the preferred location.
/// </summary>
/// <remarks>
/// <para>
/// When <see cref="EnableEndpointDiscovery"/> is true and the value of this property is non-empty,
/// the SDK uses the locations in the collection in the order they are specified to perform operations,
/// otherwise if the value of this property is not specified,
/// the SDK uses the write region as the preferred location for all operations.
/// </para>
/// <para>
/// If <see cref="EnableEndpointDiscovery"/> is set to false, the value of this property is ignored.
/// </para>
/// </remarks>
public Collection<string> PreferredLocations
{
get
{
return this.preferredLocations;
}
}
/// <summary>
/// Gets the custom private endpoints for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// Should the global endpoint become inaccessible, the CosmosClient will attempt to obtain the account information issuing requests to the custom endpoints
/// provided in the customAccountEndpoints list.
/// </para>
/// </remarks>
public Collection<Uri> AccountInitializationCustomEndpoints
{
get
{
return this.accountInitializationCustomEndpoints;
}
}
/// <summary>
/// Gets or sets the flag to enable endpoint discovery for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// When the value of this property is true, the SDK will automatically discover the
/// current write and read regions to ensure requests are sent to the correct region
/// based on the regions specified in the <see cref="PreferredLocations"/> property.
/// <value>Default value is true indicating endpoint discovery is enabled.</value>
/// </remarks>
public bool EnableEndpointDiscovery
{
get;
set;
}
public bool EnablePartitionLevelFailover
{
get;
set;
}
public bool EnablePartitionLevelCircuitBreaker
{
get;
set;
}
internal bool DisablePartitionLevelFailoverOverride
{
get;
set;
}
/// <summary>
/// Gets or sets the certificate validation callback.
/// </summary>
internal Func<X509Certificate2, X509Chain, SslPolicyErrors, Boolean> ServerCertificateCustomValidationCallback
{
get;
set;
}
/// <summary>
/// Gets or sets the flag to enable writes on any locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// When the value of this property is true, the SDK will direct write operations to
/// available writable locations of geo-replicated database account. Writable locations
/// are ordered by <see cref="PreferredLocations"/> property. Setting the property value
/// to true has no effect until <see cref="AccountProperties.EnableMultipleWriteLocations"/>
/// is also set to true.
/// <value>Default value is false indicating that writes are only directed to
/// first region in <see cref="PreferredLocations"/> property.</value>
/// </remarks>
public bool UseMultipleWriteLocations
{
get;
set;
}
/// <summary>
/// Gets or sets the maximum number of concurrent connections allowed for the target
/// service endpoint in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// This setting is only applicable in Gateway mode.
/// </remarks>
/// <value>Default value is 50.</value>
public int MaxConnectionLimit
{
get;
set;
}
/// <summary>
/// Gets or sets the <see cref="RetryOptions"/> associated
/// with the <see cref="DocumentClient"/> in the Azure Cosmos DB service.
/// </summary>
/// <seealso cref="DocumentClient"/>
/// <seealso cref="ConnectionPolicy"/>
/// <seealso cref="RetryOptions"/>
/// <example>
/// The example below creates a new <see cref="DocumentClient"/> and sets the <see cref="ConnectionPolicy"/>
/// using the <see cref="RetryOptions"/> property.
/// <para>
/// <see cref="Cosmos.RetryOptions.MaxRetryAttemptsOnThrottledRequests"/> is set to 3, so in this case, if a request operation is rate limited by exceeding the reserved
/// throughput for the collection, the request operation retries 3 times before throwing the exception to the application.
/// <see cref="Cosmos.RetryOptions.MaxRetryWaitTimeInSeconds"/> is set to 60, so in this case if the cumulative retry
/// wait time in seconds since the first request exceeds 60 seconds, the exception is thrown.
/// </para>
/// <code language="c#">
/// <![CDATA[
/// ConnectionPolicy connectionPolicy = new ConnectionPolicy();
/// connectionPolicy.RetryOptions.MaxRetryAttemptsOnThrottledRequests = 3;
/// connectionPolicy.RetryOptions.MaxRetryWaitTimeInSeconds = 60;
///
/// DocumentClient client = new DocumentClient(new Uri("service endpoint"), "auth key", connectionPolicy);
/// ]]>
/// </code>
/// </example>
/// <value>
/// If this property is not set, the SDK uses the default retry policy that has <see cref="Cosmos.RetryOptions.MaxRetryAttemptsOnThrottledRequests"/>
/// set to 9 and <see cref="Cosmos.RetryOptions.MaxRetryWaitTimeInSeconds"/> set to 30 seconds.
/// </value>
/// <remarks>
/// For more information, see <see href="https://learn.microsoft.com/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3#429">Handle rate limiting/request rate too large</see>.
/// </remarks>
public RetryOptions RetryOptions
{
get;
set;
}
/// <summary>
/// (Direct/TCP) Controls the amount of idle time after which unused connections are closed.
/// </summary>
/// <value>
/// By default, idle connections are kept open indefinitely. Value must be greater than or equal to 10 minutes. Recommended values are between 20 minutes and 24 hours.
/// </value>
/// <remarks>
/// Mainly useful for sparse infrequent access to a large database account.
/// </remarks>
public TimeSpan? IdleTcpConnectionTimeout
{
get;
set;
}
/// <summary>
/// (Direct/TCP) Controls the amount of time allowed for trying to establish a connection.
/// </summary>
/// <value>
/// The default timeout is 5 seconds. Recommended values are greater than or equal to 5 seconds.
/// </value>
/// <remarks>
/// When the time elapses, the attempt is cancelled and an error is returned. Longer timeouts will delay retries and failures.
/// </remarks>
public TimeSpan? OpenTcpConnectionTimeout
{
get;
set;
}
/// <summary>
/// (Direct/TCP) Controls the number of requests allowed simultaneously over a single TCP connection. When more requests are in flight simultaneously, the direct/TCP client will open additional connections.
/// </summary>
/// <value>
/// The default settings allow 30 simultaneous requests per connection.
/// Do not set this value lower than 4 requests per connection or higher than 50-100 requests per connection.
/// The former can lead to a large number of connections to be created.
/// The latter can lead to head of line blocking, high latency and timeouts.
/// </value>
/// <remarks>
/// Applications with a very high degree of parallelism per connection, with large requests or responses, or with very tight latency requirements might get better performance with 8-16 requests per connection.
/// </remarks>
public int? MaxRequestsPerTcpConnection
{
get;
set;
}
/// <summary>
/// (Direct/TCP) Controls the maximum number of TCP connections that may be opened to each Cosmos DB back-end.
/// Together with MaxRequestsPerTcpConnection, this setting limits the number of requests that are simultaneously sent to a single Cosmos DB back-end(MaxRequestsPerTcpConnection x MaxTcpConnectionPerEndpoint).
/// </summary>
/// <value>
/// The default value is 65,535. Value must be greater than or equal to 16.
/// </value>
public int? MaxTcpConnectionsPerEndpoint
{
get;
set;
}
/// <summary>
/// (Direct/TCP) Controls the client port reuse policy used by the transport stack.
/// </summary>
/// <value>
/// The default value is PortReuseMode.ReuseUnicastPort.
/// </value>
public PortReuseMode? PortReuseMode
{
get;
set;
}
/// <summary>
/// Gets or sets a delegate to use to obtain an HttpClient instance to be used for HTTPS communication.
/// </summary>
public Func<HttpClient> HttpClientFactory
{
get;
set;
}
/// <summary>
/// Gets or sets the boolean flag to enable replica validation.
/// </summary>
/// <value>
/// The default value for this parameter is false.
/// </value>
public bool? EnableAdvancedReplicaSelectionForTcp
{
get;
set;
}
/// <summary>
/// Availability Strategy to be used for periods of high latency
/// </summary>
/// /// <example>
/// An example on how to set an availability strategy custom serializer.
/// <code language="c#">
/// <![CDATA[
/// CosmosClient client = new CosmosClientBuilder("connection string")
/// .WithApplicationPreferredRegions(
/// new List<string> { "East US", "Central US", "West US" } )
/// .WithAvailabilityStrategy(
/// AvailabilityStrategy.CrossRegionHedgingStrategy(
/// threshold: TimeSpan.FromMilliseconds(500),
/// thresholdStep: TimeSpan.FromMilliseconds(100)
/// ))
/// .Build();
/// ]]>
/// </code>
/// </example>
/// <remarks>
/// The availability strategy in the example is a Cross Region Hedging Strategy.
/// These strategies take two values, a threshold and a threshold step.When a request that is sent
/// out takes longer than the threshold time, the SDK will hedge to the second region in the application preferred regions list.
/// If a response from either the primary request or the first hedged request is not received
/// after the threshold step time, the SDK will hedge to the third region and so on.
/// </remarks>
public AvailabilityStrategy AvailabilityStrategy { get; set; }
/// <summary>
/// (Direct/TCP) This is an advanced setting that controls the number of TCP connections that will be opened eagerly to each Cosmos DB back-end.
/// </summary>
/// <value>
/// Default value is 1. Applications with extreme performance requirements can set this value to 2.
/// </value>
/// <remarks>
/// This setting must be used with caution. When used improperly, it can lead to client machine ephemeral port exhaustion <see href="https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-outbound-connections">Azure SNAT port exhaustion</see>.
/// </remarks>
internal int? MaxTcpPartitionCount
{
get;
set;
}
/// <summary>
/// Gets or sets Client Telemetry Options like feature flags and corresponding options
/// </summary>
internal CosmosClientTelemetryOptions CosmosClientTelemetryOptions
{
get;
set;
}
/// <summary>
/// provides SessionTokenMismatchRetryPolicy optimization through customer supplied region switch hints
/// </summary>
internal SessionRetryOptions SessionRetryOptions
{
get;
set;
}
/// <summary>
/// A string containing the application name.
/// </summary>
internal string ApplicationName
{
get;
set;
}
/// <summary>
/// GlobalEndpointManager will subscribe to this event if user updates the preferredLocations list in the Azure Cosmos DB service.
/// </summary>
internal event NotifyCollectionChangedEventHandler PreferenceChanged
{
add
{
this.preferredLocations.CollectionChanged += value;
}
remove
{
this.preferredLocations.CollectionChanged -= value;
}
}
internal RetryWithConfiguration GetRetryWithConfiguration()
{
return this.RetryOptions?.GetRetryWithConfiguration();
}
}
}