aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/container/image/ContainerImageDownloader.java
blob: ab2adc061fdf242940c296254f77d69a5845e8e9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.container.image;

import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.jdisc.Timer;
import com.yahoo.vespa.hosted.node.admin.component.TaskContext;
import com.yahoo.vespa.hosted.node.admin.container.ContainerEngine;
import com.yahoo.vespa.hosted.node.admin.container.RegistryCredentialsProvider;

import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Download a container image asynchronously.
 *
 * @author mpolden
 */
public class ContainerImageDownloader {

    private static final Logger LOG = Logger.getLogger(ContainerImageDownloader.class.getName());

    private final ContainerEngine containerEngine;
    private final Timer timer;

    private final ExecutorService executorService = Executors.newSingleThreadExecutor(
            new DaemonThreadFactory("container-image-downloader")); // Download one image at a time
    private final Set<DockerImage> pendingDownloads = Collections.synchronizedSet(new HashSet<>());

    public ContainerImageDownloader(ContainerEngine containerEngine, Timer timer) {
        this.containerEngine = Objects.requireNonNull(containerEngine);
        this.timer = Objects.requireNonNull(timer);
    }

    /**
     * Download given container image.
     *
     * @return true if the image download has completed.
     */
    public boolean get(TaskContext context, DockerImage image, RegistryCredentialsProvider credentialsProvider) {
        if (pendingDownloads.contains(image)) return false;
        if (containerEngine.hasImage(context, image)) return true;
        executorService.submit(() -> {
            try {
                Instant start = timer.currentTime();
                containerEngine.pullImage(context, image, credentialsProvider.get());
                LOG.log(Level.INFO, "Downloaded container image " + image + " in " + Duration.between(start, timer.currentTime()));
            } catch (RuntimeException e) {
                LOG.log(Level.SEVERE, "Failed to download container image " + image, e);
            } finally {
                pendingDownloads.remove(image);
            }
        });
        pendingDownloads.add(image);
        return false;
    }

}