1   /**
2    * Copyright 2010 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.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HServerAddress;
29  import org.apache.hadoop.hbase.HServerInfo;
30  import org.apache.hadoop.hbase.MiniHBaseCluster;
31  import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
32  import org.apache.hadoop.hbase.YouAreDeadException;
33  import org.apache.hadoop.hbase.regionserver.HRegionServer;
34  import org.junit.AfterClass;
35  import org.junit.Before;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  
39  public class TestKillingServersFromMaster {
40    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
41    private static MiniHBaseCluster cluster;
42  
43    @BeforeClass
44    public static void beforeAllTests() throws Exception {
45      TEST_UTIL.startMiniCluster(2);
46      cluster = TEST_UTIL.getHBaseCluster();
47    }
48  
49    @AfterClass
50    public static void afterAllTests() throws IOException {
51      TEST_UTIL.shutdownMiniCluster();
52    }
53  
54    @Before
55    public void setup() throws IOException {
56      TEST_UTIL.ensureSomeRegionServersAvailable(2);
57    }
58  
59    /**
60     * Test that a region server that reports with the wrong start code
61     * gets shut down
62     * See HBASE-2613
63     * @throws Exception
64     */
65    @Test (timeout=180000)
66    public void testRsReportsWrongStartCode() throws Exception {
67      MiniHBaseClusterRegionServer firstServer =
68        (MiniHBaseClusterRegionServer)cluster.getRegionServer(0);
69      HServerInfo hsi = firstServer.getServerInfo();
70      // This constructor creates a new startcode
71      firstServer.setHServerInfo(new HServerInfo(hsi.getServerAddress(),
72        hsi.getInfoPort(), hsi.getHostname()));
73      cluster.waitOnRegionServer(0);
74      assertEquals(1, cluster.getLiveRegionServerThreads().size());
75    }
76  
77    /**
78     * Test that a region server that reports with the wrong address
79     * gets shut down
80     * See HBASE-2613
81     * @throws Exception
82     */
83    @Test (timeout=180000)
84    public void testRsReportsWrongAddress() throws Exception {
85      MiniHBaseClusterRegionServer firstServer =
86        (MiniHBaseClusterRegionServer)cluster.getRegionServer(0);
87      firstServer.getHServerInfo().setServerAddress(
88        new HServerAddress("0.0.0.0", 60010));
89      cluster.waitOnRegionServer(0);
90      assertEquals(1, cluster.getLiveRegionServerThreads().size());
91    }
92  
93    /**
94     * Send a YouAreDeadException to the region server and expect it to shut down
95     * See HBASE-2691
96     * @throws Exception
97     */
98    @Test (timeout=180000)
99    public void testSendYouAreDead() throws Exception {
100     cluster.addExceptionToSendRegionServer(0, new YouAreDeadException("bam!"));
101     cluster.waitOnRegionServer(0);
102     assertEquals(1, cluster.getLiveRegionServerThreads().size());
103   }
104 }