summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/servicedump/AbstractProducer.java
blob: a1416d3274cc197e2c10b51b14f077a4c066bc3a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.maintenance.servicedump;

import com.yahoo.vespa.hosted.node.admin.container.ContainerOperations;
import com.yahoo.vespa.hosted.node.admin.nodeagent.NodeAgentContext;
import com.yahoo.vespa.hosted.node.admin.task.util.fs.ContainerPath;
import com.yahoo.vespa.hosted.node.admin.task.util.process.CommandResult;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * @author bjorncs
 */
abstract class AbstractProducer implements ArtifactProducer {

    private final Logger log = Logger.getLogger(getClass().getName());

    private final ContainerOperations container;

    protected AbstractProducer(ContainerOperations container) { this.container = container; }

    protected ContainerOperations container() { return container; }

    protected CommandResult executeCommand(NodeAgentContext ctx, List<String> command, boolean logOutput) throws IOException {
        CommandResult result = container.executeCommandInContainerAsRoot(ctx, command.toArray(new String[0]));
        String cmdString = command.stream().map(s -> "'" + s + "'").collect(Collectors.joining(" ", "\"", "\""));
        int exitCode = result.getExitCode();
        String output = result.getOutput().trim();
        String prefixedOutput = output.contains("\n")
                ? "\n" + output
                : (output.isEmpty() ? "<no output>" : output);
        if (exitCode > 0) {
            String errorMsg = logOutput
                    ? String.format("Failed to execute %s (exited with code %d): %s", cmdString, exitCode, prefixedOutput)
                    : String.format("Failed to execute %s (exited with code %d)", cmdString, exitCode);
            throw new IOException(errorMsg);
        } else {
            String logMsg = logOutput
                    ? String.format("Executed command %s. Exited with code %d and output: %s", cmdString, exitCode, prefixedOutput)
                    : String.format("Executed command %s. Exited with code %d.", cmdString, exitCode);
            ctx.log(log, logMsg);
        }
        return result;
    }

    protected int findVespaServicePid(NodeAgentContext ctx, String configId) throws IOException {
        ContainerPath findPidBinary = ctx.paths().underVespaHome("libexec/vespa/find-pid");
        CommandResult findPidResult = executeCommand(ctx, List.of(findPidBinary.pathInContainer(), configId), true);
        return Integer.parseInt(findPidResult.getOutput());
    }

    protected double duration(NodeAgentContext ctx, ServiceDumpReport.DumpOptions options, double defaultValue) {
        double duration = options != null && options.duration() != null && options.duration() > 0
                ? options.duration() : defaultValue;
        double maxDuration = 300;
        if (duration > maxDuration) {
            ctx.log(log, Level.WARNING,
                    String.format("Specified duration %.3fs longer than max allowed (%.3fs)", duration, maxDuration));
            return maxDuration;
        }
        return duration;
    }

}