-
Notifications
You must be signed in to change notification settings - Fork 8.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HDFS-17455. Fix Client throw IndexOutOfBoundsException in DFSInputStream#fetchBlockAt #6710
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a638efd
HDFS-17455. Fix Client throw IndexOutOfBoundsException in DFSInputStr…
haiyang1987 2c9a6fb
HDFS-17455. Fix checkstytle
haiyang1987 79945e9
HDFS-17455. Modify patch based on comments
haiyang1987 cc6f05f
HDFS-17455. Modify patch based on comments
haiyang1987 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FSDataOutputStream; | ||
|
@@ -41,14 +43,21 @@ | |
import org.apache.hadoop.hdfs.protocol.DatanodeInfo; | ||
import org.apache.hadoop.hdfs.protocol.DatanodeInfoWithStorage; | ||
import org.apache.hadoop.hdfs.protocol.LocatedBlock; | ||
import org.apache.hadoop.hdfs.security.token.block.InvalidBlockTokenException; | ||
import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor; | ||
import org.apache.hadoop.io.IOUtils; | ||
import org.apache.hadoop.net.unix.DomainSocket; | ||
import org.apache.hadoop.net.unix.TemporarySocketDirectory; | ||
import org.apache.hadoop.hdfs.client.impl.DfsClientConf; | ||
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.Retry; | ||
|
||
import org.apache.hadoop.test.GenericTestUtils; | ||
import org.apache.log4j.Level; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
public class TestDFSInputStream { | ||
private void testSkipInner(MiniDFSCluster cluster) throws IOException { | ||
|
@@ -287,4 +296,67 @@ public void testReadWithoutPreferredCachingReplica() throws IOException { | |
cluster.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCreateBlockReaderWhenInvalidBlockTokenException() throws | ||
IOException, InterruptedException, TimeoutException { | ||
GenericTestUtils.setLogLevel(DFSClient.LOG, Level.DEBUG); | ||
Configuration conf = new HdfsConfiguration(); | ||
conf.setLong(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 64 * 1024); | ||
conf.setInt(HdfsClientConfigKeys.DFS_CLIENT_WRITE_PACKET_SIZE_KEY, 516); | ||
DFSClientFaultInjector oldFaultInjector = DFSClientFaultInjector.get(); | ||
FSDataOutputStream out = null; | ||
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(3).build()) { | ||
cluster.waitActive(); | ||
DistributedFileSystem fs = cluster.getFileSystem(); | ||
|
||
// Create file which only contains one UC block. | ||
String file = "/testfile"; | ||
Path path = new Path(file); | ||
out = fs.create(path, (short) 3); | ||
int bufferLen = 5120; | ||
byte[] toWrite = new byte[bufferLen]; | ||
Random rb = new Random(0); | ||
rb.nextBytes(toWrite); | ||
out.write(toWrite, 0, bufferLen); | ||
|
||
// Wait for the block length of the file to be 1. | ||
GenericTestUtils.waitFor(() -> { | ||
try { | ||
return fs.getFileBlockLocations(path, 0, bufferLen).length == 1; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
}, 100, 10000); | ||
|
||
// Set up the InjectionHandler. | ||
DFSClientFaultInjector.set(Mockito.mock(DFSClientFaultInjector.class)); | ||
DFSClientFaultInjector injector = DFSClientFaultInjector.get(); | ||
final AtomicInteger count = new AtomicInteger(0); | ||
Mockito.doAnswer(new Answer<Void>() { | ||
@Override | ||
public Void answer(InvocationOnMock invocation) throws Throwable { | ||
// Mock access token was invalid when connecting to first datanode | ||
// throw InvalidBlockTokenException. | ||
if (count.getAndIncrement() == 0) { | ||
throw new InvalidBlockTokenException("Mock InvalidBlockTokenException"); | ||
} | ||
return null; | ||
} | ||
}).when(injector).failCreateBlockReader(); | ||
|
||
try (DFSInputStream in = new DFSInputStream(fs.getClient(), file, | ||
false, null)) { | ||
int bufLen = 1024; | ||
byte[] buf = new byte[bufLen]; | ||
// Seek the offset to 1024 and which should be in the range (0, fileSize). | ||
in.seek(1024); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some comments to show that the offset should be in (0, fileSize). |
||
int read = in.read(buf, 0, bufLen); | ||
assertEquals(1024, read); | ||
} | ||
} finally { | ||
DFSClientFaultInjector.set(oldFaultInjector); | ||
IOUtils.closeStream(out); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. Please make the comments clearer.