You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "mohamed-ali24 (via GitHub)" <gi...@apache.org> on 2025/09/18 21:28:51 UTC

[I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

mohamed-ali24 opened a new issue, #36201:
URL: https://github.com/apache/beam/issues/36201

   ### What would you like to happen?
   
   The WriteToPubSub transform in the Apache Beam Python SDK does not correctly handle the ordering_key when publishing messages to Pub/Sub. While the ordering_key is correctly serialized into a [PubsubMessage protobuf](https://github.com/apache/beam/blob/9504399b3756a7f025a3fd1d7b24ce949a9d0a62/sdks/python/apache_beam/io/gcp/pubsub.py#L186), it is not passed to the `publish()` method of the `PublisherClient`. As a result, message ordering is not preserved, even when an `ordering_key` is specified.
   
   The issue lies in the _flush method of the `_PubSubWriteDoFn` class, which omits the `ordering_key` when calling `self._pub_client.publish()` [here](https://github.com/apache/beam/blob/9504399b3756a7f025a3fd1d7b24ce949a9d0a62/sdks/python/apache_beam/io/gcp/pubsub.py#L663) or [here](https://github.com/apache/beam/blob/9504399b3756a7f025a3fd1d7b24ce949a9d0a62/sdks/python/apache_beam/io/gcp/pubsub.py#L666) for
   
   The publish method in `_PubSubWriteDoFn` only passes `data` and `attributes` to the underlying client, and is missing the `ordering_key`:
   
   ```
   # From apache_beam/io/gcp/pubsub.py
   
   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)
   ```
   
   ### Issue Priority
   
   Priority: 2 (default / most feature requests should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [ ] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Samza Runner
   - [ ] Component: Twister2 Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3850491964

   Thanks @nikitagrover19 
   
   There was a related effort in https://github.com/apache/beam/pull/31608  . Reading through that work,  I think this issue is more nuanced. 
   
   I believe _PubSubWriteDoFn might be used by Beam Direct runner only, but not Dataflow Runner, so making changes to that code would have a limited effect for non-trivial usages of Beam. 
   
   To support this functionality with the Dataflow runner, we might have to support an option like `--enable_custom_pubsub_sink` in Python SDK as well or have Dataflow support https://issuetracker.google.com/issues/200955424 .
   
   cc'ing @sjvanrossum @ahmedabu98 @scwhittle who worked on https://github.com/apache/beam/pull/31608 and can correct me.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "nikitagrover19 (via GitHub)" <gi...@apache.org>.
nikitagrover19 commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3748485829

   Hi! I’m interested in taking a look. From the description, it seems like `ordering_key` is serialized into `PubsubMessage` but never passed through to `PublisherClient.publish()`, which would explain why ordering isn’t preserved.
   
   I’m new to the Beam codebase, but I’m happy to investigate.😀


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-4674473439

   > Basic XLang PubSub write end-to-end ✅
   
   To double-check, did you use ordering key in this experiment? In other words this was XLang, no attributes, ordering keys, correct?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "nikitagrover19 (via GitHub)" <gi...@apache.org>.
nikitagrover19 commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3901772496

   Thanks, that makes sense.
   
   I’ll limit the change to DirectRunner support and add a warning when ordering_key is used with DataflowRunner (without failing the pipeline), consistent with Java SDK messaging.
   
   I’ll also scope the integration tests to DirectRunner and verify they pass.
   
   I’ll push an update shortly.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn closed issue #36201: Beam Python PubSubIO Does Not Support Ordered Key Publishing
URL: https://github.com/apache/beam/issues/36201


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3887773454

   > I’m also curious whether a Python equivalent of --experiments=enable_custom_pubsub_sink would be feasible as a shorter-term option
   
   Good question, I don't know whether it would only be inefficient or there are some blockers for that. You could ask on Beam mailing list and other folks might have some knowledge about what it would take to make custom pubsub sink to work with Dataflow. I think I would probably recommend such users to use Java's custom pubsub sink in Python SDK via cross-language IO. That sounds like a viable approach and might already work, though I didn't give it a try - feel free to do so. 
   
   > A few options I see are:
   
   I think limiting the change to direct runner is fine; warning users that it won't work on Dataflow is fine; we can use consistent messaging wit Java SDK, as long as we don't introduce breaking changes (e.g. I wouldn't start failing pipelines that are already running even if they attempt to use the functionality that doesn't work yet; a warning might be better).
   
   If we go with direct runner approach, let's limit the scope of integration tests to direct runner and also verify they pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "nikitagrover19 (via GitHub)" <gi...@apache.org>.
nikitagrover19 commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-4019731231

   
    Hi @tvalentyn  apologies for the delay , had to take some time to properly investigate the XLang approach you suggested.
   
    I went ahead and tested it, and the good news is it works. I also ended up implementing `publish_with_ordering_key` through the XLang bridge while I was at it, so we have something concrete to look at.
   
    **What I tested:**
    - Basic XLang PubSub write end-to-end ✅
    - `publish_with_ordering_key=True` with `with_attributes=True` — ordering keys preserved end-to-end ✅  
    - Default path (no ordering key) — no regression ✅
   
    **Changes made:**
    - `ExternalWrite.java`: added `publishWithOrderingKey` to `Configuration`, wired to existing Java `withOrderingKey()`
    - `external/gcp/pubsub.py`: added `publish_with_ordering_key=False` param to `WriteToPubSub`
    - `PubsubIO.java`: fixed a pre-existing bug where `PubsubBoundedWriter.startBundle` ignored the transform-level `pubsubRootUrl`, always falling back to pipeline options
    - `external/gcp/pubsub.py`: also fixed a pre-existing bug where `with_attributes=True` referenced a non-existent method and would crash for any user
   
    I'm happy to go with this approach instead of the Direct Runner implementation. Just let me know if you'd like me to update the PR to drop the native Python changes in favor of the XLang path, or if you'd prefer to keep both.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "nikitagrover19 (via GitHub)" <gi...@apache.org>.
nikitagrover19 commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3748486193

   .take-issue


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-4673583643

   > publish_with_ordering_key=True with with_attributes=True — ordering keys preserved end-to-end ✅
   
   does XLang work when there are no attributes, but there are ordering keys, if you tested this? Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "nikitagrover19 (via GitHub)" <gi...@apache.org>.
nikitagrover19 commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3851083426

   Thanks for the context — reading through #31608 helped clarify why this is more complex than just ` ordering_key ` through.
   
   I understand now that simply wiring the key into ` _PubSubWriteDoFn ` would only affect DirectRunner and could give a misleading impression of Dataflow support, given the batching logic and sink overrides used by DataflowRunner.
   
   I’m happy to adjust the scope of this work based on your guidance. A few options I see are:
   
   1. **Document the limitation clearly** – add docstring or user-facing warnings that ` ordering_key `  is only honored with DirectRunner
   2. **Add runtime guards** – warn or fail fast when ` ordering_key `  is used with DataflowRunner, similar to the Java-side handling in #31608
   3. **Keep as-is** – merge DirectRunner-only support and track DataflowRunner support separately
   
   I’m also curious whether a Python equivalent of ` --experiments=enable_custom_pubsub_sink ` would be feasible as a shorter-term option, or if that’s blocked by deeper Dataflow backend work.
   
   I’m relatively new to Beam internals, so guidance on the intended direction would be really helpful. Happy to make whatever changes make sense.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-3916661993

   Dataflow has it's own implementation of pubsub sources/sinks that direct runners, it has been this way for many years and that's not changing. Streaming source wouldn't work in batch mode.
   
   > streaming Dataflow behavior might differ from DirectRunner 
   
   Yeah, ideally behaviors would match. In this case my worry is that the additional functionality in Direct runner would have limited practical application. 
   
   I think if a Python user must use Ordered Key Publishing, my first suggestion would be to try using Java Pubsub IO through XLang framework. You could try to give a try, check whether it works,  and document how to use it as a part of this issue, instead of implementing in Python's direct runner.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Beam Python PubSubIO Does Not Support Ordered Key Publishing [beam]

Posted by "tvalentyn (via GitHub)" <gi...@apache.org>.
tvalentyn commented on issue #36201:
URL: https://github.com/apache/beam/issues/36201#issuecomment-4137599999

    Keeping both is fine, though I'd prefer some very visible message that non-xlang version wouldn't work for Dataflow. Thank you so much for looking!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org