Skip to content

[Drain] Expose drain to dofn processElement and onTimer#37825

Open
stankiewicz wants to merge 2 commits intoapache:masterfrom
stankiewicz:expose_drain_to_dofn
Open

[Drain] Expose drain to dofn processElement and onTimer#37825
stankiewicz wants to merge 2 commits intoapache:masterfrom
stankiewicz:expose_drain_to_dofn

Conversation

@stankiewicz
Copy link
Contributor

  1. exposes
@ProcessElement
      public void process(ProcessContext c, CausedByDrain causedByDrain) {}
@OnTimer(timerId)
             public void onTimer(CausedByDrain causedByDrain) {}
  1. added tests

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.

@gemini-code-assist
Copy link
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 enhances Apache Beam's DoFn capabilities by exposing the CausedByDrain parameter to both @ProcessElement and @OnTimer methods. This change allows developers to write more robust data processing pipelines that can gracefully handle job draining scenarios, enabling custom logic to be executed when a pipeline is being shut down or reconfigured.

Highlights

  • CausedByDrain Parameter for ProcessElement: Introduced the CausedByDrain parameter to @ProcessElement methods, allowing DoFns to react to job draining events.
  • CausedByDrain Parameter for OnTimer: Enabled the CausedByDrain parameter for @OnTimer methods, providing timer-based DoFns with draining context.
  • Enhanced DoFn Signature Reflection: Updated the DoFn signature reflection mechanism to correctly identify and handle the new CausedByDrain parameter type.
  • Comprehensive Testing: Added comprehensive unit tests to validate the proper recognition and handling of the CausedByDrain parameter in DoFn signatures.

🧠 New Feature in Public Preview: You can now enable Memory 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.

Changelog
  • runners/core-java/src/main/java/org/apache/beam/runners/core/SimpleDoFnRunner.java
    • Implemented the causedByDrain method in DoFnProcessContext, DoFnTimerContext, and DoFnContext to provide access to the CausedByDrain object.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnInvoker.java
    • Added CausedByDrain import and introduced causedByDrain to the ArgumentProvider interface and its default/delegating implementations.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignature.java
    • Extended the Parameter hierarchy with CausedByDrainParameter and updated the visitor pattern to include it.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/reflect/DoFnSignatures.java
    • Updated allowed parameter lists for various DoFn lifecycle methods to include CausedByDrainParameter and added logic to recognize CausedByDrain as a valid parameter type.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/util/construction/SplittableParDoNaiveBounded.java
    • Updated the ProcessContext implementation to expose the causedByDrain method.
  • sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/reflect/DoFnSignaturesTest.java
    • Added new test cases and updated existing ones to verify the correct handling and reflection of the CausedByDrain parameter in DoFn signatures for both @ProcessElement and @OnTimer methods.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

Copy link
Member

@kennknowles kennknowles left a comment

Choose a reason for hiding this comment

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

LGTM - of course the thing missing is tests of actually using it :-)

@stankiewicz stankiewicz force-pushed the expose_drain_to_dofn branch from 62e7ee9 to c581911 Compare March 11, 2026 13:59
@stankiewicz
Copy link
Contributor Author

Run Java_Examples_Dataflow PreCommit

@stankiewicz
Copy link
Contributor Author

Run Java_PVR_Prism_Loopback PreCommit

@stankiewicz
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 support for a CausedByDrain parameter in Apache Beam's DoFn methods. This involved adding causedByDrain methods to various DoFnRunner implementations (SimpleDoFnRunner, SplittableParDoNaiveBounded, FnApiDoFnRunner), extending the DoFnInvoker interface and its ByteBuddy factory, and updating DoFnSignature and DoFnSignatures to recognize and process this new parameter type. A new test case was also added for CausedByDrain on onTimer methods. The code review suggests adding Javadoc comments to the newly introduced causedByDrain methods across several files for better documentation, and an inline comment to clarify the use of isAssignableFrom in DoFnSignatures.

Comment on lines +559 to +561
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return elem.causedByDrain();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

   /**
    * Provide a reference to the caused by drain.
    */
    @Override
    public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
      return elem.causedByDrain();
    }

Comment on lines +840 to +842
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return causedByDrain;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

   /**
    * Provide a reference to the caused by drain.
    */
    @Override
    public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
      return causedByDrain;
    }

Comment on lines +1133 to +1135
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
throw new UnsupportedOperationException("CausedByDrain parameters are not supported.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

   /**
    * Provide a reference to the caused by drain.
    */
    @Override
    public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
      throw new UnsupportedOperationException("CausedByDrain parameters are not supported.");
    }

Comment on lines +333 to +335
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
throw new UnsupportedOperationException(
String.format("CausedByDrain unsupported in %s", getErrorContext()));
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

Suggested change
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
throw new UnsupportedOperationException(
String.format("CausedByDrain unsupported in %s", getErrorContext()));
/** Provide a reference to the caused by drain. */
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
throw new UnsupportedOperationException(
String.format("CausedByDrain unsupported in %s", getErrorContext()));
}

Comment on lines +528 to +530
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return delegate.causedByDrain(doFn);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

Suggested change
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return delegate.causedByDrain(doFn);
}
/** Provide a reference to the caused by drain. */
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return delegate.causedByDrain(doFn);
}

Comment on lines +1365 to +1369
} else if (CausedByDrain.class.isAssignableFrom(rawType)) {
methodErrors.checkArgument(
rawType.equals(CausedByDrain.class),
"CausedByDrain argument must have type org.apache.beam.sdk.values.CausedByDrain.");
return Parameter.causedByDrainParameter();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: Consider adding a comment to explain why isAssignableFrom is used here instead of equals.

Suggested change
} else if (CausedByDrain.class.isAssignableFrom(rawType)) {
methodErrors.checkArgument(
rawType.equals(CausedByDrain.class),
"CausedByDrain argument must have type org.apache.beam.sdk.values.CausedByDrain.");
return Parameter.causedByDrainParameter();
} else if (CausedByDrain.class.isAssignableFrom(rawType)) {
methodErrors.checkArgument(
rawType.equals(CausedByDrain.class), // Ensure exact type match
"CausedByDrain argument must have type org.apache.beam.sdk.values.CausedByDrain.");
return Parameter.causedByDrainParameter();

Comment on lines +547 to +549
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return outerContext.causedByDrain();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

Suggested change
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return outerContext.causedByDrain();
}
/** Provide a reference to the caused by drain. */
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return outerContext.causedByDrain();
}

Comment on lines +1807 to +1810
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return currentElement.causedByDrain();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

Suggested change
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return currentElement.causedByDrain();
}
/** Provide a reference to the caused by drain. */
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return currentElement.causedByDrain();
}

Comment on lines +1955 to +1957
@Override
public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
return currentElement.causedByDrain();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

medium: This method should include a Javadoc comment explaining its purpose and behavior, similar to other methods in this class.

   /** Provide a reference to the caused by drain. */
    @Override
    public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {
      return currentElement.causedByDrain();
    }

@github-actions
Copy link
Contributor

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

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