1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.HColumnDescriptor;
35 import org.apache.hadoop.hbase.HConstants;
36 import org.apache.hadoop.hbase.HRegionInfo;
37 import org.apache.hadoop.hbase.HRegionLocation;
38 import org.apache.hadoop.hbase.HTableDescriptor;
39 import org.apache.hadoop.hbase.testclassification.LargeTests;
40 import org.apache.hadoop.hbase.MasterNotRunningException;
41 import org.apache.hadoop.hbase.MiniHBaseCluster;
42 import org.apache.hadoop.hbase.NotServingRegionException;
43 import org.apache.hadoop.hbase.ServerName;
44 import org.apache.hadoop.hbase.TableExistsException;
45 import org.apache.hadoop.hbase.TableName;
46 import org.apache.hadoop.hbase.TableNotDisabledException;
47 import org.apache.hadoop.hbase.TableNotEnabledException;
48 import org.apache.hadoop.hbase.TableNotFoundException;
49 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
50 import org.apache.hadoop.hbase.constraint.ConstraintException;
51 import org.apache.hadoop.hbase.master.AssignmentManager;
52 import org.apache.hadoop.hbase.master.HMaster;
53 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
54 import org.apache.hadoop.hbase.regionserver.HRegionServer;
55 import org.apache.hadoop.hbase.regionserver.Region;
56 import org.apache.hadoop.hbase.util.Bytes;
57 import org.apache.hadoop.hbase.util.Pair;
58 import org.apache.hadoop.hbase.wal.DefaultWALProvider;
59 import org.junit.After;
60 import org.junit.AfterClass;
61 import org.junit.Assert;
62 import org.junit.Before;
63 import org.junit.BeforeClass;
64 import org.junit.Test;
65 import org.junit.experimental.categories.Category;
66
67 import com.google.protobuf.ServiceException;
68
69
70
71
72
73
74
75 @Category(LargeTests.class)
76 public class TestAdmin2 {
77 final Log LOG = LogFactory.getLog(getClass());
78 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
79 private Admin admin;
80
81 @BeforeClass
82 public static void setUpBeforeClass() throws Exception {
83 TEST_UTIL.getConfiguration().setBoolean("hbase.online.schema.update.enable", true);
84 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
85 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
86 TEST_UTIL.getConfiguration().setInt("hbase.client.retries.number", 6);
87 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.metahandler.count", 30);
88 TEST_UTIL.getConfiguration().setBoolean(
89 "hbase.master.enabletable.roundrobin", true);
90 TEST_UTIL.startMiniCluster(3);
91 }
92
93 @AfterClass
94 public static void tearDownAfterClass() throws Exception {
95 TEST_UTIL.shutdownMiniCluster();
96 }
97
98 @Before
99 public void setUp() throws Exception {
100 this.admin = TEST_UTIL.getHBaseAdmin();
101 }
102
103 @After
104 public void tearDown() throws Exception {
105 for (HTableDescriptor htd : this.admin.listTables()) {
106 TEST_UTIL.deleteTable(htd.getName());
107 }
108 }
109
110 @Test (timeout=300000)
111 public void testCreateBadTables() throws IOException {
112 String msg = null;
113 try {
114 this.admin.createTable(new HTableDescriptor(TableName.META_TABLE_NAME));
115 } catch(TableExistsException e) {
116 msg = e.toString();
117 }
118 assertTrue("Unexcepted exception message " + msg, msg != null &&
119 msg.startsWith(TableExistsException.class.getName()) &&
120 msg.contains(TableName.META_TABLE_NAME.getNameAsString()));
121
122
123 final HTableDescriptor threadDesc =
124 new HTableDescriptor(TableName.valueOf("threaded_testCreateBadTables"));
125 threadDesc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
126 int count = 10;
127 Thread [] threads = new Thread [count];
128 final AtomicInteger successes = new AtomicInteger(0);
129 final AtomicInteger failures = new AtomicInteger(0);
130 final Admin localAdmin = this.admin;
131 for (int i = 0; i < count; i++) {
132 threads[i] = new Thread(Integer.toString(i)) {
133 @Override
134 public void run() {
135 try {
136 localAdmin.createTable(threadDesc);
137 successes.incrementAndGet();
138 } catch (TableExistsException e) {
139 failures.incrementAndGet();
140 } catch (IOException e) {
141 throw new RuntimeException("Failed threaded create" + getName(), e);
142 }
143 }
144 };
145 }
146 for (int i = 0; i < count; i++) {
147 threads[i].start();
148 }
149 for (int i = 0; i < count; i++) {
150 while(threads[i].isAlive()) {
151 try {
152 Thread.sleep(100);
153 } catch (InterruptedException e) {
154
155 }
156 }
157 }
158
159
160 assertEquals(1, successes.get());
161 assertEquals(count - 1, failures.get());
162 }
163
164
165
166
167
168 @Test (timeout=300000)
169 public void testTableNameClash() throws Exception {
170 String name = "testTableNameClash";
171 HTableDescriptor htd1 = new HTableDescriptor(TableName.valueOf(name + "SOMEUPPERCASE"));
172 HTableDescriptor htd2 = new HTableDescriptor(TableName.valueOf(name));
173 htd1.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
174 htd2.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
175 admin.createTable(htd1);
176 admin.createTable(htd2);
177
178 new HTable(TEST_UTIL.getConfiguration(), htd2.getTableName()).close();
179 }
180
181
182
183
184
185
186
187
188 @Test (timeout=300000)
189 public void testCreateTableRPCTimeOut() throws Exception {
190 String name = "testCreateTableRPCTimeOut";
191 int oldTimeout = TEST_UTIL.getConfiguration().
192 getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT);
193 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, 1500);
194 try {
195 int expectedRegions = 100;
196
197 byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
198 byte [] endKey = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
199 Admin hbaseadmin = new HBaseAdmin(TEST_UTIL.getConfiguration());
200 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name));
201 htd.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
202 hbaseadmin.createTable(htd, startKey, endKey, expectedRegions);
203 hbaseadmin.close();
204 } finally {
205 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, oldTimeout);
206 }
207 }
208
209
210
211
212
213 @Test (timeout=300000)
214 public void testReadOnlyTable() throws Exception {
215 TableName name = TableName.valueOf("testReadOnlyTable");
216 Table table = TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
217 byte[] value = Bytes.toBytes("somedata");
218
219 Put put = new Put(value);
220 put.add(HConstants.CATALOG_FAMILY, HConstants.CATALOG_FAMILY, value);
221 table.put(put);
222 table.close();
223 }
224
225
226
227
228
229
230 @Test (timeout=300000)
231 public void testTableNames() throws IOException {
232 byte[][] illegalNames = new byte[][] {
233 Bytes.toBytes("-bad"),
234 Bytes.toBytes(".bad")
235 };
236 for (byte[] illegalName : illegalNames) {
237 try {
238 new HTableDescriptor(TableName.valueOf(illegalName));
239 throw new IOException("Did not detect '" +
240 Bytes.toString(illegalName) + "' as an illegal user table name");
241 } catch (IllegalArgumentException e) {
242
243 }
244 }
245 byte[] legalName = Bytes.toBytes("g-oo.d");
246 try {
247 new HTableDescriptor(TableName.valueOf(legalName));
248 } catch (IllegalArgumentException e) {
249 throw new IOException("Legal user table name: '" +
250 Bytes.toString(legalName) + "' caused IllegalArgumentException: " +
251 e.getMessage());
252 }
253 }
254
255
256
257
258
259 @Test (expected=TableExistsException.class, timeout=300000)
260 public void testTableExistsExceptionWithATable() throws IOException {
261 final TableName name = TableName.valueOf("testTableExistsExceptionWithATable");
262 TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY).close();
263 TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
264 }
265
266
267
268
269
270 @Test (expected=TableNotEnabledException.class, timeout=300000)
271 public void testTableNotEnabledExceptionWithATable() throws IOException {
272 final TableName name = TableName.valueOf("testTableNotEnabledExceptionWithATable");
273 TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY).close();
274 this.admin.disableTable(name);
275 this.admin.disableTable(name);
276 }
277
278
279
280
281
282 @Test (expected=TableNotDisabledException.class, timeout=300000)
283 public void testTableNotDisabledExceptionWithATable() throws IOException {
284 final TableName name = TableName.valueOf("testTableNotDisabledExceptionWithATable");
285 Table t = TEST_UTIL.createTable(name, HConstants.CATALOG_FAMILY);
286 try {
287 this.admin.enableTable(name);
288 }finally {
289 t.close();
290 }
291 }
292
293
294
295
296
297 @Test (expected=TableNotFoundException.class, timeout=300000)
298 public void testTableNotFoundExceptionWithoutAnyTables() throws IOException {
299 TableName tableName = TableName
300 .valueOf("testTableNotFoundExceptionWithoutAnyTables");
301 Table ht = new HTable(TEST_UTIL.getConfiguration(), tableName);
302 ht.get(new Get("e".getBytes()));
303 }
304
305
306 @Test (timeout=300000)
307 public void testShouldCloseTheRegionBasedOnTheEncodedRegionName()
308 throws Exception {
309 TableName TABLENAME =
310 TableName.valueOf("TestHBACloseRegion");
311 createTableWithDefaultConf(TABLENAME);
312
313 HRegionInfo info = null;
314 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME);
315 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
316 for (HRegionInfo regionInfo : onlineRegions) {
317 if (!regionInfo.getTable().isSystemTable()) {
318 info = regionInfo;
319 admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(), rs
320 .getServerName().getServerName());
321 }
322 }
323 boolean isInList = ProtobufUtil.getOnlineRegions(
324 rs.getRSRpcServices()).contains(info);
325 long timeout = System.currentTimeMillis() + 10000;
326 while ((System.currentTimeMillis() < timeout) && (isInList)) {
327 Thread.sleep(100);
328 isInList = ProtobufUtil.getOnlineRegions(
329 rs.getRSRpcServices()).contains(info);
330 }
331
332 assertFalse("The region should not be present in online regions list.",
333 isInList);
334 }
335
336 @Test (timeout=300000)
337 public void testCloseRegionIfInvalidRegionNameIsPassed() throws Exception {
338 byte[] TABLENAME = Bytes.toBytes("TestHBACloseRegion1");
339 createTableWithDefaultConf(TABLENAME);
340
341 HRegionInfo info = null;
342 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TableName.valueOf(TABLENAME));
343 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
344 for (HRegionInfo regionInfo : onlineRegions) {
345 if (!regionInfo.isMetaTable()) {
346 if (regionInfo.getRegionNameAsString().contains("TestHBACloseRegion1")) {
347 info = regionInfo;
348 try {
349 admin.closeRegionWithEncodedRegionName("sample", rs.getServerName()
350 .getServerName());
351 } catch (NotServingRegionException nsre) {
352
353 }
354 }
355 }
356 }
357 onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
358 assertTrue("The region should be present in online regions list.",
359 onlineRegions.contains(info));
360 }
361
362 @Test (timeout=300000)
363 public void testCloseRegionThatFetchesTheHRIFromMeta() throws Exception {
364 TableName TABLENAME =
365 TableName.valueOf("TestHBACloseRegion2");
366 createTableWithDefaultConf(TABLENAME);
367
368 HRegionInfo info = null;
369 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLENAME);
370 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
371 for (HRegionInfo regionInfo : onlineRegions) {
372 if (!regionInfo.isMetaTable()) {
373
374 if (regionInfo.getRegionNameAsString().contains("TestHBACloseRegion2")) {
375 info = regionInfo;
376 admin.closeRegion(regionInfo.getRegionNameAsString(), rs
377 .getServerName().getServerName());
378 }
379 }
380 }
381
382 boolean isInList = ProtobufUtil.getOnlineRegions(
383 rs.getRSRpcServices()).contains(info);
384 long timeout = System.currentTimeMillis() + 10000;
385 while ((System.currentTimeMillis() < timeout) && (isInList)) {
386 Thread.sleep(100);
387 isInList = ProtobufUtil.getOnlineRegions(
388 rs.getRSRpcServices()).contains(info);
389 }
390
391 assertFalse("The region should not be present in online regions list.",
392 isInList);
393 }
394
395 @Test (timeout=300000)
396 public void testCloseRegionWhenServerNameIsNull() throws Exception {
397 byte[] TABLENAME = Bytes.toBytes("TestHBACloseRegion3");
398 createTableWithDefaultConf(TABLENAME);
399
400 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TableName.valueOf(TABLENAME));
401
402 try {
403 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
404 for (HRegionInfo regionInfo : onlineRegions) {
405 if (!regionInfo.isMetaTable()) {
406 if (regionInfo.getRegionNameAsString()
407 .contains("TestHBACloseRegion3")) {
408 admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(),
409 null);
410 }
411 }
412 }
413 fail("The test should throw exception if the servername passed is null.");
414 } catch (IllegalArgumentException e) {
415 }
416 }
417
418
419 @Test (timeout=300000)
420 public void testCloseRegionWhenServerNameIsEmpty() throws Exception {
421 byte[] TABLENAME = Bytes.toBytes("TestHBACloseRegionWhenServerNameIsEmpty");
422 createTableWithDefaultConf(TABLENAME);
423
424 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TableName.valueOf(TABLENAME));
425
426 try {
427 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
428 for (HRegionInfo regionInfo : onlineRegions) {
429 if (!regionInfo.isMetaTable()) {
430 if (regionInfo.getRegionNameAsString()
431 .contains("TestHBACloseRegionWhenServerNameIsEmpty")) {
432 admin.closeRegionWithEncodedRegionName(regionInfo.getEncodedName(),
433 " ");
434 }
435 }
436 }
437 fail("The test should throw exception if the servername passed is empty.");
438 } catch (IllegalArgumentException e) {
439 }
440 }
441
442 @Test (timeout=300000)
443 public void testCloseRegionWhenEncodedRegionNameIsNotGiven() throws Exception {
444 byte[] TABLENAME = Bytes.toBytes("TestHBACloseRegion4");
445 createTableWithDefaultConf(TABLENAME);
446
447 HRegionInfo info = null;
448 HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TableName.valueOf(TABLENAME));
449
450 List<HRegionInfo> onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
451 for (HRegionInfo regionInfo : onlineRegions) {
452 if (!regionInfo.isMetaTable()) {
453 if (regionInfo.getRegionNameAsString().contains("TestHBACloseRegion4")) {
454 info = regionInfo;
455 try {
456 admin.closeRegionWithEncodedRegionName(regionInfo
457 .getRegionNameAsString(), rs.getServerName().getServerName());
458 } catch (NotServingRegionException nsre) {
459
460 }
461 }
462 }
463 }
464 onlineRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
465 assertTrue("The region should be present in online regions list.",
466 onlineRegions.contains(info));
467 }
468
469 private HBaseAdmin createTable(byte[] TABLENAME) throws IOException {
470
471 Configuration config = TEST_UTIL.getConfiguration();
472 HBaseAdmin admin = new HBaseAdmin(config);
473
474 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TABLENAME));
475 HColumnDescriptor hcd = new HColumnDescriptor("value");
476
477 htd.addFamily(hcd);
478 admin.createTable(htd, null);
479 return admin;
480 }
481
482 private void createTableWithDefaultConf(byte[] TABLENAME) throws IOException {
483 createTableWithDefaultConf(TableName.valueOf(TABLENAME));
484 }
485
486 private void createTableWithDefaultConf(TableName TABLENAME) throws IOException {
487 HTableDescriptor htd = new HTableDescriptor(TABLENAME);
488 HColumnDescriptor hcd = new HColumnDescriptor("value");
489 htd.addFamily(hcd);
490
491 admin.createTable(htd, null);
492 }
493
494
495
496
497
498 @Test (timeout=300000)
499 public void testGetTableRegions() throws IOException {
500
501 final TableName tableName = TableName.valueOf("testGetTableRegions");
502
503 int expectedRegions = 10;
504
505
506 byte [] startKey = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
507 byte [] endKey = { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
508
509
510 HTableDescriptor desc = new HTableDescriptor(tableName);
511 desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
512 admin.createTable(desc, startKey, endKey, expectedRegions);
513
514 List<HRegionInfo> RegionInfos = admin.getTableRegions(tableName);
515
516 assertEquals("Tried to create " + expectedRegions + " regions " +
517 "but only found " + RegionInfos.size(),
518 expectedRegions, RegionInfos.size());
519
520 }
521
522 @Test (timeout=300000)
523 public void testWALRollWriting() throws Exception {
524 setUpforLogRolling();
525 String className = this.getClass().getName();
526 StringBuilder v = new StringBuilder(className);
527 while (v.length() < 1000) {
528 v.append(className);
529 }
530 byte[] value = Bytes.toBytes(v.toString());
531 HRegionServer regionServer = startAndWriteData(TableName.valueOf("TestLogRolling"), value);
532 LOG.info("after writing there are "
533 + DefaultWALProvider.getNumRolledLogFiles(regionServer.getWAL(null)) + " log files");
534
535
536 for (Region r : regionServer.getOnlineRegionsLocalContext()) {
537 r.flush(true);
538 }
539 admin.rollWALWriter(regionServer.getServerName());
540 int count = DefaultWALProvider.getNumRolledLogFiles(regionServer.getWAL(null));
541 LOG.info("after flushing all regions and rolling logs there are " +
542 count + " log files");
543 assertTrue(("actual count: " + count), count <= 2);
544 }
545
546 @Test (timeout=300000)
547 public void testMoveToPreviouslyAssignedRS() throws IOException, InterruptedException {
548 byte[] tableName = Bytes.toBytes("testMoveToPreviouslyAssignedRS");
549 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
550 HMaster master = cluster.getMaster();
551 HBaseAdmin localAdmin = createTable(tableName);
552 List<HRegionInfo> tableRegions = localAdmin.getTableRegions(tableName);
553 HRegionInfo hri = tableRegions.get(0);
554 AssignmentManager am = master.getAssignmentManager();
555 assertTrue("Region " + hri.getRegionNameAsString()
556 + " should be assigned properly", am.waitForAssignment(hri));
557 ServerName server = am.getRegionStates().getRegionServerOfRegion(hri);
558 localAdmin.move(hri.getEncodedNameAsBytes(), Bytes.toBytes(server.getServerName()));
559 assertEquals("Current region server and region server before move should be same.", server,
560 am.getRegionStates().getRegionServerOfRegion(hri));
561 }
562
563
564 private void setUpforLogRolling() {
565
566 TEST_UTIL.getConfiguration().setLong(HConstants.HREGION_MAX_FILESIZE,
567 768L * 1024L);
568
569
570 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.maxlogentries", 32);
571
572 TEST_UTIL.getConfiguration().setInt(
573 "hbase.regionserver.logroll.errors.tolerated", 2);
574 TEST_UTIL.getConfiguration().setInt("hbase.rpc.timeout", 10 * 1000);
575
576
577 TEST_UTIL.getConfiguration().setInt(
578 "hbase.hregion.memstore.optionalflushcount", 2);
579
580
581 TEST_UTIL.getConfiguration().setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE,
582 8192);
583
584
585 TEST_UTIL.getConfiguration().setLong("hbase.client.pause", 10 * 1000);
586
587
588
589 TEST_UTIL.getConfiguration().setInt(HConstants.THREAD_WAKE_FREQUENCY,
590 2 * 1000);
591
592
593
594 TEST_UTIL.getConfiguration().setBoolean("dfs.support.append", true);
595
596
597 TEST_UTIL.getConfiguration().setInt("dfs.namenode.heartbeat.recheck-interval", 5000);
598 TEST_UTIL.getConfiguration().setInt("dfs.heartbeat.interval", 1);
599
600
601 TEST_UTIL.getConfiguration().setInt("dfs.client.block.write.retries", 30);
602 TEST_UTIL.getConfiguration().setInt(
603 "hbase.regionserver.hlog.tolerable.lowreplication", 2);
604 TEST_UTIL.getConfiguration().setInt(
605 "hbase.regionserver.hlog.lowreplication.rolllimit", 3);
606 }
607
608 private HRegionServer startAndWriteData(TableName tableName, byte[] value)
609 throws IOException, InterruptedException {
610
611 new HTable(
612 TEST_UTIL.getConfiguration(), TableName.META_TABLE_NAME).close();
613
614
615 HTableDescriptor desc = new HTableDescriptor(tableName);
616 desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY));
617 admin.createTable(desc);
618 Table table = new HTable(TEST_UTIL.getConfiguration(), tableName);
619
620 HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(tableName);
621 for (int i = 1; i <= 256; i++) {
622 Put put = new Put(Bytes.toBytes("row" + String.format("%1$04d", i)));
623 put.add(HConstants.CATALOG_FAMILY, null, value);
624 table.put(put);
625 if (i % 32 == 0) {
626
627 try {
628 Thread.sleep(2000);
629 } catch (InterruptedException e) {
630
631 }
632 }
633 }
634
635 table.close();
636 return regionServer;
637 }
638
639
640
641
642 @Test (timeout=300000)
643 public void testCheckHBaseAvailableClosesConnection() throws Exception {
644 Configuration conf = TEST_UTIL.getConfiguration();
645
646 int initialCount = HConnectionTestingUtility.getConnectionCount();
647 HBaseAdmin.checkHBaseAvailable(conf);
648 int finalCount = HConnectionTestingUtility.getConnectionCount();
649
650 Assert.assertEquals(initialCount, finalCount) ;
651 }
652
653
654
655
656 @Test (timeout=300000)
657 public void testCheckHBaseAvailableWithoutCluster() {
658 Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
659
660
661 conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT,
662 conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, 9999)+10);
663
664 int initialCount = HConnectionTestingUtility.getConnectionCount();
665
666 long start = System.currentTimeMillis();
667 try {
668 HBaseAdmin.checkHBaseAvailable(conf);
669 assertTrue(false);
670 } catch (MasterNotRunningException ignored) {
671 } catch (ZooKeeperConnectionException ignored) {
672 } catch (ServiceException ignored) {
673 } catch (IOException ignored) {
674 }
675 long end = System.currentTimeMillis();
676
677 int finalCount = HConnectionTestingUtility.getConnectionCount();
678
679 Assert.assertEquals(initialCount, finalCount) ;
680
681 LOG.info("It took "+(end-start)+" ms to find out that" +
682 " HBase was not available");
683 }
684
685 @Test (timeout=300000)
686 public void testDisableCatalogTable() throws Exception {
687 try {
688 this.admin.disableTable(TableName.META_TABLE_NAME);
689 fail("Expected to throw ConstraintException");
690 } catch (ConstraintException e) {
691 }
692
693
694 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("testDisableCatalogTable".getBytes()));
695 HColumnDescriptor hcd = new HColumnDescriptor("cf1".getBytes());
696 htd.addFamily(hcd);
697 TEST_UTIL.getHBaseAdmin().createTable(htd);
698 }
699
700 @Test (timeout=300000)
701 public void testIsEnabledOrDisabledOnUnknownTable() throws Exception {
702 try {
703 admin.isTableEnabled(TableName.valueOf("unkownTable"));
704 fail("Test should fail if isTableEnabled called on unknown table.");
705 } catch (IOException e) {
706 }
707
708 try {
709 admin.isTableDisabled(TableName.valueOf("unkownTable"));
710 fail("Test should fail if isTableDisabled called on unknown table.");
711 } catch (IOException e) {
712 }
713 }
714
715 @Test (timeout=300000)
716 public void testGetRegion() throws Exception {
717
718
719 HBaseAdmin rawAdmin = new HBaseAdmin(TEST_UTIL.getConfiguration());
720
721 final TableName tableName = TableName.valueOf("testGetRegion");
722 LOG.info("Started " + tableName);
723 HTable t = TEST_UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY);
724
725 HRegionLocation regionLocation = t.getRegionLocation("mmm");
726 HRegionInfo region = regionLocation.getRegionInfo();
727 byte[] regionName = region.getRegionName();
728 Pair<HRegionInfo, ServerName> pair = rawAdmin.getRegion(regionName);
729 assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
730 pair = rawAdmin.getRegion(region.getEncodedNameAsBytes());
731 assertTrue(Bytes.equals(regionName, pair.getFirst().getRegionName()));
732 }
733
734 @Test(timeout = 30000)
735 public void testBalancer() throws Exception {
736 boolean initialState = admin.isBalancerEnabled();
737
738
739 boolean prevState = admin.setBalancerRunning(!initialState, true);
740
741
742 assertEquals(initialState, prevState);
743
744
745 assertEquals(!initialState, admin.isBalancerEnabled());
746
747
748 prevState = admin.setBalancerRunning(initialState, true);
749
750
751 assertEquals(!initialState, prevState);
752
753 assertEquals(initialState, admin.isBalancerEnabled());
754 }
755 }