aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/maintenance/Maintainer.java
blob: 56ad4de5b31574017ef9d001f341159ed9982a64 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.maintenance;

import com.google.gson.Gson;
import com.yahoo.io.IOUtils;
import com.yahoo.log.LogSetup;
import com.yahoo.vespa.hosted.dockerapi.ContainerName;
import com.yahoo.vespa.hosted.dockerapi.ProcessResult;
import com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec;
import com.yahoo.vespa.hosted.node.admin.util.Environment;
import com.yahoo.vespa.hosted.node.admin.util.PrefixLogger;
import io.airlift.airline.Arguments;
import io.airlift.airline.Cli;
import io.airlift.airline.Command;
import io.airlift.airline.Help;
import io.airlift.airline.Option;
import io.airlift.airline.ParseArgumentsUnexpectedException;
import io.airlift.airline.ParseOptionMissingException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
 * @author freva
 */
public class Maintainer {
    private static final Path ROOT = Paths.get("/");
    private static final Path RELATIVE_APPLICATION_STORAGE_PATH = Paths.get("home/docker/container-storage");
    private static final Path APPLICATION_STORAGE_PATH_FOR_NODE_ADMIN = Paths.get("/host").resolve(RELATIVE_APPLICATION_STORAGE_PATH);
    private static final Path APPLICATION_STORAGE_PATH_FOR_HOST = ROOT.resolve(RELATIVE_APPLICATION_STORAGE_PATH);
    private static final String APPLICATION_STORAGE_CLEANUP_PATH_PREFIX = "cleanup_";

    private static final HttpClient HTTP_CLIENT = HttpClientBuilder.create().build();
    private static final CoreCollector CORE_COLLECTOR = new CoreCollector();
    private static final Gson gson = new Gson();

    private static DateFormat filenameFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

    private static final String JOB_DELETE_OLD_APP_DATA = "delete-old-app-data";
    private static final String JOB_ARCHIVE_APP_DATA = "archive-app-data";
    private static final String JOB_CLEAN_CORE_DUMPS = "clean-core-dumps";
    private static final String JOB_CLEAN_HOME = "clean-home";
    private static final String JOB_HANDLE_CORE_DUMPS = "handle-core-dumps";

    private static Optional<String> kernelVersion = Optional.empty();

    static {
        filenameFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        LogSetup.initVespaLogging(Maintainer.class.getSimpleName().toLowerCase());

        Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("maintainer.jar")
                .withDescription("This tool makes it easy to delete old log files and other node-admin app data.")
                .withDefaultCommand(Help.class)
                .withCommands(Help.class,
                        DeleteOldAppDataArguments.class,
                        CleanCoreDumpsArguments.class,
                        CleanHomeArguments.class,
                        ArchiveApplicationData.class,
                        HandleCoreDumpsForContainer.class);

        Cli<Runnable> gitParser = builder.build();
        try {
            gitParser.parse(args).run();
        } catch (ParseArgumentsUnexpectedException | ParseOptionMissingException e) {
            System.err.println(e.getMessage());
            gitParser.parse("help").run();
        }
    }

    public static void cleanCoreDumps(PrefixLogger logger) {
        executeMaintainer(logger, JOB_CLEAN_CORE_DUMPS);
    }

    public static void deleteOldAppData(PrefixLogger logger) {
        executeMaintainer(logger, JOB_DELETE_OLD_APP_DATA);
    }

    public static void cleanHome(PrefixLogger logger) {
        executeMaintainer(logger, JOB_CLEAN_HOME);
    }

    public static void archiveAppData(PrefixLogger logger, ContainerName containerName) {
        executeMaintainer(logger, JOB_ARCHIVE_APP_DATA, containerName.asString());
    }

