Skip to content

Commit 96a0b76

Browse files
committed
fixup boolean logic
1 parent d499242 commit 96a0b76

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

src/sdam/topology_description.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,17 @@ function updateRsFromPrimary(
376376
const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
377377
const maxElectionIdIsEqual = electionIdComparison === 0;
378378
const maxElectionIdIsLess = electionIdComparison === -1;
379+
const maxSetVersionIsLessOrEqual = (maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);
379380

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
390385
maxElectionId = serverDescription.electionId;
391386
maxSetVersion = serverDescription.setVersion;
392387
} else {
393-
// this primary is stale, we must remove it
388+
// Stale primary
389+
// replace serverDescription with a default ServerDescription of type "Unknown"
394390
serverDescriptions.set(
395391
serverDescription.address,
396392
new ServerDescription(serverDescription.address)

0 commit comments

Comments
 (0)