View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  
25  import java.io.IOException;
26  import java.util.List;
27  import java.util.concurrent.ThreadPoolExecutor;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FileSystem;
33  import org.apache.hadoop.fs.Path;
34  import org.apache.hadoop.hbase.HBaseTestingUtility;
35  import org.apache.hadoop.hbase.HColumnDescriptor;
36  import org.apache.hadoop.hbase.HConstants;
37  import org.apache.hadoop.hbase.HTableDescriptor;
38  import org.apache.hadoop.hbase.TableName;
39  import org.apache.hadoop.hbase.HRegionInfo;
40  import org.apache.hadoop.hbase.client.ConnectionFactory;
41  import org.apache.hadoop.hbase.executor.ExecutorType;
42  import org.apache.hadoop.hbase.testclassification.MediumTests;
43  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
44  import org.apache.hadoop.hbase.client.Admin;
45  import org.apache.hadoop.hbase.client.Connection;
46  import org.apache.hadoop.hbase.util.Bytes;
47  import org.apache.hadoop.hbase.util.FSUtils;
48  import org.junit.AfterClass;
49  import org.junit.BeforeClass;
50  import org.junit.Rule;
51  import org.junit.Test;
52  import org.junit.experimental.categories.Category;
53  import org.junit.rules.TestName;
54  import static org.junit.Assert.fail;
55  
56  @Category({MediumTests.class, RegionServerTests.class})
57  public class TestRegionOpen {
58    @SuppressWarnings("unused")
59    private static final Log LOG = LogFactory.getLog(TestRegionOpen.class);
60    private static final int NB_SERVERS = 1;
61  
62    private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
63  
64    @Rule
65    public TestName name = new TestName();
66  
67    @BeforeClass
68    public static void before() throws Exception {
69      HTU.startMiniCluster(NB_SERVERS);
70    }
71  
72    @AfterClass
73    public static void afterClass() throws Exception {
74      HTU.shutdownMiniCluster();
75    }
76  
77    private static HRegionServer getRS() {
78      return HTU.getHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
79    }
80  
81    @Test(timeout = 60000)
82    public void testPriorityRegionIsOpenedWithSeparateThreadPool() throws Exception {
83      final TableName tableName = TableName.valueOf(TestRegionOpen.class.getSimpleName());
84      ThreadPoolExecutor exec = getRS().getExecutorService()
85          .getExecutorThreadPool(ExecutorType.RS_OPEN_PRIORITY_REGION);
86  
87      assertEquals(1, exec.getCompletedTaskCount()); // namespace region
88  
89      HTableDescriptor htd = new HTableDescriptor(tableName);
90      htd.setPriority(HConstants.HIGH_QOS);
91      htd.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
92      try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
93          Admin admin = connection.getAdmin()) {
94        admin.createTable(htd);
95      }
96  
97      assertEquals(2, exec.getCompletedTaskCount());
98    }
99  
100   @Test
101   public void testNonExistentRegionReplica() throws Exception {
102     final TableName tableName = TableName.valueOf(name.getMethodName());
103     final byte[] FAMILYNAME = Bytes.toBytes("fam");
104     FileSystem fs = HTU.getTestFileSystem();
105     Connection connection = HTU.getConnection();
106     Admin admin = connection.getAdmin();
107     Configuration conf = HTU.getConfiguration();
108     Path rootDir = HTU.getDataTestDirOnTestFS();
109 
110     HTableDescriptor htd = new HTableDescriptor(tableName);
111     htd.addFamily(new HColumnDescriptor(FAMILYNAME));
112     admin.createTable(htd);
113     HTU.waitUntilNoRegionsInTransition(60000);
114 
115     // Create new HRI with non-default region replica id
116     HRegionInfo hri = new HRegionInfo(htd.getTableName(),  Bytes.toBytes("A"), Bytes.toBytes("B"), false,
117         System.currentTimeMillis(), 2);
118     HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
119         FSUtils.getTableDir(rootDir, hri.getTable()), hri);
120     Path regionDir = regionFs.getRegionDir();
121     try {
122       HRegionFileSystem.loadRegionInfoFileContent(fs, regionDir);
123     } catch (IOException e) {
124       LOG.info("Caught expected IOE due missing .regioninfo file, due: " + e.getMessage() + " skipping region open.");
125       // We should only have 1 region online
126       List<HRegionInfo> regions = admin.getTableRegions(tableName);
127       LOG.info("Regions: " + regions);
128       if (regions.size() != 1) {
129         fail("Table " + tableName + " should have only one region, but got more: " + regions);
130       }
131       return;
132     } finally {
133       admin.close();
134     }
135     fail("Should have thrown IOE when attempting to open a non-existing region.");
136   }
137 }