    public static void handleCoreDumpsForContainer(PrefixLogger logger, ContainerNodeSpec nodeSpec, Environment environment) {
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("hostname", nodeSpec.hostname);
        attributes.put("region", environment.getRegion());
        attributes.put("environment", environment.getEnvironment());
        attributes.put("flavor", nodeSpec.nodeFlavor);
        try {
            attributes.put("kernel_version", getKernelVersion());
        } catch (Throwable ignored) {
            attributes.put("kernel_version", "unknown");
        }

        if (nodeSpec.wantedDockerImage.isPresent()) attributes.put("docker_image", nodeSpec.wantedDockerImage.get().asString());
        if (nodeSpec.vespaVersion.isPresent()) attributes.put("vespa_version", nodeSpec.vespaVersion.get());
        if (nodeSpec.owner.isPresent()) {
            attributes.put("tenant", nodeSpec.owner.get().tenant);
            attributes.put("application", nodeSpec.owner.get().application);
            attributes.put("instance", nodeSpec.owner.get().instance);
        }

        executeMaintainer(logger, JOB_HANDLE_CORE_DUMPS,
                "--container", nodeSpec.containerName.asString(),
                "--attributes", gson.toJson(attributes));
    }

    private static void executeMaintainer(PrefixLogger logger, String... params) {
        String[] baseArguments = {"sudo", "/home/y/libexec/vespa/node-admin/maintenance.sh"};
        String[] args = concatenateArrays(baseArguments, params);
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Map<String, String> env = processBuilder.environment();
        env.put("VESPA_SERVICE_NAME", "maintainer");

        try {
            ProcessResult result = exec(args);

            if (! result.getOutput().isEmpty()) logger.info(result.getOutput());
            if (! result.getErrors().isEmpty()) logger.error(result.getErrors());
        } catch (IOException | InterruptedException e) {
            logger.warning("Failed to execute command " + Arrays.toString(args), e);
        }
    }

    public static ProcessResult exec(String... args) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder(args);
        Process process = processBuilder.start();
        String output = IOUtils.readAll(new InputStreamReader(process.getInputStream()));
        String errors = IOUtils.readAll(new InputStreamReader(process.getErrorStream()));

