-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jetty 9.4.x 4105 4121 4122 queued thread pool #4146
Merged
gregw
merged 6 commits into
jetty-9.4.x
from
jetty-9.4.x-4105-4121-4122-QueuedThreadPool
Oct 2, 2019
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3ad6780
Issue #4105 QueuedThreadPool
gregw c37a4ff
Issue #4122 QueuedThreadPool
gregw 7b306d7
Issue #4121 QueuedThreadPool
gregw ad3b7fd
Simplified test to make less racy
gregw c66ea84
cleanups
gregw fba80e0
cleanups from review
gregw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,13 @@ | |
import org.eclipse.jetty.util.log.Logger; | ||
import org.eclipse.jetty.util.log.StacklessLogging; | ||
import org.eclipse.jetty.util.thread.ThreadPool.SizedThreadPool; | ||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.lessThan; | ||
import static org.hamcrest.core.StringContains.containsString; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
@@ -188,7 +187,7 @@ public void testThreadPool() throws Exception | |
QueuedThreadPool tp = new QueuedThreadPool(); | ||
tp.setMinThreads(2); | ||
tp.setMaxThreads(4); | ||
tp.setIdleTimeout(900); | ||
tp.setIdleTimeout(1000); | ||
tp.setThreadsPriority(Thread.NORM_PRIORITY - 1); | ||
|
||
tp.start(); | ||
|
@@ -199,44 +198,49 @@ public void testThreadPool() throws Exception | |
|
||
// Doesn't shrink to less than min threads | ||
Thread.sleep(3 * tp.getIdleTimeout() / 2); | ||
waitForThreads(tp, 2); | ||
waitForIdle(tp, 2); | ||
assertThat(tp.getThreads(), is(2)); | ||
assertThat(tp.getIdleThreads(), is(2)); | ||
|
||
// Run job0 | ||
RunningJob job0 = new RunningJob("JOB0"); | ||
tp.execute(job0); | ||
assertTrue(job0._run.await(10, TimeUnit.SECONDS)); | ||
waitForIdle(tp, 1); | ||
assertThat(tp.getThreads(), is(2)); | ||
assertThat(tp.getIdleThreads(), is(1)); | ||
|
||
// Run job1 | ||
RunningJob job1 = new RunningJob("JOB1"); | ||
tp.execute(job1); | ||
assertTrue(job1._run.await(10, TimeUnit.SECONDS)); | ||
waitForThreads(tp, 2); | ||
waitForIdle(tp, 0); | ||
assertThat(tp.getThreads(), is(2)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
|
||
// Run job2 | ||
RunningJob job2 = new RunningJob("JOB2"); | ||
tp.execute(job2); | ||
assertTrue(job2._run.await(10, TimeUnit.SECONDS)); | ||
waitForThreads(tp, 3); | ||
waitForIdle(tp, 0); | ||
assertThat(tp.getThreads(), is(3)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
|
||
// Run job3 | ||
RunningJob job3 = new RunningJob("JOB3"); | ||
tp.execute(job3); | ||
assertTrue(job3._run.await(10, TimeUnit.SECONDS)); | ||
waitForThreads(tp, 4); | ||
waitForIdle(tp, 0); | ||
assertThat(tp.getThreads(), is(4)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
|
||
// Check no short term change | ||
Thread.sleep(100); | ||
assertThat(tp.getThreads(), is(4)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
|
||
// Run job4. will be queued | ||
RunningJob job4 = new RunningJob("JOB4"); | ||
tp.execute(job4); | ||
assertFalse(job4._run.await(1, TimeUnit.SECONDS)); | ||
assertFalse(job4._run.await(250, TimeUnit.MILLISECONDS)); | ||
assertThat(tp.getThreads(), is(4)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
assertThat(tp.getQueueSize(), is(1)); | ||
|
||
// finish job 0 | ||
job0._stopping.countDown(); | ||
|
@@ -246,12 +250,13 @@ public void testThreadPool() throws Exception | |
assertTrue(job4._run.await(10, TimeUnit.SECONDS)); | ||
assertThat(tp.getThreads(), is(4)); | ||
assertThat(tp.getIdleThreads(), is(0)); | ||
assertThat(tp.getQueueSize(), is(0)); | ||
|
||
// finish job 1 | ||
// finish job 1, and it's thread will become idle | ||
job1._stopping.countDown(); | ||
assertTrue(job1._stopped.await(10, TimeUnit.SECONDS)); | ||
waitForIdle(tp, 1); | ||
assertThat(tp.getThreads(), is(4)); | ||
waitForThreads(tp, 4); | ||
|
||
// finish job 2,3,4 | ||
job2._stopping.countDown(); | ||
|
@@ -261,15 +266,9 @@ public void testThreadPool() throws Exception | |
assertTrue(job3._stopped.await(10, TimeUnit.SECONDS)); | ||
assertTrue(job4._stopped.await(10, TimeUnit.SECONDS)); | ||
|
||
waitForIdle(tp, 4); | ||
assertThat(tp.getThreads(), is(4)); | ||
|
||
long duration = System.nanoTime(); | ||
waitForThreads(tp, 3); | ||
assertThat(tp.getIdleThreads(), is(3)); | ||
duration = System.nanoTime() - duration; | ||
assertThat(TimeUnit.NANOSECONDS.toMillis(duration), Matchers.greaterThan(tp.getIdleTimeout() / 2L)); | ||
assertThat(TimeUnit.NANOSECONDS.toMillis(duration), Matchers.lessThan(tp.getIdleTimeout() * 2L)); | ||
// Eventually all will have 3 idle threads | ||
waitForIdle(tp, 3); | ||
assertThat(tp.getThreads(), is(3)); | ||
|
||
tp.stop(); | ||
} | ||
|
@@ -505,6 +504,57 @@ public void testShrink() throws Exception | |
tp.stop(); | ||
} | ||
|
||
@Test | ||
public void testSteadyShrink() throws Exception | ||
{ | ||
CountDownLatch latch = new CountDownLatch(1); | ||
Runnable job = () -> | ||
{ | ||
try | ||
{ | ||
latch.await(); | ||
} | ||
catch(InterruptedException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
}; | ||
|
||
QueuedThreadPool tp = new QueuedThreadPool(); | ||
tp.setMinThreads(2); | ||
tp.setMaxThreads(10); | ||
tp.setIdleTimeout(500); | ||
tp.setThreadsPriority(Thread.NORM_PRIORITY - 1); | ||
|
||
tp.start(); | ||
waitForIdle(tp, 2); | ||
waitForThreads(tp, 2); | ||
|
||
for (int i = 0; i < 10; i++) | ||
tp.execute(job); | ||
|
||
waitForThreads(tp, 10); | ||
int threads = tp.getThreads(); | ||
// let the jobs run | ||
latch.countDown(); | ||
|
||
for (int i = 5; i-- > 0; ) | ||
{ | ||
Thread.sleep(250); | ||
tp.execute(job); | ||
} | ||
|
||
// Assert that steady rate of jobs doesn't prevent some idling out | ||
assertThat(tp.getThreads(), lessThan(threads)); | ||
threads = tp.getThreads(); | ||
for (int i = 5; i-- > 0; ) | ||
{ | ||
Thread.sleep(250); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, use |
||
tp.execute(job); | ||
} | ||
assertThat(tp.getThreads(), lessThan(threads)); | ||
} | ||
|
||
@Test | ||
public void testEnsureThreads() throws Exception | ||
{ | ||
|
@@ -605,7 +655,7 @@ private void waitForIdle(QueuedThreadPool tp, int idle) | |
} | ||
now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); | ||
} | ||
assertEquals(idle, tp.getIdleThreads()); | ||
assertThat(tp.getIdleThreads(), is(idle)); | ||
} | ||
|
||
private void waitForReserved(QueuedThreadPool tp, int reserved) | ||
|
@@ -624,7 +674,7 @@ private void waitForReserved(QueuedThreadPool tp, int reserved) | |
} | ||
now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); | ||
} | ||
assertEquals(reserved, reservedThreadExecutor.getAvailable()); | ||
assertThat(reservedThreadExecutor.getAvailable(), is(reserved)); | ||
} | ||
|
||
private void waitForThreads(QueuedThreadPool tp, int threads) | ||
|
@@ -642,7 +692,7 @@ private void waitForThreads(QueuedThreadPool tp, int threads) | |
} | ||
now = TimeUnit.NANOSECONDS.toMillis(System.nanoTime()); | ||
} | ||
assertEquals(threads, tp.getThreads()); | ||
assertThat(tp.getThreads(), is(threads)); | ||
} | ||
|
||
@Test | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
timeout / 2
to clearly link the fact that you are sleeping a time that is long enough to idle some thread, but not all of them.