DiskCleanup { 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(); }  boolean cleanup(TaskContext context, List<DiskCleanupRule> rules, long bytesToRemove);  }
@Test public void nothing_deleted() throws IOException { assertFalse(diskCleanup.cleanup(context, List.of(), 0)); assertFalse(diskCleanup.cleanup(context, List.of(), 10)); DiskCleanupRuleMock rule1 = new DiskCleanupRuleMock(); DiskCleanupRuleMock rule2 = new DiskCleanupRuleMock(); assertFalse(diskCleanup.cleanup(context, List.of(rule1, rule2), 0)); assertFalse(diskCleanup.cleanup(context, List.of(rule1, rule2), 10)); tester.createFile("/path/that-should-not-be-deleted", 5); assertFalse(diskCleanup.cleanup(context, List.of(rule1, rule2), 10)); tester.assertAllFilesExistExcept(); rule1.addFile(tester.createFile("/path/file-does-not-exist", 1), Priority.HIGHEST); Files.delete(tester.path("/path/file-does-not-exist")); assertFalse(diskCleanup.cleanup(context, List.of(rule1, rule2), 10)); }
@Test public void delete_test() throws IOException { tester.createFile("/opt/vespa/var/db/do-not-delete-1.db", 1); tester.createFile("/opt/vespa/var/db/do-not-delete-2.db", 1); tester.createFile("/opt/vespa/var/zookeeper/do-not-delete-3", 1); tester.createFile("/opt/vespa/var/index/something-important", 1); DiskCleanupRuleMock rule1 = new DiskCleanupRuleMock() .addFile(tester.createFile("/opt/vespa/logs/vespa-1.log", 10), Priority.MEDIUM) .addFile(tester.createFile("/opt/vespa/logs/vespa-2.log", 8), Priority.HIGH) .addFile(tester.createFile("/opt/vespa/logs/vespa-3.log", 13), Priority.HIGHEST) .addFile(tester.createFile("/opt/vespa/logs/vespa-4.log", 10), Priority.HIGHEST); DiskCleanupRuleMock rule2 = new DiskCleanupRuleMock() .addFile(tester.createFile("/opt/vespa/var/crash/core1", 105), Priority.LOW) .addFile(tester.createFile("/opt/vespa/var/crash/vespa-proton-bin.core-232", 190), Priority.HIGH) .addFile(tester.createFile("/opt/vespa/var/crash/core3", 54), Priority.MEDIUM) .addFile(tester.createFile("/opt/vespa/var/crash/core4", 300), Priority.HIGH); assertTrue(diskCleanup.cleanup(context, List.of(rule1, rule2), 10)); tester.assertAllFilesExistExcept("/opt/vespa/logs/vespa-3.log"); assertTrue(diskCleanup.cleanup(context, List.of(rule1, rule2), 10)); tester.assertAllFilesExistExcept("/opt/vespa/logs/vespa-3.log", "/opt/vespa/logs/vespa-4.log"); assertTrue(diskCleanup.cleanup(context, List.of(rule1, rule2), 500)); tester.assertAllFilesExistExcept("/opt/vespa/logs/vespa-3.log", "/opt/vespa/logs/vespa-4.log", "/opt/vespa/var/crash/core4", "/opt/vespa/var/crash/vespa-proton-bin.core-232", "/opt/vespa/logs/vespa-2.log", "/opt/vespa/var/crash/core3"); }