Skip to content

Commit a4bba9d

Browse files
committed
Revert "PYTHON-2970 Prioritize electionId over setVersion for stale primary check (#845)"
This reverts commit 225d131.
1 parent d8c2b31 commit a4bba9d

10 files changed

+67
-488
lines changed

doc/changelog.rst

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,33 @@ Changelog
44
Changes in Version 4.1
55
----------------------
66

7+
.. warning:: PyMongo 4.1 drops support for Python 3.6.0 and 3.6.1, Python 3.6.2+ is now required.
8+
79
PyMongo 4.1 brings a number of improvements including:
810

9-
- :meth:`pymongo.collection.Collection.update_one`,
10-
:meth:`pymongo.collection.Collection.update_many`,
11-
:meth:`pymongo.collection.Collection.delete_one`,
12-
:meth:`pymongo.collection.Collection.delete_many`,
13-
:meth:`pymongo.collection.Collection.aggregate`,
14-
:meth:`pymongo.collection.Collection.find_one_and_delete`,
15-
:meth:`pymongo.collection.Collection.find_one_and_replace`,
16-
:meth:`pymongo.collection.Collection.find_one_and_update`,
17-
:meth:`pymongo.collection.Collection.find`,
18-
and :meth:`pymongo.collection.Collection.replace_one `all support a new
19-
keyword argument ``let`` which is a map of parameter names and values.
11+
- Added support for the ``let`` parameter to
12+
:meth:`~pymongo.collection.Collection.update_one`,
13+
:meth:`~pymongo.collection.Collection.update_many`,
14+
:meth:`~pymongo.collection.Collection.delete_one`,
15+
:meth:`~pymongo.collection.Collection.delete_many`,
16+
:meth:`~pymongo.collection.Collection.replace_one`,
17+
:meth:`~pymongo.collection.Collection.aggregate`,
18+
:meth:`~pymongo.collection.Collection.find_one_and_delete`,
19+
:meth:`~pymongo.collection.Collection.find_one_and_replace`,
20+
:meth:`~pymongo.collection.Collection.find_one_and_update`,
21+
:meth:`~pymongo.collection.Collection.find`,
22+
:meth:`~pymongo.collection.Collection.find_one`,
23+
and :meth:`~pymongo.collection.Collection.bulk_write`.
24+
``let`` is a map of parameter names and values.
2025
Parameters can then be accessed as variables in an aggregate expression
2126
context.
2227
- :meth:`~pymongo.collection.Collection.aggregate` now supports
2328
$merge and $out executing on secondaries on MongoDB >=5.0.
2429
aggregate() now always obeys the collection's :attr:`read_preference` on
2530
MongoDB >= 5.0.
26-
- :meth:`gridfs.GridOut.seek` now returns the new position in the file, to
31+
- :meth:`gridfs.grid_file.GridOut.seek` now returns the new position in the file, to
2732
conform to the behavior of :meth:`io.IOBase.seek`.
2833

29-
Breaking Changes in 4.1
30-
.......................
31-
- Removed support for Python 3.6.0 and 3.6.1, Python 3.6.2+ is now required.
32-
33-
Bug fixes
34-
.........
35-
36-
- Fixed a bug where the client could be unable to discover the new primary
37-
after a simultaneous replica set election and reconfig (`PYTHON-2970`_).
38-
39-
.. _PYTHON-2970: https://jira.mongodb.org/browse/PYTHON-2970
40-
4134
Issues Resolved
4235
...............
4336

pymongo/topology_description.py

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

20-
from bson.min_key import MinKey
2120
from bson.objectid import ObjectId
2221
from pymongo import common
2322
from pymongo.errors import ConfigurationError
@@ -532,16 +531,24 @@ def _update_rs_from_primary(
532531
sds.pop(server_description.address)
533532
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)
534533

535-
new_election_tuple = server_description.election_id, server_description.set_version
536-
max_election_tuple = max_election_id, max_set_version
537-
new_election_safe = tuple(MinKey() if i is None else i for i in new_election_tuple)
538-
max_election_safe = tuple(MinKey() if i is None else i for i in max_election_tuple)
539-
if new_election_safe >= max_election_safe:
540-
max_election_id, max_set_version = new_election_tuple
541-
else:
542-
# Stale primary, set to type Unknown.
543-
sds[server_description.address] = server_description.to_unknown()
544-
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
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)
544+
545+
max_election_id = server_description.election_id
546+
547+
if server_description.set_version is not None and (
548+
max_set_version is None or server_description.set_version > max_set_version
549+
):
550+
551+
max_set_version = server_description.set_version
545552

546553
# We've heard from the primary. Is it the same primary as before?
547554
for server in sds.values():

test/discovery_and_monitoring/rs/electionId_precedence_setVersion.json

Lines changed: 0 additions & 92 deletions
This file was deleted.

test/discovery_and_monitoring/rs/null_election_id.json

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,15 @@
123123
"outcome": {
124124
"servers": {
125125
"a:27017": {
126-
"type": "Unknown",
127-
"setName": null,
128-
"setVersion": null,
129-
"electionId": null
130-
},
131-
"b:27017": {
132126
"type": "RSPrimary",
133127
"setName": "rs",
134128
"setVersion": 1,
135-
"electionId": {
136-
"$oid": "000000000000000000000002"
137-
}
129+
"electionId": null
130+
},
131+
"b:27017": {
132+
"type": "Unknown",
133+
"setName": null,
134+
"electionId": null
138135
},
139136
"c:27017": {
140137
"type": "Unknown",
@@ -177,18 +174,15 @@
177174
"outcome": {
178175
"servers": {
179176
"a:27017": {
180-
"type": "Unknown",
181-
"setName": null,
182-
"setVersion": null,
183-
"electionId": null
184-
},
185-
"b:27017": {
186177
"type": "RSPrimary",
187178
"setName": "rs",
188179
"setVersion": 1,
189-
"electionId": {
190-
"$oid": "000000000000000000000002"
191-
}
180+
"electionId": null
181+
},
182+
"b:27017": {
183+
"type": "Unknown",
184+
"setName": null,
185+
"electionId": null
192186
},
193187
"c:27017": {
194188
"type": "Unknown",

test/discovery_and_monitoring/rs/secondary_ignore_ok_0.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "Secondary ignored when ok is zero",
2+
"description": "New primary",
33
"uri": "mongodb://a,b/?replicaSet=rs",
44
"phases": [
55
{

0 commit comments

Comments
 (0)