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.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.MiniClusterExtension;
027import org.apache.hadoop.hbase.StartTestingClusterOption;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.junit.jupiter.api.extension.RegisterExtension;
033
034@Tag(MediumTests.TAG)
035@Tag(MasterTests.TAG)
036public class TestAlwaysStandByHMaster {
037
038  private static final StartTestingClusterOption OPTION = StartTestingClusterOption.builder()
039    .numAlwaysStandByMasters(1).numMasters(1).numRegionServers(1).build();
040
041  @RegisterExtension
042  public static final MiniClusterExtension miniClusterExtension =
043    MiniClusterExtension.newBuilder().setMiniClusterOption(OPTION).build();
044
045  /**
046   * Tests that the AlwaysStandByHMaster does not transition to active state even if no active
047   * master exists.
048   */
049  @Test
050  public void testAlwaysStandBy() throws Exception {
051    HBaseTestingUtil testUtil = miniClusterExtension.getTestingUtility();
052    // Make sure there is an active master.
053    assertNotNull(testUtil.getMiniHBaseCluster().getMaster());
054    assertEquals(2, testUtil.getMiniHBaseCluster().getMasterThreads().size());
055    // Kill the only active master.
056    testUtil.getMiniHBaseCluster().stopMaster(0).join();
057    // Wait for 5s to make sure the always standby doesn't transition to active state.
058    assertFalse(testUtil.getMiniHBaseCluster().waitForActiveAndReadyMaster(5000));
059    // Add a new master.
060    HMaster newActive = testUtil.getMiniHBaseCluster().startMaster().getMaster();
061    assertTrue(testUtil.getMiniHBaseCluster().waitForActiveAndReadyMaster(5000));
062    // Newly added master should be the active.
063    assertEquals(newActive.getServerName(),
064      testUtil.getMiniHBaseCluster().getMaster().getServerName());
065  }
066}