Skip to content

Commit 62ad0e2

Browse files
committed
CSHARP-1481: Raise Cluster DescriptionChanged events in a predictable order and one at a time during Initialize.
1 parent 3fb2604 commit 62ad0e2

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

src/MongoDB.Driver.Core.Tests/Core/Clusters/MultiServerClusterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ public void Description_should_be_correct_after_initialization()
8787
_capturedEvents.Next().Should().BeOfType<ClusterOpeningEvent>();
8888
_capturedEvents.Next().Should().BeOfType<ClusterAddingServerEvent>();
8989
_capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
90-
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
9190
_capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
91+
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
9292
_capturedEvents.Any().Should().BeFalse();
9393
}
9494

@@ -536,8 +536,8 @@ public void Should_call_initialize_on_all_servers()
536536
_capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
537537
_capturedEvents.Next().Should().BeOfType<ClusterAddingServerEvent>();
538538
_capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
539-
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
540539
_capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
540+
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
541541
_capturedEvents.Next().Should().BeOfType<ClusterAddingServerEvent>();
542542
_capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
543543
_capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();

src/MongoDB.Driver.Core.Tests/Core/Clusters/SingleServerClusterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public void Initialize_should_create_and_initialize_the_server()
7474
_capturedEvents.Next().Should().BeOfType<ClusterOpeningEvent>();
7575
_capturedEvents.Next().Should().BeOfType<ClusterAddingServerEvent>();
7676
_capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
77-
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
7877
_capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
78+
_capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
7979
_capturedEvents.Any().Should().BeFalse();
8080
}
8181

src/MongoDB.Driver.Core/Core/Clusters/MultiServerCluster.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/MongoDB.Driver.Core/Core/Clusters/SingleServerCluster.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,21 @@ public override void Initialize()
118118
_addingServerEventHandler(new ClusterAddingServerEvent(ClusterId, _server.EndPoint));
119119
}
120120
_server.DescriptionChanged += ServerDescriptionChanged;
121-
_server.Initialize();
122121
stopwatch.Stop();
123122

124123
if (_addedServerEventHandler != null)
125124
{
126125
_addedServerEventHandler(new ClusterAddedServerEvent(_server.ServerId, stopwatch.Elapsed));
127126
}
127+
128+
UpdateClusterDescription(newClusterDescription);
129+
130+
_server.Initialize();
131+
128132
if (_openedEventHandler != null)
129133
{
130134
_openedEventHandler(new ClusterOpenedEvent(ClusterId, Settings, stopwatch.Elapsed));
131135
}
132-
133-
UpdateClusterDescription(newClusterDescription);
134136
}
135137
}
136138

0 commit comments

Comments
 (0)