1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master.procedure;
20
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.DoNotRetryIOException;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.HTableDescriptor;
32 import org.apache.hadoop.hbase.ProcedureInfo;
33 import org.apache.hadoop.hbase.TableExistsException;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.Waiter;
36 import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
37 import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
38 import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.CreateTableState;
39 import org.apache.hadoop.hbase.testclassification.MediumTests;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.apache.hadoop.hbase.util.JVMClusterUtil;
42 import org.apache.hadoop.hbase.util.ModifyRegionUtils;
43 import org.apache.hadoop.hdfs.DistributedFileSystem;
44 import org.apache.hadoop.hdfs.MiniDFSCluster;
45 import org.apache.hadoop.hdfs.protocol.HdfsConstants;
46 import org.junit.After;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.junit.experimental.categories.Category;
52
53 import static org.junit.Assert.assertEquals;
54 import static org.junit.Assert.assertTrue;
55
56 @Category(MediumTests.class)
57 public class TestSafemodeBringsDownMaster {
58 private static final Log LOG = LogFactory.getLog(TestSafemodeBringsDownMaster.class);
59
60 protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
61
62 private static void setupConf(Configuration conf) {
63 conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
64 }
65
66 @BeforeClass
67 public static void setupCluster() throws Exception {
68 setupConf(UTIL.getConfiguration());
69 UTIL.startMiniCluster(1);
70 }
71
72 @AfterClass
73 public static void cleanupTest() throws Exception {
74 try {
75 UTIL.shutdownMiniCluster();
76 } catch (Exception e) {
77 LOG.warn("failure shutting down cluster", e);
78 }
79 }
80
81 @Before
82 public void setup() throws Exception {
83 resetProcExecutorTestingKillFlag();
84 }
85
86 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
87 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
88 }
89 private void resetProcExecutorTestingKillFlag() {
90 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
91 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
92 assertTrue("expected executor to be running", procExec.isRunning());
93 }
94
95 @After
96 public void tearDown() throws Exception {
97 }
98
99 @Test(timeout=60000)
100 public void testSafemodeBringsDownMaster() throws Exception {
101 final TableName tableName = TableName.valueOf("testSafemodeBringsDownMaster");
102 final byte[][] splitKeys = new byte[][] {
103 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
104 };
105 HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
106 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
107 MiniDFSCluster dfsCluster = UTIL.getDFSCluster();
108 DistributedFileSystem dfs = (DistributedFileSystem) dfsCluster.getFileSystem();
109 dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_ENTER);
110 final long timeOut = 180000;
111 long startTime = System.currentTimeMillis();
112 int index = -1;
113 do {
114 index = UTIL.getMiniHBaseCluster().getServerWithMeta();
115 } while (index == -1 &&
116 startTime + timeOut < System.currentTimeMillis());
117
118 if (index != -1){
119 UTIL.getMiniHBaseCluster().abortRegionServer(index);
120 UTIL.getMiniHBaseCluster().waitOnRegionServer(index);
121 }
122 UTIL.waitFor(timeOut, new Waiter.Predicate<Exception>() {
123 @Override
124 public boolean evaluate() throws Exception {
125 List<JVMClusterUtil.MasterThread> threads = UTIL.getMiniHBaseCluster().getLiveMasterThreads();
126 return threads == null || threads.isEmpty();
127 }
128 });
129 dfs.setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE);
130 }
131 }