Skip to content

Commit b0f11af

Browse files
committed
HBASE-27637 Zero length value would cause value compressor read nothing and not advance the position of the InputStream
1 parent 8ba56cc commit b0f11af

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/wal/CompressionContext.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,16 @@ public int decompress(InputStream in, int inLength, byte[] outArray, int outOffs
126126
// Caller must handle short reads.
127127
// With current Hadoop compression codecs all 'outLength' bytes are read in here, so not
128128
// an issue for now.
129-
return compressedIn.read(outArray, outOffset, outLength);
129+
int read = compressedIn.read(outArray, outOffset, outLength);
130+
if (outLength == 0 && read == 0) {
131+
// The BufferedInputStream will return earlier and skip reading anything if outLength = 0,
132+
// but in fact for an empty value, the compressed output still contains some metadata so the
133+
// compressed size is not 0, so here we need to manually skip inLength bytes otherwise the
134+
// next read on this stream will start from an invalid position and cause critical problem,
135+
// such as data loss when splitting wal or replicating wal.
136+
in.skip(inLength);
137+
}
138+
return read;
130139
}
131140

132141
public void clear() {

hbase-server/src/test/java/org/apache/hadoop/hbase/wal/CompressedWALTestBase.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.apache.hadoop.hbase.wal;
1919

20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.hasSize;
2022
import static org.junit.Assert.assertEquals;
2123
import static org.junit.Assert.assertTrue;
2224

@@ -26,8 +28,9 @@
2628
import java.util.TreeMap;
2729
import org.apache.hadoop.fs.Path;
2830
import org.apache.hadoop.hbase.Cell;
31+
import org.apache.hadoop.hbase.CellBuilderFactory;
32+
import org.apache.hadoop.hbase.CellBuilderType;
2933
import org.apache.hadoop.hbase.HBaseTestingUtil;
30-
import org.apache.hadoop.hbase.KeyValue;
3134
import org.apache.hadoop.hbase.TableName;
3235
import org.apache.hadoop.hbase.client.RegionInfo;
3336
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
@@ -82,33 +85,42 @@ public void doTest(TableName tableName) throws Exception {
8285

8386
for (int i = 0; i < total; i++) {
8487
WALEdit kvs = new WALEdit();
85-
kvs.add(new KeyValue(row, family, Bytes.toBytes(i), value));
88+
kvs.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Cell.Type.Put)
89+
.setRow(row).setFamily(family).setQualifier(Bytes.toBytes(i)).setValue(value).build());
90+
kvs.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
91+
.setType(Cell.Type.DeleteFamily).setRow(row).setFamily(family).build());
8692
wal.appendData(regionInfo, new WALKeyImpl(regionInfo.getEncodedNameAsBytes(), tableName,
8793
System.currentTimeMillis(), mvcc, scopes), kvs);
94+
wal.sync();
8895
}
89-
wal.sync();
9096
final Path walPath = AbstractFSWALProvider.getCurrentFileName(wal);
9197
wals.shutdown();
9298

9399
// Confirm the WAL can be read back
94-
WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath);
95-
int count = 0;
96-
WAL.Entry entry = new WAL.Entry();
97-
while (reader.next(entry) != null) {
98-
count++;
99-
List<Cell> cells = entry.getEdit().getCells();
100-
assertTrue("Should be one KV per WALEdit", cells.size() == 1);
101-
for (Cell cell : cells) {
102-
assertTrue("Incorrect row", Bytes.equals(cell.getRowArray(), cell.getRowOffset(),
103-
cell.getRowLength(), row, 0, row.length));
104-
assertTrue("Incorrect family", Bytes.equals(cell.getFamilyArray(), cell.getFamilyOffset(),
105-
cell.getFamilyLength(), family, 0, family.length));
106-
assertTrue("Incorrect value", Bytes.equals(cell.getValueArray(), cell.getValueOffset(),
107-
cell.getValueLength(), value, 0, value.length));
100+
try (WAL.Reader reader = wals.createReader(TEST_UTIL.getTestFileSystem(), walPath)) {
101+
int count = 0;
102+
WAL.Entry entry = new WAL.Entry();
103+
while (reader.next(entry) != null) {
104+
count++;
105+
List<Cell> cells = entry.getEdit().getCells();
106+
assertThat("Should be two KVs per WALEdit", cells, hasSize(2));
107+
Cell putCell = cells.get(0);
108+
assertEquals(Cell.Type.Put, putCell.getType());
109+
assertTrue("Incorrect row", Bytes.equals(putCell.getRowArray(), putCell.getRowOffset(),
110+
putCell.getRowLength(), row, 0, row.length));
111+
assertTrue("Incorrect family", Bytes.equals(putCell.getFamilyArray(),
112+
putCell.getFamilyOffset(), putCell.getFamilyLength(), family, 0, family.length));
113+
assertTrue("Incorrect value", Bytes.equals(putCell.getValueArray(),
114+
putCell.getValueOffset(), putCell.getValueLength(), value, 0, value.length));
115+
116+
Cell deleteCell = cells.get(1);
117+
assertEquals(Cell.Type.DeleteFamily, deleteCell.getType());
118+
assertTrue("Incorrect row", Bytes.equals(deleteCell.getRowArray(),
119+
deleteCell.getRowOffset(), deleteCell.getRowLength(), row, 0, row.length));
120+
assertTrue("Incorrect family", Bytes.equals(deleteCell.getFamilyArray(),
121+
deleteCell.getFamilyOffset(), deleteCell.getFamilyLength(), family, 0, family.length));
108122
}
123+
assertEquals("Should have read back as many KVs as written", total, count);
109124
}
110-
assertEquals("Should have read back as many KVs as written", total, count);
111-
reader.close();
112125
}
113-
114126
}

hbase-server/src/test/java/org/apache/hadoop/hbase/wal/TestCompressedWALValueCompression.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121
import org.apache.hadoop.hbase.HBaseClassTestRule;
2222
import org.apache.hadoop.hbase.HBaseCommonTestingUtil;
23-
import org.apache.hadoop.hbase.HBaseTestingUtil;
2423
import org.apache.hadoop.hbase.HConstants;
2524
import org.apache.hadoop.hbase.TableName;
2625
import org.apache.hadoop.hbase.io.compress.Compression;
@@ -46,9 +45,7 @@ public class TestCompressedWALValueCompression extends CompressedWALTestBase {
4645
public static final HBaseClassTestRule CLASS_RULE =
4746
HBaseClassTestRule.forClass(TestCompressedWALValueCompression.class);
4847

49-
private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
50-
51-
@Parameters
48+
@Parameters(name = "{index}: compression={0}")
5249
public static List<Object[]> params() {
5350
return HBaseCommonTestingUtil.COMPRESSION_ALGORITHMS_PARAMETERIZED;
5451
}
@@ -81,5 +78,4 @@ public void test() throws Exception {
8178
TableName tableName = TableName.valueOf(name.getMethodName().replaceAll("[^a-zA-Z0-9]", "_"));
8279
doTest(tableName);
8380
}
84-
8581
}

0 commit comments

Comments
 (0)