aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/Container.java
blob: 477317616304d72f73502443b492b138a9d821c4 (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
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container;

import com.yahoo.component.AbstractComponent;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.filedistribution.fileacquirer.FileAcquirer;
import com.yahoo.filedistribution.fileacquirer.FileAcquirerFactory;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.service.ClientProvider;
import com.yahoo.jdisc.service.ServerProvider;
import com.yahoo.vespa.config.ConfigTransformer;
import com.yahoo.vespa.config.UrlDownloader;

import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/**
 * The container instance. This is a Vespa internal object, external code should
 * only depend on this if there are no other options, and must be prepared to
 * see it change at no warning.
 *
 * @author bratseth
 */
public class Container {

    private volatile boolean usingCustomFileAcquirer = false;
    private volatile boolean disabledUrlDownloader = false;

    private volatile ComponentRegistry<RequestHandler> requestHandlerRegistry;
    private volatile ComponentRegistry<ClientProvider> clientProviderRegistry;
    private volatile ComponentRegistry<ServerProvider> serverProviderRegistry;
    private volatile ComponentRegistry<AbstractComponent> componentRegistry;
    private volatile FileAcquirer fileAcquirer;
    private volatile UrlDownloader urlDownloader;

    private static final Logger logger = Logger.getLogger(Container.class.getName());

    // TODO: Make this final again.
    private static Container instance = new Container();

    public static Container get() { return instance; }

    public void shutdown() {
        if (fileAcquirer != null)
            fileAcquirer.shutdown();
        if (urlDownloader != null)
            urlDownloader.shutdown();
    }

    //Used to acquire files originating from the application package.
    public FileAcquirer getFileAcquirer() {
        return fileAcquirer;
    }

    /**
     * Hack. For internal use only, will be removed later.
     *
     * Used by Application to be able to repeatedly set up containers.
     */
    public static void resetInstance() {
        instance = new Container();
    }

    public ComponentRegistry<RequestHandler> getRequestHandlerRegistry() {
        return requestHandlerRegistry;
    }

    public void setRequestHandlerRegistry(ComponentRegistry<RequestHandler> requestHandlerRegistry) {
        this.requestHandlerRegistry = requestHandlerRegistry;
    }

    public ComponentRegistry<ClientProvider> getClientProviderRegistry() {
        return clientProviderRegistry;
    }

    public void setClientProviderRegistry(ComponentRegistry<ClientProvider> clientProviderRegistry) {
        this.clientProviderRegistry = clientProviderRegistry;
    }

    public ComponentRegistry<ServerProvider> getServerProviderRegistry() {
        return serverProviderRegistry;
    }

    public void setServerProviderRegistry(ComponentRegistry<ServerProvider> serverProviderRegistry) {
        this.serverProviderRegistry = serverProviderRegistry;
    }

    public ComponentRegistry<AbstractComponent> getComponentRegistry() {
        return componentRegistry;
    }

    public void setComponentRegistry(ComponentRegistry<AbstractComponent> registry) {
        registry.freeze();
        this.componentRegistry = registry;
    }

    // Only intended for use by the Server instance.
    public void setupFileAcquirer(QrConfig.Filedistributor filedistributorConfig) {
        if (usingCustomFileAcquirer)
            return;

        if (filedistributorConfig.configid().isEmpty()) {
            if (fileAcquirer != null)
                logger.warning("Disabling file distribution");
            fileAcquirer = null;
        } else {
            fileAcquirer = FileAcquirerFactory.create(filedistributorConfig.configid());
        }

        setPathAcquirer(fileAcquirer);
    }

    /** Only for internal use. */
    public void setCustomFileAcquirer(FileAcquirer fileAcquirer) {
        if (this.fileAcquirer != null) {
            throw new RuntimeException("Can't change file acquirer. Is " +
                                       this.fileAcquirer + " attempted to set to " + fileAcquirer);
        }
        usingCustomFileAcquirer = true;
        this.fileAcquirer = fileAcquirer;
        setPathAcquirer(fileAcquirer);
    }

    private static void setPathAcquirer(FileAcquirer fileAcquirer) {
        ConfigTransformer.setPathAcquirer(fileReference -> {
            try {
                return fileAcquirer.waitFor(fileReference, 15, TimeUnit.MINUTES).toPath();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException(e);
            }
        });
    }

    public void setupUrlDownloader() {
        if (disabledUrlDownloader) {
            return;
        }
        this.urlDownloader = new UrlDownloader();
        ConfigTransformer.setUrlDownloader(urlDownloader);
    }

    public void disableUrlDownloader() {
        disabledUrlDownloader = true;
    }

}