001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import static org.junit.jupiter.api.Assertions.assertThrows; 021 022import java.io.IOException; 023import java.util.List; 024import java.util.Optional; 025import org.apache.hadoop.hbase.DoNotRetryIOException; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 029import org.apache.hadoop.hbase.client.Durability; 030import org.apache.hadoop.hbase.client.Put; 031import org.apache.hadoop.hbase.client.Result; 032import org.apache.hadoop.hbase.client.ResultScanner; 033import org.apache.hadoop.hbase.client.Scan; 034import org.apache.hadoop.hbase.client.Table; 035import org.apache.hadoop.hbase.client.TableDescriptor; 036import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 037import org.apache.hadoop.hbase.coprocessor.ObserverContext; 038import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 039import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 040import org.apache.hadoop.hbase.coprocessor.RegionObserver; 041import org.apache.hadoop.hbase.io.hfile.CorruptHFileException; 042import org.apache.hadoop.hbase.testclassification.MediumTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.junit.jupiter.api.AfterAll; 045import org.junit.jupiter.api.BeforeAll; 046import org.junit.jupiter.api.Tag; 047import org.junit.jupiter.api.Test; 048import org.junit.jupiter.api.TestInfo; 049 050/** 051 * Tests a scanner on a corrupt hfile. 052 */ 053@Tag(MediumTests.TAG) 054public class TestScannerWithCorruptHFile { 055 056 private static final byte[] FAMILY_NAME = Bytes.toBytes("f"); 057 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 058 059 @BeforeAll 060 public static void setup() throws Exception { 061 TEST_UTIL.startMiniCluster(1); 062 } 063 064 @AfterAll 065 public static void tearDown() throws Exception { 066 TEST_UTIL.shutdownMiniCluster(); 067 } 068 069 public static class CorruptHFileCoprocessor implements RegionCoprocessor, RegionObserver { 070 @Override 071 public Optional<RegionObserver> getRegionObserver() { 072 return Optional.of(this); 073 } 074 075 @Override 076 public boolean preScannerNext(ObserverContext<? extends RegionCoprocessorEnvironment> e, 077 InternalScanner s, List<Result> results, int limit, boolean hasMore) throws IOException { 078 throw new CorruptHFileException("For test"); 079 } 080 } 081 082 @Test 083 public void testScanOnCorruptHFile(TestInfo testInfo) throws IOException { 084 TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName()); 085 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 086 .setCoprocessor(CorruptHFileCoprocessor.class.getName()) 087 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_NAME)).build(); 088 Table table = TEST_UTIL.createTable(tableDescriptor, null); 089 try { 090 loadTable(table, 1); 091 assertThrows(DoNotRetryIOException.class, () -> scan(table)); 092 } finally { 093 table.close(); 094 } 095 } 096 097 private void loadTable(Table table, int numRows) throws IOException { 098 for (int i = 0; i < numRows; ++i) { 099 byte[] row = Bytes.toBytes(i); 100 Put put = new Put(row); 101 put.setDurability(Durability.SKIP_WAL); 102 put.addColumn(FAMILY_NAME, null, row); 103 table.put(put); 104 } 105 } 106 107 private void scan(Table table) throws IOException { 108 Scan scan = new Scan(); 109 scan.setCaching(1); 110 scan.setCacheBlocks(false); 111 ResultScanner scanner = table.getScanner(scan); 112 try { 113 scanner.next(); 114 } finally { 115 scanner.close(); 116 } 117 } 118}