[Flink] Bound SDF self-checkpoint residual state on the portable runner#39191
[Flink] Bound SDF self-checkpoint residual state on the portable runner#39191Eliaaazzz wants to merge 1 commit into
Conversation
Summary of ChangesHello, 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 addresses a memory leak in the portable Flink runner affecting Splittable DoFns (SDFs) that perform self-checkpoints. Previously, each self-checkpoint created a unique ValueState tag, leading to an accumulation of keyed-state descriptors and eventual OutOfMemory errors. The changes introduce a stable MapState to manage these residuals, ensuring the state remains bounded across checkpoints. Note that residuals from older savepoints using the previous mechanism are not migrated and will be dropped. Highlights
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 AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request addresses unbounded checkpoint state growth for splittable DoFns (SDFs) that self-checkpoint on the portable Flink runner by migrating from per-residual state tags to a single, stable MapState tag. It also ensures consumed residuals are removed to keep the state bounded. The review feedback highlights a critical issue where reading from older savepoints can return null, leading to potential NullPointerExceptions in both FlinkExecutableStageFunction and ExecutableStageDoFnOperator. Adding null checks in these locations is recommended to safely drop the missing residuals.
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.
| WindowedValue<InputT> stateValue = residualState.get(timer.getTimerId()).read(); | ||
| residualState.remove(timer.getTimerId()); | ||
| residuals.add(stateValue); |
There was a problem hiding this comment.
If an older savepoint is used, the residual state won't be found under the new MapState tag, and residualState.get(timer.getTimerId()).read() will return null. Adding null to residuals will lead to a NullPointerException during processElements. We should add a null check to safely drop the residual as intended.
WindowedValue<InputT> stateValue = residualState.get(timer.getTimerId()).read();
residualState.remove(timer.getTimerId());
if (stateValue != null) {
residuals.add(stateValue);
}| WindowedValue<InputT> stateValue = residualState.get(timerId).read(); | ||
| processElement(stateValue); |
There was a problem hiding this comment.
If an older savepoint is used, residualState.get(timerId).read() will return null. Passing null to processElement will cause a NullPointerException. We should add a null check to safely drop the residual as intended.
| WindowedValue<InputT> stateValue = residualState.get(timerId).read(); | |
| processElement(stateValue); | |
| WindowedValue<InputT> stateValue = residualState.get(timerId).read(); | |
| if (stateValue != null) { | |
| processElement(stateValue); | |
| } |
235b200 to
a42f34d
Compare
Abacn
left a comment
There was a problem hiding this comment.
Also, as we confirmed SDK now functioning as expected, shall we remove these TODO? https://github.com/apache/beam/blob/master/runners/flink/src/main/java/org/apache/beam/runners/flink/FlinkStreamingPortablePipelineTranslator.java#L735
| return SDF_PREFIX + ":" + id + ":" + index; | ||
| } | ||
|
|
||
| /** The state id under which all SDF self-checkpoint residuals for a key/window are stored. */ |
There was a problem hiding this comment.
Just a note, it appears BundleCheckpointHandlers is only used by Flink runner, thus this bug only affects Flink runner as well
| "nullness" // TODO(https://github.com/apache/beam/issues/20497) | ||
| }) | ||
| public class FlinkExecutableStageFunction<InputT> extends AbstractRichFunction | ||
| implements MapPartitionFunction<WindowedValue<InputT>, RawUnionValue>, |
There was a problem hiding this comment.
This file is branched in runners/flink/2.0/src. Fix needs to be done in both place
|
Assigning reviewers: R: @chamikaramj added as fallback since no labels match configuration Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
a42f34d to
27aadfa
Compare
|
Thanks for the review! On the TODO at L735, I removed I also applied the same fix to the branched |
A splittable DoFn that self-checkpoints, for example a polling source using tracker.defer_remainder(), stored each residual under a new unique state tag, so every self-checkpoint registered another Flink keyed-state descriptor and the job leaked heap without bound. Store all residuals for a key and window under a single stable MapState keyed by the checkpoint id, and remove each entry once its timer fires and the residual is re-processed. The streaming and batch read paths look the residual up by timer id. Fixes apache#27648
27aadfa to
766000a
Compare
Splittable DoFns that self-checkpoint on the portable Flink runner leaked keyed state without bound.
StateAndTimerBundleCheckpointHandler.onCheckpointwrote each residual under a fresh, uniqueValueStatetag (sdf_checkpoint:<id>:<index>), so a polling SDF (for example one usingtracker.defer_remainder()) registered a new Flink keyed-state descriptor inregisteredKVStateson every self-checkpoint, and the job's heap grew until it OOMed.This stores all residuals for a key and window under a single stable
MapState(sdf_checkpoint_residuals) keyed by the per-residual checkpoint id, and removes each entry once its timer fires and the residual is re-processed. Both read paths, streamingExecutableStageDoFnOperator.onTimerand batchFlinkExecutableStageFunction, look the residual up by timer id. Timer ids stay dynamic; Flink already keeps them in a stable pending-timers map that is cleared on fire.Residuals persisted by the previous code under per-residual
ValueStatetags are not migrated, so an in-flight residual from an older savepoint is dropped on this experimental portability path.BundleCheckpointHandlersTestdrives repeated self-checkpoints through the handler and asserts a single keyed-state descriptor is used regardless of the number of self-checkpoints; it fails on the previous per-residual behavior.Fixes #27648
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
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, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.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.