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