1414*/
1515
1616using System ;
17- using System . Collections . Generic ;
1817using System . Linq ;
1918using System . Threading ;
2019using System . Threading . Tasks ;
2423using MongoDB . Bson . Serialization . Serializers ;
2524using MongoDB . Driver . Core . Bindings ;
2625using MongoDB . Driver . Core . Clusters ;
27- using MongoDB . Driver . Core . Clusters . ServerSelectors ;
2826using MongoDB . Driver . Core . Misc ;
2927using MongoDB . Driver . Core . Operations ;
30- using MongoDB . Driver . Core . Servers ;
3128using MongoDB . Driver . Core . WireProtocol . Messages . Encoders ;
3229using MongoDB . Driver . Encryption ;
3330using MongoDB . Driver . Linq ;
@@ -37,23 +34,6 @@ namespace MongoDB.Driver
3734 /// <inheritdoc/>
3835 public class MongoClient : MongoClientBase
3936 {
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-
5737 // private fields
5838 private readonly ICluster _cluster ;
5939 private readonly AutoEncryptionLibMongoCryptController _libMongoCryptController ;
@@ -146,9 +126,6 @@ internal void ConfigureAutoEncryptionMessageEncoderSettings(MessageEncoderSettin
146126 }
147127 }
148128
149- // private static methods
150-
151-
152129 // public methods
153130 /// <inheritdoc/>
154131 public sealed override void DropDatabase ( string name , CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -346,32 +323,28 @@ public sealed override Task<IAsyncCursor<BsonDocument>> ListDatabasesAsync(
346323 /// <returns>A session.</returns>
347324 internal IClientSessionHandle StartImplicitSession ( CancellationToken cancellationToken )
348325 {
349- var areSessionsSupported = AreSessionsSupported ( cancellationToken ) ;
350- return StartImplicitSession ( areSessionsSupported ) ;
326+ return StartImplicitSession ( ) ;
351327 }
352328
353329 /// <summary>
354330 /// Starts an implicit session.
355331 /// </summary>
356332 /// <returns>A Task whose result is a session.</returns>
357- internal async Task < IClientSessionHandle > StartImplicitSessionAsync ( CancellationToken cancellationToken )
333+ internal Task < IClientSessionHandle > StartImplicitSessionAsync ( CancellationToken cancellationToken )
358334 {
359- var areSessionsSupported = await AreSessionsSupportedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
360- return StartImplicitSession ( areSessionsSupported ) ;
335+ return Task . FromResult ( StartImplicitSession ( ) ) ;
361336 }
362337
363338 /// <inheritdoc/>
364339 public sealed override IClientSessionHandle StartSession ( ClientSessionOptions options = null , CancellationToken cancellationToken = default ( CancellationToken ) )
365340 {
366- var areSessionsSupported = AreSessionsSupported ( cancellationToken ) ;
367- return StartSession ( options , areSessionsSupported ) ;
341+ return StartSession ( options ) ;
368342 }
369343
370344 /// <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 ) )
372346 {
373- var areSessionsSupported = await AreSessionsSupportedAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
374- return StartSession ( options , areSessionsSupported ) ;
347+ return Task . FromResult ( StartSession ( options ) ) ;
375348 }
376349
377350 /// <inheritdoc/>
@@ -446,52 +419,6 @@ public override IMongoClient WithWriteConcern(WriteConcern writeConcern)
446419 }
447420
448421 // 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-
495422 private IAsyncCursor < string > CreateDatabaseNamesCursor ( IAsyncCursor < BsonDocument > cursor )
496423 {
497424 return new BatchTransformingAsyncCursor < BsonDocument , string > (
@@ -608,15 +535,15 @@ private MessageEncoderSettings GetMessageEncoderSettings()
608535 return messageEncoderSettings ;
609536 }
610537
611- private IClientSessionHandle StartImplicitSession ( bool areSessionsSupported )
538+ private IClientSessionHandle StartImplicitSession ( )
612539 {
613540 var options = new ClientSessionOptions { CausalConsistency = false , Snapshot = false } ;
614541
615542 ICoreSessionHandle coreSession ;
616543#pragma warning disable 618
617544 var areMultipleUsersAuthenticated = _settings . Credentials . Count ( ) > 1 ;
618545#pragma warning restore
619- if ( areSessionsSupported && ! areMultipleUsersAuthenticated )
546+ if ( ! areMultipleUsersAuthenticated )
620547 {
621548 coreSession = _cluster . StartSession ( options . ToCore ( isImplicit : true ) ) ;
622549 }
@@ -628,13 +555,8 @@ private IClientSessionHandle StartImplicitSession(bool areSessionsSupported)
628555 return new ClientSessionHandle ( this , options , coreSession ) ;
629556 }
630557
631- private IClientSessionHandle StartSession ( ClientSessionOptions options , bool areSessionsSupported )
558+ private IClientSessionHandle StartSession ( ClientSessionOptions options )
632559 {
633- if ( ! areSessionsSupported )
634- {
635- throw new NotSupportedException ( "Sessions are not supported by this version of the server." ) ;
636- }
637-
638560 if ( options != null && options . Snapshot && options . CausalConsistency == true )
639561 {
640562 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
677599 return await funcAsync ( session ) . ConfigureAwait ( false ) ;
678600 }
679601 }
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- }
692602 }
693603}
0 commit comments