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.assertNotNull;
021
022import java.io.IOException;
023import java.util.concurrent.CountDownLatch;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.MiniHBaseCluster;
029import org.apache.hadoop.hbase.StartMiniClusterOption;
030import org.apache.hadoop.hbase.testclassification.MasterTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
033import org.apache.zookeeper.KeeperException;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040/**
041 * Test to confirm that we will not hang when stop a backup master which is trying to become the
042 * active master. See HBASE-19838
043 */
044@Category({ MasterTests.class, MediumTests.class })
045public class TestShutdownBackupMaster {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestShutdownBackupMaster.class);
050
051  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
052
053  private static volatile CountDownLatch ARRIVE;
054
055  private static volatile CountDownLatch CONTINUE;
056
057  public static final class MockHMaster extends HMaster {
058
059    public MockHMaster(Configuration conf) throws IOException, KeeperException {
060      super(conf);
061    }
062
063    @Override
064    protected void initClusterSchemaService() throws IOException, InterruptedException {
065      if (ARRIVE != null) {
066        ARRIVE.countDown();
067        CONTINUE.await();
068      }
069      super.initClusterSchemaService();
070    }
071  }
072
073  @BeforeClass
074  public static void setUpBeforeClass() throws Exception {
075    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, MockHMaster.class, HMaster.class);
076    StartMiniClusterOption option = StartMiniClusterOption.builder()
077        .numMasters(2).numRegionServers(2).numDataNodes(2).build();
078    UTIL.startMiniCluster(option);
079    UTIL.waitUntilAllSystemRegionsAssigned();
080  }
081
082  @AfterClass
083  public static void tearDownAfterClass() throws Exception {
084    // make sure that we can stop the cluster cleanly
085    UTIL.shutdownMiniCluster();
086  }
087
088  @Test
089  public void testShutdownWhileBecomingActive() throws InterruptedException {
090    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
091    HMaster activeMaster = null;
092    HMaster backupMaster = null;
093    for (MasterThread t : cluster.getMasterThreads()) {
094      if (t.getMaster().isActiveMaster()) {
095        activeMaster = t.getMaster();
096      } else {
097        backupMaster = t.getMaster();
098      }
099    }
100    assertNotNull(activeMaster);
101    assertNotNull(backupMaster);
102    ARRIVE = new CountDownLatch(1);
103    CONTINUE = new CountDownLatch(1);
104    activeMaster.abort("Aborting active master for test");
105    // wait until we arrive the initClusterSchemaService
106    ARRIVE.await();
107    // killall RSes
108    cluster.getRegionServerThreads().stream().map(t -> t.getRegionServer())
109        .forEachOrdered(rs -> rs.abort("Aborting RS for test"));
110    CONTINUE.countDown();
111  }
112}