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.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
029import org.apache.hadoop.hbase.StartTestingClusterOption;
030import org.apache.hadoop.hbase.testclassification.MasterTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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 {
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    StartTestingClusterOption option =
076      StartTestingClusterOption.builder().numMasters(2).numRegionServers(2).numDataNodes(2).build();
077    UTIL.startMiniCluster(option);
078    UTIL.waitUntilAllSystemRegionsAssigned();
079  }
080
081  @AfterClass
082  public static void tearDownAfterClass() throws Exception {
083    // make sure that we can stop the cluster cleanly
084    UTIL.shutdownMiniCluster();
085  }
086
087  @Test
088  public void testShutdownWhileBecomingActive() throws InterruptedException {
089    SingleProcessHBaseCluster cluster = UTIL.getHBaseCluster();
090    HMaster activeMaster = null;
091    HMaster backupMaster = null;
092    for (MasterThread t : cluster.getMasterThreads()) {
093      if (t.getMaster().isActiveMaster()) {
094        activeMaster = t.getMaster();
095      } else {
096        backupMaster = t.getMaster();
097      }
098    }
099    assertNotNull(activeMaster);
100    assertNotNull(backupMaster);
101    ARRIVE = new CountDownLatch(1);
102    CONTINUE = new CountDownLatch(1);
103    activeMaster.abort("Aborting active master for test");
104    // wait until we arrive the initClusterSchemaService
105    ARRIVE.await();
106    // killall RSes
107    cluster.getRegionServerThreads().stream().map(t -> t.getRegionServer())
108      .forEachOrdered(rs -> rs.abort("Aborting RS for test"));
109    CONTINUE.countDown();
110  }
111}