|
@@ -18,17 +18,13 @@
|
|
|
package org.apache.hadoop.hdfs;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
-import org.apache.hadoop.fs.BlockLocation;
|
|
|
import org.apache.hadoop.fs.FSDataInputStream;
|
|
|
import org.apache.hadoop.fs.FileStatus;
|
|
|
import org.apache.hadoop.fs.FileSystem;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
-import org.apache.hadoop.hdfs.protocol.HdfsConstants;
|
|
|
-import org.apache.hadoop.hdfs.server.datanode.DataNode;
|
|
|
import org.apache.hadoop.hdfs.web.ByteRangeInputStream;
|
|
|
import org.apache.hadoop.hdfs.web.WebHdfsConstants;
|
|
|
import org.apache.hadoop.hdfs.web.WebHdfsTestUtil;
|
|
|
-import org.apache.hadoop.io.erasurecode.rawcoder.RSRawDecoder;
|
|
|
import org.junit.AfterClass;
|
|
|
import org.junit.Assert;
|
|
|
import org.junit.BeforeClass;
|
|
@@ -37,34 +33,30 @@ import org.junit.Test;
|
|
|
import java.io.EOFException;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.ByteBuffer;
|
|
|
-import java.util.Random;
|
|
|
|
|
|
-public class TestWriteReadStripedFile {
|
|
|
- private static int dataBlocks = HdfsConstants.NUM_DATA_BLOCKS;
|
|
|
- private static int parityBlocks = HdfsConstants.NUM_PARITY_BLOCKS;
|
|
|
-
|
|
|
- private final static int cellSize = HdfsConstants.BLOCK_STRIPED_CELL_SIZE;
|
|
|
- private final static int stripesPerBlock = 4;
|
|
|
- static int blockSize = cellSize * stripesPerBlock;
|
|
|
- static int numDNs = dataBlocks + parityBlocks + 2;
|
|
|
+import static org.apache.hadoop.hdfs.StripedFileTestUtil.blockSize;
|
|
|
+import static org.apache.hadoop.hdfs.StripedFileTestUtil.cellSize;
|
|
|
+import static org.apache.hadoop.hdfs.StripedFileTestUtil.dataBlocks;
|
|
|
+import static org.apache.hadoop.hdfs.StripedFileTestUtil.numDNs;
|
|
|
+import static org.apache.hadoop.hdfs.StripedFileTestUtil.stripesPerBlock;
|
|
|
|
|
|
+public class TestWriteReadStripedFile {
|
|
|
private static MiniDFSCluster cluster;
|
|
|
- private static Configuration conf;
|
|
|
private static FileSystem fs;
|
|
|
-
|
|
|
- private static Random r= new Random();
|
|
|
+ private static Configuration conf;
|
|
|
|
|
|
@BeforeClass
|
|
|
public static void setup() throws IOException {
|
|
|
- conf = new Configuration();
|
|
|
+ conf = new HdfsConfiguration();
|
|
|
conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, blockSize);
|
|
|
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(numDNs).build();
|
|
|
- cluster.getFileSystem().getClient().createErasureCodingZone("/", null, cellSize);
|
|
|
+ cluster.getFileSystem().getClient().createErasureCodingZone("/",
|
|
|
+ null, cellSize);
|
|
|
fs = cluster.getFileSystem();
|
|
|
}
|
|
|
|
|
|
@AfterClass
|
|
|
- public static void tearDown() {
|
|
|
+ public static void tearDown() throws IOException {
|
|
|
if (cluster != null) {
|
|
|
cluster.shutdown();
|
|
|
}
|
|
@@ -152,47 +144,21 @@ public class TestWriteReadStripedFile {
|
|
|
+ cellSize + 123);
|
|
|
}
|
|
|
|
|
|
- private byte[] generateBytes(int cnt) {
|
|
|
- byte[] bytes = new byte[cnt];
|
|
|
- for (int i = 0; i < cnt; i++) {
|
|
|
- bytes[i] = getByte(i);
|
|
|
- }
|
|
|
- return bytes;
|
|
|
- }
|
|
|
-
|
|
|
- private int readAll(FSDataInputStream in, byte[] buf) throws IOException {
|
|
|
- int readLen = 0;
|
|
|
- int ret;
|
|
|
- do {
|
|
|
- ret = in.read(buf, readLen, buf.length - readLen);
|
|
|
- if (ret > 0) {
|
|
|
- readLen += ret;
|
|
|
- }
|
|
|
- } while (ret >= 0 && readLen < buf.length);
|
|
|
- return readLen;
|
|
|
- }
|
|
|
-
|
|
|
- private byte getByte(long pos) {
|
|
|
- final int mod = 29;
|
|
|
- return (byte) (pos % mod + 1);
|
|
|
- }
|
|
|
-
|
|
|
private void assertSeekAndRead(FSDataInputStream fsdis, int pos,
|
|
|
int writeBytes) throws IOException {
|
|
|
fsdis.seek(pos);
|
|
|
byte[] buf = new byte[writeBytes];
|
|
|
- int readLen = readAll(fsdis, buf);
|
|
|
+ int readLen = StripedFileTestUtil.readAll(fsdis, buf);
|
|
|
Assert.assertEquals(readLen, writeBytes - pos);
|
|
|
for (int i = 0; i < readLen; i++) {
|
|
|
Assert.assertEquals("Byte at " + i + " should be the same",
|
|
|
- getByte(pos + i), buf[i]);
|
|
|
+ StripedFileTestUtil.getByte(pos + i), buf[i]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void testOneFileUsingDFSStripedInputStream(String src, int fileLength)
|
|
|
throws IOException {
|
|
|
-
|
|
|
- final byte[] expected = generateBytes(fileLength);
|
|
|
+ final byte[] expected = StripedFileTestUtil.generateBytes(fileLength);
|
|
|
Path srcPath = new Path(src);
|
|
|
DFSTestUtil.writeFile(fs, srcPath, new String(expected));
|
|
|
|
|
@@ -215,7 +181,7 @@ public class TestWriteReadStripedFile {
|
|
|
public void testWriteReadUsingWebHdfs() throws Exception {
|
|
|
int fileLength = blockSize * dataBlocks + cellSize + 123;
|
|
|
|
|
|
- final byte[] expected = generateBytes(fileLength);
|
|
|
+ final byte[] expected = StripedFileTestUtil.generateBytes(fileLength);
|
|
|
FileSystem fs = WebHdfsTestUtil.getWebHdfsFileSystem(conf,
|
|
|
WebHdfsConstants.WEBHDFS_SCHEME);
|
|
|
Path srcPath = new Path("/testWriteReadUsingWebHdfs_stripe");
|
|
@@ -231,7 +197,6 @@ public class TestWriteReadStripedFile {
|
|
|
verifySeek(fs, srcPath, fileLength);
|
|
|
verifyStatefulRead(fs, srcPath, fileLength, expected, smallBuf);
|
|
|
//webhdfs doesn't support bytebuffer read
|
|
|
-
|
|
|
}
|
|
|
|
|
|
void verifyLength(FileSystem fs, Path srcPath, int fileLength)
|
|
@@ -243,152 +208,105 @@ public class TestWriteReadStripedFile {
|
|
|
|
|
|
void verifyPread(FileSystem fs, Path srcPath, int fileLength,
|
|
|
byte[] expected, byte[] buf) throws IOException {
|
|
|
- FSDataInputStream in = fs.open(srcPath);
|
|
|
- int[] startOffsets = {0, 1, cellSize - 102, cellSize, cellSize + 102,
|
|
|
- cellSize * (dataBlocks - 1), cellSize * (dataBlocks - 1) + 102,
|
|
|
- cellSize * dataBlocks, fileLength - 102, fileLength - 1};
|
|
|
- for (int startOffset : startOffsets) {
|
|
|
- startOffset = Math.max(0, Math.min(startOffset, fileLength - 1));
|
|
|
- int remaining = fileLength - startOffset;
|
|
|
- in.readFully(startOffset, buf, 0, remaining);
|
|
|
- for (int i = 0; i < remaining; i++) {
|
|
|
- Assert.assertEquals("Byte at " + (startOffset + i) + " should be the " +
|
|
|
- "same",
|
|
|
- expected[startOffset + i], buf[i]);
|
|
|
+ try (FSDataInputStream in = fs.open(srcPath)) {
|
|
|
+ int[] startOffsets = {0, 1, cellSize - 102, cellSize, cellSize + 102,
|
|
|
+ cellSize * (dataBlocks - 1), cellSize * (dataBlocks - 1) + 102,
|
|
|
+ cellSize * dataBlocks, fileLength - 102, fileLength - 1};
|
|
|
+ for (int startOffset : startOffsets) {
|
|
|
+ startOffset = Math.max(0, Math.min(startOffset, fileLength - 1));
|
|
|
+ int remaining = fileLength - startOffset;
|
|
|
+ in.readFully(startOffset, buf, 0, remaining);
|
|
|
+ for (int i = 0; i < remaining; i++) {
|
|
|
+ Assert.assertEquals("Byte at " + (startOffset + i) + " should be the " +
|
|
|
+ "same", expected[startOffset + i], buf[i]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- in.close();
|
|
|
}
|
|
|
|
|
|
void verifyStatefulRead(FileSystem fs, Path srcPath, int fileLength,
|
|
|
byte[] expected, byte[] buf) throws IOException {
|
|
|
- FSDataInputStream in = fs.open(srcPath);
|
|
|
- final byte[] result = new byte[fileLength];
|
|
|
- int readLen = 0;
|
|
|
- int ret;
|
|
|
- do {
|
|
|
- ret = in.read(buf, 0, buf.length);
|
|
|
- if (ret > 0) {
|
|
|
+ try (FSDataInputStream in = fs.open(srcPath)) {
|
|
|
+ final byte[] result = new byte[fileLength];
|
|
|
+ int readLen = 0;
|
|
|
+ int ret;
|
|
|
+ while ((ret = in.read(buf, 0, buf.length)) >= 0) {
|
|
|
System.arraycopy(buf, 0, result, readLen, ret);
|
|
|
readLen += ret;
|
|
|
}
|
|
|
- } while (ret >= 0);
|
|
|
- Assert.assertEquals("The length of file should be the same to write size",
|
|
|
- fileLength, readLen);
|
|
|
- Assert.assertArrayEquals(expected, result);
|
|
|
- in.close();
|
|
|
+ Assert.assertEquals("The length of file should be the same to write size",
|
|
|
+ fileLength, readLen);
|
|
|
+ Assert.assertArrayEquals(expected, result);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
void verifyStatefulRead(FileSystem fs, Path srcPath, int fileLength,
|
|
|
byte[] expected, ByteBuffer buf) throws IOException {
|
|
|
- FSDataInputStream in = fs.open(srcPath);
|
|
|
- ByteBuffer result = ByteBuffer.allocate(fileLength);
|
|
|
- int readLen = 0;
|
|
|
- int ret;
|
|
|
- do {
|
|
|
- ret = in.read(buf);
|
|
|
- if (ret > 0) {
|
|
|
+ try (FSDataInputStream in = fs.open(srcPath)) {
|
|
|
+ ByteBuffer result = ByteBuffer.allocate(fileLength);
|
|
|
+ int readLen = 0;
|
|
|
+ int ret;
|
|
|
+ while ((ret = in.read(buf)) >= 0) {
|
|
|
readLen += ret;
|
|
|
buf.flip();
|
|
|
result.put(buf);
|
|
|
buf.clear();
|
|
|
}
|
|
|
- } while (ret >= 0);
|
|
|
- readLen = readLen >= 0 ? readLen : 0;
|
|
|
- Assert.assertEquals("The length of file should be the same to write size",
|
|
|
- fileLength, readLen);
|
|
|
- Assert.assertArrayEquals(expected, result.array());
|
|
|
- in.close();
|
|
|
+ Assert.assertEquals("The length of file should be the same to write size",
|
|
|
+ fileLength, readLen);
|
|
|
+ Assert.assertArrayEquals(expected, result.array());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
void verifySeek(FileSystem fs, Path srcPath, int fileLength)
|
|
|
throws IOException {
|
|
|
- FSDataInputStream in = fs.open(srcPath);
|
|
|
- // seek to 1/2 of content
|
|
|
- int pos = fileLength / 2;
|
|
|
- assertSeekAndRead(in, pos, fileLength);
|
|
|
-
|
|
|
- // seek to 1/3 of content
|
|
|
- pos = fileLength / 3;
|
|
|
- assertSeekAndRead(in, pos, fileLength);
|
|
|
-
|
|
|
- // seek to 0 pos
|
|
|
- pos = 0;
|
|
|
- assertSeekAndRead(in, pos, fileLength);
|
|
|
-
|
|
|
- if (fileLength > cellSize) {
|
|
|
- // seek to cellSize boundary
|
|
|
- pos = cellSize - 1;
|
|
|
+ try (FSDataInputStream in = fs.open(srcPath)) {
|
|
|
+ // seek to 1/2 of content
|
|
|
+ int pos = fileLength / 2;
|
|
|
assertSeekAndRead(in, pos, fileLength);
|
|
|
- }
|
|
|
|
|
|
- if (fileLength > cellSize * dataBlocks) {
|
|
|
- // seek to striped cell group boundary
|
|
|
- pos = cellSize * dataBlocks - 1;
|
|
|
+ // seek to 1/3 of content
|
|
|
+ pos = fileLength / 3;
|
|
|
assertSeekAndRead(in, pos, fileLength);
|
|
|
- }
|
|
|
|
|
|
- if (fileLength > blockSize * dataBlocks) {
|
|
|
- // seek to striped block group boundary
|
|
|
- pos = blockSize * dataBlocks - 1;
|
|
|
+ // seek to 0 pos
|
|
|
+ pos = 0;
|
|
|
assertSeekAndRead(in, pos, fileLength);
|
|
|
- }
|
|
|
|
|
|
- if(!(in.getWrappedStream() instanceof ByteRangeInputStream)){
|
|
|
- try {
|
|
|
- in.seek(-1);
|
|
|
- Assert.fail("Should be failed if seek to negative offset");
|
|
|
- } catch (EOFException e) {
|
|
|
- // expected
|
|
|
+ if (fileLength > cellSize) {
|
|
|
+ // seek to cellSize boundary
|
|
|
+ pos = cellSize - 1;
|
|
|
+ assertSeekAndRead(in, pos, fileLength);
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- in.seek(fileLength + 1);
|
|
|
- Assert.fail("Should be failed if seek after EOF");
|
|
|
- } catch (EOFException e) {
|
|
|
- // expected
|
|
|
+ if (fileLength > cellSize * dataBlocks) {
|
|
|
+ // seek to striped cell group boundary
|
|
|
+ pos = cellSize * dataBlocks - 1;
|
|
|
+ assertSeekAndRead(in, pos, fileLength);
|
|
|
}
|
|
|
- }
|
|
|
- in.close();
|
|
|
- }
|
|
|
|
|
|
- @Test
|
|
|
- public void testWritePreadWithDNFailure() throws IOException {
|
|
|
- final int failedDNIdx = 2;
|
|
|
- final int length = cellSize * (dataBlocks + 2);
|
|
|
- Path testPath = new Path("/foo");
|
|
|
- final byte[] bytes = generateBytes(length);
|
|
|
- DFSTestUtil.writeFile(fs, testPath, new String(bytes));
|
|
|
-
|
|
|
- // shut down the DN that holds the last internal data block
|
|
|
- BlockLocation[] locs = fs.getFileBlockLocations(testPath, cellSize * 5,
|
|
|
- cellSize);
|
|
|
- String name = (locs[0].getNames())[failedDNIdx];
|
|
|
- for (DataNode dn : cluster.getDataNodes()) {
|
|
|
- int port = dn.getXferPort();
|
|
|
- if (name.contains(Integer.toString(port))) {
|
|
|
- dn.shutdown();
|
|
|
- break;
|
|
|
+ if (fileLength > blockSize * dataBlocks) {
|
|
|
+ // seek to striped block group boundary
|
|
|
+ pos = blockSize * dataBlocks - 1;
|
|
|
+ assertSeekAndRead(in, pos, fileLength);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // pread
|
|
|
- int startOffsetInFile = cellSize * 5;
|
|
|
- try (FSDataInputStream fsdis = fs.open(testPath)) {
|
|
|
- byte[] buf = new byte[length];
|
|
|
- int readLen = fsdis.read(startOffsetInFile, buf, 0, buf.length);
|
|
|
- Assert.assertEquals("The length of file should be the same to write size",
|
|
|
- length - startOffsetInFile, readLen);
|
|
|
-
|
|
|
- byte[] expected = new byte[readLen];
|
|
|
- for (int i = startOffsetInFile; i < length; i++) {
|
|
|
- expected[i - startOffsetInFile] = getByte(i);
|
|
|
- }
|
|
|
- for (int i = startOffsetInFile; i < length; i++) {
|
|
|
- Assert.assertEquals("Byte at " + i + " should be the same",
|
|
|
- expected[i - startOffsetInFile], buf[i - startOffsetInFile]);
|
|
|
+ if (!(in.getWrappedStream() instanceof ByteRangeInputStream)) {
|
|
|
+ try {
|
|
|
+ in.seek(-1);
|
|
|
+ Assert.fail("Should be failed if seek to negative offset");
|
|
|
+ } catch (EOFException e) {
|
|
|
+ // expected
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ in.seek(fileLength + 1);
|
|
|
+ Assert.fail("Should be failed if seek after EOF");
|
|
|
+ } catch (EOFException e) {
|
|
|
+ // expected
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|