001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license 003 * agreements. See the NOTICE file distributed with this work for additional information regarding 004 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the 005 * "License"); you may not use this file except in compliance with the License. You may obtain a 006 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 007 * law or agreed to in writing, software distributed under the License is distributed on an "AS IS" 008 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License 009 * for the specific language governing permissions and limitations under the License. 010 */ 011package org.apache.hadoop.hbase.master.procedure; 012 013import java.io.IOException; 014import java.util.List; 015import java.util.concurrent.ExecutionException; 016import java.util.concurrent.TimeUnit; 017import java.util.concurrent.TimeoutException; 018import org.apache.hadoop.fs.Path; 019import org.apache.hadoop.hbase.HBaseClassTestRule; 020import org.apache.hadoop.hbase.HBaseTestingUtility; 021import org.apache.hadoop.hbase.TableName; 022import org.apache.hadoop.hbase.client.Admin; 023import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 024import org.apache.hadoop.hbase.client.RegionInfo; 025import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 026import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 027import org.apache.hadoop.hbase.master.MasterFileSystem; 028import org.apache.hadoop.hbase.regionserver.StoreFileWriter; 029import org.apache.hadoop.hbase.testclassification.MasterTests; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.apache.hadoop.hbase.util.CommonFSUtils; 033import org.apache.hadoop.hbase.util.FSUtils; 034import org.junit.After; 035import org.junit.AfterClass; 036import org.junit.BeforeClass; 037import org.junit.ClassRule; 038import org.junit.Rule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041import org.junit.rules.TestName; 042 043/** 044 * Testcase for HBASE-22632 045 */ 046@Category({ MasterTests.class, MediumTests.class }) 047public class TestIgnoreUnknownFamily { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestIgnoreUnknownFamily.class); 052 053 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 054 055 private static final byte[] FAMILY = Bytes.toBytes("cf"); 056 057 private static final byte[] UNKNOWN_FAMILY = Bytes.toBytes("wrong_cf"); 058 059 @Rule 060 public TestName name = new TestName(); 061 062 @BeforeClass 063 public static void setUp() throws Exception { 064 UTIL.startMiniCluster(1); 065 } 066 067 @AfterClass 068 public static void tearDown() throws Exception { 069 UTIL.shutdownMiniCluster(); 070 } 071 072 @After 073 public void tearDownAfterTest() throws IOException { 074 Admin admin = UTIL.getAdmin(); 075 for (TableName tableName : admin.listTableNames()) { 076 admin.disableTable(tableName); 077 admin.deleteTable(tableName); 078 } 079 } 080 081 private void addStoreFileToKnownFamily(RegionInfo region) throws IOException { 082 MasterFileSystem mfs = UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem(); 083 Path regionDir = 084 FSUtils.getRegionDirFromRootDir(CommonFSUtils.getRootDir(mfs.getConfiguration()), region); 085 Path familyDir = new Path(regionDir, Bytes.toString(UNKNOWN_FAMILY)); 086 StoreFileWriter writer = 087 new StoreFileWriter.Builder(mfs.getConfiguration(), mfs.getFileSystem()) 088 .withOutputDir(familyDir).withFileContext(new HFileContextBuilder().build()).build(); 089 writer.close(); 090 } 091 092 @Test 093 public void testSplit() 094 throws IOException, InterruptedException, ExecutionException, TimeoutException { 095 TableName tableName = TableName.valueOf(name.getMethodName()); 096 Admin admin = UTIL.getAdmin(); 097 admin.createTable(TableDescriptorBuilder.newBuilder(tableName) 098 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build()); 099 RegionInfo region = admin.getRegions(tableName).get(0); 100 addStoreFileToKnownFamily(region); 101 admin.splitRegionAsync(region.getRegionName(), Bytes.toBytes(0)).get(30, TimeUnit.SECONDS); 102 } 103 104 @Test 105 public void testMerge() 106 throws IOException, InterruptedException, ExecutionException, TimeoutException { 107 TableName tableName = TableName.valueOf(name.getMethodName()); 108 Admin admin = UTIL.getAdmin(); 109 admin.createTable( 110 TableDescriptorBuilder.newBuilder(tableName) 111 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(), 112 new byte[][] { Bytes.toBytes(0) }); 113 List<RegionInfo> regions = admin.getRegions(tableName); 114 addStoreFileToKnownFamily(regions.get(0)); 115 admin.mergeRegionsAsync(regions.get(0).getEncodedNameAsBytes(), 116 regions.get(1).getEncodedNameAsBytes(), false).get(30, TimeUnit.SECONDS); 117 } 118}