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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableExistsException;
35  import org.apache.hadoop.hbase.client.MetaScanner;
36  import org.apache.hadoop.hbase.executor.EventHandler.EventType;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
39  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
40  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  
45  public class TestRestartCluster {
46    private static final Log LOG = LogFactory.getLog(TestRestartCluster.class);
47    private HBaseTestingUtility UTIL = new HBaseTestingUtility();
48  
49    private static final byte[] TABLENAME = Bytes.toBytes("master_transitions");
50    private static final byte [][] FAMILIES = {Bytes.toBytes("a")};
51    private static final byte [][] TABLES = {
52        Bytes.toBytes("restartTableOne"),
53        Bytes.toBytes("restartTableTwo"),
54        Bytes.toBytes("restartTableThree")
55    };
56    private static final byte [] FAMILY = Bytes.toBytes("family");
57  
58    @After public void tearDown() throws Exception {
59      UTIL.shutdownMiniCluster();
60    }
61  
62    @Test (timeout=300000) public void testRestartClusterAfterKill()
63    throws Exception {
64      UTIL.startMiniZKCluster();
65      ZooKeeperWatcher zooKeeper =
66        new ZooKeeperWatcher(UTIL.getConfiguration(), "cluster1", null, true);
67  
68      // create the unassigned region, throw up a region opened state for META
69      String unassignedZNode = zooKeeper.assignmentZNode;
70      ZKUtil.createAndFailSilent(zooKeeper, unassignedZNode);
71  
72      ServerName sn = new ServerName(HMaster.MASTER, -1, System.currentTimeMillis());
73  
74      ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.ROOT_REGIONINFO, sn);
75  
76      ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
77  
78      LOG.debug("Created UNASSIGNED zNode for ROOT and META regions in state " +
79          EventType.M_ZK_REGION_OFFLINE);
80  
81      // start the HB cluster
82      LOG.info("Starting HBase cluster...");
83      UTIL.startMiniCluster(2);
84  
85      UTIL.createTable(TABLENAME, FAMILIES);
86      LOG.info("Created a table, waiting for table to be available...");
87      UTIL.waitTableAvailable(TABLENAME, 60*1000);
88  
89      LOG.info("Master deleted unassigned region and started up successfully.");
90    }
91  
92    @Test (timeout=300000)
93    public void testClusterRestart() throws Exception {
94      UTIL.startMiniCluster(3);
95      LOG.info("\n\nCreating tables");
96      for(byte [] TABLE : TABLES) {
97        UTIL.createTable(TABLE, FAMILY);
98        UTIL.waitTableAvailable(TABLE, 30000);
99      }
100     List<HRegionInfo> allRegions =
101       MetaScanner.listAllRegions(UTIL.getConfiguration());
102     assertEquals(3, allRegions.size());
103 
104     LOG.info("\n\nShutting down cluster");
105     UTIL.shutdownMiniHBaseCluster();
106 
107     LOG.info("\n\nSleeping a bit");
108     Thread.sleep(2000);
109 
110     LOG.info("\n\nStarting cluster the second time");
111     UTIL.restartHBaseCluster(3);
112 
113     // Need to use a new 'Configuration' so we make a new HConnection.
114     // Otherwise we're reusing an HConnection that has gone stale because
115     // the shutdown of the cluster also called shut of the connection.
116     allRegions = MetaScanner.
117       listAllRegions(new Configuration(UTIL.getConfiguration()));
118     assertEquals(3, allRegions.size());
119 
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, 30000);
129     }
130   }
131 }