aboutsummaryrefslogtreecommitdiffstats
path: root/config-proxy/src/main/java/com/yahoo/vespa/config/proxy/filedistribution/FileReferencesAndDownloadsMaintainer.java
blob: 8918973c6d43964947fe8739a2192958fbd96a2f (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright Vespa.ai. 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.io.IOUtils;
import com.yahoo.vespa.config.util.ConfigUtils;
import com.yahoo.vespa.filedistribution.FileDownloader;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.nio.file.Files.readAttributes;
import static java.util.logging.Level.INFO;

/**
 * Deletes file references and url downloads that have not been used for some time.
 * See {@link com.yahoo.vespa.config.proxy.filedistribution.RequestTracker} for how we track
 * when a file reference or download was last used.
 *
 * @author hmusum
 */
class FileReferencesAndDownloadsMaintainer implements Runnable {

    private static final Logger log = Logger.getLogger(FileReferencesAndDownloadsMaintainer.class.getName());
    private static final File defaultUrlDownloadDir = UrlDownloadRpcServer.defaultDownloadDirectory;
    private static final File defaultFileReferencesDownloadDir = FileDownloader.defaultDownloadDirectory;
    private static final Duration defaultDurationToKeepFiles = Duration.ofDays(30);
    private static final int defaultOutdatedFilesToKeep = 20;
    private static final Duration interval = Duration.ofMinutes(1);

    private final Optional<ScheduledExecutorService> executor;
    private final File urlDownloadDir;
    private final File fileReferencesDownloadDir;
    private final Duration durationToKeepFiles;
    private final int outDatedFilesToKeep;

    FileReferencesAndDownloadsMaintainer() {
        this(defaultFileReferencesDownloadDir, defaultUrlDownloadDir, keepFileReferencesDuration(),
             outDatedFilesToKeep(), configServers());
    }

    FileReferencesAndDownloadsMaintainer(File fileReferencesDownloadDir,
                                         File urlDownloadDir,
                                         Duration durationToKeepFiles,
                                         int outdatedFilesToKeep,
                                         List<String> configServers) {
        this.fileReferencesDownloadDir = fileReferencesDownloadDir;
        this.urlDownloadDir = urlDownloadDir;
        this.durationToKeepFiles = durationToKeepFiles;
        this.outDatedFilesToKeep = outdatedFilesToKeep;
        // Do not run on config servers
        if (configServers.contains(ConfigUtils.getCanonicalHostName())) {
            log.log(INFO, "Not running maintainer, since this is on a config server host");
            executor = Optional.empty();
        } else {
            executor = Optional.of(new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("file references and downloads cleanup")));
            executor.get().scheduleAtFixedRate(this, interval.toSeconds(), interval.toSeconds(), TimeUnit.SECONDS);
        }
    }

    @Override
    public void run() {
        if (executor.isEmpty()) return;

        try {
            deleteUnusedFiles(fileReferencesDownloadDir);
            deleteUnusedFiles(urlDownloadDir);
        } catch (Throwable t) {
            log.log(Level.WARNING, "Deleting unused files failed. ", t);
        }
    }

    public void close() {
        executor.ifPresent(ex -> {
            ex.shutdownNow();
            try {
                if (! ex.awaitTermination(10, TimeUnit.SECONDS))
                    throw new RuntimeException("Unable to shutdown " + executor + " before timeout");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }

    private void deleteUnusedFiles(File directory) {

        File[] files = directory.listFiles();
        if (files == null) return;

        List<File> filesToDelete = filesThatCanBeDeleted(files);
        filesToDelete.forEach(fileReference -> {
            if (IOUtils.recursiveDeleteDir(fileReference))
                log.log(Level.FINE, "Deleted " + fileReference.getAbsolutePath());
            else
                log.log(Level.WARNING, "Could not delete " + fileReference.getAbsolutePath());
        });
    }

    private List<File> filesThatCanBeDeleted(File[] files) {
        Instant deleteNotUsedSinceInstant = Instant.now().minus(durationToKeepFiles);

        Set<File> filesOnDisk = new HashSet<>(List.of(files));
        log.log(Level.FINE, () -> "Files on disk: " + filesOnDisk);
        int deleteCount = Math.max(0, filesOnDisk.size() - outDatedFilesToKeep);
        var canBeDeleted = filesOnDisk
                .stream()
                .peek(file -> log.log(Level.FINE, () -> file + ":" + fileLastModifiedTime(file.toPath())))
                .filter(fileReference -> isFileLastModifiedBefore(fileReference, deleteNotUsedSinceInstant))
                .sorted(Comparator.comparing(fileReference -> fileLastModifiedTime(fileReference.toPath())))
                .toList();

        // Make sure we keep some files
        canBeDeleted = canBeDeleted.subList(0, Math.min(canBeDeleted.size(), deleteCount));
        if (canBeDeleted.size() > 0)
            log.log(INFO, "Files that can be deleted (not accessed since " + deleteNotUsedSinceInstant +
                    ", will also keep " + outDatedFilesToKeep +
                    " no matter when last accessed): " + canBeDeleted);

        return canBeDeleted;
    }

    private boolean isFileLastModifiedBefore(File fileReference, Instant instant) {
        return fileLastModifiedTime(fileReference.toPath()).isBefore(instant);
    }

    private static Instant fileLastModifiedTime(Path fileReference) {
        try {
            BasicFileAttributes fileAttributes = readAttributes(fileReference, BasicFileAttributes.class);
            return fileAttributes.lastModifiedTime().toInstant();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private static Duration keepFileReferencesDuration() {
        String env = System.getenv("VESPA_KEEP_FILE_REFERENCES_DAYS");
        if (env != null && !env.isEmpty())
            return Duration.ofDays(Integer.parseInt(env));
        else
            return defaultDurationToKeepFiles;
    }

    private static int outDatedFilesToKeep() {
        String env = System.getenv("VESPA_KEEP_FILE_REFERENCES_COUNT");
        if (env != null && !env.isEmpty())
            return Integer.parseInt(env);
        else
            return defaultOutdatedFilesToKeep;
    }

    private static List<String> configServers() {
        String env = System.getenv("VESPA_CONFIGSERVERS");
        if (env == null || env.isEmpty())
            return List.of(ConfigUtils.getCanonicalHostName());
        else {
            return List.of(env.split(","));
        }
    }

}