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;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
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.hbase.*;
30 import org.apache.hadoop.hbase.client.MetaScanner;
31 import org.apache.hadoop.hbase.executor.EventType;
32 import org.apache.hadoop.hbase.testclassification.LargeTests;
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.getConfiguration().setBoolean("hbase.assignment.usezk", true);
63 UTIL.startMiniZKCluster();
64 ZooKeeperWatcher zooKeeper =
65 new ZooKeeperWatcher(UTIL.getConfiguration(), "cluster1", null, true);
66
67
68 String unassignedZNode = zooKeeper.assignmentZNode;
69 ZKUtil.createAndFailSilent(zooKeeper, unassignedZNode);
70
71 ServerName sn = ServerName.valueOf(HMaster.MASTER, 1, System.currentTimeMillis());
72
73 ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
74
75 LOG.debug("Created UNASSIGNED zNode for ROOT and hbase:meta regions in state " +
76 EventType.M_ZK_REGION_OFFLINE);
77
78
79 LOG.info("Starting HBase cluster...");
80 UTIL.startMiniCluster(2);
81
82 UTIL.createTable(TABLENAME, FAMILIES);
83 LOG.info("Created a table, waiting for table to be available...");
84 UTIL.waitTableAvailable(TABLENAME, 60*1000);
85
86 LOG.info("Master deleted unassigned region and started up successfully.");
87 }
88
89 @Test (timeout=300000)
90 public void testClusterRestart() throws Exception {
91 UTIL.startMiniCluster(3);
92 while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
93 Threads.sleep(1);
94 }
95 LOG.info("\n\nCreating tables");
96 for(byte [] TABLE : TABLES) {
97 UTIL.createTable(TABLE, FAMILY);
98 }
99 for(byte [] TABLE : TABLES) {
100 UTIL.waitTableEnabled(TABLE);
101 }
102
103 List<HRegionInfo> allRegions =
104 MetaScanner.listAllRegions(UTIL.getConfiguration(), true);
105 assertEquals(4, allRegions.size());
106
107 LOG.info("\n\nShutting down cluster");
108 UTIL.shutdownMiniHBaseCluster();
109
110 LOG.info("\n\nSleeping a bit");
111 Thread.sleep(2000);
112
113 LOG.info("\n\nStarting cluster the second time");
114 UTIL.restartHBaseCluster(3);
115
116
117
118
119 allRegions = MetaScanner.listAllRegions(new Configuration(UTIL.getConfiguration()), true);
120 assertEquals(4, allRegions.size());
121 LOG.info("\n\nWaiting for tables to be available");
122 for(byte [] TABLE: TABLES) {
123 try {
124 UTIL.createTable(TABLE, FAMILY);
125 assertTrue("Able to create table that should already exist", false);
126 } catch(TableExistsException tee) {
127 LOG.info("Table already exists as expected");
128 }
129 UTIL.waitTableAvailable(TABLE);
130 }
131 }
132 }