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