Skip to content

Commit 81b0597

Browse files
authored
HDFS-17455. Fix Client throw IndexOutOfBoundsException in DFSInputStream#fetchBlockAt (apache#6710). Contributed by Haiyang Hu.
Reviewed-by: ZanderXu <[email protected]> Signed-off-by: He Xiaoqiao <[email protected]>
1 parent 05964ad commit 81b0597

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSClientFaultInjector.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.hadoop.classification.InterfaceAudience;
2525
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
26+
import org.apache.hadoop.hdfs.security.token.block.InvalidBlockTokenException;
2627

2728
/**
2829
* Used for injecting faults in DFSClient and DFSOutputStream tests.
@@ -69,4 +70,5 @@ public void delayWhenRenewLeaseTimeout() {}
6970

7071
public void onCreateBlockReader(LocatedBlock block, int chunkIndex, long offset, long length) {}
7172

73+
public void failCreateBlockReader() throws InvalidBlockTokenException {}
7274
}

hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java

+9
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,14 @@ private LocatedBlock fetchBlockAt(long offset, long length, boolean useCache)
519519
// Update the LastLocatedBlock, if offset is for last block.
520520
if (offset >= locatedBlocks.getFileLength()) {
521521
setLocatedBlocksFields(newBlocks, getLastBlockLength(newBlocks));
522+
// After updating the locatedBlock, the block to which the offset belongs
523+
// should be researched like {@link DFSInputStream#getBlockAt(long)}.
524+
if (offset >= locatedBlocks.getFileLength()) {
525+
return locatedBlocks.getLastLocatedBlock();
526+
} else {
527+
targetBlockIdx = locatedBlocks.findBlock(offset);
528+
assert targetBlockIdx >= 0 && targetBlockIdx < locatedBlocks.locatedBlockCount();
529+
}
522530
} else {
523531
locatedBlocks.insertRange(targetBlockIdx,
524532
newBlocks.getLocatedBlocks());
@@ -641,6 +649,7 @@ private synchronized DatanodeInfo blockSeekTo(long target)
641649
targetBlock = retval.block;
642650

643651
try {
652+
DFSClientFaultInjector.get().failCreateBlockReader();
644653
blockReader = getBlockReader(targetBlock, offsetIntoBlock,
645654
targetBlock.getBlockSize() - offsetIntoBlock, targetAddr,
646655
storageType, chosenNode);

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSInputStream.java

+72
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.ArrayList;
3232
import java.util.List;
3333
import java.util.Random;
34+
import java.util.concurrent.TimeoutException;
35+
import java.util.concurrent.atomic.AtomicInteger;
3436

3537
import org.apache.hadoop.conf.Configuration;
3638
import org.apache.hadoop.fs.FSDataOutputStream;
@@ -41,14 +43,21 @@
4143
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
4244
import org.apache.hadoop.hdfs.protocol.DatanodeInfoWithStorage;
4345
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
46+
import org.apache.hadoop.hdfs.security.token.block.InvalidBlockTokenException;
4447
import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor;
48+
import org.apache.hadoop.io.IOUtils;
4549
import org.apache.hadoop.net.unix.DomainSocket;
4650
import org.apache.hadoop.net.unix.TemporarySocketDirectory;
4751
import org.apache.hadoop.hdfs.client.impl.DfsClientConf;
4852
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.Retry;
4953

54+
import org.apache.hadoop.test.GenericTestUtils;
55+
import org.apache.log4j.Level;
5056
import org.junit.Assume;
5157
import org.junit.Test;
58+
import org.mockito.Mockito;
59+
import org.mockito.invocation.InvocationOnMock;
60+
import org.mockito.stubbing.Answer;
5261

5362
public class TestDFSInputStream {
5463
private void testSkipInner(MiniDFSCluster cluster) throws IOException {
@@ -287,4 +296,67 @@ public void testReadWithoutPreferredCachingReplica() throws IOException {
287296
cluster.shutdown();
288297
}
289298
}
299+
300+
@Test
301+
public void testCreateBlockReaderWhenInvalidBlockTokenException() throws
302+
IOException, InterruptedException, TimeoutException {
303+
GenericTestUtils.setLogLevel(DFSClient.LOG, Level.DEBUG);
304+
Configuration conf = new HdfsConfiguration();
305+
conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 64 * 1024);
306+
conf.setInt(HdfsClientConfigKeys.DFS_CLIENT_WRITE_PACKET_SIZE_KEY, 516);
307+
DFSClientFaultInjector oldFaultInjector = DFSClientFaultInjector.get();
308+
FSDataOutputStream out = null;
309+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(3).build()) {
310+
cluster.waitActive();
311+
DistributedFileSystem fs = cluster.getFileSystem();
312+
313+
// Create file which only contains one UC block.
314+
String file = "/testfile";
315+
Path path = new Path(file);
316+
out = fs.create(path, (short) 3);
317+
int bufferLen = 5120;
318+
byte[] toWrite = new byte[bufferLen];
319+
Random rb = new Random(0);
320+
rb.nextBytes(toWrite);
321+
out.write(toWrite, 0, bufferLen);
322+
323+
// Wait for the block length of the file to be 1.
324+
GenericTestUtils.waitFor(() -> {
325+
try {
326+
return fs.getFileBlockLocations(path, 0, bufferLen).length == 1;
327+
} catch (IOException e) {
328+
return false;
329+
}
330+
}, 100, 10000);
331+
332+
// Set up the InjectionHandler.
333+
DFSClientFaultInjector.set(Mockito.mock(DFSClientFaultInjector.class));
334+
DFSClientFaultInjector injector = DFSClientFaultInjector.get();
335+
final AtomicInteger count = new AtomicInteger(0);
336+
Mockito.doAnswer(new Answer<Void>() {
337+
@Override
338+
public Void answer(InvocationOnMock invocation) throws Throwable {
339+
// Mock access token was invalid when connecting to first datanode
340+
// throw InvalidBlockTokenException.
341+
if (count.getAndIncrement() == 0) {
342+
throw new InvalidBlockTokenException("Mock InvalidBlockTokenException");
343+
}
344+
return null;
345+
}
346+
}).when(injector).failCreateBlockReader();
347+
348+
try (DFSInputStream in = new DFSInputStream(fs.getClient(), file,
349+
false, null)) {
350+
int bufLen = 1024;
351+
byte[] buf = new byte[bufLen];
352+
// Seek the offset to 1024 and which should be in the range (0, fileSize).
353+
in.seek(1024);
354+
int read = in.read(buf, 0, bufLen);
355+
assertEquals(1024, read);
356+
}
357+
} finally {
358+
DFSClientFaultInjector.set(oldFaultInjector);
359+
IOUtils.closeStream(out);
360+
}
361+
}
290362
}

0 commit comments

Comments
 (0)