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.TableName;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.client.RegionLocator;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.client.TableState;
035import org.apache.hadoop.hbase.testclassification.LargeTests;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047@Category({MasterTests.class, LargeTests.class})
048public class TestMasterRestartAfterDisablingTable {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestMasterRestartAfterDisablingTable.class);
053
054  private static final Logger LOG =
055      LoggerFactory.getLogger(TestMasterRestartAfterDisablingTable.class);
056
057  @Rule
058  public TestName name = new TestName();
059
060  @Test
061  public void testForCheckingIfEnableAndDisableWorksFineAfterSwitch()
062      throws Exception {
063    final int NUM_MASTERS = 2;
064    final int NUM_RS = 1;
065    final int NUM_REGIONS_TO_CREATE = 4;
066
067    // Start the cluster
068    log("Starting cluster");
069    Configuration conf = HBaseConfiguration.create();
070    HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
071    TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
072    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
073    log("Waiting for active/ready master");
074    cluster.waitForActiveAndReadyMaster();
075
076    // Create a table with regions
077    final TableName tableName = TableName.valueOf(name.getMethodName());
078    byte[] family = Bytes.toBytes("family");
079    log("Creating table with " + NUM_REGIONS_TO_CREATE + " regions");
080    Table ht = TEST_UTIL.createMultiRegionTable(tableName, family, NUM_REGIONS_TO_CREATE);
081    int numRegions = -1;
082    try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
083      numRegions = r.getStartKeys().length;
084    }
085    numRegions += 1; // catalogs
086    log("Waiting for no more RIT\n");
087    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
088    log("Disabling table\n");
089    TEST_UTIL.getAdmin().disableTable(tableName);
090
091    NavigableSet<String> regions = HBaseTestingUtility.getAllOnlineRegions(cluster);
092    assertEquals(
093        "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().stop(
104        "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.",
123          6, regions.size());
124    assertTrue("The table should be in enabled state",
125        cluster.getMaster().getTableStateManager()
126        .isTableState(TableName.valueOf(name.getMethodName()), TableState.State.ENABLED));
127    ht.close();
128    TEST_UTIL.shutdownMiniCluster();
129  }
130
131  private void log(String msg) {
132    LOG.debug("\n\nTRR: " + msg + "\n");
133  }
134}
135