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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.List;
024import org.apache.hadoop.hbase.MetaTableAccessor;
025import org.apache.hadoop.hbase.TableExistsException;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Tag(MasterTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestClusterRestart extends AbstractTestRestartCluster {
038
039  private static final Logger LOG = LoggerFactory.getLogger(TestClusterRestart.class);
040
041  private static final int NUM_REGIONS = 3;
042
043  @Override
044  protected boolean splitWALCoordinatedByZk() {
045    return true;
046  }
047
048  @Test
049  public void test() throws Exception {
050    UTIL.startMiniCluster(3);
051    UTIL.waitFor(60000, () -> UTIL.getMiniHBaseCluster().getMaster().isInitialized());
052    LOG.info("\n\nCreating tables");
053    for (TableName TABLE : TABLES) {
054      UTIL.createTable(TABLE, FAMILY);
055    }
056    for (TableName TABLE : TABLES) {
057      UTIL.waitTableEnabled(TABLE);
058    }
059
060    List<RegionInfo> allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
061    assertEquals(NUM_REGIONS, allRegions.size());
062
063    LOG.info("\n\nShutting down cluster");
064    UTIL.shutdownMiniHBaseCluster();
065
066    LOG.info("\n\nSleeping a bit");
067    Thread.sleep(2000);
068
069    LOG.info("\n\nStarting cluster the second time");
070    UTIL.restartHBaseCluster(3);
071
072    // Need to use a new 'Configuration' so we make a new Connection.
073    // Otherwise we're reusing an Connection that has gone stale because
074    // the shutdown of the cluster also called shut of the connection.
075    allRegions = MetaTableAccessor.getAllRegions(UTIL.getConnection(), false);
076    assertEquals(NUM_REGIONS, allRegions.size());
077    LOG.info("\n\nWaiting for tables to be available");
078    for (TableName TABLE : TABLES) {
079      try {
080        UTIL.createTable(TABLE, FAMILY);
081        assertTrue(false, "Able to create table that should already exist");
082      } catch (TableExistsException tee) {
083        LOG.info("Table already exists as expected");
084      }
085      UTIL.waitTableAvailable(TABLE);
086    }
087  }
088}