|
@@ -25,272 +25,353 @@ import org.elasticsearch.test.ESTestCase;
|
|
|
import org.elasticsearch.threadpool.ThreadPool.Names;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.Executor;
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static org.elasticsearch.common.settings.Settings.settingsBuilder;
|
|
|
-import static org.hamcrest.Matchers.equalTo;
|
|
|
-import static org.hamcrest.Matchers.instanceOf;
|
|
|
-import static org.hamcrest.Matchers.is;
|
|
|
-import static org.hamcrest.Matchers.not;
|
|
|
-import static org.hamcrest.Matchers.nullValue;
|
|
|
-import static org.hamcrest.Matchers.sameInstance;
|
|
|
+import static org.hamcrest.Matchers.*;
|
|
|
|
|
|
/**
|
|
|
*/
|
|
|
public class UpdateThreadPoolSettingsTests extends ESTestCase {
|
|
|
- private ThreadPool.Info info(ThreadPool threadPool, String name) {
|
|
|
- for (ThreadPool.Info info : threadPool.info()) {
|
|
|
- if (info.getName().equals(name)) {
|
|
|
- return info;
|
|
|
- }
|
|
|
+ public void testCorrectThreadPoolTypePermittedInSettings() throws InterruptedException {
|
|
|
+ String threadPoolName = randomThreadPoolName();
|
|
|
+ ThreadPool.ThreadPoolType correctThreadPoolType = ThreadPool.THREAD_POOL_TYPES.get(threadPoolName);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(settingsBuilder()
|
|
|
+ .put("name", "testCorrectThreadPoolTypePermittedInSettings")
|
|
|
+ .put("threadpool." + threadPoolName + ".type", correctThreadPoolType.getType())
|
|
|
+ .build());
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), correctThreadPoolType);
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testThreadPoolCanNotOverrideThreadPoolType() throws InterruptedException {
|
|
|
+ String threadPoolName = randomThreadPoolName();
|
|
|
+ ThreadPool.ThreadPoolType incorrectThreadPoolType = randomIncorrectThreadPoolType(threadPoolName);
|
|
|
+ ThreadPool.ThreadPoolType correctThreadPoolType = ThreadPool.THREAD_POOL_TYPES.get(threadPoolName);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(
|
|
|
+ settingsBuilder()
|
|
|
+ .put("name", "testThreadPoolCanNotOverrideThreadPoolType")
|
|
|
+ .put("threadpool." + threadPoolName + ".type", incorrectThreadPoolType.getType())
|
|
|
+ .build());
|
|
|
+ terminate(threadPool);
|
|
|
+ fail("expected IllegalArgumentException");
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ assertThat(
|
|
|
+ e.getMessage(),
|
|
|
+ is("setting threadpool." + threadPoolName + ".type to " + incorrectThreadPoolType.getType() + " is not permitted; must be " + correctThreadPoolType.getType()));
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testUpdateSettingsCanNotChangeThreadPoolType() throws InterruptedException {
|
|
|
+ String threadPoolName = randomThreadPoolName();
|
|
|
+ ThreadPool.ThreadPoolType invalidThreadPoolType = randomIncorrectThreadPoolType(threadPoolName);
|
|
|
+ ThreadPool.ThreadPoolType validThreadPoolType = ThreadPool.THREAD_POOL_TYPES.get(threadPoolName);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(settingsBuilder().put("name", "testUpdateSettingsCanNotChangeThreadPoolType").build());
|
|
|
+
|
|
|
+
|
|
|
+ threadPool.updateSettings(
|
|
|
+ settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".type", invalidThreadPoolType.getType())
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ fail("expected IllegalArgumentException");
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ assertThat(
|
|
|
+ e.getMessage(),
|
|
|
+ is("setting threadpool." + threadPoolName + ".type to " + invalidThreadPoolType.getType() + " is not permitted; must be " + validThreadPoolType.getType()));
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
public void testCachedExecutorType() throws InterruptedException {
|
|
|
- ThreadPool threadPool = new ThreadPool(
|
|
|
- Settings.settingsBuilder()
|
|
|
- .put("threadpool.search.type", "cached")
|
|
|
- .put("name","testCachedExecutorType").build());
|
|
|
-
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("cached"));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(5L));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
-
|
|
|
- // Replace with different type
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.type", "same").build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("same"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), is(ThreadPool.DIRECT_EXECUTOR));
|
|
|
-
|
|
|
- // Replace with different type again
|
|
|
- threadPool.updateSettings(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "scaling")
|
|
|
- .put("threadpool.search.keep_alive", "10m")
|
|
|
- .build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("scaling"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getCorePoolSize(), equalTo(1));
|
|
|
- // Make sure keep alive value changed
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(10L));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(10L));
|
|
|
-
|
|
|
- // Put old type back
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.type", "cached").build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("cached"));
|
|
|
- // Make sure keep alive value reused
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(10L));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
-
|
|
|
- // Change keep alive
|
|
|
- Executor oldExecutor = threadPool.executor(Names.SEARCH);
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.keep_alive", "1m").build());
|
|
|
- // Make sure keep alive value changed
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(1L));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(1L));
|
|
|
- // Make sure executor didn't change
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("cached"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), sameInstance(oldExecutor));
|
|
|
-
|
|
|
- // Set the same keep alive
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.keep_alive", "1m").build());
|
|
|
- // Make sure keep alive value didn't change
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(1L));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(1L));
|
|
|
- // Make sure executor didn't change
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("cached"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), sameInstance(oldExecutor));
|
|
|
- terminate(threadPool);
|
|
|
+ String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.CACHED);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(
|
|
|
+ Settings.settingsBuilder()
|
|
|
+ .put("name", "testCachedExecutorType").build());
|
|
|
+
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.CACHED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+
|
|
|
+ threadPool.updateSettings(settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".keep_alive", "10m")
|
|
|
+ .build());
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.CACHED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(0));
|
|
|
+ // Make sure keep alive value changed
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(10L));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(10L));
|
|
|
+
|
|
|
+ // Make sure keep alive value reused
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(10L));
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+
|
|
|
+ // Change keep alive
|
|
|
+ Executor oldExecutor = threadPool.executor(threadPoolName);
|
|
|
+ threadPool.updateSettings(settingsBuilder().put("threadpool." + threadPoolName + ".keep_alive", "1m").build());
|
|
|
+ // Make sure keep alive value changed
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(1L));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(1L));
|
|
|
+ // Make sure executor didn't change
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.CACHED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), sameInstance(oldExecutor));
|
|
|
+
|
|
|
+ // Set the same keep alive
|
|
|
+ threadPool.updateSettings(settingsBuilder().put("threadpool." + threadPoolName + ".keep_alive", "1m").build());
|
|
|
+ // Make sure keep alive value didn't change
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(1L));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(1L));
|
|
|
+ // Make sure executor didn't change
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.CACHED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), sameInstance(oldExecutor));
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void testFixedExecutorType() throws InterruptedException {
|
|
|
- ThreadPool threadPool = new ThreadPool(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "fixed")
|
|
|
- .put("name","testCachedExecutorType").build());
|
|
|
-
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
-
|
|
|
- // Replace with different type
|
|
|
- threadPool.updateSettings(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "scaling")
|
|
|
- .put("threadpool.search.keep_alive", "10m")
|
|
|
- .put("threadpool.search.min", "2")
|
|
|
- .put("threadpool.search.size", "15")
|
|
|
- .build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("scaling"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getCorePoolSize(), equalTo(2));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getMaximumPoolSize(), equalTo(15));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMin(), equalTo(2));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMax(), equalTo(15));
|
|
|
- // Make sure keep alive value changed
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(10L));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(10L));
|
|
|
-
|
|
|
- // Put old type back
|
|
|
- threadPool.updateSettings(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "fixed")
|
|
|
- .build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("fixed"));
|
|
|
- // Make sure keep alive value is not used
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive(), nullValue());
|
|
|
- // Make sure keep pool size value were reused
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMin(), equalTo(15));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMax(), equalTo(15));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getCorePoolSize(), equalTo(15));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getMaximumPoolSize(), equalTo(15));
|
|
|
-
|
|
|
- // Change size
|
|
|
- Executor oldExecutor = threadPool.executor(Names.SEARCH);
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.size", "10").build());
|
|
|
- // Make sure size values changed
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMax(), equalTo(10));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMin(), equalTo(10));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getMaximumPoolSize(), equalTo(10));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getCorePoolSize(), equalTo(10));
|
|
|
- // Make sure executor didn't change
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("fixed"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), sameInstance(oldExecutor));
|
|
|
-
|
|
|
- // Change queue capacity
|
|
|
- threadPool.updateSettings(settingsBuilder()
|
|
|
- .put("threadpool.search.queue", "500")
|
|
|
- .build());
|
|
|
-
|
|
|
- terminate(threadPool);
|
|
|
+ String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(settingsBuilder()
|
|
|
+ .put("name", "testCachedExecutorType").build());
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+
|
|
|
+ threadPool.updateSettings(settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".size", "15")
|
|
|
+ .build());
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(15));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(15));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMin(), equalTo(15));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMax(), equalTo(15));
|
|
|
+ // keep alive does not apply to fixed thread pools
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(0L));
|
|
|
+
|
|
|
+ // Put old type back
|
|
|
+ threadPool.updateSettings(Settings.EMPTY);
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ // Make sure keep alive value is not used
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive(), nullValue());
|
|
|
+ // Make sure keep pool size value were reused
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMin(), equalTo(15));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMax(), equalTo(15));
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(15));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(15));
|
|
|
+
|
|
|
+ // Change size
|
|
|
+ Executor oldExecutor = threadPool.executor(threadPoolName);
|
|
|
+ threadPool.updateSettings(settingsBuilder().put("threadpool." + threadPoolName + ".size", "10").build());
|
|
|
+ // Make sure size values changed
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMax(), equalTo(10));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMin(), equalTo(10));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(10));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(10));
|
|
|
+ // Make sure executor didn't change
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), sameInstance(oldExecutor));
|
|
|
+
|
|
|
+ // Change queue capacity
|
|
|
+ threadPool.updateSettings(settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".queue", "500")
|
|
|
+ .build());
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void testScalingExecutorType() throws InterruptedException {
|
|
|
- ThreadPool threadPool = new ThreadPool(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "scaling")
|
|
|
- .put("threadpool.search.size", 10)
|
|
|
- .put("name","testCachedExecutorType").build());
|
|
|
-
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMin(), equalTo(1));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMax(), equalTo(10));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(5L));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("scaling"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
-
|
|
|
- // Change settings that doesn't require pool replacement
|
|
|
- Executor oldExecutor = threadPool.executor(Names.SEARCH);
|
|
|
- threadPool.updateSettings(settingsBuilder()
|
|
|
- .put("threadpool.search.type", "scaling")
|
|
|
- .put("threadpool.search.keep_alive", "10m")
|
|
|
- .put("threadpool.search.min", "2")
|
|
|
- .put("threadpool.search.size", "15")
|
|
|
- .build());
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getType(), equalTo("scaling"));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), instanceOf(EsThreadPoolExecutor.class));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getCorePoolSize(), equalTo(2));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getMaximumPoolSize(), equalTo(15));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMin(), equalTo(2));
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getMax(), equalTo(15));
|
|
|
- // Make sure keep alive value changed
|
|
|
- assertThat(info(threadPool, Names.SEARCH).getKeepAlive().minutes(), equalTo(10L));
|
|
|
- assertThat(((EsThreadPoolExecutor) threadPool.executor(Names.SEARCH)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(10L));
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), sameInstance(oldExecutor));
|
|
|
-
|
|
|
- terminate(threadPool);
|
|
|
+ String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.SCALING);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".size", 10)
|
|
|
+ .put("name", "testCachedExecutorType").build());
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMin(), equalTo(1));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMax(), equalTo(10));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(5L));
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.SCALING);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+
|
|
|
+ // Change settings that doesn't require pool replacement
|
|
|
+ Executor oldExecutor = threadPool.executor(threadPoolName);
|
|
|
+ threadPool.updateSettings(settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".keep_alive", "10m")
|
|
|
+ .put("threadpool." + threadPoolName + ".min", "2")
|
|
|
+ .put("threadpool." + threadPoolName + ".size", "15")
|
|
|
+ .build());
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.SCALING);
|
|
|
+ assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(2));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(15));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMin(), equalTo(2));
|
|
|
+ assertThat(info(threadPool, threadPoolName).getMax(), equalTo(15));
|
|
|
+ // Make sure keep alive value changed
|
|
|
+ assertThat(info(threadPool, threadPoolName).getKeepAlive().minutes(), equalTo(10L));
|
|
|
+ assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(10L));
|
|
|
+ assertThat(threadPool.executor(threadPoolName), sameInstance(oldExecutor));
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void testShutdownNowInterrupts() throws Exception {
|
|
|
- ThreadPool threadPool = new ThreadPool(Settings.settingsBuilder()
|
|
|
- .put("threadpool.search.type", "cached")
|
|
|
- .put("name","testCachedExecutorType").build());
|
|
|
-
|
|
|
- final CountDownLatch latch = new CountDownLatch(1);
|
|
|
- ThreadPoolExecutor oldExecutor = (ThreadPoolExecutor) threadPool.executor(Names.SEARCH);
|
|
|
- threadPool.executor(Names.SEARCH).execute(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try {
|
|
|
- new CountDownLatch(1).await();
|
|
|
- } catch (InterruptedException ex) {
|
|
|
- latch.countDown();
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- threadPool.updateSettings(settingsBuilder().put("threadpool.search.type", "fixed").build());
|
|
|
- assertThat(threadPool.executor(Names.SEARCH), not(sameInstance(oldExecutor)));
|
|
|
- assertThat(oldExecutor.isShutdown(), equalTo(true));
|
|
|
- assertThat(oldExecutor.isTerminating(), equalTo(true));
|
|
|
- assertThat(oldExecutor.isTerminated(), equalTo(false));
|
|
|
- threadPool.shutdownNow(); // should interrupt the thread
|
|
|
- latch.await(3, TimeUnit.SECONDS); // If this throws then shotdownNow didn't interrupt
|
|
|
- terminate(threadPool);
|
|
|
+ String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(Settings.settingsBuilder()
|
|
|
+ .put("threadpool." + threadPoolName + ".queue_size", 1000)
|
|
|
+ .put("name", "testCachedExecutorType").build());
|
|
|
+ assertEquals(info(threadPool, threadPoolName).getQueueSize().getSingles(), 1000L);
|
|
|
+
|
|
|
+ final CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ ThreadPoolExecutor oldExecutor = (ThreadPoolExecutor) threadPool.executor(threadPoolName);
|
|
|
+ threadPool.executor(threadPoolName).execute(() -> {
|
|
|
+ try {
|
|
|
+ new CountDownLatch(1).await();
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
+ latch.countDown();
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ threadPool.updateSettings(settingsBuilder().put("threadpool." + threadPoolName + ".queue_size", 2000).build());
|
|
|
+ assertThat(threadPool.executor(threadPoolName), not(sameInstance(oldExecutor)));
|
|
|
+ assertThat(oldExecutor.isShutdown(), equalTo(true));
|
|
|
+ assertThat(oldExecutor.isTerminating(), equalTo(true));
|
|
|
+ assertThat(oldExecutor.isTerminated(), equalTo(false));
|
|
|
+ threadPool.shutdownNow(); // should interrupt the thread
|
|
|
+ latch.await(3, TimeUnit.SECONDS); // If this throws then ThreadPool#shutdownNow didn't interrupt
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void testCustomThreadPool() throws Exception {
|
|
|
- ThreadPool threadPool = new ThreadPool(Settings.settingsBuilder()
|
|
|
- .put("threadpool.my_pool1.type", "cached")
|
|
|
- .put("threadpool.my_pool2.type", "fixed")
|
|
|
- .put("threadpool.my_pool2.size", "1")
|
|
|
- .put("threadpool.my_pool2.queue_size", "1")
|
|
|
- .put("name", "testCustomThreadPool").build());
|
|
|
-
|
|
|
- ThreadPoolInfo groups = threadPool.info();
|
|
|
- boolean foundPool1 = false;
|
|
|
- boolean foundPool2 = false;
|
|
|
- outer: for (ThreadPool.Info info : groups) {
|
|
|
- if ("my_pool1".equals(info.getName())) {
|
|
|
- foundPool1 = true;
|
|
|
- assertThat(info.getType(), equalTo("cached"));
|
|
|
- } else if ("my_pool2".equals(info.getName())) {
|
|
|
- foundPool2 = true;
|
|
|
- assertThat(info.getType(), equalTo("fixed"));
|
|
|
- assertThat(info.getMin(), equalTo(1));
|
|
|
- assertThat(info.getMax(), equalTo(1));
|
|
|
- assertThat(info.getQueueSize().singles(), equalTo(1l));
|
|
|
- } else {
|
|
|
- for (Field field : Names.class.getFields()) {
|
|
|
- if (info.getName().equalsIgnoreCase(field.getName())) {
|
|
|
- // This is ok it is a default thread pool
|
|
|
- continue outer;
|
|
|
+ ThreadPool threadPool = null;
|
|
|
+ try {
|
|
|
+ threadPool = new ThreadPool(Settings.settingsBuilder()
|
|
|
+ .put("threadpool.my_pool1.type", "scaling")
|
|
|
+ .put("threadpool.my_pool2.type", "fixed")
|
|
|
+ .put("threadpool.my_pool2.size", "1")
|
|
|
+ .put("threadpool.my_pool2.queue_size", "1")
|
|
|
+ .put("name", "testCustomThreadPool").build());
|
|
|
+ ThreadPoolInfo groups = threadPool.info();
|
|
|
+ boolean foundPool1 = false;
|
|
|
+ boolean foundPool2 = false;
|
|
|
+ outer:
|
|
|
+ for (ThreadPool.Info info : groups) {
|
|
|
+ if ("my_pool1".equals(info.getName())) {
|
|
|
+ foundPool1 = true;
|
|
|
+ assertEquals(info.getThreadPoolType(), ThreadPool.ThreadPoolType.SCALING);
|
|
|
+ } else if ("my_pool2".equals(info.getName())) {
|
|
|
+ foundPool2 = true;
|
|
|
+ assertEquals(info.getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ assertThat(info.getMin(), equalTo(1));
|
|
|
+ assertThat(info.getMax(), equalTo(1));
|
|
|
+ assertThat(info.getQueueSize().singles(), equalTo(1l));
|
|
|
+ } else {
|
|
|
+ for (Field field : Names.class.getFields()) {
|
|
|
+ if (info.getName().equalsIgnoreCase(field.getName())) {
|
|
|
+ // This is ok it is a default thread pool
|
|
|
+ continue outer;
|
|
|
+ }
|
|
|
}
|
|
|
+ fail("Unexpected pool name: " + info.getName());
|
|
|
}
|
|
|
- fail("Unexpected pool name: " + info.getName());
|
|
|
}
|
|
|
- }
|
|
|
- assertThat(foundPool1, is(true));
|
|
|
- assertThat(foundPool2, is(true));
|
|
|
-
|
|
|
- // Updating my_pool2
|
|
|
- Settings settings = Settings.builder()
|
|
|
- .put("threadpool.my_pool2.size", "10")
|
|
|
- .build();
|
|
|
- threadPool.updateSettings(settings);
|
|
|
-
|
|
|
- groups = threadPool.info();
|
|
|
- foundPool1 = false;
|
|
|
- foundPool2 = false;
|
|
|
- outer: for (ThreadPool.Info info : groups) {
|
|
|
- if ("my_pool1".equals(info.getName())) {
|
|
|
- foundPool1 = true;
|
|
|
- assertThat(info.getType(), equalTo("cached"));
|
|
|
- } else if ("my_pool2".equals(info.getName())) {
|
|
|
- foundPool2 = true;
|
|
|
- assertThat(info.getMax(), equalTo(10));
|
|
|
- assertThat(info.getMin(), equalTo(10));
|
|
|
- assertThat(info.getQueueSize().singles(), equalTo(1l));
|
|
|
- assertThat(info.getType(), equalTo("fixed"));
|
|
|
- } else {
|
|
|
- for (Field field : Names.class.getFields()) {
|
|
|
- if (info.getName().equalsIgnoreCase(field.getName())) {
|
|
|
- // This is ok it is a default thread pool
|
|
|
- continue outer;
|
|
|
+ assertThat(foundPool1, is(true));
|
|
|
+ assertThat(foundPool2, is(true));
|
|
|
+
|
|
|
+ // Updating my_pool2
|
|
|
+ Settings settings = Settings.builder()
|
|
|
+ .put("threadpool.my_pool2.size", "10")
|
|
|
+ .build();
|
|
|
+ threadPool.updateSettings(settings);
|
|
|
+
|
|
|
+ groups = threadPool.info();
|
|
|
+ foundPool1 = false;
|
|
|
+ foundPool2 = false;
|
|
|
+ outer:
|
|
|
+ for (ThreadPool.Info info : groups) {
|
|
|
+ if ("my_pool1".equals(info.getName())) {
|
|
|
+ foundPool1 = true;
|
|
|
+ assertEquals(info.getThreadPoolType(), ThreadPool.ThreadPoolType.SCALING);
|
|
|
+ } else if ("my_pool2".equals(info.getName())) {
|
|
|
+ foundPool2 = true;
|
|
|
+ assertThat(info.getMax(), equalTo(10));
|
|
|
+ assertThat(info.getMin(), equalTo(10));
|
|
|
+ assertThat(info.getQueueSize().singles(), equalTo(1l));
|
|
|
+ assertEquals(info.getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
|
|
|
+ } else {
|
|
|
+ for (Field field : Names.class.getFields()) {
|
|
|
+ if (info.getName().equalsIgnoreCase(field.getName())) {
|
|
|
+ // This is ok it is a default thread pool
|
|
|
+ continue outer;
|
|
|
+ }
|
|
|
}
|
|
|
+ fail("Unexpected pool name: " + info.getName());
|
|
|
}
|
|
|
- fail("Unexpected pool name: " + info.getName());
|
|
|
+ }
|
|
|
+ assertThat(foundPool1, is(true));
|
|
|
+ assertThat(foundPool2, is(true));
|
|
|
+ } finally {
|
|
|
+ terminateThreadPoolIfNeeded(threadPool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void terminateThreadPoolIfNeeded(ThreadPool threadPool) throws InterruptedException {
|
|
|
+ if (threadPool != null) {
|
|
|
+ terminate(threadPool);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ThreadPool.Info info(ThreadPool threadPool, String name) {
|
|
|
+ for (ThreadPool.Info info : threadPool.info()) {
|
|
|
+ if (info.getName().equals(name)) {
|
|
|
+ return info;
|
|
|
}
|
|
|
}
|
|
|
- assertThat(foundPool1, is(true));
|
|
|
- assertThat(foundPool2, is(true));
|
|
|
- terminate(threadPool);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String randomThreadPoolName() {
|
|
|
+ Set<String> threadPoolNames = ThreadPool.THREAD_POOL_TYPES.keySet();
|
|
|
+ return randomFrom(threadPoolNames.toArray(new String[threadPoolNames.size()]));
|
|
|
}
|
|
|
|
|
|
+ private ThreadPool.ThreadPoolType randomIncorrectThreadPoolType(String threadPoolName) {
|
|
|
+ Set<ThreadPool.ThreadPoolType> set = new HashSet<>();
|
|
|
+ set.addAll(Arrays.asList(ThreadPool.ThreadPoolType.values()));
|
|
|
+ set.remove(ThreadPool.THREAD_POOL_TYPES.get(threadPoolName));
|
|
|
+ ThreadPool.ThreadPoolType invalidThreadPoolType = randomFrom(set.toArray(new ThreadPool.ThreadPoolType[set.size()]));
|
|
|
+ return invalidThreadPoolType;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String randomThreadPool(ThreadPool.ThreadPoolType type) {
|
|
|
+ return randomFrom(ThreadPool.THREAD_POOL_TYPES.entrySet().stream().filter(t -> t.getValue().equals(type)).map(t -> t.getKey()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
}
|