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