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.procedure; 019 020import static org.junit.Assert.assertTrue; 021 022import java.util.List; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.Waiter; 028import org.apache.hadoop.hbase.client.RegionInfo; 029import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 030import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 034import org.apache.hadoop.hbase.util.JVMClusterUtil; 035import org.apache.hadoop.hdfs.DistributedFileSystem; 036import org.apache.hadoop.hdfs.MiniDFSCluster; 037import org.apache.hadoop.hdfs.protocol.HdfsConstants; 038import org.junit.After; 039import org.junit.AfterClass; 040import org.junit.Before; 041import org.junit.BeforeClass; 042import org.junit.ClassRule; 043import org.junit.Test; 044import org.junit.experimental.categories.Category; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048@Category(MediumTests.class) 049public class TestSafemodeBringsDownMaster { 050 051 @ClassRule 052 public static final HBaseClassTestRule CLASS_RULE = 053 HBaseClassTestRule.forClass(TestSafemodeBringsDownMaster.class); 054 055 private static final Logger LOG = LoggerFactory.getLogger(TestSafemodeBringsDownMaster.class); 056 057 protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 058 059 private static void setupConf(Configuration conf) { 060 conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1); 061 } 062 063 @BeforeClass 064 public static void setupCluster() throws Exception { 065 setupConf(UTIL.getConfiguration()); 066 UTIL.startMiniCluster(1); 067 } 068 069 @AfterClass 070 public static void cleanupTest() throws Exception { 071 try { 072 UTIL.shutdownMiniCluster(); 073 } catch (Exception e) { 074 LOG.warn("failure shutting down cluster", e); 075 } 076 } 077 078 @Before 079 public void setup() throws Exception { 080 resetProcExecutorTestingKillFlag(); 081 } 082 083 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 084 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 085 } 086 087 private void resetProcExecutorTestingKillFlag() { 088 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 089 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false); 090 assertTrue("expected executor to be running", procExec.isRunning()); 091 } 092 093 @After 094 public void tearDown() throws Exception { 095 } 096 097 @Test 098 public void testSafemodeBringsDownMaster() throws Exception { 099 final TableName tableName = TableName.valueOf("testSafemodeBringsDownMaster"); 100 final byte[][] splitKeys = 101 new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") }; 102 RegionInfo[] regions = MasterProcedureTestingUtility.createTable(getMasterProcedureExecutor(), 103 tableName, splitKeys, "f1", "f2"); 104 MiniDFSCluster dfsCluster = UTIL.getDFSCluster(); 105 DistributedFileSystem dfs = (DistributedFileSystem) dfsCluster.getFileSystem(); 106 dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER); 107 final long timeOut = 180000; 108 long startTime = EnvironmentEdgeManager.currentTime(); 109 int index = -1; 110 do { 111 index = UTIL.getMiniHBaseCluster().getServerWithMeta(); 112 } while (index == -1 && startTime + timeOut < EnvironmentEdgeManager.currentTime()); 113 114 if (index != -1) { 115 UTIL.getMiniHBaseCluster().abortRegionServer(index); 116 UTIL.getMiniHBaseCluster().waitOnRegionServer(index); 117 } 118 UTIL.waitFor(timeOut, new Waiter.Predicate<Exception>() { 119 @Override 120 public boolean evaluate() throws Exception { 121 List<JVMClusterUtil.MasterThread> threads = 122 UTIL.getMiniHBaseCluster().getLiveMasterThreads(); 123 return threads == null || threads.isEmpty(); 124 } 125 }); 126 dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE); 127 } 128}