public void testRestartDFS() throws Exception {
final Configuration conf = new HdfsConfiguration();
MiniDFSCluster cluster = null;
FSNamesystem fsn = null;
int numNamenodeDirs;
DFSTestUtil files = new DFSTestUtil("TestRestartDFS", 200, 3, 8*1024);
final String dir = "/srcdat";
final Path rootpath = new Path("/");
final Path dirpath = new Path(dir);
long rootmtime;
FileStatus rootstatus;
FileStatus dirstatus;
try {
cluster = new MiniDFSCluster.Builder(conf).format(true)
.numDataNodes(NUM_DATANODES).build();
String[] nameNodeDirs = conf.getStrings(
DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY, new String[] {});
numNamenodeDirs = nameNodeDirs.length;
assertTrue("failed to get number of Namenode StorageDirs",
numNamenodeDirs != 0);
FileSystem fs = cluster.getFileSystem();
files.createFiles(fs, dir);
rootmtime = fs.getFileStatus(rootpath).getModificationTime();
rootstatus = fs.getFileStatus(dirpath);
dirstatus = fs.getFileStatus(dirpath);
fs.setOwner(rootpath, rootstatus.getOwner() + "_XXX", null);
fs.setOwner(dirpath, null, dirstatus.getGroup() + "_XXX");
} finally {
if (cluster != null) { cluster.shutdown(); }
}
try {
// Force the NN to save its images on startup so long as
// there are any uncheckpointed txns
conf.setInt(DFSConfigKeys.DFS_NAMENODE_CHECKPOINT_TXNS_KEY, 1);
// Here we restart the MiniDFScluster without formatting namenode
cluster = new MiniDFSCluster.Builder(conf).format(false)
.numDataNodes(NUM_DATANODES).build();
fsn = cluster.getNamesystem();
FileSystem fs = cluster.getFileSystem();
assertTrue("Filesystem corrupted after restart.",
files.checkFiles(fs, dir));
final FileStatus newrootstatus = fs.getFileStatus(rootpath);
assertEquals(rootmtime, newrootstatus.getModificationTime());
assertEquals(rootstatus.getOwner() + "_XXX", newrootstatus.getOwner());
assertEquals(rootstatus.getGroup(), newrootstatus.getGroup());
final FileStatus newdirstatus = fs.getFileStatus(dirpath);
assertEquals(dirstatus.getOwner(), newdirstatus.getOwner());
assertEquals(dirstatus.getGroup() + "_XXX", newdirstatus.getGroup());
rootmtime = fs.getFileStatus(rootpath).getModificationTime();
final String checkAfterRestart = checkImages(fsn, numNamenodeDirs);
// Modify the system and then perform saveNamespace
files.cleanup(fs, dir);
files.createFiles(fs, dir);
fsn.setSafeMode(SafeModeAction.SAFEMODE_ENTER);
cluster.getNameNodeRpc().saveNamespace();
final String checkAfterModify = checkImages(fsn, numNamenodeDirs);
assertFalse("Modified namespace should change fsimage contents. " +
"was: " + checkAfterRestart + " now: " + checkAfterModify,
checkAfterRestart.equals(checkAfterModify));
fsn.setSafeMode(SafeModeAction.SAFEMODE_LEAVE);
files.cleanup(fs, dir);
} finally {
if (cluster != null) { cluster.shutdown(); }
}
}