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