Skip to content

Commit b0377f4

Browse files
committed
test: add server version filtering
1 parent 3bb7c70 commit b0377f4

File tree

265 files changed

+10372
-565
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

265 files changed

+10372
-565
lines changed

src/sdam/topology_description.ts

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,26 +373,56 @@ function updateRsFromPrimary(
373373
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
374374
}
375375

376-
const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
377-
const maxElectionIdIsEqual = electionIdComparison === 0;
378-
const maxElectionIdIsLess = electionIdComparison === -1;
379-
const maxSetVersionIsLessOrEqual = (maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);
380-
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
385-
maxElectionId = serverDescription.electionId;
386-
maxSetVersion = serverDescription.setVersion;
376+
if (serverDescription.maxWireVersion >= 17) {
377+
const electionIdComparison = compareObjectId(maxElectionId, serverDescription.electionId);
378+
const maxElectionIdIsEqual = electionIdComparison === 0;
379+
const maxElectionIdIsLess = electionIdComparison === -1;
380+
const maxSetVersionIsLessOrEqual =
381+
(maxSetVersion ?? -1) <= (serverDescription.setVersion ?? -1);
382+
383+
if (maxElectionIdIsLess || (maxElectionIdIsEqual && maxSetVersionIsLessOrEqual)) {
384+
// The reported electionId was greater
385+
// or the electionId was equal and reported setVersion was greater
386+
// Always update both values, they are a tuple
387+
maxElectionId = serverDescription.electionId;
388+
maxSetVersion = serverDescription.setVersion;
389+
} else {
390+
// Stale primary
391+
// replace serverDescription with a default ServerDescription of type "Unknown"
392+
serverDescriptions.set(
393+
serverDescription.address,
394+
new ServerDescription(serverDescription.address)
395+
);
396+
397+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
398+
}
387399
} else {
388-
// Stale primary
389-
// replace serverDescription with a default ServerDescription of type "Unknown"
390-
serverDescriptions.set(
391-
serverDescription.address,
392-
new ServerDescription(serverDescription.address)
393-
);
400+
const electionId = serverDescription.electionId ? serverDescription.electionId : null;
401+
if (serverDescription.setVersion && electionId) {
402+
if (maxSetVersion && maxElectionId) {
403+
if (
404+
maxSetVersion > serverDescription.setVersion ||
405+
compareObjectId(maxElectionId, electionId) > 0
406+
) {
407+
// this primary is stale, we must remove it
408+
serverDescriptions.set(
409+
serverDescription.address,
410+
new ServerDescription(serverDescription.address)
411+
);
412+
413+
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
414+
}
415+
}
394416

395-
return [checkHasPrimary(serverDescriptions), setName, maxSetVersion, maxElectionId];
417+
maxElectionId = serverDescription.electionId;
418+
}
419+
420+
if (
421+
serverDescription.setVersion != null &&
422+
(maxSetVersion == null || serverDescription.setVersion > maxSetVersion)
423+
) {
424+
maxSetVersion = serverDescription.setVersion;
425+
}
396426
}
397427

