001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.MetaTableAccessor;
026import org.apache.hadoop.hbase.TableExistsException;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@Category({ MasterTests.class, MediumTests.class })
038public class TestClusterRestart extends AbstractTestRestartCluster {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestClusterRestart.class);
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestClusterRestart.class);
045
046  private static final int NUM_REGIONS = 3;
047
048  @Override
049  protected boolean splitWALCoordinatedByZk() {
050    return true;
051  }
052
053  @Test
054  public void test() throws Exception {
055    UTIL.startMiniCluster(3);
056    UTIL.waitFor(60000, () -> UTIL.getMiniHBaseCluster().getMaster().isInitialized());
057    LOG.info("\n\nCreating tables");
058    for (TableName TABLE : TABLES) {
059      UTIL.createTable(TABLE, FAMILY);
060    }
061    for (TableName TABLE : TABLES) {
062      UTIL.waitTableEnabled(TABLE);
063    }
064
065    List<RegionInfo> allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
066    assertEquals(NUM_REGIONS, allRegions.size());
067
068    LOG.info("\n\nShutting down cluster");
069    UTIL.shutdownMiniHBaseCluster();
070
071    LOG.info("\n\nSleeping a bit");
072    Thread.sleep(2000);
073
074    LOG.info("\n\nStarting cluster the second time");
075    UTIL.restartHBaseCluster(3);
076
077    // Need to use a new 'Configuration' so we make a new Connection.
078    // Otherwise we're reusing an Connection that has gone stale because
079    // the shutdown of the cluster also called shut of the connection.
080    allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
081    assertEquals(NUM_REGIONS, allRegions.size());
082    LOG.info("\n\nWaiting for tables to be available");
083    for (TableName TABLE : TABLES) {
084      try {
085        UTIL.createTable(TABLE, FAMILY);
086        assertTrue("Able to create table that should already exist", false);
087      } catch (TableExistsException tee) {
088        LOG.info("Table already exists as expected");
089      }
090      UTIL.waitTableAvailable(TABLE);
091    }
092  }
093}