Skip to content

Commit 38ec3b1

Browse files
committed
JAVA-1216: Removed special handling of replica set version. We ignore non-primary's host lists if there is a primary that we are connected to. We also never remove servers based on a non-primary's host list.
1 parent dcb0c90 commit 38ec3b1

9 files changed

+126
-68
lines changed

src/main/com/mongodb/DefaultServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.TimeUnit;
2626

2727
import static com.mongodb.ServerConnectionState.Connecting;
28-
import static com.mongodb.ServerConnectionState.Unconnected;
2928
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3029
import static org.bson.util.Assertions.isTrue;
3130
import static org.bson.util.Assertions.notNull;
@@ -129,7 +128,7 @@ public boolean isClosed() {
129128
}
130129

131130
private void setHeartbeat(final ChangeEvent<ServerDescription> event) {
132-
HeartbeatFrequency heartbeatFrequency = event.getNewValue().getState() == Unconnected
131+
HeartbeatFrequency heartbeatFrequency = event.getNewValue().getState() == Connecting
133132
? HeartbeatFrequency.RETRY
134133
: HeartbeatFrequency.NORMAL;
135134
long initialDelay = heartbeatFrequency.getFrequencyMS(settings);

src/main/com/mongodb/MultiServerCluster.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static com.mongodb.ClusterType.Sharded;
3030
import static com.mongodb.ClusterType.Unknown;
3131
import static com.mongodb.ServerConnectionState.Connecting;
32+
import static com.mongodb.ServerType.ReplicaSetGhost;
3233
import static com.mongodb.ServerType.ShardRouter;
3334
import static java.lang.String.format;
3435
import static org.bson.util.Assertions.isTrue;
@@ -41,7 +42,6 @@ final class MultiServerCluster extends BaseCluster {
4142

4243
private ClusterType clusterType;
4344
private String replicaSetName;
44-
private int latestSetVersion = Integer.MIN_VALUE;
4545
private final ConcurrentMap<ServerAddress, ServerTuple> addressToServerTupleMap =
4646
new ConcurrentHashMap<ServerAddress, ServerTuple>();
4747

@@ -152,7 +152,7 @@ private void handleReplicaSetMemberChanged(final ServerDescription newDescriptio
152152
return;
153153
}
154154

155-
if (newDescription.getHosts().isEmpty() || newDescription.getSetName() == null) {
155+
if (newDescription.getType() == ReplicaSetGhost) {
156156
LOGGER.info(format("Server %s does not appear to be a member of an initiated replica set.", newDescription.getAddress()));
157157
return;
158158
}
@@ -164,18 +164,13 @@ private void handleReplicaSetMemberChanged(final ServerDescription newDescriptio
164164
if (!replicaSetName.equals(newDescription.getSetName())) {
165165
LOGGER.severe(format("Expecting replica set member from set '%s', but found one from set '%s'. "
166166
+ "Removing %s from client view of cluster.",
167-
replicaSetName, newDescription.getSetName(), newDescription.getAddress()));
167+
replicaSetName, newDescription.getSetName(), newDescription.getAddress()
168+
));
168169
removeServer(newDescription.getAddress());
169170
return;
170171
}
171172

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);
179174

180175
if (newDescription.isPrimary()) {
181176
if (isNotAlreadyPrimary(newDescription.getAddress())) {
@@ -225,7 +220,6 @@ private void invalidateOldPrimaries(final ServerAddress newPrimary) {
225220
if (!serverTuple.description.getAddress().equals(newPrimary) && serverTuple.description.isPrimary()) {
226221
LOGGER.info(format("Rediscovering type of existing primary %s", serverTuple.description.getAddress()));
227222
serverTuple.server.invalidate();
228-
serverTuple.description = getConnectingServerDescription(serverTuple.description.getAddress());
229223
}
230224
}
231225
}
@@ -248,9 +242,24 @@ private List<ServerDescription> getNewServerDescriptionList() {
248242
}
249243

250244
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;
254263
}
255264

256265
private void addNewHosts(final Set<String> hosts) {
@@ -263,7 +272,7 @@ private void addNewHosts(final Set<String> hosts) {
263272
}
264273
}
265274

266-
private void removeExtras(final ServerDescription serverDescription) {
275+
private void removeExtraHosts(final ServerDescription serverDescription) {
267276
Set<ServerAddress> allServerAddresses = getAllServerAddresses(serverDescription);
268277
for (ServerTuple cur : addressToServerTupleMap.values()) {
269278
if (!allServerAddresses.contains(cur.description.getAddress())) {

src/main/com/mongodb/ServerConnectionState.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ enum ServerConnectionState {
2222
*/
2323
Connecting,
2424

25-
/**
26-
* The application failed in its last attempt to connect to the remote server
27-
*/
28-
Unconnected,
29-
3025
/**
3126
* The application is connected to the remote server.
3227
*/

src/main/com/mongodb/ServerDescription.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static com.mongodb.ServerConnectionState.Connected;
2929
import static com.mongodb.ServerType.ReplicaSetArbiter;
30+
import static com.mongodb.ServerType.ReplicaSetGhost;
3031
import static com.mongodb.ServerType.ReplicaSetOther;
3132
import static com.mongodb.ServerType.ReplicaSetPrimary;
3233
import static com.mongodb.ServerType.ReplicaSetSecondary;
@@ -67,7 +68,6 @@ class ServerDescription {
6768
private final boolean ok;
6869
private final ServerConnectionState state;
6970
private final ServerVersion version;
70-
private final Integer setVersion;
7171

7272
private final int minWireVersion;
7373
private final int maxWireVersion;
@@ -84,7 +84,6 @@ static class Builder {
8484
private int maxWriteBatchSize = DEFAULT_MAX_WRITE_BATCH_SIZE;
8585
private Tags tags = Tags.freeze(new Tags());
8686
private String setName;
87-
private Integer setVersion;
8887
private long averageLatency;
8988
private boolean ok;
9089
private ServerConnectionState state;
@@ -153,11 +152,6 @@ public Builder setName(final String setName) {
153152
return this;
154153
}
155154

156-
public Builder setVersion(final Integer setVersion) {
157-
this.setVersion = setVersion;
158-
return this;
159-
}
160-
161155
public Builder ok(final boolean ok) {
162156
this.ok = ok;
163157
return this;
@@ -241,7 +235,8 @@ public ServerAddress getAddress() {
241235
}
242236

243237
public boolean isReplicaSetMember() {
244-
return (type == ReplicaSetPrimary || type == ReplicaSetSecondary || type == ReplicaSetArbiter || type == ReplicaSetOther);
238+
return (type == ReplicaSetPrimary || type == ReplicaSetSecondary || type == ReplicaSetArbiter || type == ReplicaSetOther
239+
|| type == ReplicaSetGhost);
245240
}
246241

247242
public boolean isShardRouter() {
@@ -350,10 +345,6 @@ public ServerVersion getVersion() {
350345
return version;
351346
}
352347

353-
public Integer getSetVersion() {
354-
return setVersion;
355-
}
356-
357348
public long getAverageLatencyNanos() {
358349
return averageLatencyNanos;
359350
}
@@ -405,9 +396,6 @@ public boolean equals(final Object o) {
405396
if (setName != null ? !setName.equals(that.setName) : that.setName != null) {
406397
return false;
407398
}
408-
if (setVersion != null ? !setVersion.equals(that.setVersion) : that.setVersion != null) {
409-
return false;
410-
}
411399
if (state != that.state) {
412400
return false;
413401
}
@@ -444,7 +432,6 @@ public int hashCode() {
444432
result = 31 * result + maxWriteBatchSize;
445433
result = 31 * result + tags.hashCode();
446434
result = 31 * result + (setName != null ? setName.hashCode() : 0);
447-
result = 31 * result + (setVersion != null ? setVersion.hashCode() : 0);
448435
result = 31 * result + (ok ? 1 : 0);
449436
result = 31 * result + state.hashCode();
450437
result = 31 * result + version.hashCode();
@@ -467,7 +454,6 @@ public String toString() {
467454
+ ", maxWriteBatchSize=" + maxWriteBatchSize
468455
+ ", tags=" + tags
469456
+ ", setName='" + setName + '\''
470-
+ ", setVersion='" + setVersion + '\''
471457
+ ", averageLatencyNanos=" + averageLatencyNanos
472458
+ ", ok=" + ok
473459
+ ", state=" + state
@@ -505,7 +491,6 @@ private String getAverageLatencyFormattedInMilliseconds() {
505491
maxWriteBatchSize = builder.maxWriteBatchSize;
506492
tags = builder.tags;
507493
setName = builder.setName;
508-
setVersion = builder.setVersion;
509494
averageLatencyNanos = builder.averageLatency;
510495
ok = builder.ok;
511496
minWireVersion = builder.minWireVersion;

src/main/com/mongodb/ServerStateNotifier.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void run() {
9090
}
9191
} catch (Throwable t) {
9292
throwable = t;
93-
serverDescription = getUnconnectedServerDescription();
93+
serverDescription = getConnectingServerDescription();
9494
}
9595

9696
if (!isClosed) {
@@ -191,7 +191,11 @@ private static ServerType getServerType(final BasicDBObject isMasterResult) {
191191
return ServerType.ReplicaSetArbiter;
192192
}
193193

194-
return ServerType.ReplicaSetOther;
194+
if (isMasterResult.containsKey("setName") && isMasterResult.containsField("hosts")) {
195+
return ServerType.ReplicaSetOther;
196+
}
197+
198+
return ServerType.ReplicaSetGhost;
195199
}
196200

197201
if (isMasterResult.containsKey("msg") && isMasterResult.get("msg").equals("isdbgrid")) {
@@ -219,8 +223,4 @@ private static Tags getTagsFromDocument(final DBObject tagsDocuments) {
219223
private ServerDescription getConnectingServerDescription() {
220224
return ServerDescription.builder().type(ServerType.Unknown).state(ServerConnectionState.Connecting).address(serverAddress).build();
221225
}
222-
223-
private ServerDescription getUnconnectedServerDescription() {
224-
return ServerDescription.builder().type(ServerType.Unknown).state(ServerConnectionState.Unconnected).address(serverAddress).build();
225-
}
226226
}

src/main/com/mongodb/ServerType.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public ClusterType getClusterType() {
7070
}
7171
},
7272

73+
/**
74+
* A replica set member that does not report a set name or a hosts list
75+
*/
76+
ReplicaSetGhost {
77+
@Override
78+
public ClusterType getClusterType() {
79+
return ClusterType.ReplicaSet;
80+
}
81+
},
82+
7383
/**
7484
* A router to a sharded cluster, i.e. a mongos server.
7585
*/

0 commit comments

Comments
 (0)