Skip to content

[Java] Use fair SynchronousQueue and deflake UnboundedScheduledExecutorServiceTest - #41919

Open
kennknowles wants to merge 1 commit into
apache:masterfrom
kennknowles:unbounded-executor-flake
Open

kennknowles wants to merge 1 commit into
apache:masterfrom
kennknowles:unbounded-executor-flake

Conversation

@kennknowles

Copy link
Copy Markdown
Member

Summary

  • Switch UnboundedScheduledExecutorService's backing SynchronousQueue to fair (FIFO) mode (new SynchronousQueue<Runnable>(true)) so returning workers match the oldest waiting offer() first instead of starving older callers past the 10 ms timeout under LIFO contention.
  • Add a package-private @VisibleForTesting constructor (NanoClock, long offerTimeout, TimeUnit offerTimeoutUnit) while preserving the 10 ms production default (DEFAULT_OFFER_TIMEOUT_MILLIS = 10).
  • Deflake UnboundedScheduledExecutorServiceTest#testThreadsAreAddedOnlyAsNeededWithContention:
    • Pass a 1 s offer timeout so normal CI scheduling stalls or GC pauses between FutureTask completion (unblocking .get()) and the worker looping back to SynchronousQueue.poll() do not spuriously spawn extra threads.
    • Warm up 20 worker threads concurrently on a CountDownLatch and assert the expected pool size (21, including TaskLauncher).
    • Run 200 sequential no-op submissions per caller thread (reducing thread oversubscription while keeping handoff contention high).
    • Assert the exact expected pool size (21) with a failure message reporting the actual largestPool value, and clean up latches/executors in finally.

Background

testThreadsAreAddedOnlyAsNeededWithContention failed again on #41655 (run 45501936142), logging Created 157 threads to execute at most 100 parallel tasks (2.366 s elapsed for 200 × 5 ms sleeps). Previously tracked in #31590 (and see #41799 for a prior test-only PR).

Verification

Ran locally with Java 21:

  • ./gradlew :sdks:java:core:test --tests org.apache.beam.sdk.util.UnboundedScheduledExecutorServiceTest (12/12 passed; testThreadsAreAddedOnlyAsNeededWithContention ran in 2.093 s and created 21 threads)
  • ./gradlew :sdks:java:core:spotlessCheck :sdks:java:core:spotbugsMain :sdks:java:core:checkstyleMain :sdks:java:core:checkstyleTest

…ceTest

UnboundedScheduledExecutorService backs its ThreadPoolExecutor with a
SynchronousQueue whose offer(Runnable) waits 10 ms before spawning a new
worker thread. Using the default non-fair (LIFO) SynchronousQueue allows
older waiting offer() callers to starve past the timeout under contention
even while workers are actively returning.

Switch SynchronousQueue to fair (FIFO) mode so returning workers match
the oldest waiting offer() first. Also add a package-private
@VisibleForTesting constructor parameterizing the offer timeout (keeping
the 10 ms production default), and update
testThreadsAreAddedOnlyAsNeededWithContention to:

- Use a 1 s offer timeout so normal CI scheduling delays or GC pauses
  between FutureTask completion and SynchronousQueue.poll() do not
  spuriously create extra threads.
- Warm up 20 worker threads on a latch before running 200 sequential
  no-op submissions per caller thread, reducing runner oversubscription
  while maximizing handoff contention.
- Assert the exact expected pool size (20 workers + 1 TaskLauncher) with
  a failure message reporting the actual largestPool value.
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn added as fallback since no labels match configuration
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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).

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.23%. Comparing base (9f07630) to head (def695d).

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41919   +/-   ##
=========================================
  Coverage     58.23%   58.23%           
  Complexity     2846     2846           
=========================================
  Files          1211     1211           
  Lines        173884   173884           
  Branches       2330     2330           
=========================================
  Hits         101259   101259           
  Misses        69911    69911           
  Partials       2714     2714           

☔ 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.

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.

1 participant