}
public void testReplicationError() throws Exception {
// bring up a cluster of 1
Configuration conf = new Configuration();
MiniDFSCluster cluster = new MiniDFSCluster(conf, 1, true, null);
cluster.waitActive();
FileSystem fs = cluster.getFileSystem();
try {
// create a file of replication factor of 1
final Path fileName = new Path("/test.txt");
final int fileLen = 1;
DFSTestUtil.createFile(fs, fileName, 1, (short)1, 1L);
DFSTestUtil.waitReplication(fs, fileName, (short)1);
// get the block belonged to the created file
LocatedBlocks blocks = cluster.getNameNode().namesystem.getBlockLocations(
fileName.toString(), 0, (long)fileLen);
assertEquals(blocks.locatedBlockCount(), 1);
LocatedBlock block = blocks.get(0);
// bring up a second datanode
cluster.startDataNodes(conf, 1, true, null, null);
cluster.waitActive();
final int sndNode = 1;
DataNode datanode = cluster.getDataNodes().get(sndNode);
// replicate the block to the second datanode
InetSocketAddress target = datanode.getSelfAddr();
Socket s = new Socket(target.getAddress(), target.getPort());
//write the header.
DataOutputStream out = new DataOutputStream(
s.getOutputStream());
out.writeShort( DataTransferProtocol.DATA_TRANSFER_VERSION );
out.write( DataTransferProtocol.OP_WRITE_BLOCK );
out.writeLong( block.getBlock().getBlockId());
out.writeLong( block.getBlock().getGenerationStamp() );
out.writeInt(1);
out.writeBoolean( false ); // recovery flag
Text.writeString( out, "" );
out.writeBoolean(false); // Not sending src node information
out.writeInt(0);
BlockTokenSecretManager.DUMMY_TOKEN.write(out);
// write check header
out.writeByte( 1 );
out.writeInt( 512 );
out.flush();
// close the connection before sending the content of the block
out.close();
// the temporary block & meta files should be deleted
String dataDir = cluster.getDataDirectory();
File dir1 = new File(new File(dataDir, "data"+(2*sndNode+1)), "tmp");
File dir2 = new File(new File(dataDir, "data"+(2*sndNode+2)), "tmp");
while (dir1.listFiles().length != 0 || dir2.listFiles().length != 0) {
Thread.sleep(100);
}
// then increase the file's replication factor
fs.setReplication(fileName, (short)2);
// replication should succeed
DFSTestUtil.waitReplication(fs, fileName, (short)1);
// clean up the file
fs.delete(fileName, false);
} finally {
cluster.shutdown();
}
}