Skip to content

[Python] Add Watch transform with growth_of polling SDF#39023

Open
Eliaaazzz wants to merge 1 commit into
apache:masterfrom
Eliaaazzz:python-watch-transform
Open

[Python] Add Watch transform with growth_of polling SDF#39023
Eliaaazzz wants to merge 1 commit into
apache:masterfrom
Eliaaazzz:python-watch-transform

Conversation

@Eliaaazzz

Copy link
Copy Markdown
Contributor

This adds an experimental Watch transform to the Python SDK. It addresses #21521.

Watch watches a growing set of outputs for each input element. It calls a user poll function on an interval and emits an unbounded PCollection of input and output pairs. Polling stops per input when the poll reports completion or when a termination condition fires. The transform is a splittable DoFn, so each process call performs one poll round and then self checkpoints with defer_remainder, which keeps the polling loop durable across bundles. New outputs are deduplicated with a stable 128 bit blake2b hash of the encoded output, and a manual watermark estimator advances once per poll.

This is an initial minimal version intended for review and iteration. It implements growth_of, PollResult, PollFn, the never and after_total_of termination conditions, the growth state restriction with its coder, and the polling splittable DoFn. It is validated end to end on the DirectRunner. The output coder must be deterministic for dedup to hold across workers, and the transform logs a warning when the resolved coder is not deterministic.

The following are out of scope for this initial change and are not included here: side input poll functions, the remaining termination conditions, a separate bounded stage for exploding large poll results, and bounded growth state eviction.

Testing

  • Unit tests for the termination conditions and for the growth state coder round trips, including ordered completed hashes and a fixed width 16 byte hash coder.
  • Restriction tracker tests for claim, dedup, checkpoint, replay, boundedness, and a terminal split residual for every stop reason, so a checkpoint after a terminal poll cannot resume polling.
  • DirectRunner end to end tests for single poll completion, multiple inputs, and multi round dedup.
  • Local run reports 32 tests passing and pylint at 10.00 of 10.

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 #<ISSUE NUMBER> 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.

@codecov

codecov Bot commented Jun 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.21656% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 54.15%. Comparing base (8801afd) to head (5b039a0).
⚠️ Report is 1065 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/io/watch.py 88.21% 37 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #39023      +/-   ##
============================================
- Coverage     55.16%   54.15%   -1.01%     
  Complexity     1676     1676              
============================================
  Files          1068     1071       +3     
  Lines        167265   166847     -418     
  Branches       1208     1208              
============================================
- Hits          92265    90352    -1913     
- Misses        72816    74311    +1495     
  Partials       2184     2184              
