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.HBaseTestingUtility;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.Waiter;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer;
030import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
031import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
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 HBaseTestingUtility UTIL = new HBaseTestingUtility();
058
059  private static void setupConf(Configuration conf) {
060    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
061    conf.setInt(MasterProcedureConstants.MASTER_URGENT_PROCEDURE_THREADS, 0);
062    conf.set(BaseLoadBalancer.TABLES_ON_MASTER, "none");
063  }
064
065  @BeforeClass
066  public static void setupCluster() throws Exception {
067    setupConf(UTIL.getConfiguration());
068    UTIL.startMiniCluster(1);
069  }
070
071  @AfterClass
072  public static void cleanupTest() throws Exception {
073    try {
074      UTIL.shutdownMiniCluster();
075    } catch (Exception e) {
076      LOG.warn("failure shutting down cluster", e);
077    }
078  }
079
080  @Before
081  public void setup() throws Exception {
082    resetProcExecutorTestingKillFlag();
083  }
084
085  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
086    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
087  }
088  private void resetProcExecutorTestingKillFlag() {
089    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
090    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
091    assertTrue("expected executor to be running", procExec.isRunning());
092  }
093
094  @After
095  public void tearDown() throws Exception {
096  }
097
098  @Test
099  public void testSafemodeBringsDownMaster() throws Exception {
100    final TableName tableName = TableName.valueOf("testSafemodeBringsDownMaster");
101    final byte[][] splitKeys = new byte[][] {
102      Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
103    };
104    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
105        getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
106    MiniDFSCluster dfsCluster = UTIL.getDFSCluster();
107    DistributedFileSystem dfs = (DistributedFileSystem) dfsCluster.getFileSystem();
108    dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER);
109    final long timeOut = 180000;
110    long startTime = System.currentTimeMillis();
111    int index = -1;
112    do {
113      index = UTIL.getMiniHBaseCluster().getServerWithMeta();
114    } while (index == -1 &&
115      startTime + timeOut < System.currentTimeMillis());
116
117    if (index != -1){
118      UTIL.getMiniHBaseCluster().abortRegionServer(index);
119      UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
120    }
121    UTIL.waitFor(timeOut, new Waiter.Predicate<Exception>() {
122      @Override
123      public boolean evaluate() throws Exception {
124        List<JVMClusterUtil.MasterThread> threads = UTIL.getMiniHBaseCluster().getLiveMasterThreads();
125        return threads == null || threads.isEmpty();
126      }
127    });
128    dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE);
129  }
130}