HBASE-23949 refactor loadBalancer implements for rsgroup balance by table to achieve...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMasterRestartAfterDisablingTable.java
blobfca2866e3802fa9b55f18c83e0094b5b9e327e87
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.util.List;
24 import java.util.NavigableSet;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.MiniHBaseCluster;
30 import org.apache.hadoop.hbase.StartMiniClusterOption;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.client.Admin;
33 import org.apache.hadoop.hbase.client.RegionLocator;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.client.TableState;
36 import org.apache.hadoop.hbase.testclassification.LargeTests;
37 import org.apache.hadoop.hbase.testclassification.MasterTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
40 import org.junit.ClassRule;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.experimental.categories.Category;
44 import org.junit.rules.TestName;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 @Category({MasterTests.class, LargeTests.class})
49 public class TestMasterRestartAfterDisablingTable {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestMasterRestartAfterDisablingTable.class);
55 private static final Logger LOG =
56 LoggerFactory.getLogger(TestMasterRestartAfterDisablingTable.class);
58 @Rule
59 public TestName name = new TestName();
61 @Test
62 public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
63 throws Exception {
64 final int NUM_MASTERS = 2;
65 final int NUM_REGIONS_TO_CREATE = 4;
67 // Start the cluster
68 log("Starting cluster");
69 Configuration conf = HBaseConfiguration.create();
70 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
71 StartMiniClusterOption option = StartMiniClusterOption.builder()
72 .numMasters(NUM_MASTERS).build();
73 TEST_UTIL.startMiniCluster(option);
74 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
75 log("Waiting for active/ready master");
76 cluster.waitForActiveAndReadyMaster();
78 // Create a table with regions
79 final TableName tableName = TableName.valueOf(name.getMethodName());
80 byte[] family = Bytes.toBytes("family");
81 log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
82 Table ht = TEST_UTIL.createMultiRegionTable(tableName, family, NUM_REGIONS_TO_CREATE);
83 int numRegions = -1;
84 try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
85 numRegions = r.getStartKeys().length;
87 numRegions += 1; // catalogs
88 log("Waiting for no more RIT\n");
89 TEST_UTIL.waitUntilNoRegionsInTransition(60000);
90 log("Disabling table\n");
91 TEST_UTIL.getAdmin().disableTable(tableName);
93 NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
94 assertEquals("The number of regions for the table tableRestart should be 0 and only" +
95 "the catalog table should be present.", 1, regions.size());
97 List<MasterThread> masterThreads = cluster.getMasterThreads();
98 MasterThread activeMaster = null;
99 if (masterThreads.get(0).getMaster().isActiveMaster()) {
100 activeMaster = masterThreads.get(0);
101 } else {
102 activeMaster = masterThreads.get(1);
104 activeMaster.getMaster().stop(
105 "stopping the active master so that the backup can become active");
106 cluster.hbaseCluster.waitOnMaster(activeMaster);
107 cluster.waitForActiveAndReadyMaster();
109 assertTrue("The table should not be in enabled state",
110 cluster.getMaster().getTableStateManager().isTableState(
111 TableName.valueOf(name.getMethodName()), TableState.State.DISABLED,
112 TableState.State.DISABLING));
113 log("Enabling table\n");
114 // Need a new Admin, the previous one is on the old master
115 Admin admin = TEST_UTIL.getAdmin();
116 admin.enableTable(tableName);
117 admin.close();
118 log("Waiting for no more RIT\n");
119 TEST_UTIL.waitUntilNoRegionsInTransition(60000);
120 log("Verifying there are " + numRegions + " assigned on cluster\n");
121 regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
122 assertEquals("The assigned regions were not onlined after master" +
123 " switch except for the catalog table.", 5, regions.size());
124 assertTrue("The table should be in enabled state", cluster.getMaster().getTableStateManager()
125 .isTableState(TableName.valueOf(name.getMethodName()), TableState.State.ENABLED));
126 ht.close();
127 TEST_UTIL.shutdownMiniCluster();
130 private void log(String msg) {
131 LOG.debug("\n\nTRR: " + msg + "\n");