Skip to content

PYTHON-4018 Clarify exactly what code/label fields drivers should inspect to determine retryability #1514

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

Closed
wants to merge 1 commit into from
Closed
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
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 @@ -2132,7 +2133,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 @@ -2149,7 +2150,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 @@ -2383,9 +2387,11 @@ 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
is_mongos = conn.is_mongos
sessions_supported = (
self._session
and self._server.description.retryable_writes_supported
Expand All @@ -2401,7 +2407,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
169 changes: 169 additions & 0 deletions test/retryable_writes/legacy/bulkWrite-errorLabels.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,175 @@
]
}
}
},
{
"description": "BulkWrite succeeds after PrimarySteppedDown",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"update"
],
"errorCode": 189,
"errorLabels": [
"RetryableWriteError"
]
}
},
"operation": {
"name": "bulkWrite",
"arguments": {
"requests": [
{
"name": "deleteOne",
"arguments": {
"filter": {
"_id": 1
}
}
},
{
"name": "insertOne",
"arguments": {
"document": {
"_id": 3,
"x": 33
}
}
},
{
"name": "updateOne",
"arguments": {
"filter": {
"_id": 2
},
"update": {
"$inc": {
"x": 1
}
}
}
}
],
"options": {
"ordered": true
}
}
},
"outcome": {
"result": {
"deletedCount": 1,
"insertedCount": 1,
"insertedIds": {
"1": 3
},
"matchedCount": 1,
"modifiedCount": 1,
"upsertedCount": 0,
"upsertedIds": {}
},
"collection": {
"data": [
{
"_id": 2,
"x": 23
},
{
"_id": 3,
"x": 33
}
]
}
}
},
{
"description": "BulkWrite succeeds after WriteConcernError ShutdownInProgress",
"failPoint": {
"configureFailPoint": "failCommand",
"mode": {
"times": 1
},
"data": {
"failCommands": [
"insert"
],
"errorLabels": [
"RetryableWriteError"
],
"writeConcernError": {
"code": 91,
"errmsg": "Replication is being shut down"
}
}
},
"operation": {
"name": "bulkWrite",
"arguments": {
"requests": [
{
"name": "deleteOne",
"arguments": {
"filter": {
"_id": 1
}
}
},
{
"name": "insertOne",
"arguments": {
"document": {
"_id": 3,
"x": 33
}
}
},
{
"name": "updateOne",
"arguments": {
"filter": {
"_id": 2
},
"update": {
"$inc": {
"x": 1
}
}
}
}
],
"options": {
"ordered": true
}
}
},
"outcome": {
"result": {
"deletedCount": 1,
"insertedCount": 1,
"insertedIds": {
"1": 3
},
"matchedCount": 1,
"modifiedCount": 1,
"upsertedCount": 0,
"upsertedIds": {}
},
"collection": {
"data": [
{
"_id": 2,
"x": 23
},
{
"_id": 3,
"x": 33
}
]
}
}
}
]
}
Loading