@@ -135,22 +135,28 @@ public override void Initialize()
135
135
// but could prevent issues of conflicting reports
136
136
// from servers that are quick to respond.
137
137
var clusterDescription = Description ;
138
+ var newServers = new List < IClusterableServer > ( ) ;
138
139
lock ( _serversLock )
139
140
{
140
141
foreach ( var endPoint in Settings . EndPoints )
141
142
{
142
- clusterDescription = EnsureServer ( clusterDescription , endPoint ) ;
143
+ clusterDescription = EnsureServer ( clusterDescription , endPoint , newServers ) ;
143
144
}
144
145
}
145
146
146
147
stopwatch . Stop ( ) ;
147
148
149
+ UpdateClusterDescription ( clusterDescription ) ;
150
+
151
+ foreach ( var server in newServers )
152
+ {
153
+ server . Initialize ( ) ;
154
+ }
155
+
148
156
if ( _openedEventHandler != null )
149
157
{
150
158
_openedEventHandler ( new ClusterOpenedEvent ( ClusterId , Settings , stopwatch . Elapsed ) ) ;
151
159
}
152
-
153
- UpdateClusterDescription ( clusterDescription ) ;
154
160
}
155
161
}
156
162
@@ -240,6 +246,7 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
240
246
return ;
241
247
}
242
248
249
+ var newServers = new List < IClusterableServer > ( ) ;
243
250
if ( newServerDescription . State == ServerState . Disconnected )
244
251
{
245
252
newClusterDescription = newClusterDescription . WithServerDescription ( newServerDescription ) ;
@@ -256,7 +263,7 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
256
263
switch ( newClusterDescription . Type )
257
264
{
258
265
case ClusterType . ReplicaSet :
259
- newClusterDescription = ProcessReplicaSetChange ( newClusterDescription , args ) ;
266
+ newClusterDescription = ProcessReplicaSetChange ( newClusterDescription , args , newServers ) ;
260
267
break ;
261
268
262
269
case ClusterType . Sharded :
@@ -274,9 +281,14 @@ private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs a
274
281
}
275
282
276
283
UpdateClusterDescription ( newClusterDescription ) ;
284
+
285
+ foreach ( var server in newServers )
286
+ {
287
+ server . Initialize ( ) ;
288
+ }
277
289
}
278
290
279
- private ClusterDescription ProcessReplicaSetChange ( ClusterDescription clusterDescription , ServerDescriptionChangedEventArgs args )
291
+ private ClusterDescription ProcessReplicaSetChange ( ClusterDescription clusterDescription , ServerDescriptionChangedEventArgs args , List < IClusterableServer > newServers )
280
292
{
281
293
if ( ! args . NewServerDescription . Type . IsReplicaSetMember ( ) )
282
294
{
@@ -299,7 +311,7 @@ private ClusterDescription ProcessReplicaSetChange(ClusterDescription clusterDes
299
311
}
300
312
301
313
clusterDescription = clusterDescription . WithServerDescription ( args . NewServerDescription ) ;
302
- clusterDescription = EnsureServers ( clusterDescription , args . NewServerDescription ) ;
314
+ clusterDescription = EnsureServers ( clusterDescription , args . NewServerDescription , newServers ) ;
303
315
304
316
if ( args . NewServerDescription . CanonicalEndPoint != null &&
305
317
! EndPointHelper . Equals ( args . NewServerDescription . CanonicalEndPoint , args . NewServerDescription . EndPoint ) )
@@ -362,7 +374,7 @@ private ClusterDescription ProcessShardedChange(ClusterDescription clusterDescri
362
374
return clusterDescription . WithServerDescription ( args . NewServerDescription ) ;
363
375
}
364
376
365
- private ClusterDescription EnsureServer ( ClusterDescription clusterDescription , EndPoint endPoint )
377
+ private ClusterDescription EnsureServer ( ClusterDescription clusterDescription , EndPoint endPoint , List < IClusterableServer > newServers )
366
378
{
367
379
if ( _state . Value == State . Disposed )
368
380
{
@@ -387,10 +399,10 @@ private ClusterDescription EnsureServer(ClusterDescription clusterDescription, E
387
399
server = CreateServer ( endPoint ) ;
388
400
server . DescriptionChanged += ServerDescriptionChangedHandler ;
389
401
_servers . Add ( server ) ;
402
+ newServers . Add ( server ) ;
390
403
}
391
404
392
405
clusterDescription = clusterDescription . WithServerDescription ( server . Description ) ;
393
- server . Initialize ( ) ;
394
406
stopwatch . Stop ( ) ;
395
407
396
408
if ( _addedServerEventHandler != null )
@@ -401,14 +413,14 @@ private ClusterDescription EnsureServer(ClusterDescription clusterDescription, E
401
413
return clusterDescription ;
402
414
}
403
415
404
- private ClusterDescription EnsureServers ( ClusterDescription clusterDescription , ServerDescription serverDescription )
416
+ private ClusterDescription EnsureServers ( ClusterDescription clusterDescription , ServerDescription serverDescription , List < IClusterableServer > newServers )
405
417
{
406
418
if ( serverDescription . Type == ServerType . ReplicaSetPrimary ||
407
419
! clusterDescription . Servers . Any ( x => x . Type == ServerType . ReplicaSetPrimary ) )
408
420
{
409
421
foreach ( var endPoint in serverDescription . ReplicaSetConfig . Members )
410
422
{
411
- clusterDescription = EnsureServer ( clusterDescription , endPoint ) ;
423
+ clusterDescription = EnsureServer ( clusterDescription , endPoint , newServers ) ;
412
424
}
413
425
}
414
426
0 commit comments