|
23 | 23 | import static org.hamcrest.Matchers.instanceOf;
|
24 | 24 | import static org.hamcrest.Matchers.startsWith;
|
25 | 25 | import static org.junit.Assert.assertEquals;
|
| 26 | +import static org.junit.Assert.assertNotEquals; |
26 | 27 | import static org.junit.Assert.assertTrue;
|
27 | 28 | import static org.junit.Assert.fail;
|
28 | 29 |
|
|
55 | 56 | import org.apache.hadoop.hbase.testclassification.IOTests;
|
56 | 57 | import org.apache.hadoop.hbase.testclassification.SmallTests;
|
57 | 58 | import org.apache.hadoop.hbase.util.Bytes;
|
| 59 | +import org.apache.hadoop.hbase.util.ChecksumType; |
58 | 60 | import org.hamcrest.Description;
|
59 | 61 | import org.hamcrest.Matcher;
|
60 | 62 | import org.hamcrest.TypeSafeMatcher;
|
@@ -93,6 +95,141 @@ public TestHFileBlockHeaderCorruption() throws IOException {
|
93 | 95 | ruleChain = RuleChain.outerRule(testName).around(hFileTestRule);
|
94 | 96 | }
|
95 | 97 |
|
| 98 | + @Test |
| 99 | + public void testChecksumTypeCorruptionFirstBlock() throws Exception { |
| 100 | + HFileBlockChannelPosition firstBlock = null; |
| 101 | + try { |
| 102 | + try (HFileBlockChannelPositionIterator it = |
| 103 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 104 | + assertTrue(it.hasNext()); |
| 105 | + firstBlock = it.next(); |
| 106 | + } |
| 107 | + |
| 108 | + Corrupter c = new Corrupter(firstBlock); |
| 109 | + |
| 110 | + logHeader(firstBlock); |
| 111 | + |
| 112 | + // test corrupted HFileBlock with unknown checksumType code -1 |
| 113 | + c.write(HFileBlock.Header.CHECKSUM_TYPE_INDEX, ByteBuffer.wrap(new byte[] { -1 })); |
| 114 | + logHeader(firstBlock); |
| 115 | + try (HFileBlockChannelPositionIterator it = |
| 116 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 117 | + CountingConsumer consumer = new CountingConsumer(it); |
| 118 | + try { |
| 119 | + consumer.readFully(); |
| 120 | + fail(); |
| 121 | + } catch (Exception e) { |
| 122 | + assertThat(e, new IsThrowableMatching().withInstanceOf(IOException.class) |
| 123 | + .withMessage(startsWith("Unknown checksum type code"))); |
| 124 | + } |
| 125 | + assertEquals(0, consumer.getItemsRead()); |
| 126 | + } |
| 127 | + |
| 128 | + // valid checksumType code test |
| 129 | + for (ChecksumType t : ChecksumType.values()) { |
| 130 | + testValidChecksumTypeReadBlock(t.getCode(), c, firstBlock); |
| 131 | + } |
| 132 | + |
| 133 | + c.restore(); |
| 134 | + // test corrupted HFileBlock with unknown checksumType code 3 |
| 135 | + c.write(HFileBlock.Header.CHECKSUM_TYPE_INDEX, ByteBuffer.wrap(new byte[] { 3 })); |
| 136 | + logHeader(firstBlock); |
| 137 | + try (HFileBlockChannelPositionIterator it = |
| 138 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 139 | + CountingConsumer consumer = new CountingConsumer(it); |
| 140 | + try { |
| 141 | + consumer.readFully(); |
| 142 | + fail(); |
| 143 | + } catch (Exception e) { |
| 144 | + assertThat(e, new IsThrowableMatching().withInstanceOf(IOException.class) |
| 145 | + .withMessage(startsWith("Unknown checksum type code"))); |
| 146 | + } |
| 147 | + assertEquals(0, consumer.getItemsRead()); |
| 148 | + } |
| 149 | + } finally { |
| 150 | + if (firstBlock != null) { |
| 151 | + firstBlock.close(); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testChecksumTypeCorruptionSecondBlock() throws Exception { |
| 158 | + HFileBlockChannelPosition secondBlock = null; |
| 159 | + try { |
| 160 | + try (HFileBlockChannelPositionIterator it = |
| 161 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 162 | + assertTrue(it.hasNext()); |
| 163 | + it.next(); |
| 164 | + assertTrue(it.hasNext()); |
| 165 | + secondBlock = it.next(); |
| 166 | + } |
| 167 | + |
| 168 | + Corrupter c = new Corrupter(secondBlock); |
| 169 | + |
| 170 | + logHeader(secondBlock); |
| 171 | + // test corrupted HFileBlock with unknown checksumType code -1 |
| 172 | + c.write(HFileBlock.Header.CHECKSUM_TYPE_INDEX, ByteBuffer.wrap(new byte[] { -1 })); |
| 173 | + logHeader(secondBlock); |
| 174 | + try (HFileBlockChannelPositionIterator it = |
| 175 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 176 | + CountingConsumer consumer = new CountingConsumer(it); |
| 177 | + try { |
| 178 | + consumer.readFully(); |
| 179 | + fail(); |
| 180 | + } catch (Exception e) { |
| 181 | + assertThat(e, new IsThrowableMatching().withInstanceOf(RuntimeException.class) |
| 182 | + .withMessage(startsWith("Unknown checksum type code"))); |
| 183 | + } |
| 184 | + assertEquals(1, consumer.getItemsRead()); |
| 185 | + } |
| 186 | + |
| 187 | + // valid checksumType code test |
| 188 | + for (ChecksumType t : ChecksumType.values()) { |
| 189 | + testValidChecksumTypeReadBlock(t.getCode(), c, secondBlock); |
| 190 | + } |
| 191 | + |
| 192 | + c.restore(); |
| 193 | + // test corrupted HFileBlock with unknown checksumType code 3 |
| 194 | + c.write(HFileBlock.Header.CHECKSUM_TYPE_INDEX, ByteBuffer.wrap(new byte[] { 3 })); |
| 195 | + logHeader(secondBlock); |
| 196 | + try (HFileBlockChannelPositionIterator it = |
| 197 | + new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 198 | + CountingConsumer consumer = new CountingConsumer(it); |
| 199 | + try { |
| 200 | + consumer.readFully(); |
| 201 | + fail(); |
| 202 | + } catch (Exception e) { |
| 203 | + assertThat(e, new IsThrowableMatching().withInstanceOf(RuntimeException.class) |
| 204 | + .withMessage(startsWith("Unknown checksum type code"))); |
| 205 | + } |
| 206 | + assertEquals(1, consumer.getItemsRead()); |
| 207 | + } |
| 208 | + } finally { |
| 209 | + if (secondBlock != null) { |
| 210 | + secondBlock.close(); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public void testValidChecksumTypeReadBlock(byte checksumTypeCode, Corrupter c, |
| 216 | + HFileBlockChannelPosition testBlock) throws IOException { |
| 217 | + c.restore(); |
| 218 | + c.write(HFileBlock.Header.CHECKSUM_TYPE_INDEX, |
| 219 | + ByteBuffer.wrap(new byte[] { checksumTypeCode })); |
| 220 | + logHeader(testBlock); |
| 221 | + try ( |
| 222 | + HFileBlockChannelPositionIterator it = new HFileBlockChannelPositionIterator(hFileTestRule)) { |
| 223 | + CountingConsumer consumer = new CountingConsumer(it); |
| 224 | + try { |
| 225 | + consumer.readFully(); |
| 226 | + } catch (Exception e) { |
| 227 | + fail("test fail: valid checksumType are not executing properly"); |
| 228 | + } |
| 229 | + assertNotEquals(0, consumer.getItemsRead()); |
| 230 | + } |
| 231 | + } |
| 232 | + |
96 | 233 | @Test
|
97 | 234 | public void testOnDiskSizeWithoutHeaderCorruptionFirstBlock() throws Exception {
|
98 | 235 | HFileBlockChannelPosition firstBlock = null;
|
@@ -331,7 +468,8 @@ public HFileBlockChannelPositionIterator(HFileTestRule hFileTestRule) throws IOE
|
331 | 468 | try {
|
332 | 469 | reader = HFile.createReader(hfs, hfsPath, CacheConfig.DISABLED, true, conf);
|
333 | 470 | HFileBlock.FSReader fsreader = reader.getUncachedBlockReader();
|
334 |
| - iter = fsreader.blockRange(0, hfs.getFileStatus(hfsPath).getLen()); |
| 471 | + // The read block offset cannot out of the range:0,loadOnOpenDataOffset |
| 472 | + iter = fsreader.blockRange(0, reader.getTrailer().getLoadOnOpenDataOffset()); |
335 | 473 | } catch (IOException e) {
|
336 | 474 | if (reader != null) {
|
337 | 475 | closeQuietly(reader::close);
|
|
0 commit comments