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.Assert.assertEquals;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.concurrent.ThreadPoolExecutor;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.HColumnDescriptor;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.HRegionInfo;
034import org.apache.hadoop.hbase.HTableDescriptor;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.Admin;
037import org.apache.hadoop.hbase.client.Connection;
038import org.apache.hadoop.hbase.client.ConnectionFactory;
039import org.apache.hadoop.hbase.executor.ExecutorType;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.testclassification.RegionServerTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.apache.hadoop.hbase.util.FSUtils;
044import org.junit.AfterClass;
045import org.junit.BeforeClass;
046import org.junit.ClassRule;
047import org.junit.Rule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050import org.junit.rules.TestName;
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054@Category({MediumTests.class, RegionServerTests.class})
055public class TestRegionOpen {
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059      HBaseClassTestRule.forClass(TestRegionOpen.class);
060
061  @SuppressWarnings("unused")
062  private static final Logger LOG = LoggerFactory.getLogger(TestRegionOpen.class);
063  private static final int NB_SERVERS = 1;
064
065  private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
066
067  @Rule
068  public TestName name = new TestName();
069
070  @BeforeClass
071  public static void before() throws Exception {
072    HTU.startMiniCluster(NB_SERVERS);
073  }
074
075  @AfterClass
076  public static void afterClass() throws Exception {
077    HTU.shutdownMiniCluster();
078  }
079
080  private static HRegionServer getRS() {
081    return HTU.getHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
082  }
083
084  @Test
085  public void testPriorityRegionIsOpenedWithSeparateThreadPool() throws Exception {
086    final TableName tableName = TableName.valueOf(TestRegionOpen.class.getSimpleName());
087    ThreadPoolExecutor exec = getRS().getExecutorService()
088        .getExecutorThreadPool(ExecutorType.RS_OPEN_PRIORITY_REGION);
089    long completed = exec.getCompletedTaskCount();
090
091    HTableDescriptor htd = new HTableDescriptor(tableName);
092    htd.setPriority(HConstants.HIGH_QOS);
093    htd.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
094    try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
095        Admin admin = connection.getAdmin()) {
096      admin.createTable(htd);
097    }
098
099    assertEquals(completed + 1, exec.getCompletedTaskCount());
100  }
101
102  @Test
103  public void testNonExistentRegionReplica() throws Exception {
104    final TableName tableName = TableName.valueOf(name.getMethodName());
105    final byte[] FAMILYNAME = Bytes.toBytes("fam");
106    FileSystem fs = HTU.getTestFileSystem();
107    Admin admin = HTU.getAdmin();
108    Configuration conf = HTU.getConfiguration();
109    Path rootDir = HTU.getDataTestDirOnTestFS();
110
111    HTableDescriptor htd = new HTableDescriptor(tableName);
112    htd.addFamily(new HColumnDescriptor(FAMILYNAME));
113    admin.createTable(htd);
114    HTU.waitUntilNoRegionsInTransition(60000);
115
116    // Create new HRI with non-default region replica id
117    HRegionInfo hri = new HRegionInfo(htd.getTableName(),  Bytes.toBytes("A"), Bytes.toBytes("B"), false,
118        System.currentTimeMillis(), 2);
119    HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
120        FSUtils.getTableDir(rootDir, hri.getTable()), hri);
121    Path regionDir = regionFs.getRegionDir();
122    try {
123      HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
124    } catch (IOException e) {
125      LOG.info("Caught expected IOE due missing .regioninfo file, due: " + e.getMessage() + " skipping region open.");
126      // We should only have 1 region online
127      List<HRegionInfo> regions = admin.getTableRegions(tableName);
128      LOG.info("Regions: " + regions);
129      if (regions.size() != 1) {
130        fail("Table " + tableName + " should have only one region, but got more: " + regions);
131      }
132      return;
133    }
134    fail("Should have thrown IOE when attempting to open a non-existing region.");
135  }
136}