aboutsummaryrefslogtreecommitdiffstats
path: root/config-proxy/src/test/java/com/yahoo/vespa/config/proxy/filedistribution/FileReferencesAndDownloadsMaintainerTest.java
blob: c41305b4dc86214e53a86833b9073659c81522fa (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
// 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.io.IOUtils;
import com.yahoo.vespa.config.util.ConfigUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author hmusum
 */
public class FileReferencesAndDownloadsMaintainerTest {

    private static final Duration keepDuration = Duration.ofMinutes(1);
    private static final int outDatedFilesToKeep = 9;

    private File cachedFileReferences;
    private File cachedDownloads;
    private FileReferencesAndDownloadsMaintainer maintainer;

    @TempDir
    public File tempFolder;

    @BeforeEach
    public void setup() throws IOException {
        cachedFileReferences = newFolder(tempFolder, "cachedFileReferences");
        cachedDownloads = newFolder(tempFolder, "cachedDownloads");
    }

    @Test
    void require_old_files_to_be_deleted() {
        maintainer = new FileReferencesAndDownloadsMaintainer(cachedFileReferences, cachedDownloads, keepDuration, outDatedFilesToKeep,
                                                              List.of("host1"));
        runMaintainerAndAssertFiles(0, 0);

        var fileReferences = writeFiles(20);
        var downloads = writeDownloads(21);
        runMaintainerAndAssertFiles(20, 21);

        updateLastModifiedTimestamp(0, 5, fileReferences, downloads);
        runMaintainerAndAssertFiles(15, 16);

        updateLastModifiedTimestamp(6, 20, fileReferences, downloads);
        // Should keep at least outDatedFilesToKeep file references and downloads even if there are more that are old
        runMaintainerAndAssertFiles(outDatedFilesToKeep, outDatedFilesToKeep);
    }

    @Test
    void require_no_files_deleted_when_running_on_config_server_host() {
        maintainer = new FileReferencesAndDownloadsMaintainer(cachedFileReferences, cachedDownloads, keepDuration,
                                                              outDatedFilesToKeep, List.of(ConfigUtils.getCanonicalHostName()));
        runMaintainerAndAssertFiles(0, 0);

        var fileReferences = writeFiles(10);
        var downloads = writeDownloads(10);
        runMaintainerAndAssertFiles(10, 10);

        updateLastModifiedTimestamp(0, 10, fileReferences, downloads);
        runMaintainerAndAssertFiles(10, 10);
    }

    private void updateLastModifiedTimestamp(int startInclusive, int endExclusive, List<File> fileReferences, List<File> downloads) {
        IntStream.range(startInclusive, endExclusive).forEach(i -> {
            Instant instant = Instant.now().minus(keepDuration.plus(Duration.ofMinutes(1)).minus(Duration.ofSeconds(i)));
            updateLastModifiedTimeStamp(fileReferences.get(i), instant);
            updateLastModifiedTimeStamp(downloads.get(i), instant);
        });
    }

    private List<File> writeFiles(int count) {
        List<File> files = new ArrayList<>();
        IntStream.range(0, count).forEach(i -> {
            try {
                files.add(writeFile(cachedFileReferences, "fileReference" + i));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        return files;
    }

    private List<File> writeDownloads(int count) {
        List<File> files = new ArrayList<>();
        IntStream.range(0, count).forEach(i -> {
            try {
                files.add(writeFile(cachedDownloads, "download" + i));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        return files;
    }

    private void updateLastModifiedTimeStamp(File file, Instant instant) {
        if (!file.setLastModified(instant.toEpochMilli())) {
            throw new RuntimeException("Could not set last modified timestamp for '" + file.getAbsolutePath() + "'");
        }
    }

    private void runMaintainerAndAssertFiles(int fileReferenceCount, int downloadCount) {
        maintainer.run();
        File[] fileReferences = cachedFileReferences.listFiles();
        assertNotNull(fileReferences);
        assertEquals(fileReferenceCount, fileReferences.length);

        File[] downloads = cachedDownloads.listFiles();
        assertNotNull(downloads);
        assertEquals(downloadCount, downloads.length);
    }

    private File writeFile(File directory, String filename) throws IOException {
        File file = new File(directory, filename);
        IOUtils.writeFile(file, filename, false);
        return file;
    }

    private static File newFolder(File root, String... subDirs) throws IOException {
        String subFolder = String.join("/", subDirs);
        File result = new File(root, subFolder);
        if (!result.mkdirs()) {
            throw new IOException("Couldn't create folders " + root);
        }
        return result;
    }

}