blob: 081cefbd7ebb0e626284c635a63e36835bd8cf20 (
plain) (
blame)
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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;
import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.container.handler.threadpool.ContainerThreadPool;
import com.yahoo.container.handler.threadpool.ContainerThreadpoolConfig;
import com.yahoo.container.handler.threadpool.DefaultContainerThreadpool;
import com.yahoo.container.protect.ProcessTerminator;
import com.yahoo.jdisc.Metric;
import java.util.concurrent.Executor;
/**
* A configurable thread pool provider. This provides the worker threads used for normal request processing.
* Request an Executor injected in your component constructor if you want to use it.
*
* @author Steinar Knutsen
* @author baldersheim
* @author bratseth
*/
public class ThreadPoolProvider extends AbstractComponent implements Provider<Executor> {
private final ContainerThreadPool threadpool;
@Inject
public ThreadPoolProvider(ThreadpoolConfig config, Metric metric) {
this.threadpool = new DefaultContainerThreadpool(translateConfig(config), metric);
}
public ThreadPoolProvider(ThreadpoolConfig config, Metric metric, ProcessTerminator processTerminator) {
this.threadpool = new DefaultContainerThreadpool(translateConfig(config), metric, processTerminator);
}
/**
* The underlying {@link ContainerThreadPool} uses a different config definition ({@link ContainerThreadpoolConfig})
* as {@link ThreadpoolConfig} is currently public api.
*/
private static ContainerThreadpoolConfig translateConfig(ThreadpoolConfig config) {
int cpus = Runtime.getRuntime().availableProcessors();
int max = config.maxthreads() >= 0 ? config.maxthreads() : Math.max(8, Math.abs(config.maxthreads()) * cpus);
int min = config.corePoolSize() >= 0 ? config.corePoolSize() : Math.max(8, Math.abs(config.corePoolSize()) * cpus);
return new ContainerThreadpoolConfig(
new ContainerThreadpoolConfig.Builder()
.maxThreads(max)
.minThreads(min)
.name(config.name())
.queueSize(config.queueSize())
.keepAliveTime(config.keepAliveTime())
.maxThreadExecutionTimeSeconds(config.maxThreadExecutionTimeSeconds()));
}
/**
* Get the Executor provided by this class. This Executor will by default
* also be used for search queries and processing requests.
*
* @return a possibly shared executor
*/
@Override
public Executor get() { return threadpool.executor(); }
/**
* Shut down the thread pool, give a grace period of 1 second before forcibly
* shutting down all worker threads.
*/
@Override
public void deconstruct() {
threadpool.close();
}
}
|