From 25882b199b586633cca5c092b9fd8b015cb59476 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Thu, 3 May 2012 21:13:43 +0000 Subject: [PATCH] HDFS-3359. DFSClient.close should close cached sockets. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1333624 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 ++ .../java/org/apache/hadoop/hdfs/DFSClient.java | 2 ++ .../hadoop/hdfs/TestDistributedFileSystem.java | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 18b9378980..913684a997 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -592,6 +592,8 @@ Release 2.0.0 - UNRELEASED HDFS-3351. NameNode#initializeGenericKeys should always set fs.defaultFS regardless of whether HA or Federation is enabled. (atm) + HDFS-3359. DFSClient.close should close cached sockets. (todd) + BREAKDOWN OF HDFS-1623 SUBTASKS HDFS-2179. Add fencing framework and mechanisms for NameNode HA. (todd) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java index 43b1ba6fb8..969b058128 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSClient.java @@ -560,6 +560,7 @@ void closeConnectionToNamenode() { void abort() { clientRunning = false; closeAllFilesBeingWritten(true); + socketCache.clear(); closeConnectionToNamenode(); } @@ -597,6 +598,7 @@ private void closeAllFilesBeingWritten(final boolean abort) { public synchronized void close() throws IOException { if(clientRunning) { closeAllFilesBeingWritten(false); + socketCache.clear(); clientRunning = false; leaserenewer.closeClient(this); // close connections to the namenode diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java index 4055cd8d3d..a46a56b92d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDistributedFileSystem.java @@ -85,6 +85,7 @@ public void testFileSystemCloseAll() throws Exception { /** * Tests DFSClient.close throws no ConcurrentModificationException if * multiple files are open. + * Also tests that any cached sockets are closed. (HDFS-3359) */ @Test public void testDFSClose() throws Exception { @@ -94,11 +95,23 @@ public void testDFSClose() throws Exception { cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build(); FileSystem fileSys = cluster.getFileSystem(); - // create two files + // create two files, leaving them open fileSys.create(new Path("/test/dfsclose/file-0")); fileSys.create(new Path("/test/dfsclose/file-1")); + + // create another file, close it, and read it, so + // the client gets a socket in its SocketCache + Path p = new Path("/non-empty-file"); + DFSTestUtil.createFile(fileSys, p, 1L, (short)1, 0L); + DFSTestUtil.readFile(fileSys, p); + + DFSClient client = ((DistributedFileSystem)fileSys).dfs; + SocketCache cache = client.socketCache; + assertEquals(1, cache.size()); fileSys.close(); + + assertEquals(0, cache.size()); } finally { if (cluster != null) {cluster.shutdown();} }