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.hadoop.hbase.*;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.testclassification.LargeTests;
33  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(LargeTests.class)
38  public class TestMasterShutdown {
39    private static final Log LOG = LogFactory.getLog(TestMasterShutdown.class);
40  
41    /**
42     * Simple test of shutdown.
43     * <p>
44     * Starts with three masters.  Tells the active master to shutdown the cluster.
45     * Verifies that all masters are properly shutdown.
46     * @throws Exception
47     */
48    @Test (timeout=240000)
49    public void testMasterShutdown() throws Exception {
50  
51      final int NUM_MASTERS = 3;
52      final int NUM_RS = 3;
53  
54      // Create config to use for this cluster
55      Configuration conf = HBaseConfiguration.create();
56  
57      // Start the cluster
58      HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
59      TEST_UTIL.startMiniCluster(NUM_MASTERS, NUM_RS);
60      MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
61  
62      // get all the master threads
63      List<MasterThread> masterThreads = cluster.getMasterThreads();
64  
65      // wait for each to come online
66      for (MasterThread mt : masterThreads) {
67        assertTrue(mt.isAlive());
68      }
69  
70      // find the active master
71      HMaster active = null;
72      for (int i = 0; i < masterThreads.size(); i++) {
73        if (masterThreads.get(i).getMaster().isActiveMaster()) {
74          active = masterThreads.get(i).getMaster();
75          break;
76        }
77      }
78      assertNotNull(active);
79      // make sure the other two are backup masters
80      ClusterStatus status = active.getClusterStatus();
81      assertEquals(2, status.getBackupMastersSize());
82      assertEquals(2, status.getBackupMasters().size());
83  
84      // tell the active master to shutdown the cluster
85      active.shutdown();
86      
87      for (int i = NUM_MASTERS - 1; i >= 0 ;--i) {
88        cluster.waitOnMaster(i);
89      }
90      // make sure all the masters properly shutdown
91      assertEquals(0,masterThreads.size());
92      
93      TEST_UTIL.shutdownMiniCluster();
94    }
95  
96    @Test(timeout = 180000)
97    public void testMasterShutdownBeforeStartingAnyRegionServer() throws Exception {
98  
99      final int NUM_MASTERS = 1;
100     final int NUM_RS = 0;
101 
102     // Create config to use for this cluster
103     Configuration conf = HBaseConfiguration.create();
104 
105     // Start the cluster
106     final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
107     TEST_UTIL.startMiniDFSCluster(3);
108     TEST_UTIL.startMiniZKCluster();
109     TEST_UTIL.createRootDir();
110     final LocalHBaseCluster cluster =
111         new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class,
112             MiniHBaseCluster.MiniHBaseClusterRegionServer.class);
113     final MasterThread master = cluster.getMasters().get(0);
114     master.start();
115     Thread shutdownThread = new Thread() {
116       public void run() {
117         try {
118           TEST_UTIL.getHBaseAdmin().shutdown();
119           cluster.waitOnMaster(0);
120         } catch (Exception e) {
121         }
122       };
123     };
124     shutdownThread.start();
125     master.join();
126     shutdownThread.join();
127 
128     List<MasterThread> masterThreads = cluster.getMasters();
129     // make sure all the masters properly shutdown
130     assertEquals(0, masterThreads.size());
131 
132     TEST_UTIL.shutdownMiniZKCluster();
133     TEST_UTIL.shutdownMiniDFSCluster();
134     TEST_UTIL.cleanupTestDir();
135   }
136 
137 }
138