Skip to content

PYTHON-4960 More informative error message for stale primary #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pymongo/topology_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from bson.min_key import MinKey
from bson.objectid import ObjectId
from pymongo import common
from pymongo.errors import ConfigurationError
from pymongo.errors import ConfigurationError, PyMongoError
from pymongo.read_preferences import ReadPreference, _AggWritePref, _ServerMode
from pymongo.server_description import ServerDescription
from pymongo.server_selectors import Selection
Expand Down Expand Up @@ -563,7 +563,11 @@ def _update_rs_from_primary(
if None not in new_election_tuple:
if None not in max_election_tuple and new_election_tuple < max_election_tuple:
# Stale primary, set to type Unknown.
sds[server_description.address] = server_description.to_unknown()
sds[server_description.address] = server_description.to_unknown(
PyMongoError(
f"primary marked stale due to electionId/setVersion mismatch, {new_election_tuple} is stale compared to {max_election_tuple}"
)
)
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
max_election_id = server_description.election_id

Expand All @@ -578,7 +582,11 @@ def _update_rs_from_primary(
max_election_safe = tuple(MinKey() if i is None else i for i in max_election_tuple)
if new_election_safe < max_election_safe:
# Stale primary, set to type Unknown.
sds[server_description.address] = server_description.to_unknown()
sds[server_description.address] = server_description.to_unknown(
PyMongoError(
f"primary marked stale due to electionId/setVersion mismatch, {new_election_tuple} is stale compared to {max_election_tuple}"
)
)
return _check_has_primary(sds), replica_set_name, max_set_version, max_election_id
else:
max_election_id = server_description.election_id
Expand All @@ -591,7 +599,9 @@ def _update_rs_from_primary(
and server.address != server_description.address
):
# Reset old primary's type to Unknown.
sds[server.address] = server.to_unknown()
sds[server.address] = server.to_unknown(
PyMongoError("primary marked stale due to discovery of newer primary")
)

# There can be only one prior primary.
break
Expand Down
3 changes: 3 additions & 0 deletions test/asynchronous/test_discovery_and_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def check_outcome(self, topology, outcome):
server_type_name(expected_server_type),
server_type_name(actual_server_description.server_type),
)
expected_error = expected_server.get("error")
if expected_error:
self.assertIn(expected_error, str(actual_server_description.error))

self.assertEqual(expected_server.get("setName"), actual_server_description.replica_set_name)

Expand Down
3 changes: 2 additions & 1 deletion test/discovery_and_monitoring/rs/new_primary.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"servers": {
"a:27017": {
"type": "Unknown",
"setName": null
"setName": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down Expand Up @@ -123,7 +124,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to electionId/setVersion mismatch"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down Expand Up @@ -123,7 +124,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to electionId/setVersion mismatch"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down Expand Up @@ -124,6 +125,7 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"error": "primary marked stale due to electionId/setVersion mismatch",
"electionId": null
},
"b:27017": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down Expand Up @@ -124,6 +125,7 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"error": "primary marked stale due to electionId/setVersion mismatch",
"electionId": null
},
"b:27017": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to discovery of newer primary"
},
"b:27017": {
"type": "RSPrimary",
Expand Down Expand Up @@ -117,7 +118,8 @@
"a:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to electionId/setVersion mismatch"
},
"b:27017": {
"type": "RSPrimary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to electionId/setVersion mismatch"
}
},
"topologyType": "ReplicaSetWithPrimary",
Expand Down Expand Up @@ -128,7 +129,8 @@
"b:27017": {
"type": "Unknown",
"setName": null,
"electionId": null
"electionId": null,
"error": "primary marked stale due to electionId/setVersion mismatch"
}
},
"topologyType": "ReplicaSetWithPrimary",
Expand Down
3 changes: 3 additions & 0 deletions test/test_discovery_and_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ def check_outcome(self, topology, outcome):
server_type_name(expected_server_type),
server_type_name(actual_server_description.server_type),
)
expected_error = expected_server.get("error")
if expected_error:
self.assertIn(expected_error, str(actual_server_description.error))

self.assertEqual(expected_server.get("setName"), actual_server_description.replica_set_name)

Expand Down
Loading