398428
// We've heard from the primary. Is it the same primary as before?
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"description": "Replica set member with large maxWireVersion",
3+
"uri": "mongodb://a,b/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"helloOk": true,
12+
"isWritablePrimary": true,
13+
"setName": "rs",
14+
"hosts": [
15+
"a:27017",
16+
"b:27017"
17+
],
18+
"minWireVersion": 0,
19+
"maxWireVersion": 6
20+
}
21+
],
22+
[
23+
"b:27017",
24+
{
25+
"ok": 1,
26+
"helloOk": true,
27+
"isWritablePrimary": false,
28+
"secondary": true,
29+
"setName": "rs",
30+
"hosts": [
31+
"a:27017",
32+
"b:27017"
33+
],
34+
"minWireVersion": 0,
35+
"maxWireVersion": 1000
36+
}
37+
]
38+
],
39+
"outcome": {
40+
"servers": {
41+
"a:27017": {
42+
"type": "RSPrimary",
43+
"setName": "rs"
44+
},
45+
"b:27017": {
46+
"type": "RSSecondary",
47+
"setName": "rs"
48+
}
49+
},
50+
"topologyType": "ReplicaSetWithPrimary",
51+
"setName": "rs",
52+
"logicalSessionTimeoutMinutes": null,
53+
"compatible": true
54+
}
55+
}
56+
]
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
description: "Replica set member with large maxWireVersion"
2+
3+
uri: "mongodb://a,b/?replicaSet=rs"
4+
5+
phases: [
6+
{
7+
responses: [
8+
["a:27017", {
9+
ok: 1,
10+
helloOk: true,
11+
isWritablePrimary: true,
12+
setName: "rs",
13+
hosts: ["a:27017", "b:27017"],
14+
minWireVersion: 0,
15+
maxWireVersion: 6
16+
}],
17+
["b:27017", {
18+
ok: 1,
19+
helloOk: true,
20+
isWritablePrimary: false,
21+
secondary: true,
22+
setName: "rs",
23+
hosts: ["a:27017", "b:27017"],
24+
minWireVersion: 0,
25+
maxWireVersion: 1000
26+
}]
27+
],
28+
outcome: {
29+
servers: {
30+
"a:27017": {
31+
type: "RSPrimary",
32+
setName: "rs"
33+
},
34+
"b:27017": {
35+
type: "RSSecondary",
36+
setName: "rs"
37+
}
38+
},
39+
topologyType: "ReplicaSetWithPrimary",
40+
setName: "rs",
41+
logicalSessionTimeoutMinutes: null,
42+
compatible: true
43+
}
44+
}
45+
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"description": "Replica set member and an unknown server",
3+
"uri": "mongodb://a,b/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"helloOk": true,
12+
"isWritablePrimary": true,
13+
"setName": "rs",
14+
"hosts": [
15+
"a:27017",
16+
"b:27017"
17+
],
18+
"minWireVersion": 0,
19+
"maxWireVersion": 6
20+
}
21+
]
22+
],
23+
"outcome": {
24+
"servers": {
25+
"a:27017": {
26+
"type": "RSPrimary",
27+
"setName": "rs"
28+
},
29+
"b:27017": {
30+
"type": "Unknown"
31+
}
32+
},
33+
"topologyType": "ReplicaSetWithPrimary",
34+
"setName": "rs",
35+
"logicalSessionTimeoutMinutes": null,
36+
"compatible": true
37+
}
38+
}
39+
]
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
description: "Replica set member and an unknown server"
2+
3+
uri: "mongodb://a,b/?replicaSet=rs"
4+
5+
phases: [
6+
{
7+
responses: [
8+
["a:27017", {
9+
ok: 1,
10+
helloOk: true,
11+
isWritablePrimary: true,
12+
setName: "rs",
13+
hosts: ["a:27017", "b:27017"],
14+
minWireVersion: 0,
15+
maxWireVersion: 6
16+
}],
17+
],
18+
outcome: {
19+
servers: {
20+
"a:27017": {
21+
type: "RSPrimary",
22+
setName: "rs"
23+
},
24+
"b:27017": {
25+
type: "Unknown",
26+
}
27+
},
28+
topologyType: "ReplicaSetWithPrimary",
29+
setName: "rs",
30+
logicalSessionTimeoutMinutes: null,
31+
compatible: true
32+
}
33+
}
34+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"description": "Discover arbiters with directConnection URI option",
3+
"uri": "mongodb://a/?directConnection=false",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"helloOk": true,
12+
"isWritablePrimary": true,
13+
"hosts": [
14+
"a:27017"
15+
],
16+
"arbiters": [
17+
"b:27017"
18+
],
19+
"setName": "rs",
20+
"minWireVersion": 0,
21+
"maxWireVersion": 6
22+
}
23+
]
24+
],
25+
"outcome": {
26+
"servers": {
27+
"a:27017": {
28+
"type": "RSPrimary",
29+
"setName": "rs"
30+
},
31+
"b:27017": {
32+
"type": "Unknown",
33+
"setName": null
34+
}
35+
},
36+
"topologyType": "ReplicaSetWithPrimary",
37+
"logicalSessionTimeoutMinutes": null,
38+
"setName": "rs"
39+
}
40+
}
41+
]
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
description: "Discover arbiters with directConnection URI option"
2+
3+
uri: "mongodb://a/?directConnection=false"
4+
5+
phases: [
6+
7+
{
8+
responses: [
9+
10+
["a:27017", {
11+
12+
ok: 1,
13+
helloOk: true,
14+
isWritablePrimary: true,
15+
hosts: ["a:27017"],
16+
arbiters: ["b:27017"],
17+
setName: "rs",
18+
minWireVersion: 0,
19+
maxWireVersion: 6
20+
}]
21+
],
22+
23+
outcome: {
24+
25+
servers: {
26+
27+
"a:27017": {
28+
29+
type: "RSPrimary",
30+
setName: "rs"
31+
},
32+
33+
"b:27017": {
34+
35+
type: "Unknown",
36+
setName:
37+
}
38+
},
39+
topologyType: "ReplicaSetWithPrimary",
40+
logicalSessionTimeoutMinutes: null,
41+
setName: "rs"
42+
}
43+
}
44+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"description": "Discover arbiters with replicaSet URI option",
3+
"uri": "mongodb://a/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"helloOk": true,
12+
"isWritablePrimary": true,
13+
"hosts": [
14+
"a:27017"
15+
],
16+
"arbiters": [
17+
"b:27017"
18+
],
19+
"setName": "rs",
20+
"minWireVersion": 0,
21+
"maxWireVersion": 6
22+
}
23+
]
24+
],
25+
"outcome": {
26+
"servers": {
27+
"a:27017": {
28+
"type": "RSPrimary",
29+
"setName": "rs"
30+
},
31+
"b:27017": {
32+
"type": "Unknown",
33+
"setName": null
34+
}
35+
},
36+
"topologyType": "ReplicaSetWithPrimary",
37+
"logicalSessionTimeoutMinutes": null,
38+
"setName": "rs"
39+
}
40+
}
41+
]
42+
}

0 commit comments

Comments
 (0)