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