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