[Python] Add UnboundedSource SDF wrapper (#19137)
The Python SDK has no public UnboundedSource API: pipelines that need to read a custom unbounded source (Pub/Sub-like queues, CDC feeds, etc.) cannot do so in Python without dropping to the legacy non-portable Read primitive.
This change adds a Splittable-DoFn wrapper that brings Java's UnboundedSource semantics to the Python SDK:
- Public ABCs — new
UnboundedSource,UnboundedReader,CheckpointMarkand theReadFromUnboundedSourcePTransform. Names are Pythonic; semantics match the Java contract. - SDF wrapper —
_UnboundedSourceRestrictioncarries source / resume checkpoint / watermark / done flag / finalize checkpoint (fixed 5-tuple coder)._UnboundedSourceRestrictionTrackerdrives the reader, capturesreader.get_watermark()on the data path (JavaRead.java:594parity), closes the reader on EOF / split / reader-method exception, and advances the watermark estimator toMAX_TIMESTAMPon the terminal claim so downstream event-time windows can close. iobase.Read.expanddispatch — a function-local lazy-import branch routesRead(UnboundedSource(...))throughReadFromUnboundedSource, breaking theiobase↔unbounded_sourcecycle.- Runner-API —
Read.to_runner_api_parameterwidens to(BoundedSource, UnboundedSource), writingREAD.urn+IsBounded.UNBOUNDED. Decode rides the existingPICKLED_SOURCEURN onSourceBase. Runner-side dispatch onUNBOUNDEDinbundle_processor.IMPULSE_READ_TRANSFORMis deferred. - Output coder —
ReadFromUnboundedSource.expandcallscoders.registry.register_coder+ setselement_typeso a source'sdefault_output_coderpropagates to the output PCollection. - Initial fan-out —
_UnboundedSourceRestrictionProvider.splitinvokesUnboundedSource.split(20, options)with strictisinstancevalidation; falls back to a single restriction on split-refusal exceptions.
Tests:
unbounded_source_test.py— ABC contracts, restriction coder round-trip, tracker state machine, finalize idempotency, source-watermark vs. record-timestamp regression, finalize / resume channel separation, tracker-internal close onreader.advance/reader.get_watermarkexceptions, DoFnfinallyclose on downstream yield exception (unit viagenerator.close()+ integration with raisingMap), cloudpickle round-trip, circular import in three subprocess orderings, e2e DirectRunner withFixedWindows+GroupByKey.iobase_test.py—Read(UnboundedSource)dispatch through the newexpandbranch;Read.to_runner_api/from_runner_apiround-trip asserting theIsBounded.UNBOUNDEDenum.
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] Mention the appropriate issue in your description (for example: `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, comment `fixes #
` instead. - [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache Individual Contributor License Agreement.
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.
Summary of Changes
Hello, I'm Gemini Code Assist[^1]! 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 brings streaming source capabilities to the Python SDK by implementing the UnboundedSource abstraction via a Splittable DoFn wrapper. This change allows developers to define unbounded sources that are compatible with the portable Fn API, significantly improving the parity between Python and Java streaming I/O implementations.
Highlights
- UnboundedSource Abstractions: Introduced new abstract base classes including UnboundedSource, UnboundedReader, and CheckpointMark to the Python SDK, mirroring Java's streaming I/O model.
- SDF Wrapper Implementation: Implemented a Splittable DoFn wrapper that enables unbounded sources to run on portable runners like DirectRunner and FnApiRunner.
- Integration with iobase: Updated iobase.Read.expand to automatically dispatch UnboundedSource instances, ensuring seamless integration with existing pipeline construction patterns.
- Testing and Validation: Added a comprehensive suite of unit and integration tests covering source contracts, restriction tracker state machines, and runner API round-trip compatibility.
New Features
🧠 You can now enable Memory (public preview) 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 Assist
The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize the 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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with :thumbsup: and :thumbsdown: 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.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers
Assigning reviewers:
R: @tvalentyn for label python.
Note: If you would like to opt out of this review, comment assign to next reviewer.
Available commands:
stop reviewer notifications- opt out of the automated review toolingremind me after tests pass- tag the comment author after tests passwaiting on author- shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
The PR bot will only process comments in the main thread (not review comments).
Hi @Abacn I pushed an update addressing your latest review feedback.
Summary:
- cleaned user-facing pydoc and private comments
- kept
ReadFromUnboundedSourcedocs focused onbeam.io.Read - added warning coverage for close errors while preserving processing failures
- updated
UnboundedCountingSourcetests with realistic timestamps,@overrideusage, and optional even/odd splitting - kept
CHANGES.mdunchanged
Verified:
python -m pytest -q sdks/python/apache_beam/io/unbounded_source_test.py sdks/python/apache_beam/io/iobase_test.py -q
Could you please take another look when you have time?
Hi @Abacn, thanks for the continued review.
I pushed one squashed follow-up commit after the reviewed commit for incremental review.
Changes:
- made the unbounded
Readboundedness test graph-only - moved portability imports to module scope
- removed new-file pytype skips
- implemented an at-most-once guard for checkpoint finalization and added regression coverage
- removed the generic pickle/subprocess/syntax tests
- kept
UnboundedSourceReadon the expanded composite runner API path, with a regression test for avoiding deprecatedREADserialization
Verified locally:
python -m pytest -q sdks/python/apache_beam/io/unbounded_source_test.py sdks/python/apache_beam/io/iobase_test.py -q./gradlew :pythonFormatterPreCommit --console=plain./gradlew :pythonLintPreCommit --console=plain
I would appreciate another look when you have time. Thanks again.
Reminder, please take a look at this pr: @tvalentyn