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;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import java.util.NavigableSet;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.MiniHBaseCluster;
030import org.apache.hadoop.hbase.StartMiniClusterOption;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.RegionLocator;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.client.TableState;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.junit.rules.TestName;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048@Category({ MasterTests.class, LargeTests.class })
049public class TestMasterRestartAfterDisablingTable {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestMasterRestartAfterDisablingTable.class);
054
055  private static final Logger LOG =
056    LoggerFactory.getLogger(TestMasterRestartAfterDisablingTable.class);
057
058  @Rule
059  public TestName name = new TestName();
060
061  @Test
062  public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch() throws Exception {
063    final int NUM_MASTERS = 2;
064    final int NUM_REGIONS_TO_CREATE = 4;
065
066    // Start the cluster
067    log("Starting cluster");
068    Configuration conf = HBaseConfiguration.create();
069    HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
070    StartMiniClusterOption option =
071      StartMiniClusterOption.builder().numMasters(NUM_MASTERS).build();
072    TEST_UTIL.startMiniCluster(option);
073    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
074    log("Waiting for active/ready master");
075    cluster.waitForActiveAndReadyMaster();
076
077    // Create a table with regions
078    final TableName tableName = TableName.valueOf(name.getMethodName());
079    byte[] family = Bytes.toBytes("family");
080    log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
081    Table ht = TEST_UTIL.createMultiRegionTable(tableName, family, NUM_REGIONS_TO_CREATE);
082    int numRegions = -1;
083    try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
084      numRegions = r.getStartKeys().length;
085    }
086    numRegions += 1; // catalogs
087    log("Waiting for no more RIT\n");
088    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
089    log("Disabling table\n");
090    TEST_UTIL.getAdmin().disableTable(tableName);
091
092    NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
093    assertEquals("The number of regions for the table tableRestart should be 0 and only"
094      + "the catalog and namespace tables should be present.", 2, regions.size());
095
096    List<MasterThread> masterThreads = cluster.getMasterThreads();
097    MasterThread activeMaster = null;
098    if (masterThreads.get(0).getMaster().isActiveMaster()) {
099      activeMaster = masterThreads.get(0);
100    } else {
101      activeMaster = masterThreads.get(1);
102    }
103    activeMaster.getMaster()
104      .stop("stopping the active master so that the backup can become active");
105    cluster.hbaseCluster.waitOnMaster(activeMaster);
106    cluster.waitForActiveAndReadyMaster();
107
108    assertTrue("The table should not be in enabled state",
109      cluster.getMaster().getTableStateManager().isTableState(
110        TableName.valueOf(name.getMethodName()), TableState.State.DISABLED,
111        TableState.State.DISABLING));
112    log("Enabling table\n");
113    // Need a new Admin, the previous one is on the old master
114    Admin admin = TEST_UTIL.getAdmin();
115    admin.enableTable(tableName);
116    admin.close();
117    log("Waiting for no more RIT\n");
118    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
119    log("Verifying there are " + numRegions + " assigned on cluster\n");
120    regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
121    assertEquals("The assigned regions were not onlined after master"
122      + " switch except for the catalog and namespace tables.", 6, regions.size());
123    assertTrue("The table should be in enabled state", cluster.getMaster().getTableStateManager()
124      .isTableState(TableName.valueOf(name.getMethodName()), TableState.State.ENABLED));
125    ht.close();
126    TEST_UTIL.shutdownMiniCluster();
127  }
128
129  private void log(String msg) {
130    LOG.debug("\n\nTRR: " + msg + "\n");
131  }
132}