Skip to content

[Dataflow Streaming] Add Operation::finishKey() and move processTimers into it#38430

Open
arunpandianp wants to merge 5 commits intoapache:masterfrom
arunpandianp:multikey
Open

[Dataflow Streaming] Add Operation::finishKey() and move processTimers into it#38430
arunpandianp wants to merge 5 commits intoapache:masterfrom
arunpandianp:multikey

Conversation

@arunpandianp
Copy link
Copy Markdown
Contributor

@arunpandianp arunpandianp commented May 10, 2026

Moving the processTimers call from finish() to finishKey().

In upcoming changes there'll be multiple streaming work items in a single beam bundle. With multiple work items, we've to process elements and timers of each work item before moving to the next work items.

finishKey() will be called by the NativeIterator classes after iterating through all elements from a work item.

Batch processes timers in BatchModeUngroupingParDoFn and does not rely on the processTimers() in ParDoOperation::finish(). So removing the processTimers() call from ParDoOperation::finish() is safe. Batch also does not use the new finishKey() method.

This change also modifies the streaming NativeIterators to throw WorkItemCancelledException instead of just stopping iteration.

@arunpandianp
Copy link
Copy Markdown
Contributor Author

PostCommit Java ValidatesRunner Dataflow

@arunpandianp
Copy link
Copy Markdown
Contributor Author

PostCommit Java ValidatesRunner Dataflow Streaming

@arunpandianp arunpandianp force-pushed the multikey branch 2 times, most recently from 7f9fe09 to fd261b7 Compare May 10, 2026 11:02
Moving the processTimers call from finish() to finishKey().

In upcoming changes there'll be multiple streaming work items
in a single beam bundle. With multiple work items, we've to process
elements and timers of each work item before moving to the next work
items.

finishKey() will be called by the NativeIterator classes after iterating
through all elements from a work item.

Batch processes timers in BatchModeUngroupingParDoFn and does not rely
on the processTimers() in ParDoOperation::finish(). So removing the
processTimers() call from ParDoOperation::finish() is safe. Batch also
does not use the new finishKey() method.
@arunpandianp arunpandianp marked this pull request as ready for review May 11, 2026 01: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 refactors the Dataflow streaming execution path to support multiple work items within a single bundle. By introducing finishKey(), the system can now explicitly process timers and perform cleanup for individual keys before moving to subsequent work items. This change ensures safer execution and improved error handling for streaming operations.

Highlights

  • API Changes: Introduced Operation::finishKey() to handle key-specific cleanup, moving processTimers from finish() to this new method.
  • Streaming Execution: Updated streaming NativeIterators to throw WorkItemCancelledException instead of silently stopping iteration upon failure.
  • Execution Flow: Integrated WorkExecutor into StreamingModeExecutionContext to facilitate proper key lifecycle management across multiple work items.
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.

@arunpandianp
Copy link
Copy Markdown
Contributor Author

R: @scwhittle @reuvenlax

@github-actions
Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

Copy link
Copy Markdown
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 a finishKey() lifecycle method to the Dataflow worker's operations and executors, enabling per-key finalization and timer processing. It refactors WindmillReaderIteratorBase and its subclasses to trigger this finalization upon iterator exhaustion. Review feedback recommends implementing an idempotency flag for finishKey(), ensuring consistent cancellation checks across all reader iterators, and improving exception handling during the finalization process.

Comment on lines +278 to +281
public void finishKey() throws Exception {
checkNotNull(workExecutor, "workExecutor must be set before calling finishKey()");
workExecutor.finishKey();
}
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.

medium

Use the finishKeyCalled flag to ensure that workExecutor.finishKey() is only executed once per work item.

  public void finishKey() throws Exception {
    if (finishKeyCalled) {
      return;
    }
    checkNotNull(workExecutor, "workExecutor must be set before calling finishKey()");
    finishKeyCalled = true;
    workExecutor.finishKey();
  }

Comment on lines 182 to 183
@Override
public boolean advance() throws IOException {
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.

medium

The anonymous iterator in WindowingWindmillReader should also check context.workIsFailed() and throw WorkItemCancelledException, consistent with the changes in WindmillReaderIteratorBase. This ensures that cancelled work items are handled promptly.

        @Override
        public boolean advance() throws IOException {
          if (context.workIsFailed()) {
            throw new WorkItemCancelledException(context.getWorkItem().getShardingKey());
          }

Comment on lines +68 to +70
} catch (Exception e) {
throw new RuntimeException(e);
}
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.

medium

Instead of wrapping all exceptions in RuntimeException, consider catching IOException and RuntimeException separately to rethrow them directly, and wrapping only other checked exceptions in IOException. This maintains consistency with the advance() method's signature and allows callers to handle specific exception types.

        } catch (IOException | RuntimeException e) {
          throw e;
        } catch (Exception e) {
          throw new IOException(e);
        }

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.

1 participant