1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.TableExistsException;
35 import org.apache.hadoop.hbase.client.MetaScanner;
36 import org.apache.hadoop.hbase.executor.EventHandler.EventType;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
39 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
40 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44
45 public class TestRestartCluster {
46 private static final Log LOG = LogFactory.getLog(TestRestartCluster.class);
47 private HBaseTestingUtility UTIL = new HBaseTestingUtility();
48
49 private static final byte[] TABLENAME = Bytes.toBytes("master_transitions");
50 private static final byte [][] FAMILIES = {Bytes.toBytes("a")};
51 private static final byte [][] TABLES = {
52 Bytes.toBytes("restartTableOne"),
53 Bytes.toBytes("restartTableTwo"),
54 Bytes.toBytes("restartTableThree")
55 };
56 private static final byte [] FAMILY = Bytes.toBytes("family");
57
58 @After public void tearDown() throws Exception {
59 UTIL.shutdownMiniCluster();
60 }
61
62 @Test (timeout=300000) public void testRestartClusterAfterKill()
63 throws Exception {
64 UTIL.startMiniZKCluster();
65 ZooKeeperWatcher zooKeeper =
66 new ZooKeeperWatcher(UTIL.getConfiguration(), "cluster1", null, true);
67
68
69 String unassignedZNode = zooKeeper.assignmentZNode;
70 ZKUtil.createAndFailSilent(zooKeeper, unassignedZNode);
71
72 ServerName sn = new ServerName(HMaster.MASTER, -1, System.currentTimeMillis());
73
74 ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.ROOT_REGIONINFO, sn);
75
76 ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
77
78 LOG.debug("Created UNASSIGNED zNode for ROOT and META regions in state " +
79 EventType.M_ZK_REGION_OFFLINE);
80
81
82 LOG.info("Starting HBase cluster...");
83 UTIL.startMiniCluster(2);
84
85 UTIL.createTable(TABLENAME, FAMILIES);
86 LOG.info("Created a table, waiting for table to be available...");
87 UTIL.waitTableAvailable(TABLENAME, 60*1000);
88
89 LOG.info("Master deleted unassigned region and started up successfully.");
90 }
91
92 @Test (timeout=300000)
93 public void testClusterRestart() throws Exception {
94 UTIL.startMiniCluster(3);
95 LOG.info("\n\nCreating tables");
96 for(byte [] TABLE : TABLES) {
97 UTIL.createTable(TABLE, FAMILY);
98 UTIL.waitTableAvailable(TABLE, 30000);
99 }
100 List<HRegionInfo> allRegions =
101 MetaScanner.listAllRegions(UTIL.getConfiguration());
102 assertEquals(3, allRegions.size());
103
104 LOG.info("\n\nShutting down cluster");
105 UTIL.shutdownMiniHBaseCluster();
106
107 LOG.info("\n\nSleeping a bit");
108 Thread.sleep(2000);
109
110 LOG.info("\n\nStarting cluster the second time");
111 UTIL.restartHBaseCluster(3);
112
113
114
115
116 allRegions = MetaScanner.
117 listAllRegions(new Configuration(UTIL.getConfiguration()));
118 assertEquals(3, allRegions.size());
119
120 LOG.info("\n\nWaiting for tables to be available");
121 for(byte [] TABLE: TABLES) {
122 try {
123 UTIL.createTable(TABLE, FAMILY);
124 assertTrue("Able to create table that should already exist", false);
125 } catch(TableExistsException tee) {
126 LOG.info("Table already exists as expected");
127 }
128 UTIL.waitTableAvailable(TABLE, 30000);
129 }
130 }
131 }