Skip to content

fix(workflow-operator): correct reservoir sampling replacement odds - #9572

Open
Ma77Ball wants to merge 1 commit into
apache:mainfrom
Ma77Ball:fix/reservoir-sampling-off-by-one
Open

Ma77Ball wants to merge 1 commit into
apache:mainfrom
Ma77Ball:fix/reservoir-sampling-off-by-one

Conversation

@Ma77Ball

Copy link
Copy Markdown
Contributor

What changes were proposed in this PR?

  • ReservoirSamplingOpExec draws the replacement index with nextInt(n+1) instead of nextInt(n), matching Algorithm R so each item is kept with probability count/(n+1) and the sample is uniform.
  • A zero-length reservoir (k coerced to 0) now keeps nothing instead of throwing on nextInt(0).

Any related issues, documentation, discussions?

Closes: #9569

How was this PR tested?

  • Run sbt "WorkflowOperator/testOnly *ReservoirSamplingOpExecSpec" under JDK 17; expect 14 passing, including a new case that reproduces Algorithm R with the executor's seed and asserts the reservoir matches, and the updated k=0 case that now expects an empty result.
  • Drag a Reservoir Sampling operator onto a workflow with k well below the input size, run it a few times, and confirm the sampled rows are not clustered at the end of the input.

Was this PR authored or co-authored using generative AI tooling?

Co-authored with Claude Opus 4.8 in compliance with ASF

ReservoirSamplingOpExec draws the replacement index with Random.nextInt(n+1) instead of nextInt(n), where n is the number of tuples seen before the current one. Algorithm R replaces a reservoir slot for the (n+1)-th item with probability count/(n+1); the old nextInt(n) made the first item after the reservoir fills always replace (probability 1.0), biasing the sample toward later rows. A zero-length reservoir (k coerced to 0) now draws nextInt(1) and keeps nothing instead of throwing on nextInt(0).

ReservoirSamplingOpExecSpec adds a test that reproduces Algorithm R with the executor's seed and asserts the sampled reservoir matches it, pinning the off-by-one, and updates the k=0 case to expect an empty result rather than an IllegalArgumentException.
@Yicong-Huang Yicong-Huang added the release/v1.3 back porting to release/v1.3 label Nov 20, 2026
@Yicong-Huang Yicong-Huang added the release/v1.2 back porting to release/v1.2 label Nov 20, 2026
@github-actions
github-actions Bot requested review from mengw15 and xuang7 November 20, 2026 21:46
@github-actions

Copy link
Copy Markdown
Contributor

Backport auto-label report

