Skip to content

Commit 4c7bdd5

Browse files
committed
MAPREDUCE-5656. bzip2 codec can drop records when reading data in splits. Contributed by Jason Lowe
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1549705 13f79535-47bb-0310-9956-ffa450edef68
1 parent 6c45a6c commit 4c7bdd5

File tree

10 files changed

+442
-31
lines changed

10 files changed

+442
-31
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LineReader.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public int readLine(Text str, int maxLineLength,
175175
}
176176
}
177177

178+
protected int fillBuffer(InputStream in, byte[] buffer, boolean inDelimiter)
179+
throws IOException {
180+
return in.read(buffer);
181+
}
182+
178183
/**
179184
* Read a line terminated by one of CR, LF, or CRLF.
180185
*/
@@ -208,7 +213,7 @@ private int readDefaultLine(Text str, int maxLineLength, int maxBytesToConsume)
208213
if (prevCharCR) {
209214
++bytesConsumed; //account for CR from previous read
210215
}
211-
bufferLength = in.read(buffer);
216+
bufferLength = fillBuffer(in, buffer, prevCharCR);
212217
if (bufferLength <= 0) {
213218
break; // EOF
214219
}
@@ -296,7 +301,7 @@ private int readCustomLine(Text str, int maxLineLength, int maxBytesToConsume)
296301
int startPosn = bufferPosn; // Start from previous end position
297302
if (bufferPosn >= bufferLength) {
298303
startPosn = bufferPosn = 0;
299-
bufferLength = in.read(buffer);
304+
bufferLength = fillBuffer(in, buffer, ambiguousByteCount > 0);
300305
if (bufferLength <= 0) {
301306
str.append(recordDelimiterBytes, 0, ambiguousByteCount);
302307
break; // EOF

hadoop-mapreduce-project/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ Release 2.4.0 - UNRELEASED
231231

232232
MAPREDUCE-5632. TestRMContainerAllocator#testUpdatedNodes fails (jeagles)
233233

234+
MAPREDUCE-5656. bzip2 codec can drop records when reading data in splits
235+
(jlowe)
236+
234237
Release 2.3.0 - UNRELEASED
235238

236239
INCOMPATIBLE CHANGES

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.apache.hadoop.io.compress.Decompressor;
3737
import org.apache.hadoop.io.compress.SplitCompressionInputStream;
3838
import org.apache.hadoop.io.compress.SplittableCompressionCodec;
39+
import org.apache.hadoop.mapreduce.lib.input.CompressedSplitLineReader;
40+
import org.apache.hadoop.mapreduce.lib.input.SplitLineReader;
3941
import org.apache.commons.logging.LogFactory;
4042
import org.apache.commons.logging.Log;
4143

@@ -52,7 +54,7 @@ public class LineRecordReader implements RecordReader<LongWritable, Text> {
5254
private long start;
5355
private long pos;
5456
private long end;
55-
private LineReader in;
57+
private SplitLineReader in;
5658
private FSDataInputStream fileIn;
5759
private final Seekable filePosition;
5860
int maxLineLength;
@@ -111,17 +113,18 @@ public LineRecordReader(Configuration job, FileSplit split,
111113
((SplittableCompressionCodec)codec).createInputStream(
112114
fileIn, decompressor, start, end,
113115
SplittableCompressionCodec.READ_MODE.BYBLOCK);
114-
in = new LineReader(cIn, job, recordDelimiter);
116+
in = new CompressedSplitLineReader(cIn, job, recordDelimiter);
115117
start = cIn.getAdjustedStart();
116118
end = cIn.getAdjustedEnd();
117119
filePosition = cIn; // take pos from compressed stream
118120
} else {
119-
in = new LineReader(codec.createInputStream(fileIn, decompressor), job, recordDelimiter);
121+
in = new SplitLineReader(codec.createInputStream(fileIn,
122+
decompressor), job, recordDelimiter);
120123
filePosition = fileIn;
121124
}
122125
} else {
123126
fileIn.seek(start);
124-
in = new LineReader(fileIn, job, recordDelimiter);
127+
in = new SplitLineReader(fileIn, job, recordDelimiter);
125128
filePosition = fileIn;
126129
}
127130
// If this is not the first split, we always throw away first record
@@ -141,7 +144,7 @@ public LineRecordReader(InputStream in, long offset, long endOffset,
141144
public LineRecordReader(InputStream in, long offset, long endOffset,
142145
int maxLineLength, byte[] recordDelimiter) {
143146
this.maxLineLength = maxLineLength;
144-
this.in = new LineReader(in, recordDelimiter);
147+
this.in = new SplitLineReader(in, recordDelimiter);
145148
this.start = offset;
146149
this.pos = offset;
147150
this.end = endOffset;
@@ -159,7 +162,7 @@ public LineRecordReader(InputStream in, long offset, long endOffset,
159162
throws IOException{
160163
this.maxLineLength = job.getInt(org.apache.hadoop.mapreduce.lib.input.
161164
LineRecordReader.MAX_LINE_LENGTH, Integer.MAX_VALUE);
162-
this.in = new LineReader(in, job, recordDelimiter);
165+
this.in = new SplitLineReader(in, job, recordDelimiter);
163166
this.start = offset;
164167
this.pos = offset;
165168
this.end = endOffset;
@@ -200,7 +203,7 @@ public synchronized boolean next(LongWritable key, Text value)
200203

201204
// We always read one extra line, which lies outside the upper
202205
// split limit i.e. (end - 1)
203-
while (getFilePosition() <= end) {
206+
while (getFilePosition() <= end || in.needAdditionalRecordAfterSplit()) {
204207
key.set(pos);
205208

206209
int newSize = in.readLine(value, maxLineLength,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.mapreduce.lib.input;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
24+
import org.apache.hadoop.classification.InterfaceAudience;
25+
import org.apache.hadoop.classification.InterfaceStability;
26+
import org.apache.hadoop.conf.Configuration;
27+
import org.apache.hadoop.io.Text;
28+
import org.apache.hadoop.io.compress.SplitCompressionInputStream;
29+
30+
/**
31+
* Line reader for compressed splits
32+
*
33+
* Reading records from a compressed split is tricky, as the
34+
* LineRecordReader is using the reported compressed input stream
35+
* position directly to determine when a split has ended. In addition the
36+
* compressed input stream is usually faking the actual byte position, often
37+
* updating it only after the first compressed block after the split is
38+
* accessed.
39+
*
40+
* Depending upon where the last compressed block of the split ends relative
41+
* to the record delimiters it can be easy to accidentally drop the last
42+
* record or duplicate the last record between this split and the next.
43+
*
44+
* Split end scenarios:
45+
*
46+
* 1) Last block of split ends in the middle of a record
47+
* Nothing special that needs to be done here, since the compressed input
48+
* stream will report a position after the split end once the record
49+
* is fully read. The consumer of the next split will discard the
50+
* partial record at the start of the split normally, and no data is lost
51+
* or duplicated between the splits.
52+
*
53+
* 2) Last block of split ends in the middle of a delimiter
54+
* The line reader will continue to consume bytes into the next block to
55+
* locate the end of the delimiter. If a custom delimiter is being used
56+
* then the next record must be read by this split or it will be dropped.
57+
* The consumer of the next split will not recognize the partial
58+
* delimiter at the beginning of its split and will discard it along with
59+
* the next record.
60+
*
61+
* However for the default delimiter processing there is a special case
62+
* because CR, LF, and CRLF are all valid record delimiters. If the
63+
* block ends with a CR then the reader must peek at the next byte to see
64+
* if it is an LF and therefore part of the same record delimiter.
65+
* Peeking at the next byte is an access to the next block and triggers
66+
* the stream to report the end of the split. There are two cases based
67+
* on the next byte:
68+
*
69+
* A) The next byte is LF
70+
* The split needs to end after the current record is returned. The
71+
* consumer of the next split will discard the first record, which
72+
* is degenerate since LF is itself a delimiter, and start consuming
73+
* records after that byte. If the current split tries to read
74+
* another record then the record will be duplicated between splits.
75+
*
76+
* B) The next byte is not LF
77+
* The current record will be returned but the stream will report
78+
* the split has ended due to the peek into the next block. If the
79+
* next record is not read then it will be lost, as the consumer of
80+
* the next split will discard it before processing subsequent
81+
* records. Therefore the next record beyond the reported split end
82+
* must be consumed by this split to avoid data loss.
83+
*
84+
* 3) Last block of split ends at the beginning of a delimiter
85+
* This is equivalent to case 1, as the reader will consume bytes into
86+
* the next block and trigger the end of the split. No further records
87+
* should be read as the consumer of the next split will discard the
88+
* (degenerate) record at the beginning of its split.
89+
*
90+
* 4) Last block of split ends at the end of a delimiter
91+
* Nothing special needs to be done here. The reader will not start
92+
* examining the bytes into the next block until the next record is read,
93+
* so the stream will not report the end of the split just yet. Once the
94+
* next record is read then the next block will be accessed and the
95+
* stream will indicate the end of the split. The consumer of the next
96+
* split will correctly discard the first record of its split, and no
97+
* data is lost or duplicated.
98+
*
99+
* If the default delimiter is used and the block ends at a CR then this
100+
* is treated as case 2 since the reader does not yet know without
101+
* looking at subsequent bytes whether the delimiter has ended.
102+
*
103+
* NOTE: It is assumed that compressed input streams *never* return bytes from
104+
* multiple compressed blocks from a single read. Failure to do so will
105+
* violate the buffering performed by this class, as it will access
106+
* bytes into the next block after the split before returning all of the
107+
* records from the previous block.
108+
*/
109+
@InterfaceAudience.Private
110+
@InterfaceStability.Unstable
111+
public class CompressedSplitLineReader extends SplitLineReader {
112+
113+
SplitCompressionInputStream scin;
114+
private boolean usingCRLF;
115+
private boolean needAdditionalRecord = false;
116+
private boolean finished = false;
117+
118+
public CompressedSplitLineReader(SplitCompressionInputStream in,
119+
Configuration conf,
120+
byte[] recordDelimiterBytes)
121+
throws IOException {
122+
super(in, conf, recordDelimiterBytes);
123+
scin = in;
124+
usingCRLF = (recordDelimiterBytes == null);
125+
}
126+
127+
@Override
128+
protected int fillBuffer(InputStream in, byte[] buffer, boolean inDelimiter)
129+
throws IOException {
130+
int bytesRead = in.read(buffer);
131+
132+
// If the split ended in the middle of a record delimiter then we need
133+
// to read one additional record, as the consumer of the next split will
134+
// not recognize the partial delimiter as a record.
135+
// However if using the default delimiter and the next character is a
136+
// linefeed then next split will treat it as a delimiter all by itself
137+
// and the additional record read should not be performed.
138+
if (inDelimiter && bytesRead > 0) {
139+
if (usingCRLF) {
140+
needAdditionalRecord = (buffer[0] != '\n');
141+
} else {
142+
needAdditionalRecord = true;
143+
}
144+
}
145+
return bytesRead;
146+
}
147+
148+
@Override
149+
public int readLine(Text str, int maxLineLength, int maxBytesToConsume)
150+
throws IOException {
151+
int bytesRead = 0;
152+
if (!finished) {
153+
// only allow at most one more record to be read after the stream
154+
// reports the split ended
155+
if (scin.getPos() > scin.getAdjustedEnd()) {
156+
finished = true;
157+
}
158+
159+
bytesRead = super.readLine(str, maxLineLength, maxBytesToConsume);
160+
}
161+
return bytesRead;
162+
}
163+
164+
@Override
165+
public boolean needAdditionalRecordAfterSplit() {
166+
return !finished && needAdditionalRecord;
167+
}
168+
}

hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/lib/input/LineRecordReader.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.apache.hadoop.mapreduce.InputSplit;
3939
import org.apache.hadoop.mapreduce.RecordReader;
4040
import org.apache.hadoop.mapreduce.TaskAttemptContext;
41-
import org.apache.hadoop.util.LineReader;
4241
import org.apache.commons.logging.LogFactory;
4342
import org.apache.commons.logging.Log;
4443

@@ -55,7 +54,7 @@ public class LineRecordReader extends RecordReader<LongWritable, Text> {
5554
private long start;
5655
private long pos;
5756
private long end;
58-
private LineReader in;
57+
private SplitLineReader in;
5958
private FSDataInputStream fileIn;
6059
private Seekable filePosition;
6160
private int maxLineLength;
@@ -94,33 +93,19 @@ public void initialize(InputSplit genericSplit,
9493
((SplittableCompressionCodec)codec).createInputStream(
9594
fileIn, decompressor, start, end,
9695
SplittableCompressionCodec.READ_MODE.BYBLOCK);
97-
if (null == this.recordDelimiterBytes){
98-
in = new LineReader(cIn, job);
99-
} else {
100-
in = new LineReader(cIn, job, this.recordDelimiterBytes);
101-
}
102-
96+
in = new CompressedSplitLineReader(cIn, job,
97+
this.recordDelimiterBytes);
10398
start = cIn.getAdjustedStart();
10499
end = cIn.getAdjustedEnd();
105100
filePosition = cIn;
106101
} else {
107-
if (null == this.recordDelimiterBytes) {
108-
in = new LineReader(codec.createInputStream(fileIn, decompressor),
109-
job);
110-
} else {
111-
in = new LineReader(codec.createInputStream(fileIn,
112-
decompressor), job, this.recordDelimiterBytes);
113-
}
102+
in = new SplitLineReader(codec.createInputStream(fileIn,
103+
decompressor), job, this.recordDelimiterBytes);
114104
filePosition = fileIn;
115105
}
116106
} else {
117107
fileIn.seek(start);
118-
if (null == this.recordDelimiterBytes){
119-
in = new LineReader(fileIn, job);
120-
} else {
121-
in = new LineReader(fileIn, job, this.recordDelimiterBytes);
122-
}
123-
108+
in = new SplitLineReader(fileIn, job, this.recordDelimiterBytes);
124109
filePosition = fileIn;
125110
}
126111
// If this is not the first split, we always throw away first record
@@ -160,7 +145,7 @@ public boolean nextKeyValue() throws IOException {
160145
int newSize = 0;
161146
// We always read one extra line, which lies outside the upper
162147
// split limit i.e. (end - 1)
163-
while (getFilePosition() <= end) {
148+
while (getFilePosition() <= end || in.needAdditionalRecordAfterSplit()) {
164149
newSize = in.readLine(value, maxLineLength,
165150
Math.max(maxBytesToConsume(pos), maxLineLength));
166151
pos += newSize;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.mapreduce.lib.input;
20+
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
24+
import org.apache.hadoop.classification.InterfaceAudience;
25+
import org.apache.hadoop.classification.InterfaceStability;
26+
import org.apache.hadoop.conf.Configuration;
27+
28+
@InterfaceAudience.Private
29+
@InterfaceStability.Unstable
30+
public class SplitLineReader extends org.apache.hadoop.util.LineReader {
31+
public SplitLineReader(InputStream in, byte[] recordDelimiterBytes) {
32+
super(in, recordDelimiterBytes);
33+
}
34+
35+
public SplitLineReader(InputStream in, Configuration conf,
36+
byte[] recordDelimiterBytes) throws IOException {
37+
super(in, conf, recordDelimiterBytes);
38+
}
39+
40+
public boolean needAdditionalRecordAfterSplit() {
41+
return false;
42+
}
43+
}

0 commit comments

Comments
 (0)