Repository navigation
[Java] Use fair SynchronousQueue and deflake UnboundedScheduledExecutorServiceTest - #41919
Open
kennknowles wants to merge 1 commit into
Open
kennknowles wants to merge 1 commit into
kennknowles wants to merge 1 commit into
Conversation
…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.
Contributor
|
Assigning reviewers: R: @tvalentyn added as fallback since no labels match configuration Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UnboundedScheduledExecutorService's backingSynchronousQueueto fair (FIFO) mode (new SynchronousQueue<Runnable>(true)) so returning workers match the oldest waitingoffer()first instead of starving older callers past the 10 ms timeout under LIFO contention.@VisibleForTestingconstructor(NanoClock, long offerTimeout, TimeUnit offerTimeoutUnit)while preserving the 10 ms production default (DEFAULT_OFFER_TIMEOUT_MILLIS = 10).UnboundedScheduledExecutorServiceTest#testThreadsAreAddedOnlyAsNeededWithContention:FutureTaskcompletion (unblocking.get()) and the worker looping back toSynchronousQueue.poll()do not spuriously spawn extra threads.CountDownLatchand assert the expected pool size (21, includingTaskLauncher).21) with a failure message reporting the actuallargestPoolvalue, and clean up latches/executors infinally.Background
testThreadsAreAddedOnlyAsNeededWithContentionfailed again on #41655 (run 45501936142), loggingCreated 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;testThreadsAreAddedOnlyAsNeededWithContentionran 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