Skip to content

Commit 5fd49fd

Browse files
author
Alejandro Abdelnur
committed
MAPREDUCE-3993. Graceful handling of codec errors during decompression (kkambatl via tucu)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1359345 13f79535-47bb-0310-9956-ffa450edef68
1 parent a5dc909 commit 5fd49fd

File tree

4 files changed

+51
-1
lines changed
  • hadoop-common-project/hadoop-common/src
  • hadoop-mapreduce-project
    • hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred

4 files changed

+51
-1
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/IOUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@ public static void copyBytes(InputStream in, OutputStream out, long count,
153153
}
154154
}
155155

156+
/**
157+
* Utility wrapper for reading from {@link InputStream}. It catches any errors
158+
* thrown by the underlying stream (either IO or decompression-related), and
159+
* re-throws as an IOException.
160+
*
161+
* @param is - InputStream to be read from
162+
* @param buf - buffer the data is read into
163+
* @param off - offset within buf
164+
* @param len - amount of data to be read
165+
* @return number of bytes read
166+
*/
167+
public static int wrappedReadForCompressedData(InputStream is, byte[] buf,
168+
int off, int len) throws IOException {
169+
try {
170+
return is.read(buf, off, len);
171+
} catch (IOException ie) {
172+
throw ie;
173+
} catch (Throwable t) {
174+
throw new IOException("Error while reading compressed data", t);
175+
}
176+
}
177+
156178
/**
157179
* Reads len bytes in a loop.
158180
*

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestIOUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.ByteBuffer;
3030
import java.nio.channels.FileChannel;
3131

32+
import org.apache.hadoop.test.GenericTestUtils;
3233
import org.junit.Test;
3334
import org.mockito.Mockito;
3435

@@ -152,4 +153,26 @@ public void testWriteFully() throws IOException {
152153
}
153154
}
154155
}
156+
157+
@Test
158+
public void testWrappedReadForCompressedData() throws IOException {
159+
byte[] buf = new byte[2];
160+
InputStream mockStream = Mockito.mock(InputStream.class);
161+
Mockito.when(mockStream.read(buf, 0, 1)).thenReturn(1);
162+
Mockito.when(mockStream.read(buf, 0, 2)).thenThrow(
163+
new java.lang.InternalError());
164+
165+
try {
166+
assertEquals("Check expected value", 1,
167+
IOUtils.wrappedReadForCompressedData(mockStream, buf, 0, 1));
168+
} catch (IOException ioe) {
169+
fail("Unexpected error while reading");
170+
}
171+
try {
172+
IOUtils.wrappedReadForCompressedData(mockStream, buf, 0, 2);
173+
} catch (IOException ioe) {
174+
GenericTestUtils.assertExceptionContains(
175+
"Error while reading compressed data", ioe);
176+
}
177+
}
155178
}

hadoop-mapreduce-project/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Branch-2 ( Unreleased changes )
218218

219219
MAPREDUCE-2739. Update installation docs (remove YarnClientFactory) (bowang via tucu)
220220

221+
MAPREDUCE-3993. Graceful handling of codec errors during decompression
222+
(kkambatl via tucu)
223+
221224
Release 2.0.0-alpha - 05-23-2012
222225

223226
INCOMPATIBLE CHANGES

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapred/IFile.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.hadoop.fs.Path;
3434
import org.apache.hadoop.io.DataInputBuffer;
3535
import org.apache.hadoop.io.DataOutputBuffer;
36+
import org.apache.hadoop.io.IOUtils;
3637
import org.apache.hadoop.io.WritableUtils;
3738
import org.apache.hadoop.io.compress.CodecPool;
3839
import org.apache.hadoop.io.compress.CompressionCodec;
@@ -379,7 +380,8 @@ public long getPosition() throws IOException {
379380
private int readData(byte[] buf, int off, int len) throws IOException {
380381
int bytesRead = 0;
381382
while (bytesRead < len) {
382-
int n = in.read(buf, off+bytesRead, len-bytesRead);
383+
int n = IOUtils.wrappedReadForCompressedData(in, buf, off + bytesRead,
384+
len - bytesRead);
383385
if (n < 0) {
384386
return bytesRead;
385387
}

0 commit comments

Comments
 (0)