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-4667 Handle $clusterTime from error responses in client Bulk Write #1822

Merged
merged 7 commits into from
Sep 5, 2024

Conversation

blink1073
Copy link
Member

No description provided.

@blink1073 blink1073 changed the title PYTHON-4667 Handle from error responses in client Bulk Write PYTHON-4667 Handle $clusterTime from error responses in client Bulk Write Aug 30, 2024
@@ -312,6 +312,9 @@ async def write_command(
bwc._fail(request_id, failure, duration)
# Top-level error will be embedded in ClientBulkWriteException.
reply = {"error": exc}
# Propagate $clusterTime to the caller.
if "$clusterTime" in cmd:
reply["$clusterTime"] = cmd["$clusterTime"]
Copy link
Member

Choose a reason for hiding this comment

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

$clusterTime needs to be parsed from the (potential) server error response, not the command. We also need to do the same for operationTime.

This is the method that handles it, it probably makes more sense to call this method at a lower level if we can (perhaps in write_command):

client._process_response(command_response, session)

Or another alternative.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, updated.

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.

Could you add a test to test/mockupdb/test_cluster_time.py? Something like:

    def test_bulk_error(self):
        def callback(client: MongoClient[dict]) -> None:
            client.db.collection.bulk_write(
                [InsertOne({}), InsertOne({})]
            )

        self.cluster_time_conversation(
            callback,
            [{"ok": 0, "errmsg": "mock error"}],
        )

if isinstance(exc, OperationFailure):
await client._process_response(exc.details, bwc.session) # type: ignore[arg-type]
else:
await client._process_response({}, bwc.session) # type: ignore[arg-type]
Copy link
Member

Choose a reason for hiding this comment

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

We don't need an else here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed

@@ -308,6 +309,11 @@ async def write_command(

if bwc.publish:
bwc._fail(request_id, failure, duration)
# Process the response from the server.
if isinstance(exc, OperationFailure):
Copy link
Member

Choose a reason for hiding this comment

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

isinstance(exc, (NotPrimaryError, OperationFailure))

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

pymongo/synchronous/bulk.py Outdated Show resolved Hide resolved
@blink1073
Copy link
Member Author

Could you add a test

Done

callback,
[{"ok": 0, "errmsg": "mock error"}],
)
self.assertIn("$clusterTime", str(context.exception))
Copy link
Member

Choose a reason for hiding this comment

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

This test is skipping the clusterTime asserts in cluster_time_conversation. We need to do:

        def callback(client: MongoClient[dict]) -> None:
            with self.assertRaises(OperationFailure):
                client.db.collection.bulk_write([InsertOne({}), InsertOne({})])

        self.cluster_time_conversation(
            callback,
            [{"ok": 0, "errmsg": "mock error"}],
        )

Copy link
Member

Choose a reason for hiding this comment

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

Could you also add a similar test for client.bulk_write?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done and done

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 once the tests pass!

@blink1073 blink1073 merged commit 4d48130 into mongodb:master Sep 5, 2024
33 of 35 checks passed
@blink1073 blink1073 deleted the PYTHON-4667 branch September 5, 2024 00:40
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