@@ -376,21 +376,17 @@ function updateRsFromPrimary(
376
376
const electionIdComparison = compareObjectId ( maxElectionId , serverDescription . electionId ) ;
377
377
const maxElectionIdIsEqual = electionIdComparison === 0 ;
378
378
const maxElectionIdIsLess = electionIdComparison === - 1 ;
379
+ const maxSetVersionIsLessOrEqual = ( maxSetVersion ?? - 1 ) <= ( serverDescription . setVersion ?? - 1 ) ;
379
380
380
- const setVersionComparison = compareNumber ( maxSetVersion , serverDescription . setVersion ) ;
381
- const maxSetVersionIsLess = setVersionComparison === - 1 ;
382
- const maxSetVersionIsEqual = setVersionComparison === 0 ;
383
-
384
- if (
385
- maxElectionIdIsLess ||
386
- ( maxElectionIdIsEqual && ( maxSetVersionIsLess || maxSetVersionIsEqual ) )
387
- ) {
388
- // We've seen a higher ElectionId! Update both!
389
- // Or the electionId is the same but the setVersion increased
381
+ if ( maxElectionIdIsLess || ( maxElectionIdIsEqual && maxSetVersionIsLessOrEqual ) ) {
382
+ // The reported electionId was greater
383
+ // or the electionId was equal and reported setVersion was greater
384
+ // Always update both values, they are a tuple
390
385
maxElectionId = serverDescription . electionId ;
391
386
maxSetVersion = serverDescription . setVersion ;
392
387
} else {
393
- // this primary is stale, we must remove it
388
+ // Stale primary
389
+ // replace serverDescription with a default ServerDescription of type "Unknown"
394
390
serverDescriptions . set (
395
391
serverDescription . address ,
396
392
new ServerDescription ( serverDescription . address )
0 commit comments