View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.Waiter;
32  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
33  import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.hbase.util.JVMClusterUtil;
37  import org.apache.hadoop.hdfs.DistributedFileSystem;
38  import org.apache.hadoop.hdfs.MiniDFSCluster;
39  import org.apache.hadoop.hdfs.protocol.HdfsConstants;
40  import org.junit.After;
41  import org.junit.AfterClass;
42  import org.junit.Before;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  @Category(MediumTests.class)
48  public class TestSafemodeBringsDownMaster {
49    private static final Log LOG = LogFactory.getLog(TestSafemodeBringsDownMaster.class);
50  
51    protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    private static void setupConf(Configuration conf) {
54      conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
55    }
56  
57    @BeforeClass
58    public static void setupCluster() throws Exception {
59      setupConf(UTIL.getConfiguration());
60      UTIL.startMiniCluster(1);
61    }
62  
63    @AfterClass
64    public static void cleanupTest() throws Exception {
65      try {
66        UTIL.shutdownMiniCluster();
67      } catch (Exception e) {
68        LOG.warn("failure shutting down cluster", e);
69      }
70    }
71  
72    @Before
73    public void setup() throws Exception {
74      resetProcExecutorTestingKillFlag();
75    }
76  
77    private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
78      return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
79    }
80    private void resetProcExecutorTestingKillFlag() {
81      final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
82      ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
83      assertTrue("expected executor to be running", procExec.isRunning());
84    }
85  
86    @After
87    public void tearDown() throws Exception {
88    }
89  
90    @Test(timeout=60000)
91    public void testSafemodeBringsDownMaster() throws Exception {
92      final TableName tableName = TableName.valueOf("testSafemodeBringsDownMaster");
93      final byte[][] splitKeys = new byte[][] {
94        Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
95      };
96      HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
97          getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
98      MiniDFSCluster dfsCluster = UTIL.getDFSCluster();
99      DistributedFileSystem dfs = (DistributedFileSystem) dfsCluster.getFileSystem();
100     dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER);
101     final long timeOut = 180000;
102     long startTime = System.currentTimeMillis();
103     int index = -1;
104     do {
105       index = UTIL.getMiniHBaseCluster().getServerWithMeta();
106     } while (index == -1 &&
107       startTime + timeOut < System.currentTimeMillis());
108 
109     if (index != -1){
110       UTIL.getMiniHBaseCluster().abortRegionServer(index);
111       UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
112     }
113     UTIL.waitFor(timeOut, new Waiter.Predicate<Exception>() {
114       @Override
115       public boolean evaluate() throws Exception {
116         List<JVMClusterUtil.MasterThread> threads = UTIL.getMiniHBaseCluster().getLiveMasterThreads();
117         return threads == null || threads.isEmpty();
118       }
119     });
120     dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE);
121   }
122 }