diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2018-06-07 12:42:59 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@oath.com> | 2018-06-07 12:42:59 +0200 |
commit | b3581e68eefd554478eb32606289ba2c8ea73a4f (patch) | |
tree | 7f9b0cd93f15520acaf626cfc49a386b88788bb7 /node-maintainer | |
parent | 3de8187e910c4cfff1c14e504d9edb4bd326e4c9 (diff) |
Always close Files.list()
Diffstat (limited to 'node-maintainer')
-rw-r--r-- | node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java | 14 | ||||
-rw-r--r-- | node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/FileHelper.java | 12 |
2 files changed, 15 insertions, 11 deletions
diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java index 99dfdb48334..63c74c17dd5 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java @@ -72,7 +72,7 @@ class CoredumpHandler { FileHelper.deleteDirectories(doneCoredumpsPath, Duration.ofDays(10), Optional.empty()); } - private void handleNewCoredumps() throws IOException { + private void handleNewCoredumps() { Path processingCoredumps = enqueueCoredumps(); processAndReportCoredumps(processingCoredumps); } @@ -82,12 +82,12 @@ class CoredumpHandler { * Moves a coredump to a new directory under the processing/ directory. Limit to only processing * one coredump at the time, starting with the oldest. */ - Path enqueueCoredumps() throws IOException { + Path enqueueCoredumps() { Path processingCoredumpsPath = coredumpsPath.resolve(PROCESSING_DIRECTORY_NAME); processingCoredumpsPath.toFile().mkdirs(); - if (Files.list(processingCoredumpsPath).count() > 0) return processingCoredumpsPath; + if (!FileHelper.listContentsOfDirectory(processingCoredumpsPath).isEmpty()) return processingCoredumpsPath; - Files.list(coredumpsPath) + FileHelper.listContentsOfDirectory(coredumpsPath).stream() .filter(path -> path.toFile().isFile() && ! path.getFileName().toString().startsWith(".")) .min((Comparator.comparingLong(o -> o.toFile().lastModified()))) .ifPresent(coredumpPath -> { @@ -101,10 +101,10 @@ class CoredumpHandler { return processingCoredumpsPath; } - void processAndReportCoredumps(Path processingCoredumpsPath) throws IOException { + void processAndReportCoredumps(Path processingCoredumpsPath) { doneCoredumpsPath.toFile().mkdirs(); - Files.list(processingCoredumpsPath) + FileHelper.listContentsOfDirectory(processingCoredumpsPath).stream() .filter(path -> path.toFile().isDirectory()) .forEach(coredumpDirectory -> { try { @@ -130,7 +130,7 @@ class CoredumpHandler { String collectMetadata(Path coredumpDirectory, Map<String, Object> nodeAttributes) throws IOException { Path metadataPath = coredumpDirectory.resolve(METADATA_FILE_NAME); if (!Files.exists(metadataPath)) { - Path coredumpPath = Files.list(coredumpDirectory).findFirst() + Path coredumpPath = FileHelper.listContentsOfDirectory(coredumpDirectory).stream().findFirst() .orElseThrow(() -> new RuntimeException("No coredump file found in processing directory " + coredumpDirectory)); Map<String, Object> metadata = coreCollector.collect(coredumpPath, installStatePath); metadata.putAll(nodeAttributes); diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/FileHelper.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/FileHelper.java index ae872042853..7b93e7ad98d 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/FileHelper.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/FileHelper.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.node.maintainer; import java.io.IOException; +import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.NoSuchFileException; @@ -63,7 +64,7 @@ public class FileHelper { throw new IllegalArgumentException("Number of files to keep must be a positive number"); } - List<Path> pathsInDeleteDir = Files.list(basePath) + List<Path> pathsInDeleteDir = listContentsOfDirectory(basePath).stream() .filter(Files::isRegularFile) .sorted(Comparator.comparing(FileHelper::getLastModifiedTime)) .skip(nMostRecentToKeep) @@ -153,13 +154,16 @@ public class FileHelper { return pattern == null || pattern.matcher(path.getFileName().toString()).find(); } - static List<Path> listContentsOfDirectory(Path basePath) { + /** + * @return list all files in a directory, returns empty list if directory does not exist + */ + public static List<Path> listContentsOfDirectory(Path basePath) { try (Stream<Path> directoryStream = Files.list(basePath)) { return directoryStream.collect(Collectors.toList()); } catch (NoSuchFileException ignored) { return Collections.emptyList(); } catch (IOException e) { - throw new RuntimeException("Failed to list contents of directory " + basePath.toAbsolutePath(), e); + throw new UncheckedIOException("Failed to list contents of directory " + basePath.toAbsolutePath(), e); } } @@ -167,7 +171,7 @@ public class FileHelper { try { return Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS); } catch (IOException e) { - throw new RuntimeException("Failed to get last modified time of " + path.toAbsolutePath(), e); + throw new UncheckedIOException("Failed to get last modified time of " + path.toAbsolutePath(), e); } } } |