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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.ConnectionFactory;
036import org.apache.hadoop.hbase.client.RegionInfo;
037import org.apache.hadoop.hbase.client.RegionInfoBuilder;
038import org.apache.hadoop.hbase.client.TableDescriptor;
039import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
040import org.apache.hadoop.hbase.executor.ExecutorType;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.apache.hadoop.hbase.testclassification.RegionServerTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.util.CommonFSUtils;
045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
046import org.junit.jupiter.api.AfterAll;
047import org.junit.jupiter.api.BeforeAll;
048import org.junit.jupiter.api.Tag;
049import org.junit.jupiter.api.Test;
050import org.junit.jupiter.api.TestInfo;
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054@Tag(MediumTests.TAG)
055@Tag(RegionServerTests.TAG)
056public class TestRegionOpen {
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestRegionOpen.class);
059  private static final int NB_SERVERS = 1;
060
061  private static final HBaseTestingUtil HTU = new HBaseTestingUtil();
062
063  @BeforeAll
064  public static void before() throws Exception {
065    HTU.startMiniCluster(NB_SERVERS);
066  }
067
068  @AfterAll
069  public static void afterClass() throws Exception {
070    HTU.shutdownMiniCluster();
071  }
072
073  private static HRegionServer getRS() {
074    return HTU.getHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
075  }
076
077  @Test
078  public void testPriorityRegionIsOpenedWithSeparateThreadPool() throws Exception {
079    final TableName tableName = TableName.valueOf(TestRegionOpen.class.getSimpleName());
080    ThreadPoolExecutor exec =
081      getRS().getExecutorService().getExecutorThreadPool(ExecutorType.RS_OPEN_PRIORITY_REGION);
082    long completed = exec.getCompletedTaskCount();
083
084    TableDescriptor tableDescriptor =
085      TableDescriptorBuilder.newBuilder(tableName).setPriority(HConstants.HIGH_QOS)
086        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
087    try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
088      Admin admin = connection.getAdmin()) {
089      admin.createTable(tableDescriptor);
090    }
091
092    assertEquals(completed + 1, exec.getCompletedTaskCount());
093  }
094
095  @Test
096  public void testNonExistentRegionReplica(TestInfo testInfo) throws Exception {
097    final TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
098    final byte[] FAMILYNAME = Bytes.toBytes("fam");
099    FileSystem fs = HTU.getTestFileSystem();
100    Admin admin = HTU.getAdmin();
101    Configuration conf = HTU.getConfiguration();
102    Path rootDir = HTU.getDataTestDirOnTestFS();
103
104    TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
105      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILYNAME)).build();
106    admin.createTable(htd);
107    HTU.waitUntilNoRegionsInTransition(60000);
108
109    // Create new HRI with non-default region replica id
110    RegionInfo hri = RegionInfoBuilder.newBuilder(htd.getTableName())
111      .setStartKey(Bytes.toBytes("A")).setEndKey(Bytes.toBytes("B"))
112      .setRegionId(EnvironmentEdgeManager.currentTime()).setReplicaId(2).build();
113    HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
114      CommonFSUtils.getTableDir(rootDir, hri.getTable()), hri);
115    Path regionDir = regionFs.getRegionDir();
116    try {
117      HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
118    } catch (IOException e) {
119      LOG.info("Caught expected IOE due missing .regioninfo file, due: " + e.getMessage()
120        + " skipping region open.");
121      // We should only have 1 region online
122      List<RegionInfo> regions = admin.getRegions(tableName);
123      LOG.info("Regions: " + regions);
124      if (regions.size() != 1) {
125        fail("Table " + tableName + " should have only one region, but got more: " + regions);
126      }
127      return;
128    }
129    fail("Should have thrown IOE when attempting to open a non-existing region.");
130  }
131}