Skip to content

Commit e32a004

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

File tree

3 files changed

+120
-19
lines changed

3 files changed

+120
-19
lines changed

pymongo/topology_description.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -531,23 +531,29 @@ def _update_rs_from_primary(
531531
sds.pop(server_description.address)
532532
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)
533533

534-
max_election_tuple = max_set_version, max_election_id
535-
if None not in server_description.election_tuple:
536-
if (
537-
None not in max_election_tuple
538-
and max_election_tuple > server_description.election_tuple
539-
):
540-
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)
534+
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+
):
545+
# Stale primary, set to type Unknown.
546+
sds[server_description.address] = server_description.to_unknown()
547+
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
544548

549+
if new_election_id is not None and (
550+
max_election_id is None or new_election_id > max_election_id
551+
):
545552
max_election_id = server_description.election_id
546553

547-
if server_description.set_version is not None and (
548-
max_set_version is None or server_description.set_version > max_set_version
554+
if new_set_version is not None and (
555+
max_set_version is None or new_set_version > max_set_version
549556
):
550-
551557
max_set_version = server_description.set_version
552558

553559
# We've heard from the primary. Is it the same primary as before?
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)