Skip to content
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

PYTHON-4117 Require 4.3.1+ server version when using failCommand errorLabels option #1526

Merged
merged 3 commits into from
Feb 20, 2024
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
2 changes: 2 additions & 0 deletions pymongo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ def _get_wce_doc(result: Mapping[str, Any]) -> Optional[Mapping[str, Any]]:
# convenient to attach it to the writeConcernError doc itself.
error_labels = result.get("errorLabels")
if error_labels:
# Copy to avoid changing the original document.
wce = wce.copy()
Copy link
Member

@ShaneHarvey ShaneHarvey Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with if result is a RawBSONDocument? Can we add a test for it?

Edit: Oh I see, this is only used for write command responses so we only ever pass dict. Besides that, RawBSONDocument would already fail the wce["errorLabels"] = error_labels assignment.

wce["errorLabels"] = error_labels
return wce

Expand Down
12 changes: 9 additions & 3 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
PyMongoError,
ServerSelectionTimeoutError,
WaitQueueTimeoutError,
WriteConcernError,
)
from pymongo.lock import _HAS_REGISTER_AT_FORK, _create_lock, _release_locks
from pymongo.monitoring import ConnectionClosedReason
Expand Down Expand Up @@ -2175,7 +2176,7 @@ def _retryable_error_doc(exc: PyMongoError) -> Optional[Mapping[str, Any]]:
return None


def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int) -> None:
def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int, is_mongos: bool) -> None:
doc = _retryable_error_doc(exc)
if doc:
code = doc.get("code", 0)
Expand All @@ -2192,7 +2193,10 @@ def _add_retryable_write_error(exc: PyMongoError, max_wire_version: int) -> None
for label in doc.get("errorLabels", []):
exc._add_error_label(label)
else:
if code in helpers._RETRYABLE_ERROR_CODES:
# Do not consult writeConcernError for pre-4.4 mongos.
if isinstance(exc, WriteConcernError) and is_mongos:
pass
elif code in helpers._RETRYABLE_ERROR_CODES:
exc._add_error_label("RetryableWriteError")

# Connection errors are always retryable except NotPrimaryError and WaitQueueTimeoutError which is
Expand Down Expand Up @@ -2432,6 +2436,7 @@ def _write(self) -> T:
"""
try:
max_wire_version = 0
is_mongos = False
self._server = self._get_server()
with self._client._checkout(self._server, self._session) as conn:
max_wire_version = conn.max_wire_version
Expand All @@ -2440,6 +2445,7 @@ def _write(self) -> T:
and self._server.description.retryable_writes_supported
and conn.supports_sessions
)
is_mongos = conn.is_mongos
if not sessions_supported:
# A retry is not possible because this server does
# not support sessions raise the last error.
Expand All @@ -2450,7 +2456,7 @@ def _write(self) -> T:
if not self._retryable:
raise
# Add the RetryableWriteError label, if applicable.
_add_retryable_write_error(exc, max_wire_version)
_add_retryable_write_error(exc, max_wire_version, is_mongos)
raise

def _read(self) -> T:
Expand Down
20 changes: 10 additions & 10 deletions test/command_monitoring/writeConcernError.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.1.0",
"minServerVersion": "4.3.1",
"topologies": [
"replicaset"
],
Expand Down Expand Up @@ -66,11 +66,11 @@
"failCommands": [
"insert"
],
"errorLabels": [
"RetryableWriteError"
],
"writeConcernError": {
"code": 91,
"errorLabels": [
"RetryableWriteError"
]
"code": 91
}
}
}
Expand Down Expand Up @@ -112,11 +112,11 @@
"reply": {
"ok": 1,
"n": 1,
"errorLabels": [
"RetryableWriteError"
],
"writeConcernError": {
"code": 91,
"errorLabels": [
"RetryableWriteError"
]
"code": 91
}
},
"commandName": "insert"
Expand Down Expand Up @@ -152,4 +152,4 @@
]
}
]
}
}
Loading
Loading