aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/Handler.java
blob: 969db6553e6f2b670b4e04be304f20f6810fe75e (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.component;

import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.container.handler.threadpool.ContainerThreadpoolConfig;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.vespa.model.container.ContainerThreadpool;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * Models a jdisc RequestHandler (including ClientProvider).
 * RequestHandlers always have at least one server binding,
 * while ClientProviders have at least one client binding.
 *
 * @author gjoranv
 */
public class Handler extends Component<Component<?, ?>, ComponentModel> {

    private final Set<BindingPattern> serverBindings = new LinkedHashSet<>();
    private final List<BindingPattern> clientBindings = new ArrayList<>();

    public final boolean hasCustomThreadPool;

    public Handler(ComponentModel model) {
        this(model, null);
    }

    public Handler(ComponentModel model, ContainerThreadpool threadpool) {
        super(model);

        // The default threadpool is always added to the cluster, so cannot be added here.
        if (threadpool != null) {
            hasCustomThreadPool = true;
            addComponent(threadpool);
            inject(threadpool);
        } else {
            hasCustomThreadPool = false;
        }
    }

    public static Handler fromClassName(String className) {
        return new Handler(new ComponentModel(className, null, null, null));
    }

    public void addServerBindings(BindingPattern... bindings) {
        serverBindings.addAll(Arrays.asList(bindings));
    }

    public void addServerBindings(Collection<BindingPattern> bps) { serverBindings.addAll(bps); }

    public void removeServerBinding(BindingPattern binding) {
        serverBindings.remove(binding);
    }

    public void addClientBindings(BindingPattern... bindings) {
        clientBindings.addAll(Arrays.asList(bindings));
    }

    public final Set<BindingPattern> getServerBindings() {
        return Collections.unmodifiableSet(serverBindings);
    }

    public final List<BindingPattern> getClientBindings() {
        return Collections.unmodifiableList(clientBindings);
    }


    /**
     * The default threadpool for all handlers, except those that declare their own, e.g. SearchHandler.
     */
    public static class DefaultHandlerThreadpool extends ContainerThreadpool {

        public DefaultHandlerThreadpool(DeployState ds, Element options) {
            super(ds, "default-handler-common", options);
        }

        @Override
        public void setDefaultConfigValues(ContainerThreadpoolConfig.Builder builder) {
            builder.maxThreadExecutionTimeSeconds(190)
                    .keepAliveTime(5.0)
                    .maxThreads(-2)
                    .minThreads(-2)
                    .queueSize(-40);
        }
    }

}