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.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.client.MetaScanner;
32  import org.apache.hadoop.hbase.executor.EventHandler.EventType;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.apache.hadoop.hbase.util.Threads;
35  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
36  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
37  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
38  import org.junit.After;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  @Category(LargeTests.class)
43  public class TestRestartCluster {
44    private static final Log LOG = LogFactory.getLog(TestRestartCluster.class);
45    private HBaseTestingUtility UTIL = new HBaseTestingUtility();
46  
47    private static final byte[] TABLENAME = Bytes.toBytes("master_transitions");
48    private static final byte [][] FAMILIES = {Bytes.toBytes("a")};
49    private static final byte [][] TABLES = {
50        Bytes.toBytes("restartTableOne"),
51        Bytes.toBytes("restartTableTwo"),
52        Bytes.toBytes("restartTableThree")
53    };
54    private static final byte [] FAMILY = Bytes.toBytes("family");
55  
56    @After public void tearDown() throws Exception {
57      UTIL.shutdownMiniCluster();
58    }
59  
60    @Test (timeout=300000) public void testRestartClusterAfterKill()
61    throws Exception {
62      UTIL.startMiniZKCluster();
63      ZooKeeperWatcher zooKeeper =
64        new ZooKeeperWatcher(UTIL.getConfiguration(), "cluster1", null, true);
65  
66      // create the unassigned region, throw up a region opened state for META
67      String unassignedZNode = zooKeeper.assignmentZNode;
68      ZKUtil.createAndFailSilent(zooKeeper, unassignedZNode);
69  
70      ServerName sn = new ServerName(HMaster.MASTER, -1, System.currentTimeMillis());
71  
72      ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.ROOT_REGIONINFO, sn);
73  
74      ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
75  
76      LOG.debug("Created UNASSIGNED zNode for ROOT and META regions in state " +
77          EventType.M_ZK_REGION_OFFLINE);
78  
79      // start the HB cluster
80      LOG.info("Starting HBase cluster...");
81      UTIL.startMiniCluster(2);
82  
83      UTIL.createTable(TABLENAME, FAMILIES);
84      LOG.info("Created a table, waiting for table to be available...");
85      UTIL.waitTableAvailable(TABLENAME, 60*1000);
86  
87      LOG.info("Master deleted unassigned region and started up successfully.");
88    }
89  
90    @Test (timeout=300000)
91    public void testClusterRestart() throws Exception {
92      UTIL.startMiniCluster(3);
93      while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
94        Threads.sleep(1);
95      }
96      LOG.info("\n\nCreating tables");
97      for(byte [] TABLE : TABLES) {
98        UTIL.createTable(TABLE, FAMILY);
99      }
100     for(byte [] TABLE : TABLES) {
101       UTIL.waitTableAvailable(TABLE, 30000);
102     }
103 
104     List<HRegionInfo> allRegions =
105       MetaScanner.listAllRegions(UTIL.getConfiguration());
106     assertEquals(3, allRegions.size());
107 
108     LOG.info("\n\nShutting down cluster");
109     UTIL.shutdownMiniHBaseCluster();
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 
136   @org.junit.Rule
137   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
138     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
139 }
140