1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22 import java.util.concurrent.TimeUnit;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.client.Admin;
27 import org.apache.hadoop.hbase.testclassification.IntegrationTests;
28 import org.apache.hadoop.hbase.util.RegionSplitter;
29 import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.ClassRule;
33 import org.junit.Test;
34 import org.junit.experimental.categories.Category;
35 import org.junit.rules.TestRule;
36 import org.junit.rules.Timeout;
37
38
39
40
41
42
43
44 @Category(IntegrationTests.class)
45 public class IntegrationTestManyRegions {
46 private static final String CLASS_NAME
47 = IntegrationTestManyRegions.class.getSimpleName();
48
49 protected static final Log LOG
50 = LogFactory.getLog(IntegrationTestManyRegions.class);
51 protected static final TableName TABLE_NAME = TableName.valueOf(CLASS_NAME);
52 protected static final String REGION_COUNT_KEY
53 = String.format("hbase.%s.regions", CLASS_NAME);
54 protected static final String REGIONSERVER_COUNT_KEY
55 = String.format("hbase.%s.regionServers", CLASS_NAME);
56 protected static final String TIMEOUT_MINUTES_KEY
57 = String.format("hbase.%s.timeoutMinutes", CLASS_NAME);
58
59 protected static final IntegrationTestingUtility UTIL
60 = new IntegrationTestingUtility();
61
62 protected static final int DEFAULT_REGION_COUNT = 1000;
63 protected static final int REGION_COUNT = UTIL.getConfiguration()
64 .getInt(REGION_COUNT_KEY, DEFAULT_REGION_COUNT);
65 protected static final int DEFAULT_REGIONSERVER_COUNT = 1;
66 protected static final int REGION_SERVER_COUNT = UTIL.getConfiguration()
67 .getInt(REGIONSERVER_COUNT_KEY, DEFAULT_REGIONSERVER_COUNT);
68
69
70 protected static final int DEFAULT_TIMEOUT_MINUTES = 5;
71 protected static final int TIMEOUT_MINUTES = UTIL.getConfiguration()
72 .getInt(TIMEOUT_MINUTES_KEY, DEFAULT_TIMEOUT_MINUTES);
73
74 @ClassRule
75 public static final TestRule timeout = Timeout.builder()
76 .withTimeout(TIMEOUT_MINUTES, TimeUnit.MINUTES).withLookingForStuckThread(true)
77 .build();
78
79 private Admin admin;
80
81 @Before
82 public void setUp() throws Exception {
83 LOG.info(String.format("Initializing cluster with %d region servers.",
84 REGION_SERVER_COUNT));
85 UTIL.initializeCluster(REGION_SERVER_COUNT);
86 LOG.info("Cluster initialized");
87
88 admin = UTIL.getHBaseAdmin();
89 if (admin.tableExists(TABLE_NAME)) {
90 LOG.info(String.format("Deleting existing table %s.", TABLE_NAME));
91 if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
92 admin.deleteTable(TABLE_NAME);
93 LOG.info(String.format("Existing table %s deleted.", TABLE_NAME));
94 }
95 LOG.info("Cluster ready");
96 }
97
98 @After
99 public void tearDown() throws IOException {
100 LOG.info("Cleaning up after test.");
101 if (admin.tableExists(TABLE_NAME)) {
102 if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
103 admin.deleteTable(TABLE_NAME);
104 }
105 LOG.info("Restoring cluster.");
106 UTIL.restoreCluster();
107 LOG.info("Cluster restored.");
108 }
109
110 @Test
111 public void testCreateTableWithRegions() throws Exception {
112 HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
113 desc.addFamily(new HColumnDescriptor("cf"));
114 SplitAlgorithm algo = new RegionSplitter.HexStringSplit();
115 byte[][] splits = algo.split(REGION_COUNT);
116
117 LOG.info(String.format("Creating table %s with %d splits.", TABLE_NAME, REGION_COUNT));
118 long startTime = System.currentTimeMillis();
119 try {
120 admin.createTable(desc, splits);
121 LOG.info(String.format("Pre-split table created successfully in %dms.",
122 (System.currentTimeMillis() - startTime)));
123 } catch (IOException e) {
124 LOG.error("Failed to create table", e);
125 }
126 }
127 }