29
29
import static com .mongodb .ClusterType .Sharded ;
30
30
import static com .mongodb .ClusterType .Unknown ;
31
31
import static com .mongodb .ServerConnectionState .Connecting ;
32
+ import static com .mongodb .ServerType .ReplicaSetGhost ;
32
33
import static com .mongodb .ServerType .ShardRouter ;
33
34
import static java .lang .String .format ;
34
35
import static org .bson .util .Assertions .isTrue ;
@@ -41,7 +42,6 @@ final class MultiServerCluster extends BaseCluster {
41
42
42
43
private ClusterType clusterType ;
43
44
private String replicaSetName ;
44
- private int latestSetVersion = Integer .MIN_VALUE ;
45
45
private final ConcurrentMap <ServerAddress , ServerTuple > addressToServerTupleMap =
46
46
new ConcurrentHashMap <ServerAddress , ServerTuple >();
47
47
@@ -152,7 +152,7 @@ private void handleReplicaSetMemberChanged(final ServerDescription newDescriptio
152
152
return ;
153
153
}
154
154
155
- if (newDescription .getHosts (). isEmpty () || newDescription . getSetName () == null ) {
155
+ if (newDescription .getType () == ReplicaSetGhost ) {
156
156
LOGGER .info (format ("Server %s does not appear to be a member of an initiated replica set." , newDescription .getAddress ()));
157
157
return ;
158
158
}
@@ -164,18 +164,13 @@ private void handleReplicaSetMemberChanged(final ServerDescription newDescriptio
164
164
if (!replicaSetName .equals (newDescription .getSetName ())) {
165
165
LOGGER .severe (format ("Expecting replica set member from set '%s', but found one from set '%s'. "
166
166
+ "Removing %s from client view of cluster." ,
167
- replicaSetName , newDescription .getSetName (), newDescription .getAddress ()));
167
+ replicaSetName , newDescription .getSetName (), newDescription .getAddress ()
168
+ ));
168
169
removeServer (newDescription .getAddress ());
169
170
return ;
170
171
}
171
172
172
- if (newDescription .getSetVersion () == null || newDescription .getSetVersion () > latestSetVersion ) {
173
- if (newDescription .getSetVersion () != null ) {
174
- latestSetVersion = newDescription .getSetVersion ();
175
- }
176
-
177
- ensureServers (newDescription );
178
- }
173
+ ensureServers (newDescription );
179
174
180
175
if (newDescription .isPrimary ()) {
181
176
if (isNotAlreadyPrimary (newDescription .getAddress ())) {
@@ -225,7 +220,6 @@ private void invalidateOldPrimaries(final ServerAddress newPrimary) {
225
220
if (!serverTuple .description .getAddress ().equals (newPrimary ) && serverTuple .description .isPrimary ()) {
226
221
LOGGER .info (format ("Rediscovering type of existing primary %s" , serverTuple .description .getAddress ()));
227
222
serverTuple .server .invalidate ();
228
- serverTuple .description = getConnectingServerDescription (serverTuple .description .getAddress ());
229
223
}
230
224
}
231
225
}
@@ -248,9 +242,24 @@ private List<ServerDescription> getNewServerDescriptionList() {
248
242
}
249
243
250
244
private void ensureServers (final ServerDescription description ) {
251
- addNewHosts (description .getHosts ());
252
- addNewHosts (description .getPassives ());
253
- removeExtras (description );
245
+ if (description .isPrimary () || !hasPrimary ()) {
246
+ addNewHosts (description .getHosts ());
247
+ addNewHosts (description .getPassives ());
248
+ addNewHosts (description .getArbiters ());
249
+ }
250
+
251
+ if (description .isPrimary ()) {
252
+ removeExtraHosts (description );
253
+ }
254
+ }
255
+
256
+ private boolean hasPrimary () {
257
+ for (ServerTuple serverTuple : addressToServerTupleMap .values ()) {
258
+ if (serverTuple .description .isPrimary ()) {
259
+ return true ;
260
+ }
261
+ }
262
+ return false ;
254
263
}
255
264
256
265
private void addNewHosts (final Set <String > hosts ) {
@@ -263,7 +272,7 @@ private void addNewHosts(final Set<String> hosts) {
263
272
}
264
273
}
265
274
266
- private void removeExtras (final ServerDescription serverDescription ) {
275
+ private void removeExtraHosts (final ServerDescription serverDescription ) {
267
276
Set <ServerAddress > allServerAddresses = getAllServerAddresses (serverDescription );
268
277
for (ServerTuple cur : addressToServerTupleMap .values ()) {
269
278
if (!allServerAddresses .contains (cur .description .getAddress ())) {
0 commit comments