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.util.Bytes;
33 import org.apache.hadoop.hbase.util.Threads;
34 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
35 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
36 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37 import org.junit.After;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 @Category(LargeTests.class)
42 public class TestRestartCluster {
43 private static final Log LOG = LogFactory.getLog(TestRestartCluster.class);
44 private HBaseTestingUtility UTIL = new HBaseTestingUtility();
45
46 private static final byte[] TABLENAME = Bytes.toBytes("master_transitions");
47 private static final byte [][] FAMILIES = {Bytes.toBytes("a")};
48 private static final byte [][] TABLES = {
49 Bytes.toBytes("restartTableOne"),
50 Bytes.toBytes("restartTableTwo"),
51 Bytes.toBytes("restartTableThree")
52 };
53 private static final byte [] FAMILY = Bytes.toBytes("family");
54
55 @After public void tearDown() throws Exception {
56 UTIL.shutdownMiniCluster();
57 }
58
59 @Test (timeout=300000) public void testRestartClusterAfterKill()
60 throws Exception {
61 UTIL.getConfiguration().setBoolean("hbase.assignment.usezk", true);
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 = ServerName.valueOf(HMaster.MASTER, 1, System.currentTimeMillis());
71
72 ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
73
74 LOG.debug("Created UNASSIGNED zNode for ROOT and hbase:meta regions in state " +
75 EventType.M_ZK_REGION_OFFLINE);
76
77
78 LOG.info("Starting HBase cluster...");
79 UTIL.startMiniCluster(2);
80
81 UTIL.createTable(TABLENAME, FAMILIES);
82 LOG.info("Created a table, waiting for table to be available...");
83 UTIL.waitTableAvailable(TABLENAME, 60*1000);
84
85 LOG.info("Master deleted unassigned region and started up successfully.");
86 }
87
88 @Test (timeout=300000)
89 public void testClusterRestart() throws Exception {
90 UTIL.startMiniCluster(3);
91 while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
92 Threads.sleep(1);
93 }
94 LOG.info("\n\nCreating tables");
95 for(byte [] TABLE : TABLES) {
96 UTIL.createTable(TABLE, FAMILY);
97 }
98 for(byte [] TABLE : TABLES) {
99 UTIL.waitTableEnabled(TABLE);
100 }
101
102 List<HRegionInfo> allRegions =
103 MetaScanner.listAllRegions(UTIL.getConfiguration(), true);
104 assertEquals(4, allRegions.size());
105
106 LOG.info("\n\nShutting down cluster");
107 UTIL.shutdownMiniHBaseCluster();
108
109 LOG.info("\n\nSleeping a bit");
110 Thread.sleep(2000);
111
112 LOG.info("\n\nStarting cluster the second time");
113 UTIL.restartHBaseCluster(3);
114
115
116
117
118 allRegions = MetaScanner.listAllRegions(new Configuration(UTIL.getConfiguration()), true);
119 assertEquals(4, allRegions.size());
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);
129 }
130 }
131 }