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