View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.client.ClusterConnection;
27  import org.apache.hadoop.hbase.client.ConnectionFactory;
28  import org.apache.hadoop.hbase.testclassification.LargeTests;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  @Category({ LargeTests.class })
36  public class TestMetaAssignmentWithStopMaster {
37  
38    private static final Logger LOG =
39      LoggerFactory.getLogger(TestMetaAssignmentWithStopMaster.class);
40  
41    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
42  
43    private static final long WAIT_TIMEOUT = 120000;
44  
45    @BeforeClass
46    public static void setUp() throws Exception {
47      UTIL.startMiniCluster(2,3);
48    }
49  
50    @Test
51    public void testStopActiveMaster() throws Exception {
52      ClusterConnection conn =
53        (ClusterConnection) ConnectionFactory.createConnection(UTIL.getConfiguration());
54      ServerName oldMetaServer = conn.locateRegion(HRegionInfo.FIRST_META_REGIONINFO
55        .getRegionName()).getServerName();
56      ServerName oldMaster = UTIL.getMiniHBaseCluster().getMaster().getServerName();
57  
58      UTIL.getMiniHBaseCluster().getMaster().stop("Stop master for test");
59      long startTime = System.currentTimeMillis();
60      while (UTIL.getMiniHBaseCluster().getMaster() == null || UTIL.getMiniHBaseCluster().getMaster()
61        .getServerName().equals(oldMaster)) {
62        LOG.info("Wait the standby master become active");
63        Thread.sleep(3000);
64        if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
65          fail("Wait too long for standby master become active");
66        }
67      }
68      startTime = System.currentTimeMillis();
69      while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) {
70        LOG.info("Wait the new active master to be initialized");
71        Thread.sleep(3000);
72        if (System.currentTimeMillis() - startTime > WAIT_TIMEOUT) {
73          fail("Wait too long for the new active master to be initialized");
74        }
75      }
76  
77      ServerName newMetaServer = conn.locateRegion(HRegionInfo.FIRST_META_REGIONINFO
78        .getRegionName()).getServerName();
79      assertTrue("The new meta server " + newMetaServer + " should be same with" +
80        " the old meta server " + oldMetaServer, newMetaServer.equals(oldMetaServer));
81    }
82  }