Skip to content

Commit 7d83883

Browse files
committed
PYTHON-2984 Fix retry behavior for bulk write writeConcernError (#800)
(cherry picked from commit 2af521e)
1 parent 480d60e commit 7d83883

40 files changed

+530
-95
lines changed

doc/changelog.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
Changelog
22
=========
33

4+
Changes in Version 3.12.2
5+
-------------------------
6+
7+
Issues Resolved
8+
...............
9+
10+
Version 3.12.2 fixes a bug that prevented PyMongo from retrying bulk writes
11+
after a ``writeConcernError`` on MongoDB 4.4+ (`PYTHON-2984`_).
12+
13+
See the `PyMongo 3.12.2 release notes in JIRA`_ for the list of resolved issues
14+
in this release.
15+
16+
.. _PYTHON-2984: https://jira.mongodb.org/browse/PYTHON-2984
17+
.. _PyMongo 3.12.2 release notes in JIRA:https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=32310
18+
419
Changes in Version 3.12.1
520
-------------------------
621

pymongo/bulk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
validate_is_document_type,
2929
validate_ok_for_replace,
3030
validate_ok_for_update)
31-
from pymongo.helpers import _RETRYABLE_ERROR_CODES
31+
from pymongo.helpers import _RETRYABLE_ERROR_CODES, _get_wce_doc
3232
from pymongo.collation import validate_collation_or_none
3333
from pymongo.errors import (BulkWriteError,
3434
ConfigurationError,
@@ -126,9 +126,9 @@ def _merge_command(run, full_result, offset, result):
126126
replacement[_UOP] = run.ops[idx]
127127
full_result["writeErrors"].append(replacement)
128128

129-
wc_error = result.get("writeConcernError")
130-
if wc_error:
131-
full_result["writeConcernErrors"].append(wc_error)
129+
wce = _get_wce_doc(result)
130+
if wce:
131+
full_result["writeConcernErrors"].append(wce)
132132

133133

134134
def _raise_bulk_write_error(full_result):

pymongo/helpers.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,18 @@ def _raise_write_concern_error(error):
220220
error.get("errmsg"), error.get("code"), error)
221221

222222

223+
def _get_wce_doc(result):
224+
"""Return the writeConcernError or None."""
225+
wce = result.get("writeConcernError")
226+
if wce:
227+
# The server reports errorLabels at the top level but it's more
228+
# convenient to attach it to the writeConcernError doc itself.
229+
error_labels = result.get("errorLabels")
230+
if error_labels:
231+
wce["errorLabels"] = error_labels
232+
return wce
233+
234+
223235
def _check_write_command_response(result):
224236
"""Backward compatibility helper for write command error handling.
225237
"""
@@ -228,12 +240,9 @@ def _check_write_command_response(result):
228240
if write_errors:
229241
_raise_last_write_error(write_errors)
230242

231-
error = result.get("writeConcernError")
232-
if error:
233-
error_labels = result.get("errorLabels")
234-
if error_labels:
235-
error.update({'errorLabels': error_labels})
236-
_raise_write_concern_error(error)
243+
wce = _get_wce_doc(result)
244+
if wce:
245+
_raise_write_concern_error(wce)
237246

238247

239248
def _raise_last_error(bulk_write_result):

test/retryable_writes/bulkWrite-serverErrors.json renamed to test/retryable_writes/legacy/bulkWrite-serverErrors.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@
119119
"failCommands": [
120120
"insert"
121121
],
122+
"errorLabels": [
123+
"RetryableWriteError"
124+
],
122125
"writeConcernError": {
123126
"code": 91,
124-
"errmsg": "Replication is being shut down",
125-
"errorLabels": [
126-
"RetryableWriteError"
127-
]
127+
"errmsg": "Replication is being shut down"
128128
}
129129
}
130130
},

test/retryable_writes/deleteOne-serverErrors.json renamed to test/retryable_writes/legacy/deleteOne-serverErrors.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@
7575
"failCommands": [
7676
"delete"
7777
],
78+
"errorLabels": [
79+
"RetryableWriteError"
80+
],
7881
"writeConcernError": {
7982
"code": 91,
80-
"errmsg": "Replication is being shut down",
81-
"errorLabels": [
82-
"RetryableWriteError"
83-
]
83+
"errmsg": "Replication is being shut down"
8484
}
8585
}
8686
},

0 commit comments

Comments
 (0)