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.Path;
30 import org.apache.hadoop.hbase.TableName;
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.MediumTests;
36 import org.apache.hadoop.hbase.MiniHBaseCluster;
37 import org.apache.hadoop.hbase.Server;
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.util.Bytes;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47 @Category(MediumTests.class)
48 public class TestCreateTableHandler {
49 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50 private static final Log LOG = LogFactory.getLog(TestCreateTableHandler.class);
51 private static final byte[] TABLENAME = Bytes.toBytes("TestCreateTableHandler");
52 private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
53 private static boolean throwException = false;
54
55
56 @Before
57 public void setUp() throws Exception {
58 TEST_UTIL.startMiniCluster(1);
59 }
60
61 @After
62 public void tearDown() throws Exception {
63 TEST_UTIL.shutdownMiniCluster();
64 throwException = false;
65 }
66
67 @Test (timeout=300000)
68 public void testCreateTableHandlerIfCalledTwoTimesAndFirstOneIsUnderProgress() throws Exception {
69 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
70 final HMaster m = cluster.getMaster();
71 final HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLENAME));
72 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
73 final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getTableName(), null,
74 null) };
75 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
76 desc, cluster.getConfiguration(), hRegionInfos, m);
77 handler.prepare();
78 throwException = true;
79 handler.process();
80 throwException = false;
81 CustomCreateTableHandler handler1 = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
82 desc, cluster.getConfiguration(), hRegionInfos, m);
83 handler1.prepare();
84 handler1.process();
85 for (int i = 0; i < 100; i++) {
86 if (!TEST_UTIL.getHBaseAdmin().isTableAvailable(TABLENAME)) {
87 Thread.sleep(200);
88 }
89 }
90 assertTrue(TEST_UTIL.getHBaseAdmin().isTableEnabled(TABLENAME));
91
92 }
93 @Test (timeout=60000)
94 public void testMasterRestartAfterEnablingNodeIsCreated() throws Exception {
95 byte[] tableName = Bytes.toBytes("testMasterRestartAfterEnablingNodeIsCreated");
96 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
97 final HMaster m = cluster.getMaster();
98 final HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
99 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
100 final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getTableName(), null,
101 null) };
102 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
103 desc, cluster.getConfiguration(), hRegionInfos, m);
104 handler.prepare();
105 throwException = true;
106 handler.process();
107 abortAndStartNewMaster(cluster);
108 assertTrue(cluster.getLiveMasterThreads().size() == 1);
109
110 }
111
112 private void abortAndStartNewMaster(final MiniHBaseCluster cluster) throws IOException {
113 cluster.abortMaster(0);
114 cluster.waitOnMaster(0);
115 LOG.info("Starting new master");
116 cluster.startMaster();
117 LOG.info("Waiting for master to become active.");
118 cluster.waitForActiveAndReadyMaster();
119 }
120
121 private static class CustomCreateTableHandler extends CreateTableHandler {
122 public CustomCreateTableHandler(Server server, MasterFileSystem fileSystemManager,
123 HTableDescriptor hTableDescriptor, Configuration conf, HRegionInfo[] newRegions,
124 MasterServices masterServices) {
125 super(server, fileSystemManager, hTableDescriptor, conf, newRegions, masterServices);
126 }
127
128 @Override
129 protected List<HRegionInfo> handleCreateHdfsRegions(Path tableRootDir,
130 TableName tableName) throws IOException {
131 if (throwException) {
132 throw new IOException("Test throws exceptions.");
133 }
134 return super.handleCreateHdfsRegions(tableRootDir, tableName);
135 }
136 }
137 }