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.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Category({ LargeTests.class })
040public class TestMetaAssignmentWithStopMaster {
041
042  private static final Logger LOG =
043      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 = StartMiniClusterOption.builder()
056        .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 = System.currentTimeMillis();
074    while (UTIL.getMiniHBaseCluster().getMaster() == null || UTIL.getMiniHBaseCluster().getMaster()
075        .getServerName().equals(oldMaster)) {
076      LOG.info("Wait the standby master become active");
077      Thread.sleep(3000);
078      if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
079        fail("Wait too long for standby master become active");
080      }
081    }
082    startTime = System.currentTimeMillis();
083    while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
084      LOG.info("Wait the new active master to be initialized");
085      Thread.sleep(3000);
086      if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
087        fail("Wait too long for the new active master to be initialized");
088      }
089    }
090
091    ServerName newMetaServer = conn.locateRegions(TableName.META_TABLE_NAME).get(0).getServerName();
092    assertTrue("The new meta server " + newMetaServer + " should be same with" +
093        " the old meta server " + oldMetaServer, newMetaServer.equals(oldMetaServer));
094  }
095}