-
Notifications
You must be signed in to change notification settings - Fork 4.5k
fix(pubsub): handle pubsub message attributes correctly in write #36140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
56258f3
fix(pubsub): handle pubsub message attributes correctly in write oper…
liferoad c4e6bc5
fix(pubsub): replace NotImplementedError with warnings for unsupporte…
liferoad 6800fc7
check pipelines when raising errors
liferoad 6c045be
lint
liferoad 49c3af0
lint
liferoad df78be0
fix tests
liferoad 5223779
fix(pubsub): improve runner detection and error messaging
liferoad 396b3ab
test(pubsub): increase test timeout durations for reliability
liferoad 4433ce6
fix lint
liferoad 33a597c
fix(pubsub): handle None runner case and improve debug logging
liferoad 173d8c7
Merge branch 'master' into pubusub-protobuf
liferoad a712ac8
use output_labels_supported
liferoad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run.", | ||
| "modification": 28 | ||
| "modification": 29 | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -414,6 +414,7 @@ def __init__( | |
| self.project, self.topic_name = parse_topic(topic) | ||
| self.full_topic = topic | ||
| self._sink = _PubSubSink(topic, id_label, timestamp_attribute) | ||
| self.pipeline_options = None # Will be set during expand() | ||
|
|
||
| @staticmethod | ||
| def message_to_proto_str(element: PubsubMessage) -> bytes: | ||
|
|
@@ -429,6 +430,9 @@ def bytes_to_proto_str(element: Union[bytes, str]) -> bytes: | |
| return msg._to_proto_str(for_publish=True) | ||
|
|
||
| def expand(self, pcoll): | ||
| # Store pipeline options for use in DoFn | ||
| self.pipeline_options = pcoll.pipeline.options if pcoll.pipeline else None | ||
|
|
||
| if self.with_attributes: | ||
| pcoll = pcoll | 'ToProtobufX' >> ParDo( | ||
| _AddMetricsAndMap( | ||
|
|
@@ -564,11 +568,65 @@ def __init__(self, transform): | |
|
|
||
| # TODO(https://github.com/apache/beam/issues/18939): Add support for | ||
| # id_label and timestamp_attribute. | ||
| if transform.id_label: | ||
| raise NotImplementedError('id_label is not supported for PubSub writes') | ||
| if transform.timestamp_attribute: | ||
| raise NotImplementedError( | ||
| 'timestamp_attribute is not supported for PubSub writes') | ||
| # Only raise errors for DirectRunner or batch pipelines | ||
| pipeline_options = transform.pipeline_options | ||
| output_labels_supported = True | ||
|
|
||
| if pipeline_options: | ||
| from apache_beam.options.pipeline_options import StandardOptions | ||
|
|
||
| # Check if using DirectRunner | ||
| try: | ||
| # Get runner from pipeline options | ||
| all_options = pipeline_options.get_all_options() | ||
| runner_name = all_options.get('runner', StandardOptions.DEFAULT_RUNNER) | ||
|
|
||
| # Check if it's a DirectRunner variant | ||
| if (runner_name is None or | ||
| (runner_name in StandardOptions.LOCAL_RUNNERS or 'DirectRunner' | ||
| in str(runner_name) or 'TestDirectRunner' in str(runner_name))): | ||
| output_labels_supported = False | ||
| except Exception: | ||
| # If we can't determine runner, assume DirectRunner for safety | ||
| output_labels_supported = False | ||
|
|
||
| # Check if in batch mode (not streaming) | ||
| standard_options = pipeline_options.view_as(StandardOptions) | ||
| if not standard_options.streaming: | ||
| output_labels_supported = False | ||
| else: | ||
| # If no pipeline options available, fall back to original behavior | ||
| output_labels_supported = False | ||
|
|
||
| # Log debug information for troubleshooting | ||
| import logging | ||
| runner_info = getattr( | ||
| pipeline_options, 'runner', | ||
| 'None') if pipeline_options else 'No options' | ||
| streaming_info = 'Unknown' | ||
| if pipeline_options: | ||
| try: | ||
| standard_options = pipeline_options.view_as(StandardOptions) | ||
| streaming_info = 'streaming=%s' % standard_options.streaming | ||
| except Exception: | ||
| streaming_info = 'streaming=unknown' | ||
|
|
||
| logging.debug( | ||
| 'PubSub unsupported feature check: runner=%s, %s', | ||
| runner_info, | ||
| streaming_info) | ||
|
|
||
| if not output_labels_supported: | ||
|
|
||
| if transform.id_label: | ||
| raise NotImplementedError( | ||
| f'id_label is not supported for PubSub writes with DirectRunner ' | ||
| f'or in batch mode (runner={runner_info}, {streaming_info})') | ||
| if transform.timestamp_attribute: | ||
| raise NotImplementedError( | ||
| f'timestamp_attribute is not supported for PubSub writes with ' | ||
| f'DirectRunner or in batch mode ' | ||
| f'(runner={runner_info}, {streaming_info})') | ||
|
|
||
| def setup(self): | ||
| from google.cloud import pubsub | ||
|
|
@@ -593,11 +651,21 @@ def _flush(self): | |
|
|
||
| import time | ||
|
|
||
| # The elements in buffer are already serialized bytes from the previous | ||
| # transforms | ||
| futures = [ | ||
| self._pub_client.publish(self._topic, elem) for elem in self._buffer | ||
| ] | ||
| # The elements in buffer are serialized protobuf bytes from the previous | ||
| # transforms. We need to deserialize them to extract data and attributes. | ||
| futures = [] | ||
| for elem in self._buffer: | ||
| # Deserialize the protobuf to get the original PubsubMessage | ||
| pubsub_msg = PubsubMessage._from_proto_str(elem) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the pubsub client does not accept |
||
|
|
||
| # Publish with the correct data and attributes | ||
| if self.with_attributes and pubsub_msg.attributes: | ||
| future = self._pub_client.publish( | ||
| self._topic, pubsub_msg.data, **pubsub_msg.attributes) | ||
| else: | ||
| future = self._pub_client.publish(self._topic, pubsub_msg.data) | ||
|
|
||
| futures.append(future) | ||
|
|
||
| timer_start = time.time() | ||
| for future in futures: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
only dataflow runner with streaming can support these two parameters.
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.
Is there some method that is invoked during pickling that this could be moved to instead? It seems that you need this runner check because this class is init'ed even when Dataflow streaming will swap it out and not use it.
If we have it in the pickle method perhaps we can remove all the runner checking and just unconditionally throw the error.
Alternatively perhaps it's simpler to just implement these, it shouldn't be too complex. the id can just be a random string and the timestamp should be the element data timestamp
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.
Dataflow Runner with streaming support these two fields. We have to check these based on the runner and the pipeline type. Implementing them is tracked by #18939. Since this fix is blocking several postcommit workflows, I think we should merge it first.