aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/maintenance/disk/CoredumpCleanupRule.java
blob: 102d6964ee598c2155204f2005afc03d81036d1e (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
// 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.disk;

import com.yahoo.vespa.hosted.node.admin.maintenance.coredump.CoredumpHandler;
import com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder;

import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.yahoo.vespa.hosted.node.admin.maintenance.disk.DiskCleanupRule.PrioritizedFileAttributes;
import static com.yahoo.vespa.hosted.node.admin.maintenance.disk.DiskCleanupRule.Priority;
import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.FileAttributes;
import static com.yahoo.vespa.hosted.node.admin.task.util.file.FileFinder.nameStartsWith;

/**
 * @author freva
 */
public class CoredumpCleanupRule {

    private static final Comparator<FileAttributes> CORE_DUMP_FILE_ATTRIBUTE_COMPARATOR = Comparator
            .comparing((FileAttributes fa) -> !fa.filename().contains("vespa-"))
            .thenComparing(FileAttributes::lastModifiedTime);

    public static DiskCleanupRule forContainer(Path containerCrashPath) {
        return new ContainerCoredumpCleanupRule(containerCrashPath);
    }

    public static DiskCleanupRule forHost(Path processedCoredumpsPath) {
        return new HostCoredumpCleanupRule(processedCoredumpsPath);
    }

    /** Assigns MEDIUM priority to the oldest, unprocessed coredump and HIGHEST for the remaining */
    private static class ContainerCoredumpCleanupRule implements DiskCleanupRule {
        private final Path containerCrashPath;

        private ContainerCoredumpCleanupRule(Path containerCrashPath) {
            this.containerCrashPath = containerCrashPath;
        }

        @Override
        public Collection<PrioritizedFileAttributes> prioritize() {
            List<FileAttributes> fileAttributes = FileFinder.files(containerCrashPath)
                    .maxDepth(1).stream()
                    .sorted(CORE_DUMP_FILE_ATTRIBUTE_COMPARATOR)
                    .toList();

            return mapFirstAndRemaining(fileAttributes, Priority.MEDIUM, Priority.HIGHEST).toList();
        }
    }

    /** Assigns MEDIUM priority to the first coredump of the day for each container, HIGH for the remaining */
    private static class HostCoredumpCleanupRule implements DiskCleanupRule {
        private final Path processedCoredumpsPath;

        private HostCoredumpCleanupRule(Path processedCoredumpsPath) {
            this.processedCoredumpsPath = processedCoredumpsPath;
        }

        @Override
        public Collection<PrioritizedFileAttributes> prioritize() {
            Map<String, List<FileAttributes>> fileAttributesByContainerDay = FileFinder.files(processedCoredumpsPath)
                    .match(nameStartsWith(CoredumpHandler.COREDUMP_FILENAME_PREFIX))
                    .stream()
                    .sorted(CORE_DUMP_FILE_ATTRIBUTE_COMPARATOR)
                    .collect(Collectors.groupingBy(
                            // Group FileAttributes by string [container-name]_[day of year], e.g. zt00534-v6-2_234
                            fa -> containerNameFromProcessedCoredumpPath(fa.path()) + "_" + dayOfYear(fa.lastModifiedTime()),
                            Collectors.collectingAndThen(
                                    Collectors.toCollection(ArrayList::new),
                                    l -> { l.sort(CORE_DUMP_FILE_ATTRIBUTE_COMPARATOR); return l; } )));

            return fileAttributesByContainerDay.values().stream()
                    .flatMap(fa -> mapFirstAndRemaining(fa, Priority.MEDIUM, Priority.HIGH))
                    .toList();
        }
    }

    /**
     * Maps list of FileAttributes into list of PrioritizedFileAttributes where the first FileAttribute is given
     * {@code first} priority, while the remaining FileAttributes are given {@code remaining} priority */
    private static Stream<PrioritizedFileAttributes> mapFirstAndRemaining(List<FileAttributes> fileAttributes, Priority first, Priority remaining) {
        return IntStream.range(0, fileAttributes.size())
                .mapToObj(i -> new PrioritizedFileAttributes(fileAttributes.get(i), i == 0 ? first : remaining));
    }

    /** Extracts container-name from path under processed-coredumps or empty string */
    private static String containerNameFromProcessedCoredumpPath(Path path) {
        if (path.getNameCount() < 3) return ""; // Path is too short
        return path.getName(path.getNameCount() - 3).toString();
    }

    /** Returns day number of the year (1-365 (or 366 for leap years)) */
    private static int dayOfYear(Instant instant) {
        return instant.atOffset(ZoneOffset.UTC).get(ChronoField.DAY_OF_YEAR);
    }
}