        return new ProcessResult(process.waitFor(), output, errors);
    }

    public static String[] concatenateArrays(String[] ar1, String... ar2) {
        String[] concatenated = new String[ar1.length + ar2.length];
        System.arraycopy(ar1, 0, concatenated, 0, ar1.length);
        System.arraycopy(ar2, 0, concatenated, ar1.length, ar2.length);
        return concatenated;
    }

    @Command(name = JOB_DELETE_OLD_APP_DATA, description = "Deletes old app data")
    public static class DeleteOldAppDataArguments implements Runnable {
        @Override
        public void run() {
            String path = APPLICATION_STORAGE_PATH_FOR_NODE_ADMIN.toString();
            String regex = "^" + Pattern.quote(APPLICATION_STORAGE_CLEANUP_PATH_PREFIX);

            DeleteOldAppData.deleteDirectories(path, Duration.ofDays(7).getSeconds(), regex);
        }
    }

    @Command(name = JOB_CLEAN_CORE_DUMPS, description = "Clean core dumps")
    public static class CleanCoreDumpsArguments implements Runnable {
        @Override
        public void run() {
            File coreDumpsDir = new File("/home/y/var/crash");

            if (coreDumpsDir.exists()) {
                DeleteOldAppData.deleteFilesExceptNMostRecent(coreDumpsDir.getAbsolutePath(), 1);
            }
        }
    }

    @Command(name = JOB_CLEAN_HOME, description = "Clean home directories for large files")
    public static class CleanHomeArguments implements Runnable {
        @Override
        public void run() {
            List<String> exceptions = Arrays.asList("docker", "y", "yahoo");
            File homeDir = new File("/home/");
            long MB = 1 << 20;

            if (homeDir.exists() && homeDir.isDirectory()) {
                for (File file : homeDir.listFiles()) {
                    if (! exceptions.contains(file.getName())) {
                        DeleteOldAppData.deleteFilesLargerThan(file, 100*MB);
                    }
                }
            }
        }
    }

    @Command(name = JOB_ARCHIVE_APP_DATA, description = "Move container's container-storage to cleanup")
    public static class ArchiveApplicationData implements Runnable {
        @Arguments(description = "Name of container to archive (required)")
        public String container;

        @Override
        public void run() {
            if (container == null) {
                throw new IllegalArgumentException("<container> is required");
            }
            // Note that ContainerName verifies the name, so it cannot
            // contain / or be equal to "." or "..".
            ContainerName containerName = new ContainerName(container);

            Logger logger = Logger.getLogger(ArchiveApplicationData.class.getName());
            Maintainer maintainer = new Maintainer();

            File yVarDir = maintainer.pathInNodeAdminFromPathInNode(containerName, "/home/y/var").toFile();
            if (yVarDir.exists()) {
                logger.info("Recursively deleting " + yVarDir);
                try {
                    DeleteOldAppData.recursiveDelete(yVarDir);
                } catch (IOException e) {
                    throw new RuntimeException("Failed to delete " + yVarDir, e);
                }
            }

            Path from = maintainer.pathInNodeAdminFromPathInNode(containerName, "/");
            if (!Files.exists(from)) {
                logger.info("The container storage at " + from + " doesn't exist");
                return;
            }

            Path to = maintainer.pathInNodeAdminToNodeCleanup(containerName);
            logger.info("Moving container storage from " + from + " to " + to);
            try {
                Files.move(from, to);
            } catch (IOException e) {
                throw new RuntimeException("Failed to move " + from + " to " + to, e);
            }
        }
    }

    @SuppressWarnings("unchecked")
    @Command(name = JOB_HANDLE_CORE_DUMPS, description = "Finds container's coredumps, collects metadata and reports them")
    public static class HandleCoreDumpsForContainer implements Runnable {
        @Option(name = "--container", description = "Name of the container")
        public String container;

        @Option(name = "--attributes", description = "Comma separated key=value pairs")
        public String attributes;

        @Override
        public void run() {
            Logger logger = Logger.getLogger(HandleCoreDumpsForContainer.class.getName());

            if (container == null) {
                throw new IllegalArgumentException("<container> is required");
            }

            try {
                Map<String, Object> attributesMap = (Map<String, Object>) gson.fromJson(attributes, Map.class);

                Path path = new Maintainer().pathInNodeAdminFromPathInNode(new ContainerName(container), "/home/y/var/crash");
                CoredumpHandler coredumpHandler = new CoredumpHandler(HTTP_CLIENT, CORE_COLLECTOR, path, attributesMap);
                coredumpHandler.processAll();
            } catch (Throwable e) {
                logger.log(Level.WARNING, "Could not process coredumps", e);
            }
        }
    }


    /**
     * Absolute path in node admin container to the node cleanup directory.
     */
    public Path pathInNodeAdminToNodeCleanup(ContainerName containerName) {
        return APPLICATION_STORAGE_PATH_FOR_NODE_ADMIN.resolve(APPLICATION_STORAGE_CLEANUP_PATH_PREFIX +
                containerName.asString() + "_" + filenameFormatter.format(Date.from(Instant.now())));
    }

    /**
     * Translates an absolute path in node agent container to an absolute path in node admin container.
     * @param containerName name of the node agent container
     * @param absolutePathInNode absolute path in that container
     * @return the absolute path in node admin container pointing at the same inode
     */
    public Path pathInNodeAdminFromPathInNode(ContainerName containerName, String absolutePathInNode) {
        Path pathInNode = Paths.get(absolutePathInNode);
        if (! pathInNode.isAbsolute()) {
            throw new IllegalArgumentException("The specified path in node was not absolute: " + absolutePathInNode);
        }

        return APPLICATION_STORAGE_PATH_FOR_NODE_ADMIN
                .resolve(containerName.asString())
                .resolve(ROOT.relativize(pathInNode));
    }

    /**
     * Translates an absolute path in node agent container to an absolute path in host.
     * @param containerName name of the node agent container
     * @param absolutePathInNode absolute path in that container
     * @return the absolute path in host pointing at the same inode
     */
    public Path pathInHostFromPathInNode(ContainerName containerName, String absolutePathInNode) {
        Path pathInNode = Paths.get(absolutePathInNode);
        if (! pathInNode.isAbsolute()) {
            throw new IllegalArgumentException("The specified path in node was not absolute: " + absolutePathInNode);
        }

        return APPLICATION_STORAGE_PATH_FOR_HOST
                .resolve(containerName.asString())
                .resolve(ROOT.relativize(pathInNode));
    }

    public static String getKernelVersion() throws IOException, InterruptedException {
        if (! kernelVersion.isPresent()) {
            ProcessResult result = exec("uname", "-r");
            if (result.isSuccess()) {
                kernelVersion = Optional.of(result.getOutput().trim());
            } else {
                throw new RuntimeException("Failed to get kernel version\n" + result);
            }
        }

        return kernelVersion.get();
    }
}