Skip to content

Commit 85d1813

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

32 files changed

+2027
-67
lines changed

src/sdam/topology_description.ts

+48-18
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?

test/spec/server-discovery-and-monitoring/rs/electionId_precedence_setVersion.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"$oid": "000000000000000000000001"
2121
},
2222
"minWireVersion": 0,
23-
"maxWireVersion": 6
23+
"maxWireVersion": 17
2424
}
2525
],
2626
[
@@ -39,7 +39,7 @@
3939
"$oid": "000000000000000000000001"
4040
},
4141
"minWireVersion": 0,
42-
"maxWireVersion": 6
42+
"maxWireVersion": 17
4343
}
4444
],
4545
[
@@ -58,7 +58,7 @@
5858
"$oid": "000000000000000000000002"
5959
},
6060
"minWireVersion": 0,
61-
"maxWireVersion": 6
61+
"maxWireVersion": 17
6262
}
6363
]
6464
],

test/spec/server-discovery-and-monitoring/rs/electionId_precedence_setVersion.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ phases:
1414
electionId:
1515
$oid: "000000000000000000000001"
1616
minWireVersion: 0
17-
maxWireVersion: 6
17+
maxWireVersion: 17
1818
- - "b:27017"
1919
- ok: 1
2020
helloOk: true
@@ -27,7 +27,7 @@ phases:
2727
electionId:
2828
$oid: "000000000000000000000001"
2929
minWireVersion: 0
30-
maxWireVersion: 6
30+
maxWireVersion: 17
3131
- - "a:27017"
3232
- ok: 1
3333
helloOk: true
@@ -40,7 +40,7 @@ phases:
4040
electionId:
4141
$oid: "000000000000000000000002"
4242
minWireVersion: 0
43-
maxWireVersion: 6
43+
maxWireVersion: 17
4444
outcome:
4545
servers:
4646
"a:27017":
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"description": "Pre 6.0 Primaries with and without electionIds",
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+
"b:27017",
16+
"c:27017"
17+
],
18+
"setVersion": 1,
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+
"setVersion": 1,
31+
"electionId": null
32+
},
33+
"b:27017": {
34+
"type": "Unknown",
35+
"setName": null,
36+
"electionId": null
37+
},
38+
"c:27017": {
39+
"type": "Unknown",
40+
"setName": null,
41+
"electionId": null
42+
}
43+
},
44+
"topologyType": "ReplicaSetWithPrimary",
45+
"logicalSessionTimeoutMinutes": null,
46+
"setName": "rs",
47+
"maxSetVersion": 1
48+
}
49+
},
50+
{
51+
"responses": [
52+
[
53+
"b:27017",
54+
{
55+
"ok": 1,
56+
"helloOk": true,
57+
"isWritablePrimary": true,
58+
"hosts": [
59+
"a:27017",
60+
"b:27017",
61+
"c:27017"
62+
],
63+
"setName": "rs",
64+
"setVersion": 1,
65+
"electionId": {
66+
"$oid": "000000000000000000000002"
67+
},
68+
"minWireVersion": 0,
69+
"maxWireVersion": 6
70+
}
71+
]
72+
],
73+
"outcome": {
74+
"servers": {
75+
"a:27017": {
76+
"type": "Unknown",
77+
"setName": null,
78+
"electionId": null
79+
},
80+
"b:27017": {
81+
"type": "RSPrimary",
82+
"setName": "rs",
83+
"setVersion": 1,
84+
"electionId": {
85+
"$oid": "000000000000000000000002"
86+
}
87+
},
88+
"c:27017": {
89+
"type": "Unknown",
90+
"setName": null,
91+
"electionId": null
92+
}
93+
},
94+
"topologyType": "ReplicaSetWithPrimary",
95+
"logicalSessionTimeoutMinutes": null,
96+
"setName": "rs",
97+
"maxSetVersion": 1,
98+
"maxElectionId": {
99+
"$oid": "000000000000000000000002"
100+
}
101+
}
102+
},
103+
{
104+
"responses": [
105+
[
106+
"a:27017",
107+
{
108+
"ok": 1,
109+
"helloOk": true,
110+
"isWritablePrimary": true,
111+
"hosts": [
112+
"a:27017",
113+
"b:27017",
114+
"c:27017"
115+
],
116+
"setVersion": 1,
117+
"setName": "rs",
118+
"minWireVersion": 0,
119+
"maxWireVersion": 6
120+
}
121+
]
122+
],
123+
"outcome": {
124+
"servers": {
125+
"a:27017": {
126+
"type": "RSPrimary",
127+
"setName": "rs",
128+
"setVersion": 1,
129+
"electionId": null
130+
},
131+
"b:27017": {
132+
"type": "Unknown",
133+
"setName": null,
134+
"electionId": null
135+
},
136+
"c:27017": {
137+
"type": "Unknown",
138+
"setName": null,
139+
"electionId": null
140+
}
141+
},
142+
"topologyType": "ReplicaSetWithPrimary",
143+
"logicalSessionTimeoutMinutes": null,
144+
"setName": "rs",
145+
"maxSetVersion": 1,
146+
"maxElectionId": {
147+
"$oid": "000000000000000000000002"
148+
}
149+
}
150+
},
151+
{
152+
"responses": [
153+
[
154+
"c:27017",
155+
{
156+
"ok": 1,
157+
"helloOk": true,
158+
"isWritablePrimary": true,
159+
"hosts": [
160+
"a:27017",
161+
"b:27017",
162+
"c:27017"
163+
],
164+
"setName": "rs",
165+
"setVersion": 1,
166+
"electionId": {
167+
"$oid": "000000000000000000000001"
168+
},
169+
"minWireVersion": 0,
170+
"maxWireVersion": 6
171+
}
172+
]
173+
],
174+
"outcome": {
175+
"servers": {
176+
"a:27017": {
177+
"type": "RSPrimary",
178+
"setName": "rs",
179+
"setVersion": 1,
180+
"electionId": null
181+
},
182+
"b:27017": {
183+
"type": "Unknown",
184+
"setName": null,
185+
"electionId": null
186+
},
187+
"c:27017": {
188+
"type": "Unknown",
189+
"setName": null,
190+
"electionId": null
191+
}
192+
},
193+
"topologyType": "ReplicaSetWithPrimary",
194+
"logicalSessionTimeoutMinutes": null,
195+
"setName": "rs",
196+
"maxSetVersion": 1,
197+
"maxElectionId": {
198+
"$oid": "000000000000000000000002"
199+
}
200+
}
201+
}
202+
]
203+
}

0 commit comments

Comments
 (0)