View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.ClusterStatus;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.hbase.LocalHBaseCluster;
36  import org.apache.hadoop.hbase.MiniHBaseCluster;
37  import org.apache.hadoop.hbase.client.Admin;
38  import org.apache.hadoop.hbase.client.Connection;
39  import org.apache.hadoop.hbase.client.ConnectionFactory;
40  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(LargeTests.class)
45  public class TestMasterShutdown {
46    private static final Log LOG = LogFactory.getLog(TestMasterShutdown.class);
47  
48    /**
49     * Simple test of shutdown.
50     * <p>
51     * Starts with three masters.  Tells the active master to shutdown the cluster.
52     * Verifies that all masters are properly shutdown.
53     * @throws Exception
54     */
55    @Test (timeout=120000)
56    public void testMasterShutdown() throws Exception {
57      final int NUM_MASTERS = 3;
58      final int NUM_RS = 3;
59  
60      // Create config to use for this cluster
61      Configuration conf = HBaseConfiguration.create();
62  
63      // Start the cluster
64      HBaseTestingUtility htu = new HBaseTestingUtility(conf);
65      htu.startMiniCluster(NUM_MASTERS, NUM_RS);
66      MiniHBaseCluster cluster = htu.getHBaseCluster();
67  
68      // get all the master threads
69      List<MasterThread> masterThreads = cluster.getMasterThreads();
70  
71      // wait for each to come online
72      for (MasterThread mt : masterThreads) {
73        assertTrue(mt.isAlive());
74      }
75  
76      // find the active master
77      HMaster active = null;
78      for (int i = 0; i < masterThreads.size(); i++) {
79        if (masterThreads.get(i).getMaster().isActiveMaster()) {
80          active = masterThreads.get(i).getMaster();
81          break;
82        }
83      }
84      assertNotNull(active);
85      // make sure the other two are backup masters
86      ClusterStatus status = active.getClusterStatus();
87      assertEquals(2, status.getBackupMastersSize());
88      assertEquals(2, status.getBackupMasters().size());
89  
90      // tell the active master to shutdown the cluster
91      active.shutdown();
92  
93      for (int i = NUM_MASTERS - 1; i >= 0 ;--i) {
94        cluster.waitOnMaster(i);
95      }
96      // make sure all the masters properly shutdown
97      assertEquals(0, masterThreads.size());
98  
99      htu.shutdownMiniCluster();
100   }
101 
102   @Test(timeout = 60000)
103   public void testMasterShutdownBeforeStartingAnyRegionServer() throws Exception {
104     final int NUM_MASTERS = 1;
105     final int NUM_RS = 0;
106 
107     // Create config to use for this cluster
108     Configuration conf = HBaseConfiguration.create();
109     conf.setInt("hbase.ipc.client.failed.servers.expiry", 200);
110     conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
111 
112     // Start the cluster
113     final HBaseTestingUtility util = new HBaseTestingUtility(conf);
114     util.startMiniDFSCluster(3);
115     util.startMiniZKCluster();
116     util.createRootDir();
117     final LocalHBaseCluster cluster =
118         new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class,
119             MiniHBaseCluster.MiniHBaseClusterRegionServer.class);
120     final int MASTER_INDEX = 0;
121     final MasterThread master = cluster.getMasters().get(MASTER_INDEX);
122     master.start();
123     LOG.info("Called master start on " + master.getName());
124     Thread shutdownThread = new Thread() {
125       public void run() {
126         LOG.info("Before call to shutdown master");
127         try {
128           try (Connection connection =
129               ConnectionFactory.createConnection(util.getConfiguration())) {
130             try (Admin admin = connection.getAdmin()) {
131               admin.shutdown();
132             }
133           }
134           LOG.info("After call to shutdown master");
135           cluster.waitOnMaster(MASTER_INDEX);
136         } catch (Exception e) {
137         }
138       };
139     };
140     shutdownThread.start();
141     LOG.info("Called master join on " + master.getName());
142     master.join();
143     shutdownThread.join();
144 
145     List<MasterThread> masterThreads = cluster.getMasters();
146     // make sure all the masters properly shutdown
147     assertEquals(0, masterThreads.size());
148 
149     util.shutdownMiniZKCluster();
150     util.shutdownMiniDFSCluster();
151     util.cleanupTestDir();
152   }
153 }