Hi all,
I'd like to restart the discussion on
https://github.com/apache/beam/issues/24655 (originally raised by Jan in
the Dec 2022 thread "@RequiresStableInput and Pipeline fusion"). I've been
working on @RequiresStableInput in the Flink runner (#41448, #41514,
#41519) and would like to help move this one forward.
== The problem ==
A runner can only guarantee stable input at the boundary of an executable
stage. If GreedyPipelineFuser fuses NonDeterministicDoFn -> StableDoFn into
one stage, the runner makes the input to the *stage* stable, but on retry
the leading DoFn re-runs and the StableDoFn sees different input.
RequiresStableInputIT reproduces this with MapElements(PairWithRandomKeyFn)
followed by a stable DoFn. Today the only workaround is an explicit
Reshuffle (or other fusion break) right before the stable DoFn.
The counter-concern (Kenn, on the issue): always breaking fusion is
expensive. Dataflow saw major regressions when it did this, because many
SDK transforms put a cheap projection between a checkpoint and the stable
DoFn (e.g. GBK -> drop keys -> write files). For context, the original
Dataflow implementation (PR 5787 in the design doc,
s.apache.org/requires-stable-input) was exactly this: every stable ParDo T
was replaced by Reshuffle + T, so that's the behaviour the regression
numbers refer to.
== Options ==
1. Unconditional fusion break in the fuser
GreedyPipelineFuser (and the Python fn_api fuser) ends the stage before
any ParDo with requires_stable_input = true, unless its input is already a
stage boundary. This also covers the "ParDo immediately after a GBK" corner
case left open in the design doc's future work.
+ Simple, correct by default, no new user-facing API.
- Extra materialization for the GBK -> projection -> sink pattern. As
far as I understand, this only affects runners using the shared fuser
(portable Flink/Spark/Samza, etc.), not Dataflow.
2. Opt-in determinism marker (the direction from the issue)
Add a determinism property (e.g. @Deterministic in Java, plus a field
next to requires_stable_input in the proto so it works cross-language). A
PCollection is stable if it comes from a stage boundary/checkpoint, or from
a deterministic transform applied to stable input. The fuser keeps fusing
only through stable chains. The default is non-deterministic, i.e. break
fusion. (This isn't covered in the original design doc.)
+ Correct by default, and recovers the performance of option 1 wherever
transforms are marked.
- New API surface. Determinism belongs to the user fn, not the transform
(MapElements can't be blanket-marked). Mis-annotation silently brings the
bug back, and it is hard to test.
- Realistically most adoption comes from SDK-internal transforms
(Values, Keys, file-write internals), IO authors, and auto-insertion by
SQL/YAML, where the logic is easier to analyze.
3. Heuristics
Infer non-determinism from signals like @StartBundle/@FinishBundle,
state/timers, or side inputs.
- As Jan noted, these aren't sufficient. At best they work as a negative
signal ("never treat as deterministic"), not as a way to prove determinism.
4. Keep fusion as is: validate and document
Runners check the pipeline requirements and reject (or warn on) a stable
ParDo fused behind non-boundary transforms, as Luke suggested in the
original thread. Document Reshuffle as the required pattern. Also tie this
to the Redistribute work: either Redistribute is defined as an
unconditional fusion break, or we add an explicit "must break fusion" hint,
so deprecating Reshuffle doesn't remove the workaround.
+ Cheapest, no behaviour change.
- Leaves a silent correctness hole for anyone who doesn't read the docs.
Note: a fusion break is necessary but not sufficient. It only puts the
stable DoFn at the start of a stage; each runner still has to make that
stage's input stable (e.g. buffering until checkpoint on Flink). As Luke
suggested in the original thread, runners can check requires_stable_input
on the ExecutableStage to decide this.
== Strawman ==
- Phase 1: option 1 in the shared fusers (correctness first), possibly
behind an opt-out option for users who knowingly rely on the old behaviour.
Add a portable ValidatesRunner test based on RequiresStableInputIT without
the Reshuffle.
- Phase 2: option 2 with a proto-level determinism field. Mark the obvious
SDK-internal transforms first, then explore auto-marking for SQL/YAML.
- In parallel: settle the fusion-break semantics of Redistribute, or add an
explicit hint.
== Questions for the list ==
- Is a default fusion break in GreedyPipelineFuser acceptable for the
portable runners, given that the regression data we have comes from
Dataflow?
- Should determinism be a DoFn-level or a transform-level property, and
should it live in ParDoPayload or in a transform annotation?
- Which SDK transforms sit between a checkpoint and a stable DoFn today, so
they could be marked first? (Pointers welcome.)
- Any objection to tying this to the Redistribute/fusion-hint discussion?
I'm happy to write this up as a design doc once there's rough agreement on
the direction.
Thanks,
Alisha