View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.CoordinatedStateManager;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.testclassification.LargeTests;
33  import org.apache.hadoop.hbase.LocalHBaseCluster;
34  import org.apache.hadoop.hbase.MiniHBaseCluster;
35  import org.apache.hadoop.hbase.ServerName;
36  import org.apache.hadoop.hbase.master.HMaster;
37  import org.apache.hadoop.hbase.master.ServerManager;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
39  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
40  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
41  import org.apache.hadoop.hbase.util.Threads;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  /**
46   * Tests region server termination during startup.
47   */
48  @Category(LargeTests.class)
49  public class TestRSKilledWhenInitializing {
50    private static boolean masterActive = false;
51    private static AtomicBoolean firstRS = new AtomicBoolean(true);
52  
53    /**
54     * Test verifies whether a region server is removing from online servers list in master if it went
55     * down after registering with master.
56     * @throws Exception
57     */
58    @Test(timeout = 180000)
59    public void testRSTermnationAfterRegisteringToMasterBeforeCreatingEphemeralNod() throws Exception {
60  
61      final int NUM_MASTERS = 1;
62      final int NUM_RS = 2;
63      firstRS.set(true);
64      // Create config to use for this cluster
65      Configuration conf = HBaseConfiguration.create();
66      conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
67  
68      // Start the cluster
69      final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
70      TEST_UTIL.startMiniDFSCluster(3);
71      TEST_UTIL.startMiniZKCluster();
72      TEST_UTIL.createRootDir();
73      final LocalHBaseCluster cluster =
74          new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class, MockedRegionServer.class);
75      final MasterThread master = cluster.getMasters().get(0);
76      master.start();
77      try {
78        long startTime = System.currentTimeMillis();
79        while (!master.getMaster().isActiveMaster()) {
80          try {
81            Thread.sleep(100);
82          } catch (InterruptedException ignored) {
83          }
84          if (System.currentTimeMillis() > startTime + 30000) {
85            throw new RuntimeException("Master not active after 30 seconds");
86          }
87        }
88        masterActive = true;
89        cluster.getRegionServers().get(0).start();
90        cluster.getRegionServers().get(1).start();
91        Thread.sleep(10000);
92        List<ServerName> onlineServersList =
93            master.getMaster().getServerManager().getOnlineServersList();
94        while (onlineServersList.size() > 1) {
95          Thread.sleep(100);
96          onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
97        }
98        assertEquals(onlineServersList.size(), 1);
99        cluster.shutdown();
100     } finally {
101       masterActive = false;
102       firstRS.set(true);
103       TEST_UTIL.shutdownMiniCluster();
104     }
105   }
106 
107   public static class MockedRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
108 
109     public MockedRegionServer(Configuration conf, CoordinatedStateManager cp)
110       throws IOException, InterruptedException {
111       super(conf, cp);
112     }
113 
114     @Override
115     protected void handleReportForDutyResponse(RegionServerStartupResponse c) throws IOException {
116       if (firstRS.getAndSet(false)) {
117         for (NameStringPair e : c.getMapEntriesList()) {
118           String key = e.getName();
119           // The hostname the master sees us as.
120           if (key.equals(HConstants.KEY_FOR_HOSTNAME_SEEN_BY_MASTER)) {
121             String hostnameFromMasterPOV = e.getValue();
122             assertEquals(super.getRpcServer().getListenerAddress().getHostName(),
123               hostnameFromMasterPOV);
124           }
125         }
126         while (!masterActive) {
127           Threads.sleep(100);
128         }
129         super.kill();
130       } else {
131         super.handleReportForDutyResponse(c);
132       }
133     }
134   }
135 }