14
14
*/
15
15
16
16
using System ;
17
- using System . Collections . Generic ;
18
17
using System . Linq ;
19
18
using System . Threading ;
20
19
using System . Threading . Tasks ;
24
23
using MongoDB . Bson . Serialization . Serializers ;
25
24
using MongoDB . Driver . Core . Bindings ;
26
25
using MongoDB . Driver . Core . Clusters ;
27
- using MongoDB . Driver . Core . Clusters . ServerSelectors ;
28
26
using MongoDB . Driver . Core . Misc ;
29
27
using MongoDB . Driver . Core . Operations ;
30
- using MongoDB . Driver . Core . Servers ;
31
28
using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
32
29
using MongoDB . Driver . Encryption ;
33
30
using MongoDB . Driver . Linq ;
@@ -37,23 +34,6 @@ namespace MongoDB.Driver
37
34
/// <inheritdoc/>
38
35
public class MongoClient : MongoClientBase
39
36
{
40
- #region static
41
- // private static methods
42
- private static IEnumerable < ServerDescription > SelectServersThatDetermineWhetherSessionsAreSupported ( ClusterDescription cluster , IEnumerable < ServerDescription > servers )
43
- {
44
- var connectedServers = servers . Where ( s => s . State == ServerState . Connected ) ;
45
-
46
- if ( cluster . IsDirectConnection )
47
- {
48
- return connectedServers ;
49
- }
50
- else
51
- {
52
- return connectedServers . Where ( s => s . IsDataBearing ) ;
53
- }
54
- }
55
- #endregion
56
-
57
37
// private fields
58
38
private readonly ICluster _cluster ;
59
39
private readonly AutoEncryptionLibMongoCryptController _libMongoCryptController ;
@@ -146,9 +126,6 @@ internal void ConfigureAutoEncryptionMessageEncoderSettings(MessageEncoderSettin
146
126
}
147
127
}
148
128
149
- // private static methods
150
-
151
-
152
129
// public methods
153
130
/// <inheritdoc/>
154
131
public sealed override void DropDatabase ( string name , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -346,32 +323,28 @@ public sealed override Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
346
323
/// <returns>A session.</returns>
347
324
internal IClientSessionHandle StartImplicitSession ( CancellationToken cancellationToken )
348
325
{
349
- var areSessionsSupported = AreSessionsSupported ( cancellationToken ) ;
350
- return StartImplicitSession ( areSessionsSupported ) ;
326
+ return StartImplicitSession ( ) ;
351
327
}
352
328
353
329
/// <summary>
354
330
/// Starts an implicit session.
355
331
/// </summary>
356
332
/// <returns>A Task whose result is a session.</returns>
357
- internal async Task < IClientSessionHandle > StartImplicitSessionAsync ( CancellationToken cancellationToken )
333
+ internal Task < IClientSessionHandle > StartImplicitSessionAsync ( CancellationToken cancellationToken )
358
334
{
359
- var areSessionsSupported = await AreSessionsSupportedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
360
- return StartImplicitSession ( areSessionsSupported ) ;
335
+ return Task . FromResult ( StartImplicitSession ( ) ) ;
361
336
}
362
337
363
338
/// <inheritdoc/>
364
339
public sealed override IClientSessionHandle StartSession ( ClientSessionOptions options = null , CancellationToken cancellationToken = default ( CancellationToken ) )
365
340
{
366
- var areSessionsSupported = AreSessionsSupported ( cancellationToken ) ;
367
- return StartSession ( options , areSessionsSupported ) ;
341
+ return StartSession ( options ) ;
368
342
}
369
343
370
344
/// <inheritdoc/>
371
- public sealed override async Task < IClientSessionHandle > StartSessionAsync ( ClientSessionOptions options = null , CancellationToken cancellationToken = default ( CancellationToken ) )
345
+ public sealed override Task < IClientSessionHandle > StartSessionAsync ( ClientSessionOptions options = null , CancellationToken cancellationToken = default ( CancellationToken ) )
372
346
{
373
- var areSessionsSupported = await AreSessionsSupportedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
374
- return StartSession ( options , areSessionsSupported ) ;
347
+ return Task . FromResult ( StartSession ( options ) ) ;
375
348
}
376
349
377
350
/// <inheritdoc/>
@@ -446,52 +419,6 @@ public override IMongoClient WithWriteConcern(WriteConcern writeConcern)
446
419
}
447
420
448
421
// private methods
449
- private bool AreSessionsSupported ( CancellationToken cancellationToken )
450
- {
451
- return AreSessionsSupported ( _cluster . Description ) ?? AreSessionsSupportedAfterServerSelection ( cancellationToken ) ;
452
- }
453
-
454
- private async Task < bool > AreSessionsSupportedAsync ( CancellationToken cancellationToken )
455
- {
456
- return AreSessionsSupported ( _cluster . Description ) ?? await AreSessionsSupportedAfterServerSelectionAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
457
- }
458
-
459
- private bool ? AreSessionsSupported ( ClusterDescription clusterDescription )
460
- {
461
- if ( clusterDescription . LogicalSessionTimeout . HasValue || clusterDescription . Type == ClusterType . LoadBalanced )
462
- {
463
- return true ;
464
- }
465
- else
466
- {
467
- var selectedServers = SelectServersThatDetermineWhetherSessionsAreSupported ( clusterDescription , clusterDescription . Servers ) . ToList ( ) ;
468
- if ( selectedServers . Count == 0 )
469
- {
470
- return null ;
471
- }
472
- else
473
- {
474
- return false ;
475
- }
476
- }
477
- }
478
-
479
- private bool AreSessionsSupportedAfterServerSelection ( CancellationToken cancellationToken )
480
- {
481
- var selector = new AreSessionsSupportedServerSelector ( ) ;
482
- var selectedServer = _cluster . SelectServer ( selector , cancellationToken ) ;
483
- var clusterDescription = selector . ClusterDescription ?? _cluster . Description ; // LB cluster doesn't use server selector, so clusterDescription is null for this case
484
- return AreSessionsSupported ( clusterDescription ) ?? false ;
485
- }
486
-
487
- private async Task < bool > AreSessionsSupportedAfterServerSelectionAsync ( CancellationToken cancellationToken )
488
- {
489
- var selector = new AreSessionsSupportedServerSelector ( ) ;
490
- var selectedServer = await _cluster . SelectServerAsync ( selector , cancellationToken ) . ConfigureAwait ( false ) ;
491
- var clusterDescription = selector . ClusterDescription ?? _cluster . Description ; // LB cluster doesn't use server selector, so clusterDescription is null for this case
492
- return AreSessionsSupported ( clusterDescription ) ?? false ;
493
- }
494
-
495
422
private IAsyncCursor < string > CreateDatabaseNamesCursor ( IAsyncCursor < BsonDocument > cursor )
496
423
{
497
424
return new BatchTransformingAsyncCursor < BsonDocument , string > (
@@ -608,15 +535,15 @@ private MessageEncoderSettings GetMessageEncoderSettings()
608
535
return messageEncoderSettings ;
609
536
}
610
537
611
- private IClientSessionHandle StartImplicitSession ( bool areSessionsSupported )
538
+ private IClientSessionHandle StartImplicitSession ( )
612
539
{
613
540
var options = new ClientSessionOptions { CausalConsistency = false , Snapshot = false } ;
614
541
615
542
ICoreSessionHandle coreSession ;
616
543
#pragma warning disable 618
617
544
var areMultipleUsersAuthenticated = _settings . Credentials . Count ( ) > 1 ;
618
545
#pragma warning restore
619
- if ( areSessionsSupported && ! areMultipleUsersAuthenticated )
546
+ if ( ! areMultipleUsersAuthenticated )
620
547
{
621
548
coreSession = _cluster . StartSession ( options . ToCore ( isImplicit : true ) ) ;
622
549
}
@@ -628,13 +555,8 @@ private IClientSessionHandle StartImplicitSession(bool areSessionsSupported)
628
555
return new ClientSessionHandle ( this , options , coreSession ) ;
629
556
}
630
557
631
- private IClientSessionHandle StartSession ( ClientSessionOptions options , bool areSessionsSupported )
558
+ private IClientSessionHandle StartSession ( ClientSessionOptions options )
632
559
{
633
- if ( ! areSessionsSupported )
634
- {
635
- throw new NotSupportedException ( "Sessions are not supported by this version of the server." ) ;
636
- }
637
-
638
560
if ( options != null && options . Snapshot && options . CausalConsistency == true )
639
561
{
640
562
throw new NotSupportedException ( "Combining both causal consistency and snapshot options is not supported." ) ;
@@ -677,17 +599,5 @@ private async Task<TResult> UsingImplicitSessionAsync<TResult>(Func<IClientSessi
677
599
return await funcAsync ( session ) . ConfigureAwait ( false ) ;
678
600
}
679
601
}
680
-
681
- // nested types
682
- private class AreSessionsSupportedServerSelector : IServerSelector
683
- {
684
- public ClusterDescription ClusterDescription ;
685
-
686
- public IEnumerable < ServerDescription > SelectServers ( ClusterDescription cluster , IEnumerable < ServerDescription > servers )
687
- {
688
- ClusterDescription = cluster ;
689
- return SelectServersThatDetermineWhetherSessionsAreSupported ( cluster , servers ) ;
690
- }
691
- }
692
602
}
693
603
}
0 commit comments