From 8a9eff39195d5fbce3f9eb435b8e9b85cc97a6b1 Mon Sep 17 00:00:00 2001 From: Chris Nauroth Date: Mon, 14 Apr 2014 04:46:26 +0000 Subject: [PATCH] HDFS-6238. TestDirectoryScanner leaks file descriptors. Contributed by Chris Nauroth. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1587148 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 ++ .../server/datanode/TestDirectoryScanner.java | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 8f74fd6acc4..40d35d9037d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -341,6 +341,8 @@ Release 2.5.0 - UNRELEASED HDFS-6237. TestDFSShell#testGet fails on Windows due to invalid file system path. (cnauroth) + HDFS-6238. TestDirectoryScanner leaks file descriptors. (cnauroth) + Release 2.4.1 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java index d1a82ef5061..05924acc5af 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDirectoryScanner.java @@ -46,6 +46,7 @@ import org.apache.hadoop.hdfs.server.common.GenerationStamp; import org.apache.hadoop.hdfs.server.datanode.fsdataset.FsDatasetSpi; import org.apache.hadoop.hdfs.server.datanode.fsdataset.FsVolumeSpi; import org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetTestUtil; +import org.apache.hadoop.io.IOUtils; import org.junit.Test; /** @@ -85,11 +86,17 @@ public class TestDirectoryScanner { File mf = b.getMetaFile(); // Truncate a block file that has a corresponding metadata file if (f.exists() && f.length() != 0 && mf.exists()) { - FileOutputStream s = new FileOutputStream(f); - FileChannel channel = s.getChannel(); - channel.truncate(0); - LOG.info("Truncated block file " + f.getAbsolutePath()); - return b.getBlockId(); + FileOutputStream s = null; + FileChannel channel = null; + try { + s = new FileOutputStream(f); + channel = s.getChannel(); + channel.truncate(0); + LOG.info("Truncated block file " + f.getAbsolutePath()); + return b.getBlockId(); + } finally { + IOUtils.cleanup(LOG, channel, s); + } } } }