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  @Override
047  protected boolean splitWALCoordinatedByZk() {
048    return true;
049  }
050
051  @Test
052  public void test() throws Exception {
053    UTIL.startMiniCluster(3);
054    UTIL.waitFor(60000, () -> UTIL.getMiniHBaseCluster().getMaster().isInitialized());
055    LOG.info("\n\nCreating tables");
056    for (TableName TABLE : TABLES) {
057      UTIL.createTable(TABLE, FAMILY);
058    }
059    for (TableName TABLE : TABLES) {
060      UTIL.waitTableEnabled(TABLE);
061    }
062
063    List<RegionInfo> allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
064    assertEquals(4, allRegions.size());
065
066    LOG.info("\n\nShutting down cluster");
067    UTIL.shutdownMiniHBaseCluster();
068
069    LOG.info("\n\nSleeping a bit");
070    Thread.sleep(2000);
071
072    LOG.info("\n\nStarting cluster the second time");
073    UTIL.restartHBaseCluster(3);
074
075    // Need to use a new 'Configuration' so we make a new Connection.
076    // Otherwise we're reusing an Connection that has gone stale because
077    // the shutdown of the cluster also called shut of the connection.
078    allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
079    assertEquals(4, allRegions.size());
080    LOG.info("\n\nWaiting for tables to be available");
081    for (TableName TABLE : TABLES) {
082      try {
083        UTIL.createTable(TABLE, FAMILY);
084        assertTrue("Able to create table that should already exist", false);
085      } catch (TableExistsException tee) {
086        LOG.info("Table already exists as expected");
087      }
088      UTIL.waitTableAvailable(TABLE);
089    }
090  }
091}