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.assertTrue;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.StartTestingClusterOption;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.hadoop.hbase.client.ConnectionFactory;
029import org.apache.hadoop.hbase.client.RegionLocator;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
032import org.junit.jupiter.api.AfterAll;
033import org.junit.jupiter.api.BeforeAll;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Tag(LargeTests.TAG)
040public class TestMetaAssignmentWithStopMaster {
041
042  private static final Logger LOG = LoggerFactory.getLogger(TestMetaAssignmentWithStopMaster.class);
043
044  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
045
046  private static final long WAIT_TIMEOUT = 120000;
047
048  @BeforeAll
049  public static void setUpBeforeClass() throws Exception {
050    StartTestingClusterOption option =
051      StartTestingClusterOption.builder().numMasters(2).numRegionServers(3).numDataNodes(3).build();
052    UTIL.startMiniCluster(option);
053  }
054
055  @AfterAll
056  public static void tearDownAfterClass() throws Exception {
057    UTIL.shutdownMiniCluster();
058  }
059
060  @Test
061  public void testStopActiveMaster() throws Exception {
062    try (Connection conn = ConnectionFactory.createConnection(UTIL.getConfiguration());
063      RegionLocator locator = conn.getRegionLocator(TableName.META_TABLE_NAME)) {
064      ServerName oldMetaServer = locator.getAllRegionLocations().get(0).getServerName();
065      ServerName oldMaster = UTIL.getMiniHBaseCluster().getMaster().getServerName();
066
067      UTIL.getMiniHBaseCluster().getMaster().stop("Stop master for test");
068      long startTime = EnvironmentEdgeManager.currentTime();
069      while (
070        UTIL.getMiniHBaseCluster().getMaster() == null
071          || UTIL.getMiniHBaseCluster().getMaster().getServerName().equals(oldMaster)
072      ) {
073        LOG.info("Wait the standby master become active");
074        Thread.sleep(3000);
075        if (EnvironmentEdgeManager.currentTime() - startTime > WAIT_TIMEOUT) {
076          fail("Wait too long for standby master become active");
077        }
078      }
079      startTime = EnvironmentEdgeManager.currentTime();
080      while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
081        LOG.info("Wait the new active master to be initialized");
082        Thread.sleep(3000);
083        if (EnvironmentEdgeManager.currentTime() - startTime > WAIT_TIMEOUT) {
084          fail("Wait too long for the new active master to be initialized");
085        }
086      }
087
088      ServerName newMetaServer = locator.getAllRegionLocations().get(0).getServerName();
089      assertTrue(newMetaServer.equals(oldMetaServer), "The new meta server " + newMetaServer
090        + " should be same with" + " the old meta server " + oldMetaServer);
091    }
092  }
093}