This fix: PR was checked against each actively-supported release branch. A release/* label nominates a backport target; the branch's release manager approving this PR is what sends the fix there. The required Backport Approvals check stays red until every label below is approved, so each manager either approves or removes their own label — which is why the labels left on a merged PR are exactly the branches it reached.

Release branch Analysis
✅ release/v1.3 Change detected on this branch — label added; this fix is queued to backport here. @mengw15 decides: approving sends the fix here, removing this label declines it. The merge waits on one or the other. Review requested.
✅ release/v1.2 Change detected on this branch — label added; this fix is queued to backport here. @xuang7 decides: approving sends the fix here, removing this label declines it. The merge waits on one or the other. Review requested.

Auto-label run.

@github-actions

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @aglinxinyuan
    You can notify them by mentioning @aglinxinyuan in a comment.

@codecov-commenter

codecov-commenter commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.08%. Comparing base (7f9bdf3) to head (ff4ba06).

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #9572      +/-   ##
============================================
- Coverage     91.57%   89.08%   -2.49%     
- Complexity     5158     7511    +2353     
============================================
  Files          1300     1417     +117     
  Lines         58092    69985   +11893     
  Branches       7615     9514    +1899     
============================================
+ Hits          53195    62344    +9149     
- Misses         2959     5426    +2467     
- Partials       1938     2215     +277     
Flag Coverage Δ *Carryforward flag
access-control-service 79.26% <ø> (+1.82%) ⬆️
agent-service 99.56% <ø> (ø) Carriedforward from 7f9bdf3
amber 82.90% <100.00%> (-2.53%) ⬇️
computing-unit-managing-service 59.97% <ø> (-3.91%) ⬇️
config-service 80.57% <ø> (-6.93%) ⬇️
file-service 80.47% <ø> (-1.69%) ⬇️
frontend 96.45% <ø> (ø) Carriedforward from 7f9bdf3
notebook-migration-service 85.84% <ø> (+2.06%) ⬆️
pyamber 98.38% <ø> (ø) Carriedforward from 7f9bdf3
result-service 85.71% <ø> (ø)
workflow-compiling-service 76.50% <ø> (-1.80%) ⬇️

*This pull request uses carry forward flags. 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:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Benchmark changes need a look

🟢 2 better · 🔴 3 worse · ⚪ 10 noise (<±5%) · 0 without baseline

Compared against main 7f9bdf3 benchmarked on this same runner, so the delta is largely free of cross-runner hardware noise. The "7d avg" column still reflects the gh-pages dashboard. Treat <±5% as noise unless repeated.

Dashboard · Run

config throughput MB/s latency max Δ latest / 7d
🔴 bs=10 sw=10 sl=64 610 0.372 15,754/24,988/24,988 us 🔴 +9.8% / 🔴 +80.3%
🟢 bs=100 sw=10 sl=64 1,280 0.781 78,244/90,230/90,230 us 🟢 -23.0% / 🟢 -15.6%
🔴 bs=1000 sw=10 sl=64 1,450 0.885 686,134/788,577/788,577 us 🔴 +10.3% / 🟢 -11.6%
Baseline details

Latest main 7f9bdf3 from same runner

config metric PR latest main 7d avg Δ latest Δ 7d
bs=10 sw=10 sl=64 throughput 610 tuples/sec 595 tuples/sec 1,019 tuples/sec +2.5% -40.2%
bs=10 sw=10 sl=64 MB/s 0.372 MB/s 0.363 MB/s 0.622 MB/s +2.5% -40.2%
bs=10 sw=10 sl=64 p50 15,754 us 14,351 us 10,136 us +9.8% +55.4%
bs=10 sw=10 sl=64 p95 24,988 us 24,571 us 13,860 us +1.7% +80.3%
bs=10 sw=10 sl=64 p99 24,988 us 24,571 us 16,484 us +1.7% +51.6%
bs=100 sw=10 sl=64 throughput 1,280 tuples/sec 1,262 tuples/sec 1,363 tuples/sec +1.4% -6.1%
bs=100 sw=10 sl=64 MB/s 0.781 MB/s 0.77 MB/s 0.832 MB/s +1.4% -6.1%
bs=100 sw=10 sl=64 p50 78,244 us 75,993 us 79,194 us +3.0% -1.2%
bs=100 sw=10 sl=64 p95 90,230 us 117,151 us 87,022 us -23.0% +3.7%
bs=100 sw=10 sl=64 p99 90,230 us 117,151 us 106,894 us -23.0% -15.6%
bs=1000 sw=10 sl=64 throughput 1,450 tuples/sec 1,476 tuples/sec 1,407 tuples/sec -1.8% +3.1%
bs=1000 sw=10 sl=64 MB/s 0.885 MB/s 0.901 MB/s 0.859 MB/s -1.8% +3.1%
bs=1000 sw=10 sl=64 p50 686,134 us 678,668 us 776,422 us +1.1% -11.6%
bs=1000 sw=10 sl=64 p95 788,577 us 715,254 us 814,091 us +10.3% -3.1%
bs=1000 sw=10 sl=64 p99 788,577 us 715,254 us 860,577 us +10.3% -8.4%
Raw CSV
config_idx,batch_size,schema_width,string_len,num_batches,total_ms,total_tuples,total_bytes,tuples_per_sec,mb_per_sec,lat_p50_us,lat_p95_us,lat_p99_us
0,10,10,64,20,327.96,200,128000,610,0.372,15754.30,24988.47,24988.47
1,100,10,64,20,1562.80,2000,1280000,1280,0.781,78243.88,90230.13,90230.13
2,1000,10,64,20,13793.45,20000,12800000,1450,0.885,686134.44,788576.76,788576.76

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common fix release/v1.2 back porting to release/v1.2 release/v1.3 back porting to release/v1.3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reservoir sampling is biased toward later rows

3 participants