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.HBaseTestingUtility; 028import org.apache.hadoop.hbase.MasterNotRunningException; 029import org.apache.hadoop.hbase.MiniHBaseCluster; 030import org.apache.hadoop.hbase.StartMiniClusterOption; 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 n 048 */ 049 @Test 050 public void testMasterFailoverBalancerPersistence() throws Exception { 051 // Start the cluster 052 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 053 054 StartMiniClusterOption option = StartMiniClusterOption.builder().numMasters(3).build(); 055 TEST_UTIL.startMiniCluster(option); 056 MiniHBaseCluster 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 n * @return the new active master n 086 * * @throws java.io.IOException 087 */ 088 private HMaster killActiveAndWaitForNewActive(MiniHBaseCluster 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(MiniHBaseCluster cluster) throws MasterNotRunningException { 106 // get all the master threads 107 List<JVMClusterUtil.MasterThread> masterThreads = cluster.getMasterThreads(); 108 109 for (int i = 0; i < masterThreads.size(); i++) { 110 if (masterThreads.get(i).getMaster().isActiveMaster()) { 111 return i; 112 } 113 } 114 throw new MasterNotRunningException(); 115 } 116 117}