int fileLen = Math.min(conf.getInt("dfs.block.size", 4096), 4096);
createFile(fileSys, file, fileLen);
// get the first blockid for the file
Block firstBlock = DFSTestUtil.getFirstBlock(fileSys, file);
long newBlockId = firstBlock.getBlockId() + 1;
recvBuf.reset();
sendBuf.reset();
// bad version
recvOut.writeShort((short)(DataTransferProtocol.DATA_TRANSFER_VERSION-1));
sendOut.writeShort((short)(DataTransferProtocol.DATA_TRANSFER_VERSION-1));
sendRecvData("Wrong Version", true);
// bad ops
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)(DataTransferProtocol.OP_WRITE_BLOCK-1));
sendRecvData("Wrong Op Code", true);
/* Test OP_WRITE_BLOCK */
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_WRITE_BLOCK);
sendOut.writeLong(newBlockId); // block id
sendOut.writeLong(0); // generation stamp
sendOut.writeInt(0); // targets in pipeline
sendOut.writeBoolean(false); // recoveryFlag
Text.writeString(sendOut, "cl");// clientID
sendOut.writeBoolean(false); // no src node info
sendOut.writeInt(0); // number of downstream targets
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendOut.writeByte((byte)DataChecksum.CHECKSUM_CRC32);
// bad bytes per checksum
sendOut.writeInt(-1-random.nextInt(oneMil));
recvBuf.reset();
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_ERROR);
sendRecvData("wrong bytesPerChecksum while writing", true);
sendBuf.reset();
recvBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_WRITE_BLOCK);
sendOut.writeLong(newBlockId);
sendOut.writeLong(0); // generation stamp
sendOut.writeInt(0); // targets in pipeline
sendOut.writeBoolean(false); // recoveryFlag
Text.writeString(sendOut, "cl");// clientID
sendOut.writeBoolean(false); // no src node info
// bad number of targets
sendOut.writeInt(-1-random.nextInt(oneMil));
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_ERROR);
sendRecvData("bad targets len while writing block " + newBlockId, true);
sendBuf.reset();
recvBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_WRITE_BLOCK);
sendOut.writeLong(++newBlockId);
sendOut.writeLong(0); // generation stamp
sendOut.writeInt(0); // targets in pipeline
sendOut.writeBoolean(false); // recoveryFlag
Text.writeString(sendOut, "cl");// clientID
sendOut.writeBoolean(false); // no src node info
sendOut.writeInt(0);
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendOut.writeByte((byte)DataChecksum.CHECKSUM_CRC32);
sendOut.writeInt((int)512);
sendOut.writeInt(4); // size of packet
sendOut.writeLong(0); // OffsetInBlock
sendOut.writeLong(100); // sequencenumber
sendOut.writeBoolean(false); // lastPacketInBlock
// bad data chunk length
sendOut.writeInt(-1-random.nextInt(oneMil));
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_SUCCESS);
Text.writeString(recvOut, ""); // first bad node
recvOut.writeLong(100); // sequencenumber
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_ERROR);
sendRecvData("negative DATA_CHUNK len while writing block " + newBlockId,
true);
// test for writing a valid zero size block
sendBuf.reset();
recvBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_WRITE_BLOCK);
sendOut.writeLong(++newBlockId);
sendOut.writeLong(0); // generation stamp
sendOut.writeInt(0); // targets in pipeline
sendOut.writeBoolean(false); // recoveryFlag
Text.writeString(sendOut, "cl");// clientID
sendOut.writeBoolean(false); // no src node info
sendOut.writeInt(0);
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendOut.writeByte((byte)DataChecksum.CHECKSUM_CRC32);
sendOut.writeInt((int)512); // checksum size
sendOut.writeInt(8); // size of packet
sendOut.writeLong(0); // OffsetInBlock
sendOut.writeLong(100); // sequencenumber
sendOut.writeBoolean(true); // lastPacketInBlock
sendOut.writeInt(0); // chunk length
sendOut.writeInt(0); // zero checksum
//ok finally write a block with 0 len
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_SUCCESS);
Text.writeString(recvOut, ""); // first bad node
new DataTransferProtocol.PipelineAck(100,
new short[]{DataTransferProtocol.OP_STATUS_SUCCESS}).write(recvOut);
sendRecvData("Writing a zero len block blockid " + newBlockId, false);
/* Test OP_READ_BLOCK */
// bad block id
sendBuf.reset();
recvBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
newBlockId = firstBlock.getBlockId()-1;
sendOut.writeLong(newBlockId);
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(0L);
sendOut.writeLong(fileLen);
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_ERROR);
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendRecvData("Wrong block ID " + newBlockId + " for read", false);
// negative block start offset
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
sendOut.writeLong(firstBlock.getBlockId());
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(-1L);
sendOut.writeLong(fileLen);
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendRecvData("Negative start-offset for read for block " +
firstBlock.getBlockId(), false);
// bad block start offset
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
sendOut.writeLong(firstBlock.getBlockId());
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(fileLen);
sendOut.writeLong(fileLen);
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendRecvData("Wrong start-offset for reading block " +
firstBlock.getBlockId(), false);
// negative length is ok. Datanode assumes we want to read the whole block.
recvBuf.reset();
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_SUCCESS);
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
sendOut.writeLong(firstBlock.getBlockId());
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(0);
sendOut.writeLong(-1-random.nextInt(oneMil));
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendRecvData("Negative length for reading block " +
firstBlock.getBlockId(), false);
// length is more than size of block.
recvBuf.reset();
recvOut.writeShort((short)DataTransferProtocol.OP_STATUS_ERROR);
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
sendOut.writeLong(firstBlock.getBlockId());
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(0);
sendOut.writeLong(fileLen + 1);
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
sendRecvData("Wrong length for reading block " +
firstBlock.getBlockId(), false);
//At the end of all this, read the file to make sure that succeeds finally.
sendBuf.reset();
sendOut.writeShort((short)DataTransferProtocol.DATA_TRANSFER_VERSION);
sendOut.writeByte((byte)DataTransferProtocol.OP_READ_BLOCK);
sendOut.writeLong(firstBlock.getBlockId());
sendOut.writeLong(firstBlock.getGenerationStamp());
sendOut.writeLong(0);
sendOut.writeLong(fileLen);
Text.writeString(sendOut, "cl");
BlockTokenSecretManager.DUMMY_TOKEN.write(sendOut);
readFile(fileSys, file, fileLen);