summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/threadpool/UpdateThreadPoolSettingsTests.java
blob: 09653c12e0795363fdb53ce161b23083733f66d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.threadpool;

import org.elasticsearch.common.settings.ClusterSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor;
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;

/**
 */
public class UpdateThreadPoolSettingsTests extends ESTestCase {
    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());
            ThreadPool.Info info = info(threadPool, threadPoolName);
            if (ThreadPool.Names.SAME.equals(threadPoolName)) {
                assertNull(info); // we don't report on the "same" threadpool
            } else {
                // otherwise check we have the expected type
                assertEquals(info.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 testIndexingThreadPoolsMaxSize() throws InterruptedException {
        String threadPoolName = randomThreadPoolName();
        for (String name : new String[] {ThreadPool.Names.BULK, ThreadPool.Names.INDEX}) {
            ThreadPool threadPool = null;
            try {

                int maxSize = EsExecutors.boundedNumberOfProcessors(Settings.EMPTY);

                // try to create a too-big (maxSize+1) thread pool
                threadPool = new ThreadPool(settingsBuilder()
                                               .put("name", "testIndexingThreadPoolsMaxSize")
                                               .put("threadpool." + name + ".size", maxSize+1)
                                               .build());

                // confirm it clipped us at the maxSize:
                assertEquals(maxSize, ((ThreadPoolExecutor) threadPool.executor(name)).getMaximumPoolSize());

                ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
                threadPool.setClusterSettings(clusterSettings);

                // update it to a tiny size:
                clusterSettings.applySettings(
                        settingsBuilder()
                        .put("threadpool." + name + ".size", 1)
                        .build()
                );

                // confirm it worked:
                assertEquals(1, ((ThreadPoolExecutor) threadPool.executor(name)).getMaximumPoolSize());

                // try to update to too-big size:
                clusterSettings.applySettings(
                        settingsBuilder()
                        .put("threadpool." + name + ".size", maxSize+1)
                        .build()
                );

                // confirm it clipped us at the maxSize:
                assertEquals(maxSize, ((ThreadPoolExecutor) threadPool.executor(name)).getMaximumPoolSize());
            } 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());
            ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);

            clusterSettings.applySettings(
                    settingsBuilder()
                            .put("threadpool." + threadPoolName + ".type", invalidThreadPoolType.getType())
                            .build()
            );
            fail("expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            assertEquals("illegal value can't update [threadpool.] from [{}] to [{" + threadPoolName + ".type=" + invalidThreadPoolType.getType() + "}]", e.getMessage());
            assertThat(
                    e.getCause().getMessage(),
                    is("setting threadpool." + threadPoolName + ".type to " + invalidThreadPoolType.getType() + " is not permitted; must be " + validThreadPoolType.getType()));
        } finally {
            terminateThreadPoolIfNeeded(threadPool);
        }
    }

    public void testCachedExecutorType() throws InterruptedException {
        String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.CACHED);
        ThreadPool threadPool = null;
        try {
            Settings nodeSettings = Settings.settingsBuilder()
                    .put("name", "testCachedExecutorType").build();
            threadPool = new ThreadPool(nodeSettings);
            ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);

            assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.CACHED);
            assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));

            Settings settings = clusterSettings.applySettings(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);
            settings = clusterSettings.applySettings(settingsBuilder().put(settings).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
            settings = clusterSettings.applySettings(settingsBuilder().put(settings).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);
        }
    }

    private static int getExpectedThreadPoolSize(Settings settings, String name, int size) {
        if (name.equals(ThreadPool.Names.BULK) || name.equals(ThreadPool.Names.INDEX)) {
            return Math.min(size, EsExecutors.boundedNumberOfProcessors(settings));
        } else {
            return size;
        }
    }

    public void testFixedExecutorType() throws InterruptedException {
        String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED);
        ThreadPool threadPool = null;

        try {
            Settings nodeSettings = Settings.settingsBuilder()
                    .put("name", "testFixedExecutorType").build();
            threadPool = new ThreadPool(nodeSettings);
            ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);
            assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
            Settings settings = clusterSettings.applySettings(settingsBuilder()
                    .put("threadpool." + threadPoolName + ".size", "15")
                    .build());

            int expectedSize = getExpectedThreadPoolSize(nodeSettings, threadPoolName, 15);
            assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
            assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(expectedSize));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(expectedSize));
            assertThat(info(threadPool, threadPoolName).getMin(), equalTo(expectedSize));
            assertThat(info(threadPool, threadPoolName).getMax(), equalTo(expectedSize));
            // keep alive does not apply to fixed thread pools
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getKeepAliveTime(TimeUnit.MINUTES), equalTo(0L));

            // Put old type back
            settings = clusterSettings.applySettings(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(expectedSize));
            assertThat(info(threadPool, threadPoolName).getMax(), equalTo(expectedSize));
            assertThat(threadPool.executor(threadPoolName), instanceOf(EsThreadPoolExecutor.class));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(expectedSize));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(expectedSize));

            // Change size
            Executor oldExecutor = threadPool.executor(threadPoolName);
            settings = clusterSettings.applySettings(settingsBuilder().put(settings).put("threadpool." + threadPoolName + ".size", "10").build());

            expectedSize = getExpectedThreadPoolSize(nodeSettings, threadPoolName, 10);

            // Make sure size values changed
            assertThat(info(threadPool, threadPoolName).getMax(), equalTo(expectedSize));
            assertThat(info(threadPool, threadPoolName).getMin(), equalTo(expectedSize));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getMaximumPoolSize(), equalTo(expectedSize));
            assertThat(((EsThreadPoolExecutor) threadPool.executor(threadPoolName)).getCorePoolSize(), equalTo(expectedSize));
            // Make sure executor didn't change
            assertEquals(info(threadPool, threadPoolName).getThreadPoolType(), ThreadPool.ThreadPoolType.FIXED);
            assertThat(threadPool.executor(threadPoolName), sameInstance(oldExecutor));

            // Change queue capacity
            settings = clusterSettings.applySettings(settingsBuilder().put(settings).put("threadpool." + threadPoolName + ".queue", "500")
                    .build());
        } finally {
            terminateThreadPoolIfNeeded(threadPool);
        }
    }

    public void testScalingExecutorType() throws InterruptedException {
        String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.SCALING);
        ThreadPool threadPool = null;
        try {
            Settings nodeSettings = settingsBuilder()
                    .put("threadpool." + threadPoolName + ".size", 10)
                    .put("name", "testScalingExecutorType").build();
            threadPool = new ThreadPool(nodeSettings);
            ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);
            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);
            clusterSettings.applySettings(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 {
        String threadPoolName = randomThreadPool(ThreadPool.ThreadPoolType.FIXED);
        ThreadPool threadPool = null;
        try {
            Settings nodeSettings = Settings.settingsBuilder()
                    .put("threadpool." + threadPoolName + ".queue_size", 1000)
                    .put("name", "testCachedExecutorType").build();
            threadPool = new ThreadPool(nodeSettings);
            ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);
            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();
                        }
                    }
            );
            clusterSettings.applySettings(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 = null;
        try {
            Settings nodeSettings = 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();
            threadPool = new ThreadPool(nodeSettings);
            ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
            threadPool.setClusterSettings(clusterSettings);
            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());
                }
            }
            assertThat(foundPool1, is(true));
            assertThat(foundPool2, is(true));

            // Updating my_pool2
            Settings settings = Settings.builder()
                    .put("threadpool.my_pool2.size", "10")
                    .build();
            clusterSettings.applySettings(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());
                }
            }
            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;
            }
        }
        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()));
    }
}