1
2
3
4
5
6
7
8
9
10
11 package org.apache.hadoop.hbase.namespace;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19
20 import java.io.IOException;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.concurrent.CountDownLatch;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.Coprocessor;
33 import org.apache.hadoop.hbase.CoprocessorEnvironment;
34 import org.apache.hadoop.hbase.DoNotRetryIOException;
35 import org.apache.hadoop.hbase.HBaseTestingUtility;
36 import org.apache.hadoop.hbase.HColumnDescriptor;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.HRegionInfo;
39 import org.apache.hadoop.hbase.HTableDescriptor;
40 import org.apache.hadoop.hbase.MiniHBaseCluster;
41 import org.apache.hadoop.hbase.NamespaceDescriptor;
42 import org.apache.hadoop.hbase.TableName;
43 import org.apache.hadoop.hbase.Waiter;
44 import org.apache.hadoop.hbase.client.Connection;
45 import org.apache.hadoop.hbase.client.ConnectionFactory;
46 import org.apache.hadoop.hbase.client.HBaseAdmin;
47 import org.apache.hadoop.hbase.client.HTable;
48 import org.apache.hadoop.hbase.client.Mutation;
49 import org.apache.hadoop.hbase.client.Table;
50 import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
51 import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
52 import org.apache.hadoop.hbase.coprocessor.BaseRegionServerObserver;
53 import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
54 import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
55 import org.apache.hadoop.hbase.coprocessor.ObserverContext;
56 import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
57 import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
58 import org.apache.hadoop.hbase.coprocessor.RegionServerObserver;
59 import org.apache.hadoop.hbase.mapreduce.TableInputFormatBase;
60 import org.apache.hadoop.hbase.master.HMaster;
61 import org.apache.hadoop.hbase.master.RegionState;
62 import org.apache.hadoop.hbase.master.RegionStates;
63 import org.apache.hadoop.hbase.master.TableNamespaceManager;
64 import org.apache.hadoop.hbase.quotas.MasterQuotaManager;
65 import org.apache.hadoop.hbase.quotas.QuotaExceededException;
66 import org.apache.hadoop.hbase.quotas.QuotaUtil;
67 import org.apache.hadoop.hbase.regionserver.HRegion;
68 import org.apache.hadoop.hbase.regionserver.HRegionServer;
69 import org.apache.hadoop.hbase.regionserver.Region;
70 import org.apache.hadoop.hbase.regionserver.RegionServerCoprocessorHost;
71 import org.apache.hadoop.hbase.testclassification.MediumTests;
72 import org.apache.hadoop.hbase.util.Bytes;
73 import org.apache.hadoop.hbase.util.FSUtils;
74 import org.apache.zookeeper.KeeperException;
75 import org.junit.After;
76 import org.junit.AfterClass;
77 import org.junit.BeforeClass;
78 import org.junit.Test;
79 import org.junit.experimental.categories.Category;
80
81 import com.google.common.collect.Sets;
82
83 @Category(MediumTests.class)
84 public class TestNamespaceAuditor {
85 private static final Log LOG = LogFactory.getLog(TestNamespaceAuditor.class);
86 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
87 private static HBaseAdmin ADMIN;
88 private String prefix = "TestNamespaceAuditor";
89
90 @BeforeClass
91 public static void before() throws Exception {
92 UTIL.getConfiguration().setBoolean("hbase.assignment.usezk", true);
93 setupOnce();
94 }
95
96 public static void setupOnce() throws Exception, IOException {
97 Configuration conf = UTIL.getConfiguration();
98 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 5);
99 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, CustomObserver.class.getName());
100 conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, MasterSyncObserver.class.getName());
101 conf.setBoolean(QuotaUtil.QUOTA_CONF_KEY, true);
102 conf.setClass("hbase.coprocessor.regionserver.classes", CPRegionServerObserver.class,
103 RegionServerObserver.class);
104 UTIL.startMiniCluster(1, 1);
105 waitForQuotaEnabled();
106 ADMIN = UTIL.getHBaseAdmin();
107 }
108
109 @AfterClass
110 public static void tearDown() throws Exception {
111 UTIL.shutdownMiniCluster();
112 }
113
114 @After
115 public void cleanup() throws Exception, KeeperException {
116 for (HTableDescriptor table : ADMIN.listTables()) {
117 ADMIN.disableTable(table.getTableName());
118 deleteTable(table.getTableName());
119 }
120 for (NamespaceDescriptor ns : ADMIN.listNamespaceDescriptors()) {
121 if (ns.getName().startsWith(prefix)) {
122 ADMIN.deleteNamespace(ns.getName());
123 }
124 }
125 assertTrue("Quota manager not enabled", UTIL.getHBaseCluster().getMaster()
126 .getMasterQuotaManager().isQuotaEnabled());
127 }
128
129 @Test(timeout = 60000)
130 public void testTableOperations() throws Exception {
131 String nsp = prefix + "_np2";
132 NamespaceDescriptor nspDesc =
133 NamespaceDescriptor.create(nsp)
134 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "5")
135 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
136 ADMIN.createNamespace(nspDesc);
137 assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
138 assertEquals(ADMIN.listNamespaceDescriptors().length, 3);
139 HTableDescriptor tableDescOne =
140 new HTableDescriptor(TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1"));
141 HTableDescriptor tableDescTwo =
142 new HTableDescriptor(TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table2"));
143 HTableDescriptor tableDescThree =
144 new HTableDescriptor(TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table3"));
145 ADMIN.createTable(tableDescOne);
146 boolean constraintViolated = false;
147 try {
148 ADMIN.createTable(tableDescTwo, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 5);
149 } catch (Exception exp) {
150 assertTrue(exp instanceof IOException);
151 constraintViolated = true;
152 } finally {
153 assertTrue("Constraint not violated for table " + tableDescTwo.getTableName(),
154 constraintViolated);
155 }
156 ADMIN.createTable(tableDescTwo, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);
157 NamespaceTableAndRegionInfo nspState = getQuotaManager().getState(nsp);
158 assertNotNull(nspState);
159 assertTrue(nspState.getTables().size() == 2);
160 assertTrue(nspState.getRegionCount() == 5);
161 constraintViolated = false;
162 try {
163 ADMIN.createTable(tableDescThree);
164 } catch (Exception exp) {
165 assertTrue(exp instanceof IOException);
166 constraintViolated = true;
167 } finally {
168 assertTrue("Constraint not violated for table " + tableDescThree.getTableName(),
169 constraintViolated);
170 }
171 }
172
173 @Test
174 public void testValidQuotas() throws Exception {
175 boolean exceptionCaught = false;
176 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
177 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
178 NamespaceDescriptor nspDesc =
179 NamespaceDescriptor.create(prefix + "vq1")
180 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "hihdufh")
181 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
182 try {
183 ADMIN.createNamespace(nspDesc);
184 } catch (Exception exp) {
185 LOG.warn(exp);
186 exceptionCaught = true;
187 } finally {
188 assertTrue(exceptionCaught);
189 assertFalse(fs.exists(FSUtils.getNamespaceDir(rootDir, nspDesc.getName())));
190 }
191 nspDesc =
192 NamespaceDescriptor.create(prefix + "vq2")
193 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "-456")
194 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
195 try {
196 ADMIN.createNamespace(nspDesc);
197 } catch (Exception exp) {
198 LOG.warn(exp);
199 exceptionCaught = true;
200 } finally {
201 assertTrue(exceptionCaught);
202 assertFalse(fs.exists(FSUtils.getNamespaceDir(rootDir, nspDesc.getName())));
203 }
204 nspDesc =
205 NamespaceDescriptor.create(prefix + "vq3")
206 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "10")
207 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "sciigd").build();
208 try {
209 ADMIN.createNamespace(nspDesc);
210 } catch (Exception exp) {
211 LOG.warn(exp);
212 exceptionCaught = true;
213 } finally {
214 assertTrue(exceptionCaught);
215 assertFalse(fs.exists(FSUtils.getNamespaceDir(rootDir, nspDesc.getName())));
216 }
217 nspDesc =
218 NamespaceDescriptor.create(prefix + "vq4")
219 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "10")
220 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "-1500").build();
221 try {
222 ADMIN.createNamespace(nspDesc);
223 } catch (Exception exp) {
224 LOG.warn(exp);
225 exceptionCaught = true;
226 } finally {
227 assertTrue(exceptionCaught);
228 assertFalse(fs.exists(FSUtils.getNamespaceDir(rootDir, nspDesc.getName())));
229 }
230 }
231
232 @Test
233 public void testDeleteTable() throws Exception {
234 String namespace = prefix + "_dummy";
235 NamespaceDescriptor nspDesc =
236 NamespaceDescriptor.create(namespace)
237 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "100")
238 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "3").build();
239 ADMIN.createNamespace(nspDesc);
240 assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(namespace));
241 NamespaceTableAndRegionInfo stateInfo = getNamespaceState(nspDesc.getName());
242 assertNotNull("Namespace state found null for " + namespace, stateInfo);
243 HTableDescriptor tableDescOne =
244 new HTableDescriptor(TableName.valueOf(namespace + TableName.NAMESPACE_DELIM + "table1"));
245 HTableDescriptor tableDescTwo =
246 new HTableDescriptor(TableName.valueOf(namespace + TableName.NAMESPACE_DELIM + "table2"));
247 ADMIN.createTable(tableDescOne);
248 ADMIN.createTable(tableDescTwo, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 5);
249 stateInfo = getNamespaceState(nspDesc.getName());
250 assertNotNull("Namespace state found to be null.", stateInfo);
251 assertEquals(2, stateInfo.getTables().size());
252 assertEquals(5, stateInfo.getRegionCountOfTable(tableDescTwo.getTableName()));
253 assertEquals(6, stateInfo.getRegionCount());
254 ADMIN.disableTable(tableDescOne.getTableName());
255 deleteTable(tableDescOne.getTableName());
256 stateInfo = getNamespaceState(nspDesc.getName());
257 assertNotNull("Namespace state found to be null.", stateInfo);
258 assertEquals(5, stateInfo.getRegionCount());
259 assertEquals(1, stateInfo.getTables().size());
260 ADMIN.disableTable(tableDescTwo.getTableName());
261 deleteTable(tableDescTwo.getTableName());
262 ADMIN.deleteNamespace(namespace);
263 stateInfo = getNamespaceState(namespace);
264 assertNull("Namespace state not found to be null.", stateInfo);
265 }
266
267 public static class CPRegionServerObserver extends BaseRegionServerObserver {
268 private volatile boolean shouldFailMerge = false;
269
270 public void failMerge(boolean fail) {
271 shouldFailMerge = fail;
272 }
273
274 private boolean triggered = false;
275
276 public synchronized void waitUtilTriggered() throws InterruptedException {
277 while (!triggered) {
278 wait();
279 }
280 }
281
282 @Override
283 public synchronized void preMerge(ObserverContext<RegionServerCoprocessorEnvironment> ctx,
284 Region regionA, Region regionB) throws IOException {
285 triggered = true;
286 notifyAll();
287 if (shouldFailMerge) {
288 throw new IOException("fail merge");
289 }
290 }
291 }
292
293 @Test
294 public void testRegionMerge() throws Exception {
295 String nsp1 = prefix + "_regiontest";
296 NamespaceDescriptor nspDesc =
297 NamespaceDescriptor.create(nsp1)
298 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "3")
299 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
300 ADMIN.createNamespace(nspDesc);
301 final TableName tableTwo = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table2");
302 byte[] columnFamily = Bytes.toBytes("info");
303 HTableDescriptor tableDescOne = new HTableDescriptor(tableTwo);
304 tableDescOne.addFamily(new HColumnDescriptor(columnFamily));
305 final int initialRegions = 3;
306 ADMIN.createTable(tableDescOne, Bytes.toBytes("1"), Bytes.toBytes("2000"), initialRegions);
307 try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
308 Table table = connection.getTable(tableTwo)) {
309 UTIL.loadNumericRows(table, Bytes.toBytes("info"), 1000, 1999);
310 }
311 ADMIN.flush(tableTwo);
312 List<HRegionInfo> hris = ADMIN.getTableRegions(tableTwo);
313 Collections.sort(hris);
314
315 final Set<String> encodedRegionNamesToMerge =
316 Sets.newHashSet(hris.get(0).getEncodedName(), hris.get(1).getEncodedName());
317 ADMIN.mergeRegions(hris.get(0).getEncodedNameAsBytes(), hris.get(1).getEncodedNameAsBytes(),
318 false);
319 waitForMergeToComplete(tableTwo, encodedRegionNamesToMerge);
320 hris = ADMIN.getTableRegions(tableTwo);
321 assertEquals(initialRegions - 1, hris.size());
322 Collections.sort(hris);
323
324 final HRegionInfo hriToSplit = hris.get(1);
325 ADMIN.split(tableTwo, Bytes.toBytes("500"));
326
327 UTIL.waitFor(10000, 100, new Waiter.ExplainingPredicate<Exception>() {
328
329 @Override
330 public boolean evaluate() throws Exception {
331 RegionStates regionStates =
332 UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
333 for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
334 if (hri.getEncodedName().equals(hriToSplit.getEncodedName())) {
335 return false;
336 }
337 if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
338 return false;
339 }
340 }
341 return true;
342 }
343
344 @Override
345 public String explainFailure() throws Exception {
346 RegionStates regionStates =
347 UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
348 for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
349 if (hri.getEncodedName().equals(hriToSplit.getEncodedName())) {
350 return hriToSplit + " which is expected to be split is still online";
351 }
352 if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
353 return hri + " is still in not opened";
354 }
355 }
356 return "Unknown";
357 }
358 });
359 hris = ADMIN.getTableRegions(tableTwo);
360 assertEquals(initialRegions, hris.size());
361 Collections.sort(hris);
362
363
364 MiniHBaseCluster cluster = UTIL.getHBaseCluster();
365 HRegionServer regionServer = cluster.getRegionServer(0);
366 RegionServerCoprocessorHost cpHost = regionServer.getRegionServerCoprocessorHost();
367 Coprocessor coprocessor = cpHost.findCoprocessor(CPRegionServerObserver.class.getName());
368 CPRegionServerObserver regionServerObserver = (CPRegionServerObserver) coprocessor;
369 regionServerObserver.failMerge(true);
370 regionServerObserver.triggered = false;
371
372 ADMIN.mergeRegions(hris.get(1).getEncodedNameAsBytes(), hris.get(2).getEncodedNameAsBytes(),
373 false);
374 regionServerObserver.waitUtilTriggered();
375 hris = ADMIN.getTableRegions(tableTwo);
376 assertEquals(initialRegions, hris.size());
377 Collections.sort(hris);
378
379 HRegionInfo hriToSplit2 = hris.get(1);
380 ADMIN.split(tableTwo,
381 TableInputFormatBase.getSplitKey(hriToSplit2.getStartKey(), hriToSplit2.getEndKey(), true));
382 waitForMergeToComplete(tableTwo, encodedRegionNamesToMerge);
383 assertEquals(initialRegions, ADMIN.getTableRegions(tableTwo).size());
384 }
385
386 private void waitForMergeToComplete(final TableName tableTwo,
387 final Set<String> encodedRegionNamesToMerge) throws Exception {
388 UTIL.waitFor(10000, 100, new Waiter.ExplainingPredicate<Exception>() {
389
390 @Override
391 public boolean evaluate() throws Exception {
392 RegionStates regionStates =
393 UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
394 for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
395 if (encodedRegionNamesToMerge.contains(hri.getEncodedName())) {
396 return false;
397 }
398 if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
399 return false;
400 }
401 }
402 return true;
403 }
404
405 @Override
406 public String explainFailure() throws Exception {
407 RegionStates regionStates =
408 UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
409 for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
410 if (encodedRegionNamesToMerge.contains(hri.getEncodedName())) {
411 return hri + " which is expected to be merged is still online";
412 }
413 if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
414 return hri + " is still in not opened";
415 }
416 }
417 return "Unknown";
418 }
419 });
420 }
421
422 @Test
423 public void testRegionOperations() throws Exception {
424 String nsp1 = prefix + "_regiontest";
425 NamespaceDescriptor nspDesc =
426 NamespaceDescriptor.create(nsp1)
427 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "2")
428 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
429 ADMIN.createNamespace(nspDesc);
430 boolean constraintViolated = false;
431 final TableName tableOne = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table1");
432 byte[] columnFamily = Bytes.toBytes("info");
433 HTableDescriptor tableDescOne = new HTableDescriptor(tableOne);
434 tableDescOne.addFamily(new HColumnDescriptor(columnFamily));
435 NamespaceTableAndRegionInfo stateInfo;
436 try {
437 ADMIN.createTable(tableDescOne, Bytes.toBytes("1"), Bytes.toBytes("1000"), 7);
438 } catch (Exception exp) {
439 assertTrue(exp instanceof DoNotRetryIOException);
440 LOG.info(exp);
441 constraintViolated = true;
442 } finally {
443 assertTrue(constraintViolated);
444 }
445 assertFalse(ADMIN.tableExists(tableOne));
446
447 ADMIN.createTable(tableDescOne);
448 Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
449 HTable htable = (HTable) connection.getTable(tableOne);
450 UTIL.loadNumericRows(htable, Bytes.toBytes("info"), 1, 1000);
451 ADMIN.flush(tableOne);
452 stateInfo = getNamespaceState(nsp1);
453 assertEquals(1, stateInfo.getTables().size());
454 assertEquals(1, stateInfo.getRegionCount());
455 restartMaster();
456 ADMIN.split(tableOne, Bytes.toBytes("500"));
457 HRegion actualRegion = UTIL.getHBaseCluster().getRegions(tableOne).get(0);
458 CustomObserver observer =
459 (CustomObserver) actualRegion.getCoprocessorHost().findCoprocessor(
460 CustomObserver.class.getName());
461 assertNotNull(observer);
462 observer.postSplit.await();
463 assertEquals(2, ADMIN.getTableRegions(tableOne).size());
464 actualRegion = UTIL.getHBaseCluster().getRegions(tableOne).get(0);
465 observer =
466 (CustomObserver) actualRegion.getCoprocessorHost().findCoprocessor(
467 CustomObserver.class.getName());
468 assertNotNull(observer);
469 ADMIN.split(
470 tableOne,
471 getSplitKey(actualRegion.getRegionInfo().getStartKey(), actualRegion.getRegionInfo()
472 .getEndKey()));
473 observer.postSplit.await();
474
475 List<HRegionInfo> hris = ADMIN.getTableRegions(tableOne);
476 assertEquals(2, hris.size());
477 assertTrue("split completed", observer.preSplitBeforePONR.getCount() == 1);
478
479 htable.close();
480 }
481
482
483
484
485
486
487 @Test(timeout = 180000)
488 public void testRecreateTableWithSameNameAfterFirstTimeFailure() throws Exception {
489 String nsp1 = prefix + "_testRecreateTable";
490 NamespaceDescriptor nspDesc =
491 NamespaceDescriptor.create(nsp1)
492 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "20")
493 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "1").build();
494 ADMIN.createNamespace(nspDesc);
495 final TableName tableOne = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table1");
496 byte[] columnFamily = Bytes.toBytes("info");
497 HTableDescriptor tableDescOne = new HTableDescriptor(tableOne);
498 tableDescOne.addFamily(new HColumnDescriptor(columnFamily));
499 MasterSyncObserver.throwExceptionInPreCreateTable = true;
500 try {
501 try {
502 ADMIN.createTable(tableDescOne);
503 fail("Table " + tableOne.toString() + "creation should fail.");
504 } catch (Exception exp) {
505 LOG.error(exp);
506 }
507 assertFalse(ADMIN.tableExists(tableOne));
508
509 NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp1);
510 assertEquals("First table creation failed in namespace so number of tables in namespace "
511 + "should be 0.", 0, nstate.getTables().size());
512
513 MasterSyncObserver.throwExceptionInPreCreateTable = false;
514 try {
515 ADMIN.createTable(tableDescOne);
516 } catch (Exception e) {
517 fail("Table " + tableOne.toString() + "creation should succeed.");
518 LOG.error(e);
519 }
520 assertTrue(ADMIN.tableExists(tableOne));
521 nstate = getNamespaceState(nsp1);
522 assertEquals("First table was created successfully so table size in namespace should "
523 + "be one now.", 1, nstate.getTables().size());
524 } finally {
525 MasterSyncObserver.throwExceptionInPreCreateTable = false;
526 if (ADMIN.tableExists(tableOne)) {
527 ADMIN.disableTable(tableOne);
528 deleteTable(tableOne);
529 }
530 ADMIN.deleteNamespace(nsp1);
531 }
532 }
533
534 private NamespaceTableAndRegionInfo getNamespaceState(String namespace) throws KeeperException,
535 IOException {
536 return getQuotaManager().getState(namespace);
537 }
538
539 byte[] getSplitKey(byte[] startKey, byte[] endKey) {
540 String skey = Bytes.toString(startKey);
541 int key;
542 if (StringUtils.isBlank(skey)) {
543 key = Integer.parseInt(Bytes.toString(endKey)) / 2;
544 } else {
545 key = (int) (Integer.parseInt(skey) * 1.5);
546 }
547 return Bytes.toBytes("" + key);
548 }
549
550 public static class CustomObserver extends BaseRegionObserver {
551 volatile CountDownLatch postSplit;
552 volatile CountDownLatch preSplitBeforePONR;
553
554 @Override
555 public void postCompleteSplit(ObserverContext<RegionCoprocessorEnvironment> ctx)
556 throws IOException {
557 postSplit.countDown();
558 }
559
560 @Override
561 public void preSplitBeforePONR(ObserverContext<RegionCoprocessorEnvironment> ctx,
562 byte[] splitKey, List<Mutation> metaEntries) throws IOException {
563 preSplitBeforePONR.countDown();
564 }
565
566 @Override
567 public void start(CoprocessorEnvironment e) throws IOException {
568 postSplit = new CountDownLatch(1);
569 preSplitBeforePONR = new CountDownLatch(1);
570 }
571 }
572
573 @Test
574 public void testStatePreserve() throws Exception {
575 final String nsp1 = prefix + "_testStatePreserve";
576 NamespaceDescriptor nspDesc =
577 NamespaceDescriptor.create(nsp1)
578 .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "20")
579 .addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "10").build();
580 ADMIN.createNamespace(nspDesc);
581 TableName tableOne = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table1");
582 TableName tableTwo = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table2");
583 TableName tableThree = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table3");
584 HTableDescriptor tableDescOne = new HTableDescriptor(tableOne);
585 HTableDescriptor tableDescTwo = new HTableDescriptor(tableTwo);
586 HTableDescriptor tableDescThree = new HTableDescriptor(tableThree);
587 ADMIN.createTable(tableDescOne, Bytes.toBytes("1"), Bytes.toBytes("1000"), 3);
588 ADMIN.createTable(tableDescTwo, Bytes.toBytes("1"), Bytes.toBytes("1000"), 3);
589 ADMIN.createTable(tableDescThree, Bytes.toBytes("1"), Bytes.toBytes("1000"), 4);
590 ADMIN.disableTable(tableThree);
591 deleteTable(tableThree);
592
593 UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
594 @Override
595 public boolean evaluate() throws Exception {
596 return (getNamespaceState(nsp1).getTables().size() == 2);
597 }
598 });
599 NamespaceTableAndRegionInfo before = getNamespaceState(nsp1);
600 restartMaster();
601 NamespaceTableAndRegionInfo after = getNamespaceState(nsp1);
602 assertEquals("Expected: " + before.getTables() + " Found: " + after.getTables(), before
603 .getTables().size(), after.getTables().size());
604 }
605
606 private static void waitForQuotaEnabled() throws Exception {
607 UTIL.waitFor(60000, new Waiter.Predicate<Exception>() {
608 @Override
609 public boolean evaluate() throws Exception {
610 HMaster master = UTIL.getHBaseCluster().getMaster();
611 if (master == null) {
612 return false;
613 }
614 MasterQuotaManager quotaManager = master.getMasterQuotaManager();
615 return quotaManager != null && quotaManager.isQuotaEnabled();
616 }
617 });
618 }
619
620 private void restartMaster() throws Exception {
621 UTIL.getHBaseCluster().getMaster(0).stop("Stopping to start again");
622 UTIL.getHBaseCluster().waitOnMaster(0);
623 UTIL.getHBaseCluster().startMaster();
624 waitForQuotaEnabled();
625 }
626
627 private NamespaceAuditor getQuotaManager() {
628 return UTIL.getHBaseCluster().getMaster().getMasterQuotaManager().getNamespaceQuotaManager();
629 }
630
631 public static class MasterSyncObserver extends BaseMasterObserver {
632 volatile CountDownLatch tableDeletionLatch;
633 static boolean throwExceptionInPreCreateTable;
634
635 @Override
636 public void preDeleteTable(ObserverContext<MasterCoprocessorEnvironment> ctx,
637 TableName tableName) throws IOException {
638 tableDeletionLatch = new CountDownLatch(1);
639 }
640
641 @Override
642 public void postDeleteTableHandler(final ObserverContext<MasterCoprocessorEnvironment> ctx,
643 TableName tableName) throws IOException {
644 tableDeletionLatch.countDown();
645 }
646
647 @Override
648 public void preCreateTable(ObserverContext<MasterCoprocessorEnvironment> ctx,
649 HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
650 if (throwExceptionInPreCreateTable) {
651 throw new IOException("Throw exception as it is demanded.");
652 }
653 }
654 }
655
656 private void deleteTable(final TableName tableName) throws Exception {
657
658
659 MasterSyncObserver observer =
660 (MasterSyncObserver) UTIL.getHBaseCluster().getMaster().getMasterCoprocessorHost()
661 .findCoprocessor(MasterSyncObserver.class.getName());
662 ADMIN.deleteTable(tableName);
663 observer.tableDeletionLatch.await();
664 }
665
666 @Test(expected = QuotaExceededException.class, timeout = 30000)
667 public void testExceedTableQuotaInNamespace() throws Exception {
668 String nsp = prefix + "_testExceedTableQuotaInNamespace";
669 NamespaceDescriptor nspDesc =
670 NamespaceDescriptor.create(nsp).addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "1")
671 .build();
672 ADMIN.createNamespace(nspDesc);
673 assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
674 assertEquals(ADMIN.listNamespaceDescriptors().length, 3);
675 HTableDescriptor tableDescOne =
676 new HTableDescriptor(TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1"));
677 HTableDescriptor tableDescTwo =
678 new HTableDescriptor(TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table2"));
679 ADMIN.createTable(tableDescOne);
680 ADMIN.createTable(tableDescTwo, Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);
681 }
682 }