Skip to content

Commit

Permalink
test: add server version filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Sep 29, 2022
1 parent 3bb7c70 commit d305339
Show file tree
Hide file tree
Showing 31 changed files with 2,026 additions and 66 deletions.
66 changes: 48 additions & 18 deletions src/sdam/topology_description.ts
Expand Up @@ -373,26 +373,56 @@ function updateRsFromPrimary(
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}

const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
const maxElectionIdIsEqual = electionIdComparison === 0;
const maxElectionIdIsLess = electionIdComparison === -1;
const maxSetVersionIsLessOrEqual = (maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);

if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) {
// The reported electionId was greater
// or the electionId was equal and reported setVersion was greater
// Always update both values, they are a tuple
maxElectionId = serverDescription.electionId;
maxSetVersion = serverDescription.setVersion;
if (serverDescription.maxWireVersion >= 17) {
const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
const maxElectionIdIsEqual = electionIdComparison === 0;
const maxElectionIdIsLess = electionIdComparison === -1;
const maxSetVersionIsLessOrEqual =
(maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);

if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) {
// The reported electionId was greater
// or the electionId was equal and reported setVersion was greater
// Always update both values, they are a tuple
maxElectionId = serverDescription.electionId;
maxSetVersion = serverDescription.setVersion;
} else {
// Stale primary
// replace serverDescription with a default ServerDescription of type "Unknown"
serverDescriptions.set(
serverDescription.address,
new ServerDescription(serverDescription.address)
);

return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}
} else {
// Stale primary
// replace serverDescription with a default ServerDescription of type "Unknown"
serverDescriptions.set(
serverDescription.address,
new ServerDescription(serverDescription.address)
);
const electionId = serverDescription.electionId ? serverDescription.electionId : null;
if (serverDescription.setVersion && electionId) {
if (maxSetVersion && maxElectionId) {
if (
maxSetVersion > serverDescription.setVersion ||
compareObjectId(maxElectionId, electionId) > 0
) {
// this primary is stale, we must remove it
serverDescriptions.set(
serverDescription.address,
new ServerDescription(serverDescription.address)
);

return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
}
}

return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
maxElectionId = serverDescription.electionId;
}

if (
serverDescription.setVersion != null &&
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion)
) {
maxSetVersion = serverDescription.setVersion;
}
}

// We've heard from the primary. Is it the same primary as before?
Expand Down
Expand Up @@ -20,7 +20,7 @@
"$oid": "000000000000000000000001"
},
"minWireVersion": 0,
"maxWireVersion": 6
"maxWireVersion": 17
}
],
[
Expand All @@ -39,7 +39,7 @@
"$oid": "000000000000000000000001"
},
"minWireVersion": 0,
"maxWireVersion": 6
"maxWireVersion": 17
}
],
[
Expand All @@ -58,7 +58,7 @@
"$oid": "000000000000000000000002"
},
"minWireVersion": 0,
"maxWireVersion": 6
"maxWireVersion": 17
}
]
],
Expand Down
Expand Up @@ -14,7 +14,7 @@ phases:
electionId:
$oid: "000000000000000000000001"
minWireVersion: 0
maxWireVersion: 6
maxWireVersion: 17
- - "b:27017"
- ok: 1
helloOk: true
Expand All @@ -27,7 +27,7 @@ phases:
electionId:
$oid: "000000000000000000000001"
minWireVersion: 0
maxWireVersion: 6
maxWireVersion: 17
- - "a:27017"
- ok: 1
helloOk: true
Expand All @@ -40,7 +40,7 @@ phases:
electionId:
$oid: "000000000000000000000002"
minWireVersion: 0
maxWireVersion: 6
maxWireVersion: 17
outcome:
servers:
"a:27017":
Expand Down
@@ -0,0 +1,203 @@
{
"description": "Pre 6.0 Primaries with and without electionIds",
"uri": "mongodb://a/?replicaSet=rs",
"phases": [
{
"responses": [
[
"a:27017",
{
"ok": 1,
"helloOk": true,
"isWritablePrimary": true,
"hosts": [
"a:27017",
"b:27017",
"c:27017"
],
"setVersion": 1,
"setName": "rs",
"minWireVersion": 0,
"maxWireVersion": 6
}
]
],
"outcome": {
"servers": {
"a:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": null
},
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"c:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
}
},
"topologyType": "ReplicaSetWithPrimary",
"logicalSessionTimeoutMinutes": null,
"setName": "rs",
"maxSetVersion": 1
}
},
{
"responses": [
[
"b:27017",
{
"ok": 1,
"helloOk": true,
"isWritablePrimary": true,
"hosts": [
"a:27017",
"b:27017",
"c:27017"
],
"setName": "rs",
"setVersion": 1,
"electionId": {
"$oid": "000000000000000000000002"
},
"minWireVersion": 0,
"maxWireVersion": 6
}
]
],
"outcome": {
"servers": {
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"b:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": {
"$oid": "000000000000000000000002"
}
},
"c:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
}
},
"topologyType": "ReplicaSetWithPrimary",
"logicalSessionTimeoutMinutes": null,
"setName": "rs",
"maxSetVersion": 1,
"maxElectionId": {
"$oid": "000000000000000000000002"
}
}
},
{
"responses": [
[
"a:27017",
{
"ok": 1,
"helloOk": true,
"isWritablePrimary": true,
"hosts": [
"a:27017",
"b:27017",
"c:27017"
],
"setVersion": 1,
"setName": "rs",
"minWireVersion": 0,
"maxWireVersion": 6
}
]
],
"outcome": {
"servers": {
"a:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": null
},
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"c:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
}
},
"topologyType": "ReplicaSetWithPrimary",
"logicalSessionTimeoutMinutes": null,
"setName": "rs",
"maxSetVersion": 1,
"maxElectionId": {
"$oid": "000000000000000000000002"
}
}
},
{
"responses": [
[
"c:27017",
{
"ok": 1,
"helloOk": true,
"isWritablePrimary": true,
"hosts": [
"a:27017",
"b:27017",
"c:27017"
],
"setName": "rs",
"setVersion": 1,
"electionId": {
"$oid": "000000000000000000000001"
},
"minWireVersion": 0,
"maxWireVersion": 6
}
]
],
"outcome": {
"servers": {
"a:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": null
},
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"c:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
}
},
"topologyType": "ReplicaSetWithPrimary",
"logicalSessionTimeoutMinutes": null,
"setName": "rs",
"maxSetVersion": 1,
"maxElectionId": {
"$oid": "000000000000000000000002"
}
}
}
]
}

0 comments on commit d305339

Please sign in to comment.