aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/ThreadPoolProvider.java
blob: 362d82891d02143d4f67774a52fcccb375113b49 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler;

import com.yahoo.component.annotation.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.ContainerThreadpoolImpl;
import com.yahoo.container.protect.ProcessTerminator;
import com.yahoo.jdisc.Metric;

import java.util.concurrent.Executor;

/**
 * A configurable thread pool provider for the jdisc default threadpool.
 * This provides the worker threads used for normal request processing.
 * Request an {@link 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 ContainerThreadpoolImpl(translateConfig(config), metric);
    }

    public ThreadPoolProvider(ThreadpoolConfig config, Metric metric, ProcessTerminator processTerminator) {
        this.threadpool = new ContainerThreadpoolImpl(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) {
        return new ContainerThreadpoolConfig(
                new ContainerThreadpoolConfig.Builder()
                        .maxThreads(config.maxthreads())
                        .minThreads(config.corePoolSize())
                        .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();
    }

}