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 java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.executor.RegionTransitionEventData;
31 import org.apache.hadoop.hbase.executor.HBaseEventHandler.HBaseEventType;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.util.Writables;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWrapper;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39
40 public class TestRestartCluster {
41 private static final Log LOG = LogFactory.getLog(TestRestartCluster.class);
42 private static Configuration conf;
43 private static HBaseTestingUtility utility;
44 private static ZooKeeperWrapper zkWrapper;
45 private static final byte[] TABLENAME = Bytes.toBytes("master_transitions");
46 private static final byte [][] FAMILIES = new byte [][] {Bytes.toBytes("a")};
47
48 @BeforeClass public static void beforeAllTests() throws Exception {
49 conf = HBaseConfiguration.create();
50 utility = new HBaseTestingUtility(conf);
51 }
52
53 @AfterClass public static void afterAllTests() throws IOException {
54 utility.shutdownMiniCluster();
55 }
56
57 @Before public void setup() throws IOException {
58 }
59
60 @Test (timeout=300000) public void testRestartClusterAfterKill()throws Exception {
61 utility.startMiniZKCluster();
62 zkWrapper = ZooKeeperWrapper.createInstance(conf, "cluster1");
63
64
65 String unassignedZNode = zkWrapper.getRegionInTransitionZNode();
66 zkWrapper.createZNodeIfNotExists(unassignedZNode);
67 byte[] data = null;
68 HBaseEventType hbEventType = HBaseEventType.RS2ZK_REGION_OPENED;
69 try {
70 data = Writables.getBytes(new RegionTransitionEventData(hbEventType, HMaster.MASTER));
71 } catch (IOException e) {
72 LOG.error("Error creating event data for " + hbEventType, e);
73 }
74 zkWrapper.createOrUpdateUnassignedRegion(
75 HRegionInfo.ROOT_REGIONINFO.getEncodedName(), data);
76 zkWrapper.createOrUpdateUnassignedRegion(
77 HRegionInfo.FIRST_META_REGIONINFO.getEncodedName(), data);
78 LOG.debug("Created UNASSIGNED zNode for ROOT and META regions in state " + HBaseEventType.M2ZK_REGION_OFFLINE);
79
80
81 LOG.info("Starting HBase cluster...");
82 utility.startMiniCluster(2);
83
84 utility.createTable(TABLENAME, FAMILIES);
85 LOG.info("Created a table, waiting for table to be available...");
86 utility.waitTableAvailable(TABLENAME, 60*1000);
87
88 LOG.info("Master deleted unassgined region and started up successfully.");
89 }
90 }