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
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HRegionInfo;
28 import org.apache.hadoop.hbase.HTableDescriptor;
29 import org.apache.hadoop.hbase.TableExistsException;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
32 import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
33 import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.CreateTableState;
34 import org.apache.hadoop.hbase.testclassification.MediumTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.apache.hadoop.hbase.util.ModifyRegionUtils;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44 import static org.junit.Assert.assertTrue;
45
46 @Category(MediumTests.class)
47 public class TestCreateTableProcedure {
48 private static final Log LOG = LogFactory.getLog(TestCreateTableProcedure.class);
49
50 protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
51
52 private static void setupConf(Configuration conf) {
53 conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
54 }
55
56 @BeforeClass
57 public static void setupCluster() throws Exception {
58 setupConf(UTIL.getConfiguration());
59 UTIL.startMiniCluster(1);
60 }
61
62 @AfterClass
63 public static void cleanupTest() throws Exception {
64 try {
65 UTIL.shutdownMiniCluster();
66 } catch (Exception e) {
67 LOG.warn("failure shutting down cluster", e);
68 }
69 }
70
71 @Before
72 public void setup() throws Exception {
73 resetProcExecutorTestingKillFlag();
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 resetProcExecutorTestingKillFlag();
79 for (HTableDescriptor htd: UTIL.getHBaseAdmin().listTables()) {
80 LOG.info("Tear down, remove table=" + htd.getTableName());
81 UTIL.deleteTable(htd.getTableName());
82 }
83 }
84
85 private void resetProcExecutorTestingKillFlag() {
86 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
87 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
88 assertTrue("expected executor to be running", procExec.isRunning());
89 }
90
91 @Test(timeout=60000)
92 public void testSimpleCreate() throws Exception {
93 final TableName tableName = TableName.valueOf("testSimpleCreate");
94 final byte[][] splitKeys = null;
95 testSimpleCreate(tableName, splitKeys);
96 }
97
98 @Test(timeout=60000)
99 public void testSimpleCreateWithSplits() throws Exception {
100 final TableName tableName = TableName.valueOf("testSimpleCreateWithSplits");
101 final byte[][] splitKeys = new byte[][] {
102 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
103 };
104 testSimpleCreate(tableName, splitKeys);
105 }
106
107 private void testSimpleCreate(final TableName tableName, byte[][] splitKeys) throws Exception {
108 HRegionInfo[] regions = MasterProcedureTestingUtility.createTable(
109 getMasterProcedureExecutor(), tableName, splitKeys, "f1", "f2");
110 MasterProcedureTestingUtility.validateTableCreation(
111 UTIL.getHBaseCluster().getMaster(), tableName, regions, "f1", "f2");
112 }
113
114 @Test(timeout=60000, expected=TableExistsException.class)
115 public void testCreateExisting() throws Exception {
116 final TableName tableName = TableName.valueOf("testCreateExisting");
117 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
118 final HTableDescriptor htd = MasterProcedureTestingUtility.createHTD(tableName, "f");
119 final HRegionInfo[] regions = ModifyRegionUtils.createHRegionInfos(htd, null);
120
121
122 long procId1 = procExec.submitProcedure(
123 new CreateTableProcedure(procExec.getEnvironment(), htd, regions));
124
125
126 ProcedurePrepareLatch latch2 = new ProcedurePrepareLatch.CompatibilityLatch();
127 long procId2 = procExec.submitProcedure(
128 new CreateTableProcedure(procExec.getEnvironment(), htd, regions, latch2));
129
130 ProcedureTestingUtility.waitProcedure(procExec, procId1);
131 ProcedureTestingUtility.assertProcNotFailed(procExec.getResult(procId1));
132
133 ProcedureTestingUtility.waitProcedure(procExec, procId2);
134 latch2.await();
135 }
136
137 @Test(timeout=60000)
138 public void testRecoveryAndDoubleExecution() throws Exception {
139 final TableName tableName = TableName.valueOf("testRecoveryAndDoubleExecution");
140
141
142 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
143 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
144
145
146 byte[][] splitKeys = null;
147 HTableDescriptor htd = MasterProcedureTestingUtility.createHTD(tableName, "f1", "f2");
148 HRegionInfo[] regions = ModifyRegionUtils.createHRegionInfos(htd, splitKeys);
149 long procId = procExec.submitProcedure(
150 new CreateTableProcedure(procExec.getEnvironment(), htd, regions));
151
152
153
154
155 MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(
156 procExec, procId, 6, CreateTableState.values());
157
158 MasterProcedureTestingUtility.validateTableCreation(
159 UTIL.getHBaseCluster().getMaster(), tableName, regions, "f1", "f2");
160 }
161
162 @Test(timeout=90000)
163 public void testRollbackAndDoubleExecution() throws Exception {
164 final TableName tableName = TableName.valueOf("testRollbackAndDoubleExecution");
165
166
167 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
168 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
169
170
171 final byte[][] splitKeys = new byte[][] {
172 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
173 };
174 HTableDescriptor htd = MasterProcedureTestingUtility.createHTD(tableName, "f1", "f2");
175 htd.setRegionReplication(3);
176 HRegionInfo[] regions = ModifyRegionUtils.createHRegionInfos(htd, splitKeys);
177 long procId = procExec.submitProcedure(
178 new CreateTableProcedure(procExec.getEnvironment(), htd, regions));
179
180
181
182 MasterProcedureTestingUtility.testRollbackAndDoubleExecution(
183 procExec, procId, 4, CreateTableState.values());
184
185 MasterProcedureTestingUtility.validateTableDeletion(
186 UTIL.getHBaseCluster().getMaster(), tableName, regions, "f1", "f2");
187
188
189 resetProcExecutorTestingKillFlag();
190 testSimpleCreate(tableName, splitKeys);
191 }
192
193 @Test(timeout=90000)
194 public void testRollbackRetriableFailure() throws Exception {
195 final TableName tableName = TableName.valueOf("testRollbackRetriableFailure");
196
197
198 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
199 ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);
200
201
202 final byte[][] splitKeys = new byte[][] {
203 Bytes.toBytes("a"), Bytes.toBytes("b"), Bytes.toBytes("c")
204 };
205 HTableDescriptor htd = MasterProcedureTestingUtility.createHTD(tableName, "f1", "f2");
206 HRegionInfo[] regions = ModifyRegionUtils.createHRegionInfos(htd, splitKeys);
207 long procId = procExec.submitProcedure(
208 new CreateTableProcedure(procExec.getEnvironment(), htd, regions));
209
210
211
212 MasterProcedureTestingUtility.testRollbackRetriableFailure(
213 procExec, procId, 4, CreateTableState.values());
214
215 MasterProcedureTestingUtility.validateTableDeletion(
216 UTIL.getHBaseCluster().getMaster(), tableName, regions, "f1", "f2");
217
218
219 resetProcExecutorTestingKillFlag();
220 testSimpleCreate(tableName, splitKeys);
221 }
222
223 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
224 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
225 }
226
227 public static class FaultyCreateTableProcedure extends CreateTableProcedure {
228 private int retries = 0;
229
230 public FaultyCreateTableProcedure() {
231
232 }
233
234 public FaultyCreateTableProcedure(final MasterProcedureEnv env,
235 final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions)
236 throws IOException {
237 super(env, hTableDescriptor, newRegions);
238 }
239
240 @Override
241 protected void rollbackState(final MasterProcedureEnv env, final CreateTableState state)
242 throws IOException {
243 if (retries++ < 3) {
244 LOG.info("inject rollback failure state=" + state);
245 throw new IOException("injected failure number " + retries);
246 } else {
247 super.rollbackState(env, state);
248 retries = 0;
249 }
250 }
251 }
252 }