@@ -224,7 +224,9 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
224
224
}
225
225
226
226
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
+ }
228
230
return true ;
229
231
}
230
232
@@ -245,25 +247,46 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
245
247
if (newDescription .getCanonicalAddress () != null
246
248
&& !newDescription .getAddress ().equals (new ServerAddress (newDescription .getCanonicalAddress ()))
247
249
&& !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
+ }
250
254
removeServer (newDescription .getAddress ());
251
255
return true ;
252
256
}
253
257
254
258
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) "
262
265
+ "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 ;
267
290
}
268
291
269
292
if (isNotAlreadyPrimary (newDescription .getAddress ())) {
@@ -274,23 +297,14 @@ private boolean handleReplicaSetMemberChanged(final ServerDescription newDescrip
274
297
return true ;
275
298
}
276
299
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 ;
289
303
}
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 ) );
294
308
}
295
309
296
310
private boolean isNotAlreadyPrimary (final ServerAddress address ) {
0 commit comments