Skip to content

feat: add supervision strategy support for groupedWeightedWithin costFn#3180

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:feat/stream-groupedweightedwithin-supervision
Jun 27, 2026
Merged

feat: add supervision strategy support for groupedWeightedWithin costFn#3180
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:feat/stream-groupedweightedwithin-supervision

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 25, 2026

Copy link
Copy Markdown
Member

Motivation

Per the stream error handling docs, operators that apply user-provided functions should consult the configured SupervisionStrategy. The groupedWeightedWithin operator's costFn was called without a try-catch, so any exception failed the stream unconditionally — even under Supervision.Resume/Restart.

This is part of the meta-issue #3110 (add supervisor strategy support to stream operators that accept user functions). One operator per PR.

Modification

  • Wrap costFn(elem) inside GroupedWeightedWithin.nextElement() with a try/catch (NonFatal) that consults the SupervisionStrategy decider. The cost is computed before any state mutation, so the happy path is unchanged:
    • Stop → fail the stage (unchanged fail-fast behavior)
    • Resume → skip the offending element, keep the current group (weight/number accounting is preserved)
    • Restartdrop the in-progress group via a new resetGroupState() helper, and reschedule the time-window timer so the fresh group gets a full window
  • decider is a lazy val → zero overhead on the happy path.
  • Document supervision adherence on both groupedWeightedWithin overloads in the Scala/Java DSL scaladoc and the operator reference page.

Result

groupedWeightedWithin now adheres to the SupervisionStrategy attribute with well-defined Resume/Restart semantics.

Tests

  • sbt "stream-tests/Test/testOnly org.apache.pekko.stream.scaladsl.FlowGroupedWithinSpec"21/21 passed, including 5 new deterministic directional tests (Stop, default Stop, Resume keeps group, Resume preserves weight accounting, Restart drops group). They use a 30s window that never fires, so they are deterministic and run in PR validation (intentionally not tagged TimingTest).
  • sbt "stream/mimaReportBinaryIssues" — clean (binary compatible)

References

Refs #3110

This is a clean-room implementation written directly for Apache Pekko.

Motivation:
The groupedWeightedWithin operator's costFn is user-provided and may throw,
but it was called without a try-catch. Any exception failed the stream
unconditionally, ignoring the configured SupervisionStrategy. This was
inconsistent with operators such as map, filter and batch.

Modification:
Wrap costFn(elem) inside GroupedWeightedWithin.nextElement() with a try-catch
that consults the SupervisionStrategy decider. The cost is computed before any
state mutation so the happy path is unchanged. Stop fails the stage, Resume
skips the offending element while keeping the current group, and Restart drops
the in-progress group via a new resetGroupState() helper and reschedules the
time-window timer so the fresh group gets a full window. The decider is a lazy
val for zero overhead on the happy path. Update the Scala and Java DSL scaladoc
(both overloads) and the operator reference page to document supervision
adherence.

Result:
groupedWeightedWithin now adheres to the SupervisionStrategy attribute with
well-defined Resume and Restart semantics.

Tests:
- sbt "stream-tests/Test/testOnly org.apache.pekko.stream.scaladsl.FlowGroupedWithinSpec" -- 21/21 passed

References:
Refs apache#3110
@He-Pin He-Pin marked this pull request as ready for review June 26, 2026 21:17

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@pjfanning pjfanning added this to the 2.0.0-M4 milestone Jun 27, 2026
@He-Pin He-Pin merged commit 22084da into apache:main Jun 27, 2026
9 checks passed
@He-Pin He-Pin deleted the feat/stream-groupedweightedwithin-supervision branch June 27, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants