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.set(BaseLoadBalancer.TABLES_ON_MASTER, "none");
062  }
063
064  @BeforeClass
065  public static void setupCluster() throws Exception {
066    setupConf(UTIL.getConfiguration());
067    UTIL.startMiniCluster(1);
068  }
069
070  @AfterClass
071  public static void cleanupTest() throws Exception {
072    try {
073      UTIL.shutdownMiniCluster();
074    } catch (Exception e) {
075      LOG.warn("failure shutting down cluster", e);
076    }
077  }
078
079  @Before
080  public void setup() throws Exception {
081    resetProcExecutorTestingKillFlag();
082  }
083
084  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
085    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
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 = new byte[][] {
101      Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
102    };
103    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
104        getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
105    MiniDFSCluster dfsCluster = UTIL.getDFSCluster();
106    DistributedFileSystem dfs = (DistributedFileSystem) dfsCluster.getFileSystem();
107    dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER);
108    final long timeOut = 180000;
109    long startTime = System.currentTimeMillis();
110    int index = -1;
111    do {
112      index = UTIL.getMiniHBaseCluster().getServerWithMeta();
113    } while (index == -1 &&
114      startTime + timeOut < System.currentTimeMillis());
115
116    if (index != -1){
117      UTIL.getMiniHBaseCluster().abortRegionServer(index);
118      UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
119    }
120    UTIL.waitFor(timeOut, new Waiter.Predicate<Exception>() {
121      @Override
122      public boolean evaluate() throws Exception {
123        List<JVMClusterUtil.MasterThread> threads = UTIL.getMiniHBaseCluster().getLiveMasterThreads();
124        return threads == null || threads.isEmpty();
125      }
126    });
127    dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE);
128  }
129}