Skip to content

[Spark 4] Clear the watermark of keys without timers or state in the streaming stateful ParDo - #40550

Merged
Abacn merged 2 commits into
apache:masterfrom
schizophrenicmaniac:fix/40517-task-clear-per-key-watermark-and-timer
Oct 20, 2026
Merged

Abacn merged 2 commits into
apache:masterfrom
schizophrenicmaniac:fix/40517-task-clear-per-key-watermark-and-timer

Conversation

@schizophrenicmaniac

@schizophrenicmaniac schizophrenicmaniac commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

BeamStatefulProcessor wrote beamWatermark on every visit and never removed it, so each key that ever went through a streaming stateful ParDo kept a row in the state store even after all its windows expired. beamTimers was already cleared by persistTimers once no timers remain. The watermark was the only thing left behind.

storeWatermark, called from handleInputRows and handleExpiredTimer, now clears beamWatermark when the key has no timers and beamState is empty, for example after the GC timer of its last window fired. The change stays inside those two code paths, so it should rebase cleanly onto #40516 and the GroupByKey base-class split.

On late data: after the cleanup, the key's next visit is a first visit and filters late data against TIMESTAMP_MIN_VALUE, so a late element for an already expired window of that key is processed instead of dropped. I went with accepting this as part of #40446. A brand-new key already behaves this way. Keeping any marker per key means keeping the same Long the issue wants gone. A TTLConfig is processing-time based, so it doesn't line up with allowed lateness. I updated the contract comment in timerInternals to cover cleared keys.

The new test, testExpiredKeyLeavesNoState, sends a single element for one key and lets its only window expire as the source watermark advances. It then reads the checkpoint through the statestore data source and asserts that beamState, beamTimers and beamWatermark have no rows left. Without the change one beamWatermark row remains.

Fixes #40517

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.03%. Comparing base (76f6e6d) to head (dab7e06).
⚠️ Report is 143 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #40550      +/-   ##
============================================
+ Coverage     56.16%   65.03%   +8.86%     
- Complexity     2288    13947   +11659     
============================================
  Files          1124     1728     +604     
  Lines        178336   108384   -69952     
  Branches       1489    11286    +9797     
============================================
- Hits         100170    70492   -29678     
+ Misses        75645    33692   -41953     
- Partials       2521     4200    +1679     
Flag Coverage Δ
go ?
java 65.03% <ø> (-10.75%) ⬇️
python ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @damccorm added as fallback since no labels match configuration

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@damccorm

Copy link
Copy Markdown
Contributor

R: @tkaymak

would you mind taking a first look at this one?

@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

@tkaymak tkaymak left a comment •

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.

Thanks @schizophrenicmaniac, the diagnosis is right, and the condition and ordering in storeWatermark hold against persistTimers, reconcileWakeupTimer and Spark deleting the fired timer itself.
The test should prove the cleanup without pinning the late data behavior, see inline.
#40516 adds a test at the same spot in StatefulParDoStreamingTest, please rebase once it merges.
LGTM from my side once the late element and the skew override are gone and the comment states the contract, @Abacn for the merge.

Set<String> collected = StreamingTestUtils.collected(collectorId);
assertTrue(collected.contains("k1-0"));
// The late element finds the key cleared and is filtered like a first visit, see #40446.
assertTrue(collected.contains("k1-" + LATE_INDEX));

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.

Please drop the late element, LATE_INDEX, the else branch in ExpiryKeySelectorFn and this assertion. It pins the first visit behavior that #40446 wants to tighten, and the zero row assertions already prove the fix, without the change beamWatermark still has one row here. The window expires anyway, the source watermark is declared on the source Dataset and advances on the filtered elements too.

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.

Done, the late element, LATE_INDEX, the else branch and the assertion are gone. The test now only checks the element got through and that the three state variables have no rows.

}

@Override
public Duration getAllowedTimestampSkew() {

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.

getAllowedTimestampSkew is deprecated, with the late element gone this override goes as well.

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.

Removed.

private void storeWatermark(SparkTimerInternals timerInternals) {
checkStateNotNull(beamWatermark).update(timerInternals.currentInputWatermarkTime().getMillis());
ValueState<Long> watermarkState = checkStateNotNull(beamWatermark);
// A key without timers and state, e.g. after all its windows expired, keeps nothing in the

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.

Contract only please, for example: A key without timers and state keeps no row, its next visit is a first visit. The reasoning belongs in #40517, not in the code.

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.

Done, I used your wording.

}
}

private static final class ExpiryTestFn extends DoFn<KV<String, String>, String> {

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.

nit: ExpiryTestFn is DownstreamFn with another output string. Reuse DownstreamFn and assert on TestUnboundedSource.element(TAG_EXPIRY, 0, 0).

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.

Done, switched to DownstreamFn and the assertion is on TestUnboundedSource.element(TAG_EXPIRY, 0, 0) now.

@tkaymak tkaymak left a comment

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.

Thanks, all four points are addressed and the test now proves only the cleanup. LGTM. #40516 adds a test at the same spot in StatefulParDoStreamingTest, whichever lands second rebases, the conflict is only in the test file. @Abacn for the merge.

@tkaymak

tkaymak commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

#40516 is merged, so this needs a rebase on master. The conflict should only be the new test and the forget line in StatefulParDoStreamingTest. After that it is ready from my side, @Abacn for the merge.

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.

[Task]: Clear per key watermark and timer state of the Spark 4 streaming stateful processor

4 participants