YARN-10976. Fix resource leak due to Files.walk (#3552)

Signed-off-by: Akira Ajisaka <aajisaka@apache.org>
This commit is contained in:
lujiefsi 2021-10-18 14:24:15 +08:00 committed by GitHub
parent d09b380dc2
commit ae95caa60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import java.nio.file.Paths;
import java.util.Set; import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.hadoop.util.Shell; import org.apache.hadoop.util.Shell;
@ -58,12 +59,12 @@ class VEDeviceDiscoverer {
public Set<Device> getDevicesFromPath(String path) throws IOException { public Set<Device> getDevicesFromPath(String path) throws IOException {
MutableInt counter = new MutableInt(0); MutableInt counter = new MutableInt(0);
try (Stream<Path> stream = Files.walk(Paths.get(path), 1)) {
return Files.walk(Paths.get(path), 1) return stream.filter(p -> p.toFile().getName().startsWith("veslot"))
.filter(p -> p.toFile().getName().startsWith("veslot"))
.map(p -> toDevice(p, counter)) .map(p -> toDevice(p, counter))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
}
private Device toDevice(Path p, MutableInt counter) { private Device toDevice(Path p, MutableInt counter) {
CommandExecutor executor = CommandExecutor executor =