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