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.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.List;
025import org.apache.hadoop.hbase.ClusterMetrics;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.MasterNotRunningException;
029import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
030import org.apache.hadoop.hbase.StartTestingClusterOption;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.util.JVMClusterUtil;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ MasterTests.class, LargeTests.class })
039public class TestMasterFailoverBalancerPersistence {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestMasterFailoverBalancerPersistence.class);
044
045  /**
046   * Test that if the master fails, the load balancer maintains its state (running or not) when the
047   * next master takes over
048   */
049  @Test
050  public void testMasterFailoverBalancerPersistence() throws Exception {
051    // Start the cluster
052    HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
053
054    StartTestingClusterOption option = StartTestingClusterOption.builder().numMasters(3).build();
055    TEST_UTIL.startMiniCluster(option);
056    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
057
058    assertTrue(cluster.waitForActiveAndReadyMaster());
059    HMaster active = cluster.getMaster();
060    // check that the balancer is on by default for the active master
061    ClusterMetrics clusterStatus = active.getClusterMetrics();
062    assertTrue(clusterStatus.getBalancerOn());
063
064    active = killActiveAndWaitForNewActive(cluster);
065
066    // ensure the load balancer is still running on new master
067    clusterStatus = active.getClusterMetrics();
068    assertTrue(clusterStatus.getBalancerOn());
069
070    // turn off the load balancer
071    active.balanceSwitch(false);
072
073    // once more, kill active master and wait for new active master to show up
074    active = killActiveAndWaitForNewActive(cluster);
075
076    // ensure the load balancer is not running on the new master
077    clusterStatus = active.getClusterMetrics();
078    assertFalse(clusterStatus.getBalancerOn());
079
080    // Stop the cluster
081    TEST_UTIL.shutdownMiniCluster();
082  }
083
084  /**
085   * Kill the master and wait for a new active master to show up
086   * @return the new active master
087   */
088  private HMaster killActiveAndWaitForNewActive(SingleProcessHBaseCluster cluster)
089    throws InterruptedException, IOException {
090    int activeIndex = getActiveMasterIndex(cluster);
091    HMaster active = cluster.getMaster();
092    cluster.stopMaster(activeIndex);
093    cluster.waitOnMaster(activeIndex);
094    assertTrue(cluster.waitForActiveAndReadyMaster());
095    // double check this is actually a new master
096    HMaster newActive = cluster.getMaster();
097    assertFalse(active == newActive);
098    return newActive;
099  }
100
101  /**
102   * return the index of the active master in the cluster
103   * @throws org.apache.hadoop.hbase.MasterNotRunningException if no active master found
104   */
105  private int getActiveMasterIndex(SingleProcessHBaseCluster cluster)
106    throws MasterNotRunningException {
107    // get all the master threads
108    List<JVMClusterUtil.MasterThread> masterThreads = cluster.getMasterThreads();
109
110    for (int i = 0; i < masterThreads.size(); i++) {
111      if (masterThreads.get(i).getMaster().isActiveMaster()) {
112        return i;
113      }
114    }
115    throw new MasterNotRunningException();
116  }
117
118}