Skip to content

Commit c7c7787

Browse files
committed
Revert "Prioritize electionId over setVersion when updating topology (#886)"
This reverts commit a17c52e. JAVA-4375
1 parent 0e5a87e commit c7c7787

10 files changed

+76
-575
lines changed

driver-core/src/main/com/mongodb/internal/connection/AbstractMultiServerCluster.java

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
224224
}
225225

226226
if (newDescription.getType() == REPLICA_SET_GHOST) {
227-
LOGGER.info(format("Server %s does not appear to be a member of an initiated replica set.", newDescription.getAddress()));
227+
if (LOGGER.isInfoEnabled()) {
228+
LOGGER.info(format("Server %s does not appear to be a member of an initiated replica set.", newDescription.getAddress()));
229+
}
228230
return true;
229231
}
230232

@@ -245,25 +247,46 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
245247
if (newDescription.getCanonicalAddress() != null
246248
&& !newDescription.getAddress().equals(new ServerAddress(newDescription.getCanonicalAddress()))
247249
&& !newDescription.isPrimary()) {
248-
LOGGER.info(format("Canonical address %s does not match server address. Removing %s from client view of cluster",
249-
newDescription.getCanonicalAddress(), newDescription.getAddress()));
250+
if (LOGGER.isInfoEnabled()) {
251+
LOGGER.info(format("Canonical address %s does not match server address. Removing %s from client view of cluster",
252+
newDescription.getCanonicalAddress(), newDescription.getAddress()));
253+
}
250254
removeServer(newDescription.getAddress());
251255
return true;
252256
}
253257

254258
if (newDescription.isPrimary()) {
255-
if (isNonStalePrimary(newDescription.getElectionId(), newDescription.getSetVersion())) {
256-
LOGGER.info(format("Setting max election id to %s and max set version to %d from replica set primary %s",
257-
newDescription.getElectionId(), newDescription.getSetVersion(), newDescription.getAddress()));
258-
maxElectionId = newDescription.getElectionId();
259-
maxSetVersion = newDescription.getSetVersion();
260-
} else {
261-
LOGGER.info(format("Invalidating potential primary %s whose (set version, election id) tuple of (%d, %s) "
259+
ObjectId electionId = newDescription.getElectionId();
260+
Integer setVersion = newDescription.getSetVersion();
261+
if (setVersion != null && electionId != null) {
262+
if (isStalePrimary(newDescription)) {
263+
if (LOGGER.isInfoEnabled()) {
264+
LOGGER.info(format("Invalidating potential primary %s whose (set version, election id) tuple of (%d, %s) "
262265
+ "is less than one already seen of (%d, %s)",
263-
newDescription.getAddress(), newDescription.getSetVersion(), newDescription.getElectionId(),
264-
maxSetVersion, maxElectionId));
265-
addressToServerTupleMap.get(newDescription.getAddress()).server.resetToConnecting();
266-
return false;
266+
newDescription.getAddress(),
267+
setVersion, electionId,
268+
maxSetVersion, maxElectionId));
269+
}
270+
addressToServerTupleMap.get(newDescription.getAddress()).server.resetToConnecting();
271+
return false;
272+
}
273+
274+
if (!electionId.equals(maxElectionId)) {
275+
if (LOGGER.isInfoEnabled()) {
276+
LOGGER.info(format("Setting max election id to %s from replica set primary %s", electionId,
277+
newDescription.getAddress()));
278+
}
279+
maxElectionId = electionId;
280+
}
281+
}
282+
283+
if (setVersion != null
284+
&& (maxSetVersion == null || setVersion.compareTo(maxSetVersion) > 0)) {
285+
if (LOGGER.isInfoEnabled()) {
286+
LOGGER.info(format("Setting max set version to %d from replica set primary %s", setVersion,
287+
newDescription.getAddress()));
288+
}
289+
maxSetVersion = setVersion;
267290
}
268291

269292
if (isNotAlreadyPrimary(newDescription.getAddress())) {
@@ -274,23 +297,14 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
274297
return true;
275298
}
276299

277-
private boolean isNonStalePrimary(final ObjectId electionId, final Integer setVersion) {
278-
return nullSafeCompareTo(electionId, maxElectionId) > 0
279-
|| (nullSafeCompareTo(electionId, maxElectionId) == 0 && nullSafeCompareTo(setVersion, maxSetVersion) >= 0);
280-
}
281-
282-
/**
283-
* Implements the same contract as {@link Comparable#compareTo(Object)}, except that a null value is always considers less-than any
284-
* other value (except null, which it considers as equal-to).
285-
*/
286-
private static <T extends Comparable<T>> int nullSafeCompareTo(final T first, final T second) {
287-
if (first == null) {
288-
return second == null ? 0 : -1;
300+
private boolean isStalePrimary(final ServerDescription newDescription) {
301+
if (maxSetVersion == null || maxElectionId == null) {
302+
return false;
289303
}
290-
if (second == null) {
291-
return 1;
292-
}
293-
return first.compareTo(second);
304+
305+
Integer setVersion = newDescription.getSetVersion();
306+
return (setVersion == null || maxSetVersion.compareTo(setVersion) > 0
307+
|| (maxSetVersion.equals(setVersion) && maxElectionId.compareTo(newDescription.getElectionId()) > 0));
294308
}
295309

296310
private boolean isNotAlreadyPrimary(final ServerAddress address) {

driver-core/src/test/resources/server-discovery-and-monitoring/rs/electionId_precedence_setVersion.json

Lines changed: 0 additions & 92 deletions
This file was deleted.

driver-core/src/test/resources/server-discovery-and-monitoring/rs/electionId_setVersion.json

Lines changed: 0 additions & 92 deletions
This file was deleted.

driver-core/src/test/resources/server-discovery-and-monitoring/rs/null_election_id.json

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,15 @@
123123
"outcome": {
124124
"servers": {
125125
"a:27017": {
126-
"type": "Unknown",
127-
"setName": null,
128-
"setVersion": null,
129-
"electionId": null
130-
},
131-
"b:27017": {
132126
"type": "RSPrimary",
133127
"setName": "rs",
134128
"setVersion": 1,
135-
"electionId": {
136-
"$oid": "000000000000000000000002"
137-
}
129+
"electionId": null
130+
},
131+
"b:27017": {
132+
"type": "Unknown",
133+
"setName": null,
134+
"electionId": null
138135
},
139136
"c:27017": {
140137
"type": "Unknown",
@@ -177,18 +174,15 @@
177174
"outcome": {
178175
"servers": {
179176
"a:27017": {
180-
"type": "Unknown",
181-
"setName": null,
182-
"setVersion": null,
183-
"electionId": null
184-
},
185-
"b:27017": {
186177
"type": "RSPrimary",
187178
"setName": "rs",
188179
"setVersion": 1,
189-
"electionId": {
190-
"$oid": "000000000000000000000002"
191-
}
180+
"electionId": null
181+
},
182+
"b:27017": {
183+
"type": "Unknown",
184+
"setName": null,
185+
"electionId": null
192186
},
193187
"c:27017": {
194188
"type": "Unknown",

driver-core/src/test/resources/server-discovery-and-monitoring/rs/secondary_ignore_ok_0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "Secondary ignored when ok is zero",
2+
"description": "New primary",
33
"uri": "mongodb://a,b/?replicaSet=rs",
44
"phases": [
55
{

0 commit comments

Comments
 (0)