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.handler;
20
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HColumnDescriptor;
33 import org.apache.hadoop.hbase.HRegionInfo;
34 import org.apache.hadoop.hbase.HTableDescriptor;
35 import org.apache.hadoop.hbase.MiniHBaseCluster;
36 import org.apache.hadoop.hbase.Server;
37 import org.apache.hadoop.hbase.TableName;
38 import org.apache.hadoop.hbase.master.HMaster;
39 import org.apache.hadoop.hbase.master.MasterFileSystem;
40 import org.apache.hadoop.hbase.master.MasterServices;
41 import org.apache.hadoop.hbase.master.RegionState.State;
42 import org.apache.hadoop.hbase.master.RegionStates;
43 import org.apache.hadoop.hbase.testclassification.MasterTests;
44 import org.apache.hadoop.hbase.testclassification.MediumTests;
45 import org.apache.hadoop.hbase.util.Bytes;
46 import org.apache.hadoop.hbase.util.FSUtils;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51
52 @Category({MasterTests.class, MediumTests.class})
53 public class TestCreateTableHandler {
54 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55 private static final Log LOG = LogFactory.getLog(TestCreateTableHandler.class);
56 private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
57 private static boolean throwException = false;
58
59
60 @Before
61 public void setUp() throws Exception {
62 TEST_UTIL.startMiniCluster(1);
63 }
64
65 @After
66 public void tearDown() throws Exception {
67 TEST_UTIL.shutdownMiniCluster();
68 throwException = false;
69 }
70
71 @Test (timeout=300000)
72 public void testCreateTableCalledTwiceAndFirstOneInProgress() throws Exception {
73 final TableName tableName = TableName.valueOf("testCreateTableCalledTwiceAndFirstOneInProgress");
74 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
75 final HMaster m = cluster.getMaster();
76 final HTableDescriptor desc = new HTableDescriptor(tableName);
77 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
78 final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getTableName(), null,
79 null) };
80 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
81 desc, cluster.getConfiguration(), hRegionInfos, m);
82 handler.prepare();
83 throwException = true;
84 handler.process();
85 throwException = false;
86 CustomCreateTableHandler handler1 = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
87 desc, cluster.getConfiguration(), hRegionInfos, m);
88 handler1.prepare();
89 handler1.process();
90 for (int i = 0; i < 100; i++) {
91 if (!TEST_UTIL.getHBaseAdmin().isTableAvailable(tableName)) {
92 Thread.sleep(200);
93 }
94 }
95 assertTrue(TEST_UTIL.getHBaseAdmin().isTableEnabled(tableName));
96 }
97
98 @Test (timeout=300000)
99 public void testCreateTableWithSplitRegion() throws Exception {
100 final TableName tableName = TableName.valueOf("testCreateTableWithSplitRegion");
101 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
102 final HMaster m = cluster.getMaster();
103 final HTableDescriptor desc = new HTableDescriptor(tableName);
104 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
105 byte[] splitPoint = Bytes.toBytes("split-point");
106 long ts = System.currentTimeMillis();
107 HRegionInfo d1 = new HRegionInfo(desc.getTableName(), null, splitPoint, false, ts);
108 HRegionInfo d2 = new HRegionInfo(desc.getTableName(), splitPoint, null, false, ts + 1);
109 HRegionInfo parent = new HRegionInfo(desc.getTableName(), null, null, true, ts + 2);
110 parent.setOffline(true);
111
112 Path tempdir = m.getMasterFileSystem().getTempDir();
113 FileSystem fs = m.getMasterFileSystem().getFileSystem();
114 Path tempTableDir = FSUtils.getTableDir(tempdir, desc.getTableName());
115 fs.delete(tempTableDir, true);
116
117 final HRegionInfo[] hRegionInfos = new HRegionInfo[] {d1, d2, parent};
118 CreateTableHandler handler = new CreateTableHandler(m, m.getMasterFileSystem(),
119 desc, cluster.getConfiguration(), hRegionInfos, m);
120 handler.prepare();
121 handler.process();
122 for (int i = 0; i < 100; i++) {
123 if (!TEST_UTIL.getHBaseAdmin().isTableAvailable(tableName)) {
124 Thread.sleep(300);
125 }
126 }
127 assertTrue(TEST_UTIL.getHBaseAdmin().isTableEnabled(tableName));
128 assertTrue(TEST_UTIL.getHBaseAdmin().isTableAvailable(tableName));
129 assertTrue(TEST_UTIL.getHBaseAdmin().isTableAvailable(tableName, new byte[][] { splitPoint }));
130 RegionStates regionStates = m.getAssignmentManager().getRegionStates();
131 assertTrue("Parent should be in SPLIT state",
132 regionStates.isRegionInState(parent, State.SPLIT));
133 }
134
135 @Test (timeout=60000)
136 public void testMasterRestartAfterEnablingNodeIsCreated() throws Exception {
137 byte[] tableName = Bytes.toBytes("testMasterRestartAfterEnablingNodeIsCreated");
138 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
139 final HMaster m = cluster.getMaster();
140 final HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
141 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
142 final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getTableName(), null,
143 null) };
144 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
145 desc, cluster.getConfiguration(), hRegionInfos, m);
146 handler.prepare();
147 throwException = true;
148 handler.process();
149 abortAndStartNewMaster(cluster);
150 assertTrue(cluster.getLiveMasterThreads().size() == 1);
151 }
152
153 private void abortAndStartNewMaster(final MiniHBaseCluster cluster) throws IOException {
154 cluster.abortMaster(0);
155 cluster.waitOnMaster(0);
156 LOG.info("Starting new master");
157 cluster.startMaster();
158 LOG.info("Waiting for master to become active.");
159 cluster.waitForActiveAndReadyMaster();
160 }
161
162 private static class CustomCreateTableHandler extends CreateTableHandler {
163 public CustomCreateTableHandler(Server server, MasterFileSystem fileSystemManager,
164 HTableDescriptor hTableDescriptor, Configuration conf, HRegionInfo[] newRegions,
165 MasterServices masterServices) {
166 super(server, fileSystemManager, hTableDescriptor, conf, newRegions, masterServices);
167 }
168
169 @Override
170 protected List<HRegionInfo> handleCreateHdfsRegions(Path tableRootDir,
171 TableName tableName) throws IOException {
172 if (throwException) {
173 throw new IOException("Test throws exceptions.");
174 }
175 return super.handleCreateHdfsRegions(tableRootDir, tableName);
176 }
177 }
178 }