HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMasterFailover.java
bloba27936df07a45d342570bbfde3cb6d037fbd76a9
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
24 import java.util.List;
25 import org.apache.hadoop.hbase.ClusterMetrics;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseTestingUtility;
28 import org.apache.hadoop.hbase.MiniHBaseCluster;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.apache.hadoop.hbase.StartMiniClusterOption;
31 import org.apache.hadoop.hbase.master.RegionState.State;
32 import org.apache.hadoop.hbase.regionserver.HRegionServer;
33 import org.apache.hadoop.hbase.testclassification.FlakeyTests;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
36 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
37 import org.junit.ClassRule;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.experimental.categories.Category;
41 import org.junit.rules.TestName;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @Category({FlakeyTests.class, LargeTests.class})
46 public class TestMasterFailover {
48 @ClassRule
49 public static final HBaseClassTestRule CLASS_RULE =
50 HBaseClassTestRule.forClass(TestMasterFailover.class);
52 private static final Logger LOG = LoggerFactory.getLogger(TestMasterFailover.class);
53 @Rule public TestName name = new TestName();
55 /**
56 * Simple test of master failover.
57 * <p>
58 * Starts with three masters. Kills a backup master. Then kills the active
59 * master. Ensures the final master becomes active and we can still contact
60 * the cluster.
62 @Test
63 public void testSimpleMasterFailover() throws Exception {
64 final int NUM_MASTERS = 3;
65 final int NUM_RS = 3;
67 // Start the cluster
68 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
69 try {
70 StartMiniClusterOption option = StartMiniClusterOption.builder()
71 .numMasters(NUM_MASTERS).numRegionServers(NUM_RS).numDataNodes(NUM_RS).build();
72 TEST_UTIL.startMiniCluster(option);
73 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
75 // get all the master threads
76 List<MasterThread> masterThreads = cluster.getMasterThreads();
78 // wait for each to come online
79 for (MasterThread mt : masterThreads) {
80 assertTrue(mt.isAlive());
83 // verify only one is the active master and we have right number
84 int numActive = 0;
85 int activeIndex = -1;
86 ServerName activeName = null;
87 HMaster active = null;
88 for (int i = 0; i < masterThreads.size(); i++) {
89 if (masterThreads.get(i).getMaster().isActiveMaster()) {
90 numActive++;
91 activeIndex = i;
92 active = masterThreads.get(activeIndex).getMaster();
93 activeName = active.getServerName();
96 assertEquals(1, numActive);
97 assertEquals(NUM_MASTERS, masterThreads.size());
98 LOG.info("Active master " + activeName);
100 // Check that ClusterStatus reports the correct active and backup masters
101 assertNotNull(active);
102 ClusterMetrics status = active.getClusterMetrics();
103 assertTrue(status.getMasterName().equals(activeName));
104 assertEquals(2, status.getBackupMasterNames().size());
106 // attempt to stop one of the inactive masters
107 int backupIndex = (activeIndex == 0 ? 1 : activeIndex - 1);
108 HMaster master = cluster.getMaster(backupIndex);
109 LOG.debug("\n\nStopping a backup master: " + master.getServerName() + "\n");
110 cluster.stopMaster(backupIndex, false);
111 cluster.waitOnMaster(backupIndex);
113 // Verify still one active master and it's the same
114 for (int i = 0; i < masterThreads.size(); i++) {
115 if (masterThreads.get(i).getMaster().isActiveMaster()) {
116 assertTrue(activeName.equals(masterThreads.get(i).getMaster().getServerName()));
117 activeIndex = i;
118 active = masterThreads.get(activeIndex).getMaster();
121 assertEquals(1, numActive);
122 assertEquals(2, masterThreads.size());
123 int rsCount = masterThreads.get(activeIndex).getMaster().getClusterMetrics()
124 .getLiveServerMetrics().size();
125 LOG.info("Active master " + active.getServerName() + " managing " + rsCount +
126 " regions servers");
127 assertEquals(3, rsCount);
129 // Check that ClusterStatus reports the correct active and backup masters
130 assertNotNull(active);
131 status = active.getClusterMetrics();
132 assertTrue(status.getMasterName().equals(activeName));
133 assertEquals(1, status.getBackupMasterNames().size());
135 // kill the active master
136 LOG.debug("\n\nStopping the active master " + active.getServerName() + "\n");
137 cluster.stopMaster(activeIndex, false);
138 cluster.waitOnMaster(activeIndex);
140 // wait for an active master to show up and be ready
141 assertTrue(cluster.waitForActiveAndReadyMaster());
143 LOG.debug("\n\nVerifying backup master is now active\n");
144 // should only have one master now
145 assertEquals(1, masterThreads.size());
147 // and he should be active
148 active = masterThreads.get(0).getMaster();
149 assertNotNull(active);
150 status = active.getClusterMetrics();
151 ServerName mastername = status.getMasterName();
152 assertTrue(mastername.equals(active.getServerName()));
153 assertTrue(active.isActiveMaster());
154 assertEquals(0, status.getBackupMasterNames().size());
155 int rss = status.getLiveServerMetrics().size();
156 LOG.info("Active master " + mastername.getServerName() + " managing " +
157 rss + " region servers");
158 assertEquals(3, rss);
159 } finally {
160 // Stop the cluster
161 TEST_UTIL.shutdownMiniCluster();
166 * Test meta in transition when master failover.
167 * This test used to manipulate region state up in zk. That is not allowed any more in hbase2
168 * so I removed that messing. That makes this test anemic.
170 @Test
171 public void testMetaInTransitionWhenMasterFailover() throws Exception {
172 // Start the cluster
173 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
174 TEST_UTIL.startMiniCluster();
175 try {
176 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
177 LOG.info("Cluster started");
179 HMaster activeMaster = cluster.getMaster();
180 ServerName metaServerName = cluster.getServerHoldingMeta();
181 HRegionServer hrs = cluster.getRegionServer(metaServerName);
183 // Now kill master, meta should remain on rs, where we placed it before.
184 LOG.info("Aborting master");
185 activeMaster.abort("test-kill");
186 cluster.waitForMasterToStop(activeMaster.getServerName(), 30000);
187 LOG.info("Master has aborted");
189 // meta should remain where it was
190 RegionState metaState = MetaTableLocator.getMetaRegionState(hrs.getZooKeeper());
191 assertEquals("hbase:meta should be online on RS",
192 metaState.getServerName(), metaServerName);
193 assertEquals("hbase:meta should be online on RS", State.OPEN, metaState.getState());
195 // Start up a new master
196 LOG.info("Starting up a new master");
197 activeMaster = cluster.startMaster().getMaster();
198 LOG.info("Waiting for master to be ready");
199 cluster.waitForActiveAndReadyMaster();
200 LOG.info("Master is ready");
202 // ensure meta is still deployed on RS
203 metaState = MetaTableLocator.getMetaRegionState(activeMaster.getZooKeeper());
204 assertEquals("hbase:meta should be online on RS",
205 metaState.getServerName(), metaServerName);
206 assertEquals("hbase:meta should be online on RS", State.OPEN, metaState.getState());
208 // Done, shutdown the cluster
209 } finally {
210 TEST_UTIL.shutdownMiniCluster();