-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-2452 Ensure command-responses with RetryableWriteError label are retried on MongoDB 4.4+ #530
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-2452 Ensure command-responses with RetryableWriteError label are retried on MongoDB 4.4+ #530
Conversation
…4.4+ is propagated to WriteConcernError exception
test/test_retryable_writes.py
Outdated
@@ -453,6 +454,27 @@ def test_batch_splitting_retry_fails(self): | |||
self.assertEqual(final_txn, expected_txn) | |||
self.assertEqual(coll.find_one(projection={'_id': True}), {'_id': 1}) | |||
|
|||
@client_context.require_version_min(4, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs @require_failCommand_fail_point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why require 4.4? Shouldn't this test also work the same on 4.2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#determining-retryable-errors seems to suggest that only MongoDB 4.4+ will add the RetryableWritesError
to its command response. So the command assertion wouldn't work with MongoDB 4.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct but if I understand correctly, the spec says that on <4.4 the driver adds the RetryableWritesError label to retryable errors. So shouldn't the label be there either way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this to client_context.require_version_min(3, 6)
to make sure we add the error label correctly on <4.4. We can then make the final assertion server version specific:
if version >= 4.4:
self.assertIn(
'RetryableWriteError',
listener.results['succeeded'][-1].reply['errorLabels'])
pymongo/helpers.py
Outdated
error = result.get("writeConcernError") | ||
error = result.get("writeConcernError", {}) | ||
error_labels = result.get("errorLabels", []) | ||
error.update({'errorLabels': error_labels}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should ensure that _check_command_response
above has the same behavior. Also is it ever possible for error
to be RawBSONDocument (where .update
would fail)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the behavior to _check_command_response
. The question about RawBSONDocument
needs further investigation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is flawed because it ends up always making error
true. It should be:
error = result.get("writeConcernError", {})
if error:
error_labels = result.get("errorLabels", [])
if error_labels:
error.update({'errorLabels': error_labels})
_raise_write_concern_error(error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not resolved because we still need to answer the RawBSONDocument question.
pymongo/helpers.py
Outdated
_raise_write_concern_error(response['writeConcernError']) | ||
_error = response["writeConcernError"] | ||
_labels = response.get("errorLabels", []) | ||
_error.update({'errorLabels': _labels}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
if _labels:
_error.update({'errorLabels': _labels})
So that we don't add an empty errorLabels to the error when the response doesn't have errorLabels.
pymongo/helpers.py
Outdated
error = result.get("writeConcernError") | ||
error = result.get("writeConcernError", {}) | ||
error_labels = result.get("errorLabels", []) | ||
error.update({'errorLabels': error_labels}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is flawed because it ends up always making error
true. It should be:
error = result.get("writeConcernError", {})
if error:
error_labels = result.get("errorLabels", [])
if error_labels:
error.update({'errorLabels': error_labels})
_raise_write_concern_error(error)
test/test_retryable_writes.py
Outdated
@@ -453,6 +454,27 @@ def test_batch_splitting_retry_fails(self): | |||
self.assertEqual(final_txn, expected_txn) | |||
self.assertEqual(coll.find_one(projection={'_id': True}), {'_id': 1}) | |||
|
|||
@client_context.require_version_min(4, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why require 4.4? Shouldn't this test also work the same on 4.2?
test/test_retryable_writes.py
Outdated
|
||
with self.fail_point(fail_insert): | ||
with self.assertRaises(WriteConcernError) as cm: | ||
client.testdb.testcoll.insert_one({}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using the standard pymongo_test
database name here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/test_retryable_writes.py
Outdated
with self.assertRaises(WriteConcernError) as cm: | ||
client.pymongo_test.testcoll.insert_one({}) | ||
self.assertIn('RetryableWriteError', | ||
cm.exception._error_labels) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use the has_error_label api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/test_retryable_writes.py
Outdated
@@ -453,6 +454,27 @@ def test_batch_splitting_retry_fails(self): | |||
self.assertEqual(final_txn, expected_txn) | |||
self.assertEqual(coll.find_one(projection={'_id': True}), {'_id': 1}) | |||
|
|||
@client_context.require_version_min(4, 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update this to client_context.require_version_min(3, 6)
to make sure we add the error label correctly on <4.4. We can then make the final assertion server version specific:
if version >= 4.4:
self.assertIn(
'RetryableWriteError',
listener.results['succeeded'][-1].reply['errorLabels'])
Updated the test to account for https://jira.mongodb.org/browse/SERVER-39292 in MongoDB 4.0.x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you get clarification from the server/retryable writes team on whether it's possible for errorLabels to appear in two places?
Still waiting on the two questions on expected server behavior and RawBSONDocument. |
Also, I confirmed that the server only returns |
Hmm, thanks for doing that investigation.
Can you add this as a test so that we don't regress on this behavior? Edit: In particular, I've considered getting rid of the internal |
Added a test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This change will be backported to 3.11 and 3.12 branches