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