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