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