Skip to content

Commit f7e172a

Browse files
committed
PYTHON-2970 Resync tests, simplifiy comparison with MinKey
1 parent 2bb2521 commit f7e172a

File tree

2 files changed

+156
-20
lines changed

2 files changed

+156
-20
lines changed

pymongo/topology_description.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from random import sample
1818
from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple
1919

20+
from bson.min_key import MinKey
2021
from bson.objectid import ObjectId
2122
from pymongo import common
2223
from pymongo.errors import ConfigurationError
@@ -532,30 +533,16 @@ def _update_rs_from_primary(
532533
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)
533534

534535
max_election_tuple = max_election_id, max_set_version
535-
new_election_tuple = tuple(reversed(server_description.election_tuple))
536-
new_election_id = server_description.election_id
537-
new_set_version = server_description.set_version
538-
if None not in new_election_tuple and (
539-
None not in max_election_tuple
540-
and (
541-
max_election_id > new_election_id
542-
or (max_election_id == new_election_id and max_set_version > new_set_version)
543-
)
544-
):
536+
new_election_tuple = server_description.election_id, server_description.set_version
537+
max_election_safe = tuple(i if i is not None else MinKey() for i in max_election_tuple)
538+
new_election_safe = tuple(i if i is not None else MinKey() for i in new_election_tuple)
539+
if new_election_safe >= max_election_safe:
540+
max_election_id, max_set_version = new_election_tuple
541+
else:
545542
# Stale primary, set to type Unknown.
546543
sds[server_description.address] = server_description.to_unknown()
547544
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
548545

549-
if new_election_id is not None and (
550-
max_election_id is None or new_election_id > max_election_id
551-
):
552-
max_election_id = server_description.election_id
553-
554-
if new_set_version is not None and (
555-
max_set_version is None or new_set_version > max_set_version
556-
):
557-
max_set_version = server_description.set_version
558-
559546
# We've heard from the primary. Is it the same primary as before?
560547
for server in sds.values():
561548
if (
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"description": "Set version rolls back after new primary with higher election Id",
3+
"uri": "mongodb://a/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"responses": [
7+
[
8+
"a:27017",
9+
{
10+
"ok": 1,
11+
"hello": true,
12+
"isWritablePrimary": true,
13+
"hosts": [
14+
"a:27017",
15+
"b:27017"
16+
],
17+
"setName": "rs",
18+
"setVersion": 2,
19+
"electionId": {
20+
"$oid": "000000000000000000000001"
21+
},
22+
"minWireVersion": 0,
23+
"maxWireVersion": 6
24+
}
25+
]
26+
],
27+
"outcome": {
28+
"servers": {
29+
"a:27017": {
30+
"type": "RSPrimary",
31+
"setName": "rs",
32+
"setVersion": 2,
33+
"electionId": {
34+
"$oid": "000000000000000000000001"
35+
}
36+
},
37+
"b:27017": {
38+
"type": "Unknown",
39+
"setName": null,
40+
"electionId": null
41+
}
42+
},
43+
"topologyType": "ReplicaSetWithPrimary",
44+
"logicalSessionTimeoutMinutes": null,
45+
"setName": "rs",
46+
"maxSetVersion": 2,
47+
"maxElectionId": {
48+
"$oid": "000000000000000000000001"
49+
}
50+
}
51+
},
52+
{
53+
"_comment": "Response from new primary with newer election Id",
54+
"responses": [
55+
[
56+
"b:27017",
57+
{
58+
"ok": 1,
59+
"hello": true,
60+
"isWritablePrimary": true,
61+
"hosts": [
62+
"a:27017",
63+
"b:27017"
64+
],
65+
"setName": "rs",
66+
"setVersion": 1,
67+
"electionId": {
68+
"$oid": "000000000000000000000002"
69+
},
70+
"minWireVersion": 0,
71+
"maxWireVersion": 6
72+
}
73+
]
74+
],
75+
"outcome": {
76+
"servers": {
77+
"a:27017": {
78+
"type": "Unknown",
79+
"setName": null,
80+
"electionId": null
81+
},
82+
"b:27017": {
83+
"type": "RSPrimary",
84+
"setName": "rs",
85+
"setVersion": 1,
86+
"electionId": {
87+
"$oid": "000000000000000000000002"
88+
}
89+
}
90+
},
91+
"topologyType": "ReplicaSetWithPrimary",
92+
"logicalSessionTimeoutMinutes": null,
93+
"setName": "rs",
94+
"maxSetVersion": 1,
95+
"maxElectionId": {
96+
"$oid": "000000000000000000000002"
97+
}
98+
}
99+
},
100+
{
101+
"_comment": "Response from stale primary",
102+
"responses": [
103+
[
104+
"a:27017",
105+
{
106+
"ok": 1,
107+
"hello": true,
108+
"isWritablePrimary": true,
109+
"hosts": [
110+
"a:27017",
111+
"b:27017"
112+
],
113+
"setName": "rs",
114+
"setVersion": 2,
115+
"electionId": {
116+
"$oid": "000000000000000000000001"
117+
},
118+
"minWireVersion": 0,
119+
"maxWireVersion": 6
120+
}
121+
]
122+
],
123+
"outcome": {
124+
"servers": {
125+
"a:27017": {
126+
"type": "Unknown",
127+
"setName": null,
128+
"electionId": null
129+
},
130+
"b:27017": {
131+
"type": "RSPrimary",
132+
"setName": "rs",
133+
"setVersion": 1,
134+
"electionId": {
135+
"$oid": "000000000000000000000002"
136+
}
137+
}
138+
},
139+
"topologyType": "ReplicaSetWithPrimary",
140+
"logicalSessionTimeoutMinutes": null,
141+
"setName": "rs",
142+
"maxSetVersion": 1,
143+
"maxElectionId": {
144+
"$oid": "000000000000000000000002"
145+
}
146+
}
147+
}
148+
]
149+
}

0 commit comments

Comments
 (0)