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