Skip to content

Commit fed498d

Browse files
committed
PYTHON-2970 SDAM should give priority to electionId over setVersion when updating topology
1 parent 5169124 commit fed498d

File tree

3 files changed

+120
-20
lines changed

3 files changed

+120
-20
lines changed

pymongo/topology_description.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,23 +540,28 @@ def _update_rs_from_primary(
540540
max_set_version,
541541
max_election_id)
542542

543-
max_election_tuple = max_set_version, max_election_id
544-
if None not in server_description.election_tuple:
545-
if (None not in max_election_tuple and
546-
max_election_tuple > server_description.election_tuple):
547-
548-
# Stale primary, set to type Unknown.
549-
sds[server_description.address] = server_description.to_unknown()
550-
return (_check_has_primary(sds),
551-
replica_set_name,
552-
max_set_version,
553-
max_election_id)
543+
max_election_tuple = max_election_id, max_set_version
544+
new_election_tuple = tuple(reversed(server_description.election_tuple))
545+
new_election_id = server_description.election_id
546+
new_set_version = server_description.set_version
547+
if (None not in new_election_tuple and (None not in max_election_tuple and (
548+
max_election_id > new_election_id or (
549+
max_election_id == new_election_id and
550+
max_set_version > new_set_version)))):
551+
# Stale primary, set to type Unknown.
552+
sds[server_description.address] = server_description.to_unknown()
553+
return (_check_has_primary(sds),
554+
replica_set_name,
555+
max_set_version,
556+
max_election_id)
557+
558+
if (new_election_id is not None and (
559+
max_election_id is None or new_election_id > max_election_id)):
554560

555561
max_election_id = server_description.election_id
556562

557-
if (server_description.set_version is not None and
558-
(max_set_version is None or
559-
server_description.set_version > max_set_version)):
563+
if (new_set_version is not None and (
564+
max_set_version is None or new_set_version > max_set_version)):
560565

561566
max_set_version = server_description.set_version
562567

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
{
2+
"description": "ElectionId is considered higher precedence than setVersion",
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+
],
17+
"setName": "rs",
18+
"setVersion": 1,
19+
"electionId": {
20+
"$oid": "000000000000000000000001"
21+
},
22+
"minWireVersion": 0,
23+
"maxWireVersion": 6
24+
}
25+
],
26+
[
27+
"b:27017",
28+
{
29+
"ok": 1,
30+
"helloOk": true,
31+
"isWritablePrimary": true,
32+
"hosts": [
33+
"a:27017",
34+
"b:27017"
35+
],
36+
"setName": "rs",
37+
"setVersion": 2,
38+
"electionId": {
39+
"$oid": "000000000000000000000001"
40+
},
41+
"minWireVersion": 0,
42+
"maxWireVersion": 6
43+
}
44+
],
45+
[
46+
"a:27017",
47+
{
48+
"ok": 1,
49+
"helloOk": true,
50+
"isWritablePrimary": true,
51+
"hosts": [
52+
"a:27017",
53+
"b:27017"
54+
],
55+
"setName": "rs",
56+
"setVersion": 1,
57+
"electionId": {
58+
"$oid": "000000000000000000000002"
59+
},
60+
"minWireVersion": 0,
61+
"maxWireVersion": 6
62+
}
63+
]
64+
],
65+
"outcome": {
66+
"servers": {
67+
"a:27017": {
68+
"type": "RSPrimary",
69+
"setName": "rs",
70+
"setVersion": 1,
71+
"electionId": {
72+
"$oid": "000000000000000000000002"
73+
}
74+
},
75+
"b:27017": {
76+
"type": "Unknown",
77+
"setName": null,
78+
"setVersion": null,
79+
"electionId": null
80+
}
81+
},
82+
"topologyType": "ReplicaSetWithPrimary",
83+
"logicalSessionTimeoutMinutes": null,
84+
"setName": "rs",
85+
"maxSetVersion": 2,
86+
"maxElectionId": {
87+
"$oid": "000000000000000000000002"
88+
}
89+
}
90+
}
91+
]
92+
}

test/discovery_and_monitoring/rs/use_setversion_without_electionid.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,25 @@
115115
"outcome": {
116116
"servers": {
117117
"a:27017": {
118+
"type": "RSPrimary",
119+
"setName": "rs",
120+
"setVersion": 1,
121+
"electionId": {
122+
"$oid": "000000000000000000000002"
123+
}
124+
},
125+
"b:27017": {
118126
"type": "Unknown",
119127
"setName": null,
120128
"electionId": null
121-
},
122-
"b:27017": {
123-
"type": "RSPrimary",
124-
"setName": "rs",
125-
"setVersion": 2
126129
}
127130
},
128131
"topologyType": "ReplicaSetWithPrimary",
129132
"logicalSessionTimeoutMinutes": null,
130133
"setName": "rs",
131134
"maxSetVersion": 2,
132135
"maxElectionId": {
133-
"$oid": "000000000000000000000001"
136+
"$oid": "000000000000000000000002"
134137
}
135138
}
136139
}

0 commit comments

Comments
 (0)