final Configuration conf = new Configuration();
// create cluster
conf.setInt(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 512);
final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null);
DFSDataInputStream in = null;
FSDataOutputStream out = null;
DistributedFileSystem dfs = null;
try {
Path path = new Path(MiniDFSCluster.getBaseDir().getPath(), "test");
dfs = (DistributedFileSystem) cluster.getFileSystem();
out = dfs.create(path);
int fileLength = 1030;
out.write(new byte[fileLength]);
out.sync();
cluster.restartNameNode();
cluster.waitActive();
in = (DFSDataInputStream) dfs.open(path, 1024);
// Verify the length when we just restart NN. DNs will register
// immediately.
Assert.assertEquals(fileLength, in.getVisibleLength());
cluster.shutdownDataNodes();
cluster.restartNameNode(false);
// This is just for ensuring NN started.
verifyNNIsInSafeMode(dfs);
try {
in = (DFSDataInputStream) dfs.open(path);
Assert.fail("Expected IOException");
} catch (IOException e) {
Assert.assertTrue(e.getLocalizedMessage().indexOf(
"Name node is in safe mode") >= 0);
}
} finally {
if (null != in) {
in.close();
}
if (null != dfs) {
dfs.dfs.clientRunning = false;
}
cluster.shutdown();