blob: 54cf932490954379acd45cbf2af856c0ef290183 (
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
|
// Copyright Vespa.ai. 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.component.TaskContext;
import com.yahoo.vespa.hosted.node.admin.task.util.file.DiskSize;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;
import static com.yahoo.vespa.hosted.node.admin.maintenance.disk.DiskCleanupRule.PrioritizedFileAttributes;
import static com.yahoo.yolean.Exceptions.uncheck;
/**
* @author freva
*/
public class DiskCleanup {
private static final Logger logger = Logger.getLogger(DiskCleanup.class.getName());
private static final Comparator<PrioritizedFileAttributes> PRIORITIZED_FILE_ATTRIBUTES_COMPARATOR = Comparator
.comparing(PrioritizedFileAttributes::priority)
.thenComparingLong(f -> f.fileAttributes().size())
.reversed();
public boolean cleanup(TaskContext context, List<DiskCleanupRule> rules, long bytesToRemove) {
if (bytesToRemove <= 0) return false;
long[] btr = new long[] { bytesToRemove };
List<Path> deletedPaths = new ArrayList<>();
try {
rules.stream()
.flatMap(rule -> rule.prioritize().stream())
.sorted(PRIORITIZED_FILE_ATTRIBUTES_COMPARATOR)
.takeWhile(fa -> btr[0] > 0)
.forEach(pfa -> {
if (uncheck(() -> Files.deleteIfExists(pfa.fileAttributes().path()))) {
btr[0] -= pfa.fileAttributes().size();
deletedPaths.add(pfa.fileAttributes().path());
}
});
} finally {
String wantedDeleteSize = DiskSize.of(bytesToRemove).asString();
String deletedSize = DiskSize.of(bytesToRemove - btr[0]).asString();
if (deletedPaths.size() > 20) {
context.log(logger, "Deleted %d files (%s) because disk was getting full", deletedPaths.size(), deletedSize);
} else if (deletedPaths.size() > 0) {
context.log(logger, "Deleted %s because disk was getting full from: %s", deletedSize, deletedPaths);
} else {
context.log(logger, "Wanted to delete %s, but failed to find any files to delete", wantedDeleteSize);
}
}
return !deletedPaths.isEmpty();
}
}
|