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