Fix cached thread pool cannot recycle idle core threads (#8342) - #16450
Open
ABin-Huang wants to merge 2 commits into
Open
Fix cached thread pool cannot recycle idle core threads (#8342)#16450ABin-Huang wants to merge 2 commits into
ABin-Huang wants to merge 2 commits into
Conversation
Allow core threads of the cached thread pool to time out via ThreadPoolExecutor.allowCoreThreadTimeOut(true), so that idle threads (including core ones configured via corethreads) can be fully recycled after the keep-alive time, matching CachedThreadPool's self-tuned contract. Default corethreads=0 behavior is unchanged, and fixed/ limited/eager pools are not touched.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16450 +/- ##
============================================
- Coverage 60.91% 60.91% -0.01%
- Complexity 15 11762 +11747
============================================
Files 1953 1953
Lines 89271 89273 +2
Branches 13473 13473
============================================
- Hits 54383 54381 -2
- Misses 29309 29315 +6
+ Partials 5579 5577 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ 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.
What is the purpose of the change?
Fix #8342.
When
threadpool=cachedis configured with an explicit positivecorethreads(e.g.corethreads=2, threads=5, alive=15000), idle core threads are never reclaimed, so the pool can never shrink belowcorePoolSize, even when every thread stays idle far longer than the configured keep-alive (alive). This contradicts the contract documented onCachedThreadPool— "Thread will be recycled after idle for one minute" — and the behavior ofjava.util.concurrent.Executors#newCachedThreadPool()that it is modeled on.Root cause
CachedThreadPool#getExecutorbuilds a JDKThreadPoolExecutorbut never callsallowCoreThreadTimeOut(true). By default aThreadPoolExecutordoes not time out core threads while they wait on the queue, so withcorethreads > 0those core threads stay alive forever.Fix
Call
executor.allowCoreThreadTimeOut(true)for the cached pool so idle core threads are also reclaimed once they have been idle foralive.corethreads = 0, so the default behavior is unchanged.fixed/limited/eagerpools are intentionally left untouched and keep their current (non-shrinking) semantics.Tests
CachedThreadPoolTest#idleCoreThreadsShouldBeRecycledAfterKeepAlive: grow the pool tomaximumPoolSize, release all tasks and assert the pool shrinks back to 0 after keep-alive. The test fails (times out) before this fix and passes after.allowsCoreThreadTimeOut()assertion togetExecutor1.org.apache.dubbo.common.threadpooltest package passes locally (41 tests, 0 failures/errors); spotless formatting applied.Checklist