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