aboutsummaryrefslogtreecommitdiffstats
path: root/config-proxy/src/main/java/com/yahoo/vespa/config/proxy/filedistribution/FileDistributionAndUrlDownload.java
blob: 3e7f7418ce2c5a9f21bb225f13066f2a601eb23d (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.proxy.filedistribution;

import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.config.subscription.ConfigSourceSet;
import com.yahoo.jrt.Supervisor;
import com.yahoo.vespa.config.ConnectionPool;
import com.yahoo.vespa.config.JRTConnectionPool;
import com.yahoo.vespa.filedistribution.FileDistributionConnectionPool;
import com.yahoo.vespa.filedistribution.FileDownloader;

import java.time.Duration;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import static java.util.logging.Level.INFO;

/**
 * Keeps track of file distribution and url download rpc servers.
 *
 * @author hmusum
 */
public class FileDistributionAndUrlDownload {

    private static final Logger log = Logger.getLogger(FileDistributionAndUrlDownload.class.getName());
    private static final Duration delay = Duration.ofMinutes(1);

    private final FileDistributionRpcServer fileDistributionRpcServer;
    private final UrlDownloadRpcServer urlDownloadRpcServer;
    private final ScheduledExecutorService cleanupExecutor =
            new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("file references and downloads cleanup"));

    public FileDistributionAndUrlDownload(Supervisor supervisor, ConfigSourceSet source) {
        fileDistributionRpcServer =
                new FileDistributionRpcServer(supervisor,
                                              new FileDownloader(createConnectionPool(supervisor, source), supervisor, Duration.ofMinutes(5)));
        urlDownloadRpcServer = new UrlDownloadRpcServer(supervisor);
        cleanupExecutor.scheduleAtFixedRate(new CachedFilesMaintainer(), delay.toSeconds(), delay.toSeconds(), TimeUnit.SECONDS);
    }

    public void close() {
        fileDistributionRpcServer.close();
        urlDownloadRpcServer.close();
        cleanupExecutor.shutdownNow();
        try {
            if ( ! cleanupExecutor.awaitTermination(10, TimeUnit.SECONDS))
                throw new RuntimeException("Unable to shutdown " + cleanupExecutor + " before timeout");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static ConnectionPool createConnectionPool(Supervisor supervisor, ConfigSourceSet source) {
        String useFileDistributionConnectionPool = System.getenv("VESPA_CONFIG_PROXY_USE_FILE_DISTRIBUTION_CONNECTION_POOL");
        // TODO: Lower log level or remove when verified
        log.log(INFO, "VESPA_CONFIG_PROXY_USE_FILE_DISTRIBUTION_CONNECTION_POOL: " + useFileDistributionConnectionPool);
        if (useFileDistributionConnectionPool != null && useFileDistributionConnectionPool.equalsIgnoreCase("true"))
            return new FileDistributionConnectionPool(source, supervisor);
        else
            return new JRTConnectionPool(source, supervisor);
    }

}