Skip to content

Commit

Permalink
Revert "PYTHON-2970 Prioritize electionId over setVersion for stale p…
Browse files Browse the repository at this point in the history
…rimary check (#845)"

This reverts commit 225d131.
  • Loading branch information
ShaneHarvey committed Mar 29, 2022
1 parent d8c2b31 commit a4bba9d
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 488 deletions.
41 changes: 17 additions & 24 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,33 @@ Changelog
Changes in Version 4.1
----------------------

.. warning:: PyMongo 4.1 drops support for Python 3.6.0 and 3.6.1, Python 3.6.2+ is now required.

PyMongo 4.1 brings a number of improvements including:

- :meth:`pymongo.collection.Collection.update_one`,
:meth:`pymongo.collection.Collection.update_many`,
:meth:`pymongo.collection.Collection.delete_one`,
:meth:`pymongo.collection.Collection.delete_many`,
:meth:`pymongo.collection.Collection.aggregate`,
:meth:`pymongo.collection.Collection.find_one_and_delete`,
:meth:`pymongo.collection.Collection.find_one_and_replace`,
:meth:`pymongo.collection.Collection.find_one_and_update`,
:meth:`pymongo.collection.Collection.find`,
and :meth:`pymongo.collection.Collection.replace_one `all support a new
keyword argument ``let`` which is a map of parameter names and values.
- Added support for the ``let`` parameter to
:meth:`~pymongo.collection.Collection.update_one`,
:meth:`~pymongo.collection.Collection.update_many`,
:meth:`~pymongo.collection.Collection.delete_one`,
:meth:`~pymongo.collection.Collection.delete_many`,
:meth:`~pymongo.collection.Collection.replace_one`,
:meth:`~pymongo.collection.Collection.aggregate`,
:meth:`~pymongo.collection.Collection.find_one_and_delete`,
:meth:`~pymongo.collection.Collection.find_one_and_replace`,
:meth:`~pymongo.collection.Collection.find_one_and_update`,
:meth:`~pymongo.collection.Collection.find`,
:meth:`~pymongo.collection.Collection.find_one`,
and :meth:`~pymongo.collection.Collection.bulk_write`.
``let`` is a map of parameter names and values.
Parameters can then be accessed as variables in an aggregate expression
context.
- :meth:`~pymongo.collection.Collection.aggregate` now supports
$merge and $out executing on secondaries on MongoDB >=5.0.
aggregate() now always obeys the collection's :attr:`read_preference` on
MongoDB >= 5.0.
- :meth:`gridfs.GridOut.seek` now returns the new position in the file, to
- :meth:`gridfs.grid_file.GridOut.seek` now returns the new position in the file, to
conform to the behavior of :meth:`io.IOBase.seek`.

Breaking Changes in 4.1
.......................
- Removed support for Python 3.6.0 and 3.6.1, Python 3.6.2+ is now required.

Bug fixes
.........

- Fixed a bug where the client could be unable to discover the new primary
after a simultaneous replica set election and reconfig (`PYTHON-2970`_).

.. _PYTHON-2970: https://jira.mongodb.org/browse/PYTHON-2970

Issues Resolved
...............

Expand Down
29 changes: 18 additions & 11 deletions pymongo/topology_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from random import sample
from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple

from bson.min_key import MinKey
from bson.objectid import ObjectId
from pymongo import common
from pymongo.errors import ConfigurationError
Expand Down Expand Up @@ -532,16 +531,24 @@ def _update_rs_from_primary(
sds.pop(server_description.address)
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)

new_election_tuple = server_description.election_id, server_description.set_version
max_election_tuple = max_election_id, max_set_version
new_election_safe = tuple(MinKey() if i is None else i for i in new_election_tuple)
max_election_safe = tuple(MinKey() if i is None else i for i in max_election_tuple)
if new_election_safe >= max_election_safe:
max_election_id, max_set_version = new_election_tuple
else:
# Stale primary, set to type Unknown.
sds[server_description.address] = server_description.to_unknown()
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
max_election_tuple = max_set_version, max_election_id
if None not in server_description.election_tuple:
if (
None not in max_election_tuple
and max_election_tuple > server_description.election_tuple
):

# Stale primary, set to type Unknown.
sds[server_description.address] = server_description.to_unknown()
return (_check_has_primary(sds), replica_set_name, max_set_version, max_election_id)

max_election_id = server_description.election_id

if server_description.set_version is not None and (
max_set_version is None or server_description.set_version > max_set_version
):

max_set_version = server_description.set_version

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

This file was deleted.

30 changes: 12 additions & 18 deletions test/discovery_and_monitoring/rs/null_election_id.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,15 @@
"outcome": {
"servers": {
"a:27017": {
"type": "Unknown",
"setName": null,
"setVersion": null,
"electionId": null
},
"b:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": {
"$oid": "000000000000000000000002"
}
"electionId": null
},
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"c:27017": {
"type": "Unknown",
Expand Down Expand Up @@ -177,18 +174,15 @@
"outcome": {
"servers": {
"a:27017": {
"type": "Unknown",
"setName": null,
"setVersion": null,
"electionId": null
},
"b:27017": {
"type": "RSPrimary",
"setName": "rs",
"setVersion": 1,
"electionId": {
"$oid": "000000000000000000000002"
}
"electionId": null
},
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
},
"c:27017": {
"type": "Unknown",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "Secondary ignored when ok is zero",
"description": "New primary",
"uri": "mongodb://a,b/?replicaSet=rs",
"phases": [
{
Expand Down
Loading

0 comments on commit a4bba9d

Please sign in to comment.