beam icon indicating copy to clipboard operation
beam copied to clipboard

[Python] Add UnboundedSource SDF wrapper (#19137)

Open Eliaaazzz opened this issue 6 months ago • 6 comments

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, CheckpointMark and the ReadFromUnboundedSource PTransform. Names are Pythonic; semantics match the Java contract.
  • SDF wrapper — _UnboundedSourceRestriction carries source / resume checkpoint / watermark / done flag / finalize checkpoint (fixed 5-tuple coder). _UnboundedSourceRestrictionTracker drives the reader, captures reader.get_watermark() on the data path (Java Read.java:594 parity), closes the reader on EOF / split / reader-method exception, and advances the watermark estimator to MAX_TIMESTAMP on the terminal claim so downstream event-time windows can close.
  • iobase.Read.expand dispatch — a function-local lazy-import branch routes Read(UnboundedSource(...)) through ReadFromUnboundedSource, breaking the iobase ↔ unbounded_source cycle.
  • Runner-API — Read.to_runner_api_parameter widens to (BoundedSource, UnboundedSource), writing READ.urn + IsBounded.UNBOUNDED. Decode rides the existing PICKLED_SOURCE URN on SourceBase. Runner-side dispatch on UNBOUNDED in bundle_processor.IMPULSE_READ_TRANSFORM is deferred.
  • Output coder — ReadFromUnboundedSource.expand calls coders.registry.register_coder + sets element_type so a source's default_output_coder propagates to the output PCollection.
  • Initial fan-out — _UnboundedSourceRestrictionProvider.split invokes UnboundedSource.split(20, options) with strict isinstance validation; 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 on reader.advance / reader.get_watermark exceptions, DoFn finally close on downstream yield exception (unit via generator.close() + integration with raising Map), cloudpickle round-trip, circular import in three subprocess orderings, e2e DirectRunner with FixedWindows + GroupByKey.
  • iobase_test.py — Read(UnboundedSource) dispatch through the new expand branch; Read.to_runner_api / from_runner_api round-trip asserting the IsBounded.UNBOUNDED enum.

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)

Build python source distribution and wheels Python tests Java tests Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

Eliaaazzz avatar May 28 '26 08:05 Eliaaazzz

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.

gemini-code-assist avatar May 28 '26 08:05 gemini-code-assist

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

github-actions[bot] avatar May 28 '26 10:05 github-actions[bot]

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 tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting 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).

github-actions[bot] avatar Jun 01 '26 16:06 github-actions[bot]

Hi @Abacn I pushed an update addressing your latest review feedback.

Summary:

  • cleaned user-facing pydoc and private comments
  • kept ReadFromUnboundedSource docs focused on beam.io.Read
  • added warning coverage for close errors while preserving processing failures
  • updated UnboundedCountingSource tests with realistic timestamps, @override usage, and optional even/odd splitting
  • kept CHANGES.md unchanged

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?

Eliaaazzz avatar Jun 02 '26 12:06 Eliaaazzz

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 Read boundedness 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 UnboundedSource Read on the expanded composite runner API path, with a regression test for avoiding deprecated READ serialization

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.

Eliaaazzz avatar Jun 04 '26 12:06 Eliaaazzz

Reminder, please take a look at this pr: @tvalentyn

github-actions[bot] avatar Jun 24 '26 13:06 github-actions[bot]