View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.*;
30  import org.apache.hadoop.hbase.client.MetaScanner;
31  import org.apache.hadoop.hbase.executor.EventType;
32  import org.apache.hadoop.hbase.testclassification.LargeTests;
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.getConfiguration().setBoolean("hbase.assignment.usezk", true);
63      UTIL.startMiniZKCluster();
64      ZooKeeperWatcher zooKeeper =
65        new ZooKeeperWatcher(UTIL.getConfiguration(), "cluster1", null, true);
66  
67      // create the unassigned region, throw up a region opened state for META
68      String unassignedZNode = zooKeeper.assignmentZNode;
69      ZKUtil.createAndFailSilent(zooKeeper, unassignedZNode);
70  
71      ServerName sn = ServerName.valueOf(HMaster.MASTER, 1, System.currentTimeMillis());
72  
73      ZKAssign.createNodeOffline(zooKeeper, HRegionInfo.FIRST_META_REGIONINFO, sn);
74  
75      LOG.debug("Created UNASSIGNED zNode for ROOT and hbase:meta regions in state " +
76          EventType.M_ZK_REGION_OFFLINE);
77  
78      // start the HB cluster
79      LOG.info("Starting HBase cluster...");
80      UTIL.startMiniCluster(2);
81  
82      UTIL.createTable(TABLENAME, FAMILIES);
83      LOG.info("Created a table, waiting for table to be available...");
84      UTIL.waitTableAvailable(TABLENAME, 60*1000);
85  
86      LOG.info("Master deleted unassigned region and started up successfully.");
87    }
88  
89    @Test (timeout=300000)
90    public void testClusterRestart() throws Exception {
91      UTIL.startMiniCluster(3);
92      while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
93        Threads.sleep(1);
94      }
95      LOG.info("\n\nCreating tables");
96      for(byte [] TABLE : TABLES) {
97        UTIL.createTable(TABLE, FAMILY);
98      }
99      for(byte [] TABLE : TABLES) {
100       UTIL.waitTableEnabled(TABLE);
101     }
102 
103     List<HRegionInfo> allRegions =
104       MetaScanner.listAllRegions(UTIL.getConfiguration(), true);
105     assertEquals(4, allRegions.size());
106 
107     LOG.info("\n\nShutting down cluster");
108     UTIL.shutdownMiniHBaseCluster();
109 
110     LOG.info("\n\nSleeping a bit");
111     Thread.sleep(2000);
112 
113     LOG.info("\n\nStarting cluster the second time");
114     UTIL.restartHBaseCluster(3);
115 
116     // Need to use a new 'Configuration' so we make a new HConnection.
117     // Otherwise we're reusing an HConnection that has gone stale because
118     // the shutdown of the cluster also called shut of the connection.
119     allRegions = MetaScanner.listAllRegions(new Configuration(UTIL.getConfiguration()), true);
120     assertEquals(4, allRegions.size());
121     LOG.info("\n\nWaiting for tables to be available");
122     for(byte [] TABLE: TABLES) {
123       try {
124         UTIL.createTable(TABLE, FAMILY);
125         assertTrue("Able to create table that should already exist", false);
126       } catch(TableExistsException tee) {
127         LOG.info("Table already exists as expected");
128       }
129       UTIL.waitTableAvailable(TABLE);
130     }
131   }
132 }