1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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      // create the unassigned region, throw up a region opened state for META
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      // start the HB cluster
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  }