Flag Coverage Δ
python 78.76% <88.21%> (-2.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Abacn Abacn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, went through watch.py changes

Comment thread sdks/python/apache_beam/io/watch.py Outdated
pending: PollResult


_GrowthState = Any # Union[_PollingGrowthState, _NonPollingGrowthState]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of making an Any alias?

Would it be preferred to make it an empty dataclass and make _PollingGrowthState, _NonPollingGrowthState inherit it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a cleaner design, thank you. I replaced the Any alias with an empty _GrowthState base class and had both restriction states inherit it, so it now reads as a real union type.

Comment thread sdks/python/apache_beam/io/watch.py Outdated
# ------------------------------------------------------------------------------


class _HashCode128Coder(Coder):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python coders are not implemented identically as Java. In Java we have HashCode128Coder to write fix size 16 bytes. In Python encode() encodes to bytes, and will be length-prefixed in the raw stream.

Just use Beam's BytesCoder may have a better performance (reduced a layer of wrapper) while the length check can be done elsewhere

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a point that the wrapper adds little in Python. I switched to BytesCoder, and since the digest is always 16 bytes from blake2b, I dropped the length guard as redundant.

return True


class _TimestampedValueCoder(Coder):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python SDK ships no coder for TimestampedValue

A little bit surprising. Likely due to Python SDK has some special handling:

elif isinstance(result, TimestampedValue):

converting TimestampedValue to a WindowedValue, and WindowedValue has a standard coder

self._register_coder_internal(

I'm considering make TimestampedValue a public interface like Java SDK we current have (could be a separate PR)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation, that helps a lot. Since TimestampedValue gets unwrapped into a WindowedValue, I reworded the docstring to say the coder only exists to keep it inside the restriction state. Please let me know if you would like me to build on your public coder PR once it lands.

output_coder: Optional[Coder] = None,
now_fn: Optional[Callable[[], float]] = None):
super().__init__()
self._poll_fn = poll_fn

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to wrap the poll_fn with a PollFn if it's typehint is compatible; or we should be able to derive typehints for the callables in expand beneath.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I took another pass at this. In expand I now derive the output element type from the input type and the resolved coder and set it with with_output_types, so the (input, output) pairs stay typed downstream. A PollFn can still declare its coder through default_output_coder, and I added a test that checks the derived type.

Comment thread sdks/python/apache_beam/io/watch.py Outdated
"""Optional base for a poll function ``input -> PollResult``.

Any callable with that signature works; subclass only to attach an output
coder hint via :meth:`default_output_coder`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's declared in __all__, consider add an example about creating a PollFn that is callable and has default_output_coder implemented

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a helpful suggestion, thank you. I added a short example to the PollFn docstring that shows a callable alongside a default_output_coder implementation.

Comment thread sdks/python/apache_beam/io/watch.py Outdated
output_coder = hint or coders.PickleCoder()
if not output_coder.is_deterministic():
_LOGGER.warning(
'Watch dedup uses a non-deterministic output coder (%s); equal '

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning message needs more consideration --- if it's statement is true we should raise here, as the whole point of the Watch.growth is that it only emits newly seen elements.

I think as long as key is deterministic it should be fine.

In Java it made a distinction between "output" and "outputKey", as well as its own Fn and coders. KeyCoder needs to be deterministic.

I see Python implementation has a key_coder as well. Would check key coder be sufficient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a warning is too soft here. I changed expand to raise when the resolved coder is non-deterministic. The dedup key is the encoded output, and the key coder is the output coder in this version, so this single check covers the key. I also updated the end-to-end tests to pass a deterministic coder and added a test for the new error.

Comment thread sdks/python/apache_beam/io/watch.py Outdated

element = holder[0]
now = Timestamp.of(self._now())
result = self._poll_fn(element)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put poll inside try_claim is not recommended as long running try_claim could cause issues (see #36750, it's fixed in Java, not sure if it could also happens in Python, probably worth testing)

poll can take very long if it makes API calls (e.g. list all files in gcs). Would it be possible to put it inside DoFn's process element?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an important point, thank you for raising it. I moved the poll into process ahead of the claim, so it no longer runs while the tracker lock is held. The dedup and checkpoint logic now sits in a small pure function, and try_claim simply records the primary and residual. I reran the tests and lint afterward and both pass.

Add an experimental Watch transform that watches a growing set of outputs
per input element. Watch.growth_of(poll_fn) runs a periodic poll loop as a
splittable DoFn and emits an unbounded PCollection of (input, output) pairs.

Each process() performs one poll round and self-checkpoints via
defer_remainder. New outputs are deduplicated with a stable 128-bit blake2b
hash of the encoded output, and a manual watermark estimator advances per
poll. Per-input termination supports never() and after_total_of(); polling
also stops when a poll returns PollResult.complete(). The DoFn is its own
RestrictionProvider, and restriction state serializes through a tagged
GrowthState coder.

Tests cover termination conditions, coder round-trips, the restriction
tracker claim/checkpoint/dedup logic, and DirectRunner end-to-end runs.
@Eliaaazzz Eliaaazzz force-pushed the python-watch-transform branch from 9acab87 to 5a54aa6 Compare July 1, 2026 06:56
@Eliaaazzz

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review. I addressed all the comments and pushed. PTAL when you have time.

@Eliaaazzz Eliaaazzz marked this pull request as ready for review July 5, 2026 00:48
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 introduces an experimental Watch transform to the Apache Beam Python SDK, designed to handle periodic polling of sources. It provides a robust mechanism for watching growing inputs, deduplicating results, and managing watermarks in an unbounded PCollection. The implementation leverages splittable DoFns to ensure polling remains durable across bundles and worker restarts, with comprehensive support for custom termination policies.

Highlights

  • Experimental Watch Transform: Introduced the Watch transform in the Python SDK, enabling periodic polling of growing sets of outputs for each input element.
  • Splittable DoFn Implementation: Implemented the transform as a splittable DoFn, allowing for durable polling loops that self-checkpoint using defer_remainder.
  • Deduplication and Watermarking: Added deduplication using 128-bit blake2b hashes of encoded outputs and a manual watermark estimator to manage progress.
  • Termination Conditions: Included support for termination conditions such as 'never' and 'after_total_of' to control the polling lifecycle.
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 👍 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.

Footnotes

  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 gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an experimental Watch transform for the Apache Beam Python SDK, which continuously polls for new outputs per input element using a Splittable DoFn. The review feedback highlights critical issues with watermark estimation during both the replay and active polling phases. Specifically, unconditionally setting the watermark to the input element's timestamp before yielding outputs can prematurely advance the watermark and cause outputs to be treated as late data. The reviewer recommends holding the watermark at the minimum of the input timestamp and the planned watermark before yielding, and releasing the watermark hold to MAX_TIMESTAMP upon termination to prevent the pipeline watermark from getting stuck.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +571 to +578
if isinstance(restriction, _NonPollingGrowthState):
# Replay the outputs already emitted this round, then stop. No poll.
if not tracker.try_claim(_replay_plan(restriction)):
return
_set_watermark_if_greater(watermark_estimator, timestamp)
for output in restriction.pending.outputs:
yield TimestampedValue((element, output.value), output.timestamp)
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In the replay block for _NonPollingGrowthState, the watermark is unconditionally set to timestamp before yielding the outputs. If the pending watermark (restriction.pending.watermark) is less than timestamp, this will advance the watermark past the outputs' timestamps, causing them to be treated as late data. Additionally, since this is a terminal state that stops polling, the watermark hold must be released to MAX_TIMESTAMP after yielding the outputs to prevent the pipeline watermark from being permanently stuck.

    if isinstance(restriction, _NonPollingGrowthState):\n      # Replay the outputs already emitted this round, then stop. No poll.\n      if not tracker.try_claim(_replay_plan(restriction)):\n        return\n      watermark = restriction.pending.watermark\n      if watermark is not None:\n        _set_watermark_if_greater(watermark_estimator, min(timestamp, watermark))\n      else:\n        _set_watermark_if_greater(watermark_estimator, timestamp)\n      for output in restriction.pending.outputs:\n        yield TimestampedValue((element, output.value), output.timestamp)\n      _set_watermark_if_greater(watermark_estimator, MAX_TIMESTAMP)\n      return

Comment on lines +588 to +598
# Seed the watermark hold from the input event time after the claim.
_set_watermark_if_greater(watermark_estimator, timestamp)
for output in plan.emit:
yield TimestampedValue((element, output.value), output.timestamp)
if plan.stop:
# The input is finished, so release the watermark hold to MAX.
_set_watermark_if_greater(watermark_estimator, MAX_TIMESTAMP)
return
if plan.watermark is not None:
_set_watermark_if_greater(watermark_estimator, plan.watermark)
tracker.defer_remainder(self._poll_interval)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Unconditionally setting the watermark to timestamp before yielding the outputs can cause correctness issues. If plan.watermark is less than timestamp, setting the watermark to timestamp first will advance the watermark past the outputs' timestamps, causing them to be treated as late data. To prevent this, the watermark should be held at min(timestamp, plan.watermark) (if plan.watermark is not None) before yielding, and then advanced to plan.watermark after yielding.

Suggested change
# Seed the watermark hold from the input event time after the claim.
_set_watermark_if_greater(watermark_estimator, timestamp)
for output in plan.emit:
yield TimestampedValue((element, output.value), output.timestamp)
if plan.stop:
# The input is finished, so release the watermark hold to MAX.
_set_watermark_if_greater(watermark_estimator, MAX_TIMESTAMP)
return
if plan.watermark is not None:
_set_watermark_if_greater(watermark_estimator, plan.watermark)
tracker.defer_remainder(self._poll_interval)
# Seed the watermark hold from the input event time after the claim.\n if plan.watermark is not None:\n _set_watermark_if_greater(watermark_estimator, min(timestamp, plan.watermark))\n else:\n _set_watermark_if_greater(watermark_estimator, timestamp)\n for output in plan.emit:\n yield TimestampedValue((element, output.value), output.timestamp)\n if plan.stop:\n # The input is finished, so release the watermark hold to MAX.\n _set_watermark_if_greater(watermark_estimator, MAX_TIMESTAMP)\n return\n if plan.watermark is not None:\n _set_watermark_if_greater(watermark_estimator, plan.watermark)\n tracker.defer_remainder(self._poll_interval)

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants