@@ -135,22 +135,28 @@ public override void Initialize()
135135 // but could prevent issues of conflicting reports
136136 // from servers that are quick to respond.
137137 var clusterDescription = Description ;
138+ var newServers = new List < IClusterableServer > ( ) ;
138139 lock ( _serversLock )
139140 {
140141 foreach ( var endPoint in Settings . EndPoints )
141142 {
142- clusterDescription = EnsureServer ( clusterDescription , endPoint ) ;
143+ clusterDescription = EnsureServer ( clusterDescription , endPoint , newServers ) ;
143144 }
144145 }
145146
146147 stopwatch . Stop ( ) ;
147148
149+ UpdateClusterDescription ( clusterDescription ) ;
150+
151+ foreach ( var server in newServers )
152+ {
153+ server . Initialize ( ) ;
154+ }
155+
148156 if ( _openedEventHandler != null )
149157 {
150158 _openedEventHandler ( new ClusterOpenedEvent ( ClusterId , Settings , stopwatch . Elapsed ) ) ;
151159 }
152-
153- UpdateClusterDescription ( clusterDescription ) ;
154160 }
155161 }
156162
@@ -240,6 +246,7 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
240246 return ;
241247 }
242248
249+ var newServers = new List < IClusterableServer > ( ) ;
243250 if ( newServerDescription . State == ServerState . Disconnected )
244251 {
245252 newClusterDescription = newClusterDescription . WithServerDescription ( newServerDescription ) ;
@@ -256,7 +263,7 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
256263 switch ( newClusterDescription . Type )
257264 {
258265 case ClusterType . ReplicaSet :
259- newClusterDescription = ProcessReplicaSetChange ( newClusterDescription , args ) ;
266+ newClusterDescription = ProcessReplicaSetChange ( newClusterDescription , args , newServers ) ;
260267 break ;
261268
262269 case ClusterType . Sharded :
@@ -274,9 +281,14 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
274281 }
275282
276283 UpdateClusterDescription ( newClusterDescription ) ;
284+
285+ foreach ( var server in newServers )
286+ {
287+ server . Initialize ( ) ;
288+ }
277289 }
278290
279- private ClusterDescription ProcessReplicaSetChange ( ClusterDescription clusterDescription , ServerDescriptionChangedEventArgs args )
291+ private ClusterDescription ProcessReplicaSetChange ( ClusterDescription clusterDescription , ServerDescriptionChangedEventArgs args , List < IClusterableServer > newServers )
280292 {
281293 if ( ! args . NewServerDescription . Type . IsReplicaSetMember ( ) )
282294 {
@@ -299,7 +311,7 @@ private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDes
299311 }
300312
301313 clusterDescription = clusterDescription . WithServerDescription ( args . NewServerDescription ) ;
302- clusterDescription = EnsureServers ( clusterDescription , args . NewServerDescription ) ;
314+ clusterDescription = EnsureServers ( clusterDescription , args . NewServerDescription , newServers ) ;
303315
304316 if ( args . NewServerDescription . CanonicalEndPoint != null &&
305317 ! EndPointHelper . Equals ( args . NewServerDescription . CanonicalEndPoint , args . NewServerDescription . EndPoint ) )
@@ -362,7 +374,7 @@ private ClusterDescription ProcessShardedChange(ClusterDescription clusterDescri
362374 return clusterDescription . WithServerDescription ( args . NewServerDescription ) ;
363375 }
364376
365- private ClusterDescription EnsureServer ( ClusterDescription clusterDescription , EndPoint endPoint )
377+ private ClusterDescription EnsureServer ( ClusterDescription clusterDescription , EndPoint endPoint , List < IClusterableServer > newServers )
366378 {
367379 if ( _state . Value == State . Disposed )
368380 {
@@ -387,10 +399,10 @@ private ClusterDescription EnsureServer(ClusterDescription clusterDescription, E
387399 server = CreateServer ( endPoint ) ;
388400 server . DescriptionChanged += ServerDescriptionChangedHandler ;
389401 _servers . Add ( server ) ;
402+ newServers . Add ( server ) ;
390403 }
391404
392405 clusterDescription = clusterDescription . WithServerDescription ( server . Description ) ;
393- server . Initialize ( ) ;
394406 stopwatch . Stop ( ) ;
395407
396408 if ( _addedServerEventHandler != null )
@@ -401,14 +413,14 @@ private ClusterDescription EnsureServer(ClusterDescription clusterDescription, E
401413 return clusterDescription ;
402414 }
403415
404- private ClusterDescription EnsureServers ( ClusterDescription clusterDescription , ServerDescription serverDescription )
416+ private ClusterDescription EnsureServers ( ClusterDescription clusterDescription , ServerDescription serverDescription , List < IClusterableServer > newServers )
405417 {
406418 if ( serverDescription . Type == ServerType . ReplicaSetPrimary ||
407419 ! clusterDescription . Servers . Any ( x => x . Type == ServerType . ReplicaSetPrimary ) )
408420 {
409421 foreach ( var endPoint in serverDescription . ReplicaSetConfig . Members )
410422 {
411- clusterDescription = EnsureServer ( clusterDescription , endPoint ) ;
423+ clusterDescription = EnsureServer ( clusterDescription , endPoint , newServers ) ;
412424 }
413425 }
414426
0 commit comments