Skip to content

[#40949] Spark 4 streaming: stateful ParDo with additional outputs - #41426

Open
tkaymak wants to merge 2 commits into
apache:masterfrom
tkaymak:spark4-streaming-stateful-tags
Open

tkaymak wants to merge 2 commits into
apache:masterfrom
tkaymak:spark4-streaming-stateful-tags

Conversation

@tkaymak

@tkaymak tkaymak commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #40949, part of #36841.

The streaming translator now accepts a stateful ParDo with additional outputs. It mirrors StatefulParDoTranslatorBatch: with more than one consumed output the transformWithState processor emits wide rows with one column per tag, encoded with oneOfEncoder, and each output filters its column. The batch helpers skipUnconsumedOutputs, tagsColumnIndex and createEncoders are reused, so they and ParDoTranslatorBatch are now @Internal public. ParDoStatefulProcessor gets singleOutput and multiOutput factories, and BeamStatefulProcessor is generic on the row type with an abstract output receiver, like StatefulDoFnGroupFunction. The single output path and GroupByKeyStatefulProcessor behave as before.

Like the stateless case in #41220, streaming datasets are not persisted, so each consumed tag runs its own transformWithState with its own state. This is not new with tags, one output with two consumers already does the same on master. The copies can see a key's elements in a different order or in different micro batches, and fire timers at different points, so the tags agree only for a DoFn whose output per tag does not depend on that. A local probe with a dedup DoFn (first element per key to main, the rest to a duplicates tag) gave consistent tags in 6 of 6 runs, local mode reads deterministically. An exchange at the fan out does not help, the copies get different operator ids, so this is tracked in #41425 and is not fixed by #41198.

When only additional outputs are consumed, the unconsumed main output is dropped, so it does not start a streaming query and state store of its own. testStatefulParDoOnlyAdditionalOutputConsumed covers it.

ParDoTest$StateTests.testValueStateTaggedOutput failed with the rejection before and passes now, its exclusion is removed. The new StatefulParDoStreamingTest case flattens a main output from @ProcessElement and an additional output from @OnTimer into one query and declares a third tag that is never consumed. Locally the streaming suite runs 213 tests with 0 failures, the batch Structured Streaming suites on Spark 4 and 3 and the full :runners:spark:4:test and :runners:spark:3:test pass. The trigger files run both ValidatesRunner PostCommits here.

Merge order: it bumps the same Spark4 trigger line as #41256, whichever lands second rebases.

@tkaymak

tkaymak commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

R: @Abacn

@github-actions github-actions Bot added runners spark size/m M: 50-249 lines changed labels Nov 17, 2026
@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

TupleTag<OutputT> mainOutputTag = transform.getMainOutputTag();
PCollection<OutputT> output = cxt.getOutput(mainOutputTag);
Coder<OutputT> outputCoder = output.getCoder();
Map<TupleTag<?>, PCollection<?>> outputs =

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.

(from gemini) Unconsumed mainTag with a consumed additionalTag spawns a redundant streaming query:

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.

Thanks, fixed for the stateful path. StatefulParDoStreamingTranslator now drops the main output when it is unconsumed and another output is consumed, so no extra query or state store starts. testStatefulParDoOnlyAdditionalOutputConsumed covers it, the DoFn counter was 8 before and is 4 now. The stateless path in ParDoTranslatorBatch is unchanged, it is shared with batch and builds its single output path on the main tag, and there the extra query only reruns a stateless DoFn, the same cost as #41198.

@tkaymak
tkaymak force-pushed the spark4-streaming-stateful-tags branch from 82457e3 to 28fa603 Compare November 24, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

runners size/m M: 50-249 lines changed spark

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Task]: Spark 4 streaming runner, support ParDo with additional outputs

2 participants