Skip to content

Commit 85f0987

Browse files
committed
PYTHON-3400 Only use new electionId/setVersion logic on 6.0+
1 parent 64d7d6d commit 85f0987

14 files changed

+559
-40
lines changed

pymongo/topology_description.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,19 +531,35 @@ def _update_rs_from_primary(
531531
# We found a primary but it doesn't have the replica_set_name
532532
# provided by the user.
533533
sds.pop(server_description.address)
534-
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)
535-
536-
new_election_tuple = server_description.election_id, server_description.set_version
537-
max_election_tuple = max_election_id, max_set_version
538-
new_election_safe = tuple(MinKey() if i is None else i for i in new_election_tuple)
539-
max_election_safe = tuple(MinKey() if i is None else i for i in max_election_tuple)
540-
if new_election_safe >= max_election_safe:
541-
max_election_id, max_set_version = new_election_tuple
542-
else:
543-
# Stale primary, set to type Unknown.
544-
sds[server_description.address] = server_description.to_unknown()
545534
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
546535

536+
if server_description.max_wire_version is None or server_description.max_wire_version < 17:
537+
new_election_tuple = server_description.set_version, server_description.election_id
538+
max_election_tuple = max_set_version, max_election_id
539+
if None not in new_election_tuple:
540+
if None not in max_election_tuple and new_election_tuple < max_election_tuple:
541+
# Stale primary, set to type Unknown.
542+
sds[server_description.address] = server_description.to_unknown()
543+
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
544+
max_election_id = server_description.election_id
545+
546+
if server_description.set_version is not None and (
547+
max_set_version is None or server_description.set_version > max_set_version
548+
):
549+
max_set_version = server_description.set_version
550+
else:
551+
new_election_tuple = server_description.election_id, server_description.set_version
552+
max_election_tuple = max_election_id, max_set_version
553+
new_election_safe = tuple(MinKey() if i is None else i for i in new_election_tuple)
554+
max_election_safe = tuple(MinKey() if i is None else i for i in max_election_tuple)
555+
if new_election_safe < max_election_safe:
556+
# Stale primary, set to type Unknown.
557+
sds[server_description.address] = server_description.to_unknown()
558+
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
559+
else:
560+
max_election_id = server_description.election_id
561+
max_set_version = server_description.set_version
562+
547563
# We've heard from the primary. Is it the same primary as before?
548564
for server in sds.values():
549565
if (

test/discovery_and_monitoring/rs/electionId_precedence_setVersion.json

Lines changed: 3 additions & 3 deletions
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
],
Lines changed: 203 additions & 0 deletions
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+
}

test/discovery_and_monitoring/rs/null_election_id.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"setVersion": 1,
1919
"setName": "rs",
2020
"minWireVersion": 0,
21-
"maxWireVersion": 6
21+
"maxWireVersion": 17
2222
}
2323
]
2424
],
@@ -66,7 +66,7 @@
6666
"$oid": "000000000000000000000002"
6767
},
6868
"minWireVersion": 0,
69-
"maxWireVersion": 6
69+
"maxWireVersion": 17
7070
}
7171
]
7272
],
@@ -116,7 +116,7 @@
116116
"setVersion": 1,
117117
"setName": "rs",
118118
"minWireVersion": 0,
119-
"maxWireVersion": 6
119+
"maxWireVersion": 17
120120
}
121121
]
122122
],
@@ -170,7 +170,7 @@
170170
"$oid": "000000000000000000000001"
171171
},
172172
"minWireVersion": 0,
173-
"maxWireVersion": 6
173+
"maxWireVersion": 17
174174
}
175175
]
176176
],
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"description": "Pre 6.0 New primary",
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": 6
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+
"logicalSessionTimeoutMinutes": null,
52+
"setName": "rs"
53+
}
54+
},
55+
{
56+
"responses": [
57+
[
58+
"b:27017",
59+
{
60+
"ok": 0,
61+
"minWireVersion": 0,
62+
"maxWireVersion": 6
63+
}
64+
]
65+
],
66+
"outcome": {
67+
"servers": {
68+
"a:27017": {
69+
"type": "RSPrimary",
70+
"setName": "rs"
71+
},
72+
"b:27017": {
73+
"type": "Unknown",
74+
"setName": null
75+
}
76+
},
77+
"topologyType": "ReplicaSetWithPrimary",
78+
"logicalSessionTimeoutMinutes": null,
79+
"setName": "rs"
80+
}
81+
}
82+
]
83+
}

0 commit comments

Comments
 (0)