|
| 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 | +} |
0 commit comments