Skip to content

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

Conversation

prashantmital
Copy link
Contributor

This change will be backported to 3.11 and 3.12 branches

…4.4+ is propagated to WriteConcernError exception
@@ -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)
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Member

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'])

error = result.get("writeConcernError")
error = result.get("writeConcernError", {})
error_labels = result.get("errorLabels", [])
error.update({'errorLabels': error_labels})
Copy link
Member

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)?

Copy link
Contributor Author

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.

Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Thanks.

Copy link
Member

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.

_raise_write_concern_error(response['writeConcernError'])
_error = response["writeConcernError"]
_labels = response.get("errorLabels", [])
_error.update({'errorLabels': _labels})
Copy link
Member

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.

error = result.get("writeConcernError")
error = result.get("writeConcernError", {})
error_labels = result.get("errorLabels", [])
error.update({'errorLabels': error_labels})
Copy link
Member

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)

@@ -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)
Copy link
Member

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?


with self.fail_point(fail_insert):
with self.assertRaises(WriteConcernError) as cm:
client.testdb.testcoll.insert_one({})
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

with self.assertRaises(WriteConcernError) as cm:
client.pymongo_test.testcoll.insert_one({})
self.assertIn('RetryableWriteError',
cm.exception._error_labels)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -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)
Copy link
Member

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'])

@prashantmital
Copy link
Contributor Author

Updated the test to account for https://jira.mongodb.org/browse/SERVER-39292 in MongoDB 4.0.x

Copy link
Member

@ShaneHarvey ShaneHarvey left a 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?

@ShaneHarvey
Copy link
Member

Still waiting on the two questions on expected server behavior and RawBSONDocument.

@prashantmital
Copy link
Contributor Author

prashantmital commented Dec 14, 2020

RawBSONDocument will not break db.command because we set check_command_response to False
here which gets passed down here and ensures that we never check for the writeConcernError document within responses to db.command. I also quickly tested this by setting a failpoint that returned writeConcernError on insert and then triggering it via db.command.

Also, I confirmed that the server only returns errorLabels as a top-level field. See the HELP ticket linked to the JIRA ticket for details.

@ShaneHarvey
Copy link
Member

ShaneHarvey commented Dec 14, 2020

Hmm, thanks for doing that investigation.

I also quickly tested this by setting a failpoint that returned writeConcernError on insert and then triggering it via db.command.

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 parse_write_concern_error option and instead always check for writeConcernError. If we did that I suspect this behavior might break and we wouldn't know without a test.

@prashantmital
Copy link
Contributor Author

Added a test.

Copy link
Member

@ShaneHarvey ShaneHarvey left a comment

Choose a reason for hiding this comment

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

LGTM

@prashantmital prashantmital changed the title PYTHON-2452 Ensure RetryableWriteError error label return is propagated PYTHON-2452 Ensure command-responses with RetryableWriteError label are retried on MongoDB 4.4+ Dec 15, 2020
@prashantmital prashantmital merged commit f458473 into mongodb:master Dec 15, 2020
prashantmital added a commit that referenced this pull request Dec 15, 2020
…re retried on MongoDB 4.4+ (#530)

(cherry picked from commit f458473)
prashantmital added a commit that referenced this pull request Dec 15, 2020
…re retried on MongoDB 4.4+ (#530)

(cherry picked from commit f458473)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants