Fix WriteToPubSub to pass ordering_key to publish() method#37345
Fix WriteToPubSub to pass ordering_key to publish() method#37345nikitagrover19 wants to merge 5 commits intoapache:masterfrom
Conversation
Summary of ChangesHello @nikitagrover19, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug in the Apache Beam Python SDK's Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request correctly adds support for ordering_key in WriteToPubSub. The changes to the _flush method are functional, but I've suggested a refactoring to improve code clarity and maintainability. I've also added suggestions to strengthen the new unit and integration tests by making the assertions more specific and robust. Overall, this is a good fix.
| if self.with_attributes and pubsub_msg.attributes: | ||
| future = self._pub_client.publish( | ||
| self._topic, pubsub_msg.data, **pubsub_msg.attributes) | ||
| self._topic, | ||
| pubsub_msg.data, | ||
| ordering_key=pubsub_msg.ordering_key | ||
| if pubsub_msg.ordering_key else '', | ||
| **pubsub_msg.attributes) | ||
| else: | ||
| future = self._pub_client.publish(self._topic, pubsub_msg.data) | ||
| if pubsub_msg.ordering_key: | ||
| future = self._pub_client.publish( | ||
| self._topic, | ||
| pubsub_msg.data, | ||
| ordering_key=pubsub_msg.ordering_key) | ||
| else: | ||
| future = self._pub_client.publish(self._topic, pubsub_msg.data) |
There was a problem hiding this comment.
The logic for publishing messages can be simplified to improve readability and reduce code duplication. You can determine the attributes and ordering_key to use and then have a single call to self._pub_client.publish. This makes the code easier to maintain.
attributes = {}
if self.with_attributes and pubsub_msg.attributes:
attributes = pubsub_msg.attributes
if pubsub_msg.ordering_key:
future = self._pub_client.publish(
self._topic,
pubsub_msg.data,
ordering_key=pubsub_msg.ordering_key,
**attributes)
else:
future = self._pub_client.publish(
self._topic, pubsub_msg.data, **attributes)| for received_message in response.received_messages: | ||
| self.assertIn('ordering_key', dir(received_message.message)) | ||
| self.sub_client.acknowledge( | ||
| request={ | ||
| "subscription": self.output_sub.name, | ||
| "ack_ids": [received_message.ack_id], | ||
| }) |
There was a problem hiding this comment.
The current verification for ordering_key only checks for its presence. To make the test more robust, you should assert that the ordering_key has the expected value for each message.
Additionally, you can improve efficiency by acknowledging all messages in a single batch request after pulling them, rather than one by one inside the loop.
| for received_message in response.received_messages: | |
| self.assertIn('ordering_key', dir(received_message.message)) | |
| self.sub_client.acknowledge( | |
| request={ | |
| "subscription": self.output_sub.name, | |
| "ack_ids": [received_message.ack_id], | |
| }) | |
| received_map = {msg.message.data: msg for msg in response.received_messages} | |
| self.assertEqual(received_map[b'order_data001'].message.ordering_key, 'key1') | |
| self.assertEqual(received_map[b'order_data002'].message.ordering_key, 'key1') | |
| self.assertEqual(received_map[b'order_data003'].message.ordering_key, 'key2') | |
| ack_ids = [msg.ack_id for msg in response.received_messages] | |
| if ack_ids: | |
| self.sub_client.acknowledge( | |
| request={ | |
| "subscription": self.output_sub.name, | |
| "ack_ids": ack_ids, | |
| }) |
| # Verify that publish was called | ||
| mock_pubsub.return_value.publish.assert_called() |
There was a problem hiding this comment.
To make this test more robust, you should also verify that ordering_key is not passed to the publish method when no ordering key is provided in the PubsubMessage. You can do this by checking the call_args of the mock.
| # Verify that publish was called | |
| mock_pubsub.return_value.publish.assert_called() | |
| # Verify that publish was called | |
| mock_pubsub.return_value.publish.assert_called() | |
| call_args = mock_pubsub.return_value.publish.call_args | |
| self.assertNotIn('ordering_key', call_args.kwargs) |
|
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment |
|
FYI: CI failures look flaky (PortableRunner gRPC
|
|
assign set of reviewers |
|
Assigning reviewers: R: @tvalentyn for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
Reminder, please take a look at this pr: @tvalentyn |
There was a problem hiding this comment.
Code Review
This pull request correctly adds support for ordering_key when writing to Pub/Sub, which is a valuable fix. The changes are logical and include new unit and integration tests to cover the new functionality. I have two suggestions for improvement: one to refactor the publishing logic in pubsub.py for better readability and maintainability, and another to strengthen the assertions in the new integration test to make it more robust.
| if self.with_attributes and pubsub_msg.attributes: | ||
| future = self._pub_client.publish( | ||
| self._topic, pubsub_msg.data, **pubsub_msg.attributes) | ||
| self._topic, | ||
| pubsub_msg.data, | ||
| ordering_key=pubsub_msg.ordering_key | ||
| if pubsub_msg.ordering_key else '', | ||
| **pubsub_msg.attributes) | ||
| else: | ||
| future = self._pub_client.publish(self._topic, pubsub_msg.data) | ||
| if pubsub_msg.ordering_key: | ||
| future = self._pub_client.publish( | ||
| self._topic, | ||
| pubsub_msg.data, | ||
| ordering_key=pubsub_msg.ordering_key) | ||
| else: | ||
| future = self._pub_client.publish(self._topic, pubsub_msg.data) |
There was a problem hiding this comment.
The logic for publishing messages can be simplified to reduce code duplication and improve readability. Instead of separate code paths for messages with and without attributes, you can build a dictionary of keyword arguments for the publish call. This makes the code cleaner and easier to maintain.
publish_kwargs = {}
if self.with_attributes and pubsub_msg.attributes:
publish_kwargs.update(pubsub_msg.attributes)
if pubsub_msg.ordering_key:
publish_kwargs['ordering_key'] = pubsub_msg.ordering_key
future = self._pub_client.publish(
self._topic, pubsub_msg.data, **publish_kwargs)| for received_message in response.received_messages: | ||
| self.assertIn('ordering_key', dir(received_message.message)) | ||
| self.sub_client.acknowledge( | ||
| request={ | ||
| "subscription": self.output_sub.name, | ||
| "ack_ids": [received_message.ack_id], | ||
| }) |
There was a problem hiding this comment.
The assertion in this test is quite weak. It only checks for the presence of the ordering_key attribute on the message object using dir(), but doesn't verify its value. A more robust test would be to check that the received messages have the correct ordering keys and attributes corresponding to the messages that were sent. Also, acknowledging messages one by one in a loop is inefficient; it's better to collect all ack_ids and acknowledge them in a single call.
| for received_message in response.received_messages: | |
| self.assertIn('ordering_key', dir(received_message.message)) | |
| self.sub_client.acknowledge( | |
| request={ | |
| "subscription": self.output_sub.name, | |
| "ack_ids": [received_message.ack_id], | |
| }) | |
| # Verify ordering keys and attributes were preserved | |
| received_msgs_map = { | |
| msg.message.data: msg.message | |
| for msg in response.received_messages | |
| } | |
| for sent_msg in test_messages: | |
| self.assertIn(sent_msg.data, received_msgs_map) | |
| received_msg = received_msgs_map[sent_msg.data] | |
| self.assertEqual(received_msg.ordering_key, sent_msg.ordering_key) | |
| self.assertEqual(dict(received_msg.attributes), sent_msg.attributes) | |
| # Acknowledge all messages at once for efficiency | |
| ack_ids = [msg.ack_id for msg in response.received_messages] | |
| if ack_ids: | |
| self.sub_client.acknowledge( | |
| request={ | |
| "subscription": self.output_sub.name, | |
| "ack_ids": ack_ids, | |
| }) |
|
Reminder, please take a look at this pr: @tvalentyn |
|
responded on the issue |
Fixes #36201
This PR fixes the
WriteToPubSubtransform in the Python SDK to properly pass theordering_keyparameter to the Google Cloud Pub/Subpublish()method.The Problem
While the
ordering_keyfield was correctly serialized into thePubsubMessageprotobuf, it was not being extracted and passed toPublisherClient.publish()in the_PubSubWriteDoFn._flush()method. This caused message ordering to not work even when users explicitly specified anordering_key.The Solution:
Modified
_PubSubWriteDoFn._flush()to:ordering_keyfrom the deserializedPubsubMessagepublish()in both code paths (with attributes and data-only)ordering_keyfor backward compatibilityThank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.