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