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.EnvironmentEdgeManager;
035import org.apache.hadoop.hbase.util.JVMClusterUtil;
036import org.apache.hadoop.hdfs.DistributedFileSystem;
037import org.apache.hadoop.hdfs.MiniDFSCluster;
038import org.apache.hadoop.hdfs.protocol.HdfsConstants;
039import org.junit.After;
040import org.junit.AfterClass;
041import org.junit.Before;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049@Category(MediumTests.class)
050public class TestSafemodeBringsDownMaster {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestSafemodeBringsDownMaster.class);
055
056  private static final Logger LOG = LoggerFactory.getLogger(TestSafemodeBringsDownMaster.class);
057
058  protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
059
060  private static void setupConf(Configuration conf) {
061    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
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
089  private void resetProcExecutorTestingKillFlag() {
090    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
091    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
092    assertTrue("expected executor to be running", procExec.isRunning());
093  }
094
095  @After
096  public void tearDown() throws Exception {
097  }
098
099  @Test
100  public void testSafemodeBringsDownMaster() throws Exception {
101    final TableName tableName = TableName.valueOf("testSafemodeBringsDownMaster");
102    final byte[][] splitKeys =
103      new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c") };
104    RegionInfo[] regions = MasterProcedureTestingUtility.createTable(getMasterProcedureExecutor(),
105      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 = EnvironmentEdgeManager.currentTime();
111    int index = -1;
112    do {
113      index = UTIL.getMiniHBaseCluster().getServerWithMeta();
114    } while (index == -1 && startTime + timeOut < EnvironmentEdgeManager.currentTime());
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 =
124          UTIL.getMiniHBaseCluster().getLiveMasterThreads();
125        return threads == null || threads.isEmpty();
126      }
127    });
128    dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE);
129  }
130}