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.assertFalse;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HColumnDescriptor;
34 import org.apache.hadoop.hbase.HRegionInfo;
35 import org.apache.hadoop.hbase.HTableDescriptor;
36 import org.apache.hadoop.hbase.MediumTests;
37 import org.apache.hadoop.hbase.MiniHBaseCluster;
38 import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
39 import org.apache.hadoop.hbase.Server;
40 import org.apache.hadoop.hbase.TableExistsException;
41 import org.apache.hadoop.hbase.catalog.CatalogTracker;
42 import org.apache.hadoop.hbase.master.AssignmentManager;
43 import org.apache.hadoop.hbase.master.HMaster;
44 import org.apache.hadoop.hbase.master.MasterFileSystem;
45 import org.apache.hadoop.hbase.master.ServerManager;
46 import org.apache.hadoop.hbase.util.Bytes;
47 import org.apache.hadoop.hbase.util.FSUtils;
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.experimental.categories.Category;
52
53 @Category(MediumTests.class)
54 public class TestCreateTableHandler {
55 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56 private static final Log LOG = LogFactory.getLog(TestCreateTableHandler.class);
57 private static final byte[] FAMILYNAME = Bytes.toBytes("fam");
58 private static boolean throwException = false;
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 = true;
69 }
70
71 @Test
72 public void testCreateTableCalledTwiceAndFirstOneInProgress() throws Exception {
73 final byte[] tableName = Bytes.toBytes("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.getName(), null,
79 null) };
80 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
81 m.getServerManager(), desc, cluster.getConfiguration(), hRegionInfos,
82 m.getCatalogTracker(), m.getAssignmentManager());
83 throwException = true;
84 handler.process();
85 throwException = false;
86 CustomCreateTableHandler handler1 = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
87 m.getServerManager(), desc, cluster.getConfiguration(), hRegionInfos,
88 m.getCatalogTracker(), m.getAssignmentManager());
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 byte[] tableName = Bytes.toBytes("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(tableName, null, splitPoint, false, ts);
108 HRegionInfo d2 = new HRegionInfo(tableName, splitPoint, null, false, ts + 1);
109 HRegionInfo parent = new HRegionInfo(tableName, 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.getTablePath(tempdir, tableName);
115 fs.delete(tempTableDir, true);
116
117 final HRegionInfo[] hRegionInfos = new HRegionInfo[] {d1, d2, parent};
118 CreateTableHandler handler = new CreateTableHandler(m, m.getMasterFileSystem(),
119 m.getServerManager(), desc, cluster.getConfiguration(), hRegionInfos,
120 m.getCatalogTracker(), m.getAssignmentManager());
121 handler.process();
122 for (int i = 0; i < 200; 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 List<HRegionInfo> regions = m.getAssignmentManager().getRegionsOfTable(tableName);
130 assertFalse("Split parent should not be assigned", regions.contains(parent));
131 }
132
133 @Test (timeout=60000)
134 public void testMasterRestartAfterEnablingNodeIsCreated() throws Exception {
135 byte[] tableName = Bytes.toBytes("testMasterRestartAfterEnablingNodeIsCreated");
136 final MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
137 final HMaster m = cluster.getMaster();
138 final HTableDescriptor desc = new HTableDescriptor(tableName);
139 desc.addFamily(new HColumnDescriptor(FAMILYNAME));
140 final HRegionInfo[] hRegionInfos = new HRegionInfo[] { new HRegionInfo(desc.getName(), null,
141 null) };
142 CustomCreateTableHandler handler = new CustomCreateTableHandler(m, m.getMasterFileSystem(),
143 m.getServerManager(), desc, cluster.getConfiguration(), hRegionInfos,
144 m.getCatalogTracker(), m.getAssignmentManager());
145 throwException = true;
146 handler.process();
147 abortAndStartNewMaster(cluster);
148 assertTrue(cluster.getLiveMasterThreads().size() == 1);
149 }
150
151 private void abortAndStartNewMaster(final MiniHBaseCluster cluster) throws IOException {
152 cluster.abortMaster(0);
153 cluster.waitOnMaster(0);
154 LOG.info("Starting new master");
155 cluster.startMaster();
156 LOG.info("Waiting for master to become active.");
157 cluster.waitForActiveAndReadyMaster();
158 }
159
160 private static class CustomCreateTableHandler extends CreateTableHandler {
161 public CustomCreateTableHandler(Server server, MasterFileSystem fileSystemManager,
162 ServerManager sm, HTableDescriptor hTableDescriptor, Configuration conf,
163 HRegionInfo[] newRegions, CatalogTracker ct, AssignmentManager am)
164 throws NotAllMetaRegionsOnlineException, TableExistsException, IOException {
165 super(server, fileSystemManager, sm, hTableDescriptor, conf, newRegions, ct, am);
166 }
167
168 @Override
169 protected List<HRegionInfo> handleCreateHdfsRegions(Path tableRootDir, String tableName)
170 throws IOException {
171 if (throwException) {
172 throw new IOException("Test throws exceptions.");
173 }
174 return super.handleCreateHdfsRegions(tableRootDir, tableName);
175 }
176 }
177 }