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