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