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