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.master.procedure;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.concurrent.ExecutionException;
023import java.util.concurrent.TimeUnit;
024import java.util.concurrent.TimeoutException;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
034import org.apache.hadoop.hbase.master.MasterFileSystem;
035import org.apache.hadoop.hbase.regionserver.StoreFileWriter;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.CommonFSUtils;
040import org.apache.hadoop.hbase.util.FSUtils;
041import org.junit.After;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Rule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.junit.rules.TestName;
049
050/**
051 * Testcase for HBASE-22632
052 */
053@Category({ MasterTests.class, MediumTests.class })
054public class TestIgnoreUnknownFamily {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestIgnoreUnknownFamily.class);
059
060  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
061
062  private static final byte[] FAMILY = Bytes.toBytes("cf");
063
064  private static final byte[] UNKNOWN_FAMILY = Bytes.toBytes("wrong_cf");
065
066  @Rule
067  public TestName name = new TestName();
068
069  @BeforeClass
070  public static void setUp() throws Exception {
071    UTIL.startMiniCluster(1);
072  }
073
074  @AfterClass
075  public static void tearDown() throws Exception {
076    UTIL.shutdownMiniCluster();
077  }
078
079  @After
080  public void tearDownAfterTest() throws IOException {
081    Admin admin = UTIL.getAdmin();
082    for (TableName tableName : admin.listTableNames()) {
083      admin.disableTable(tableName);
084      admin.deleteTable(tableName);
085    }
086  }
087
088  private void addStoreFileToKnownFamily(RegionInfo region) throws IOException {
089    MasterFileSystem mfs = UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
090    Path regionDir =
091      FSUtils.getRegionDirFromRootDir(CommonFSUtils.getRootDir(mfs.getConfiguration()), region);
092    Path familyDir = new Path(regionDir, Bytes.toString(UNKNOWN_FAMILY));
093    StoreFileWriter writer =
094      new StoreFileWriter.Builder(mfs.getConfiguration(), mfs.getFileSystem())
095        .withOutputDir(familyDir).withFileContext(new HFileContextBuilder().build()).build();
096    writer.close();
097  }
098
099  @Test
100  public void testSplit()
101    throws IOException, InterruptedException, ExecutionException, TimeoutException {
102    TableName tableName = TableName.valueOf(name.getMethodName());
103    Admin admin = UTIL.getAdmin();
104    admin.createTable(TableDescriptorBuilder.newBuilder(tableName)
105      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build());
106    RegionInfo region = admin.getRegions(tableName).get(0);
107    addStoreFileToKnownFamily(region);
108    admin.splitRegionAsync(region.getRegionName(), Bytes.toBytes(0)).get(30, TimeUnit.SECONDS);
109  }
110
111  @Test
112  public void testMerge()
113    throws IOException, InterruptedException, ExecutionException, TimeoutException {
114    TableName tableName = TableName.valueOf(name.getMethodName());
115    Admin admin = UTIL.getAdmin();
116    admin.createTable(
117      TableDescriptorBuilder.newBuilder(tableName)
118        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(),
119      new byte[][] { Bytes.toBytes(0) });
120    List<RegionInfo> regions = admin.getRegions(tableName);
121    addStoreFileToKnownFamily(regions.get(0));
122    admin.mergeRegionsAsync(regions.get(0).getEncodedNameAsBytes(),
123      regions.get(1).getEncodedNameAsBytes(), false).get(30, TimeUnit.SECONDS);
124  }
125}