Skip to content

Commit

Permalink
HDFS-5356. MiniDFSCluster should close all open FileSystems when shut…
Browse files Browse the repository at this point in the history
…down() (Contributed by Rakesh R)
  • Loading branch information
vinayakumarb committed Mar 17, 2015
1 parent e537047 commit 018893e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt
Expand Up @@ -327,6 +327,9 @@ Release 2.8.0 - UNRELEASED

BUG FIXES

HDFS-5356. MiniDFSCluster should close all open FileSystems when shutdown()
(Rakesh R via vinayakumarb)

Release 2.7.0 - UNRELEASED

INCOMPATIBLE CHANGES
Expand Down
Expand Up @@ -60,6 +60,7 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -118,6 +119,7 @@
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
* This class creates a single-process DFS cluster for junit testing.
Expand Down Expand Up @@ -523,7 +525,8 @@ public void setDnArgs(String ... args) {
private boolean federation;
private boolean checkExitOnShutdown = true;
protected final int storagesPerDatanode;

private Set<FileSystem> fileSystems = Sets.newHashSet();

/**
* A unique instance identifier for the cluster. This
* is used to disambiguate HA filesystems in the case where
Expand Down Expand Up @@ -1705,6 +1708,13 @@ public void shutdown() {
* Shutdown all the nodes in the cluster.
*/
public void shutdown(boolean deleteDfsDir) {
shutdown(deleteDfsDir, true);
}

/**
* Shutdown all the nodes in the cluster.
*/
public void shutdown(boolean deleteDfsDir, boolean closeFileSystem) {
LOG.info("Shutting down the Mini HDFS Cluster");
if (checkExitOnShutdown) {
if (ExitUtil.terminateCalled()) {
Expand All @@ -1714,6 +1724,16 @@ public void shutdown(boolean deleteDfsDir) {
throw new AssertionError("Test resulted in an unexpected exit");
}
}
if (closeFileSystem) {
for (FileSystem fs : fileSystems) {
try {
fs.close();
} catch (IOException ioe) {
LOG.warn("Exception while closing file system", ioe);
}
}
fileSystems.clear();
}
shutdownDataNodes();
for (NameNodeInfo nnInfo : nameNodes) {
if (nnInfo == null) continue;
Expand Down Expand Up @@ -2144,16 +2164,20 @@ public DistributedFileSystem getFileSystem() throws IOException {
* Get a client handle to the DFS cluster for the namenode at given index.
*/
public DistributedFileSystem getFileSystem(int nnIndex) throws IOException {
return (DistributedFileSystem)FileSystem.get(getURI(nnIndex),
nameNodes[nnIndex].conf);
DistributedFileSystem dfs = (DistributedFileSystem) FileSystem.get(
getURI(nnIndex), nameNodes[nnIndex].conf);
fileSystems.add(dfs);
return dfs;
}

/**
* Get another FileSystem instance that is different from FileSystem.get(conf).
* This simulating different threads working on different FileSystem instances.
*/
public FileSystem getNewFileSystemInstance(int nnIndex) throws IOException {
return FileSystem.newInstance(getURI(nnIndex), nameNodes[nnIndex].conf);
FileSystem dfs = FileSystem.newInstance(getURI(nnIndex), nameNodes[nnIndex].conf);
fileSystems.add(dfs);
return dfs;
}

/**
Expand Down
Expand Up @@ -675,7 +675,7 @@ public void testFileCreationNamenodeRestart() throws IOException {

// restart cluster with the same namenode port as before.
// This ensures that leases are persisted in fsimage.
cluster.shutdown();
cluster.shutdown(false, false);
try {
Thread.sleep(2*MAX_IDLE_TIME);
} catch (InterruptedException e) {
Expand All @@ -687,7 +687,7 @@ public void testFileCreationNamenodeRestart() throws IOException {

// restart cluster yet again. This triggers the code to read in
// persistent leases from fsimage.
cluster.shutdown();
cluster.shutdown(false, false);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Expand Down
Expand Up @@ -519,8 +519,8 @@ private void restartClusterAndCheckImage(boolean compareQuota)
File fsnAfter = new File(testDir, "dumptree_after");

SnapshotTestHelper.dumpTree2File(fsdir, fsnBefore);
cluster.shutdown();

cluster.shutdown(false, false);
cluster = new MiniDFSCluster.Builder(conf).format(false)
.numDataNodes(REPL).build();
cluster.waitActive();
Expand Down

0 comments on commit 018893e

Please sign in to comment.