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.net.InetSocketAddress;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.CoordinatedStateManager;
30  import org.apache.hadoop.hbase.HBaseConfiguration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.testclassification.LargeTests;
34  import org.apache.hadoop.hbase.LocalHBaseCluster;
35  import org.apache.hadoop.hbase.MiniHBaseCluster;
36  import org.apache.hadoop.hbase.ServerName;
37  import org.apache.hadoop.hbase.master.HMaster;
38  import org.apache.hadoop.hbase.master.ServerManager;
39  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
40  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
41  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
42  import org.apache.hadoop.hbase.util.Threads;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  /**
47   * Tests region server termination during startup.
48   */
49  @Category(LargeTests.class)
50  public class TestRSKilledWhenInitializing {
51    private static boolean masterActive = false;
52    private static AtomicBoolean firstRS = new AtomicBoolean(true);
53  
54    /**
55     * Test verifies whether a region server is removing from online servers list in master if it went
56     * down after registering with master.
57     * @throws Exception
58     */
59    @Test(timeout = 180000)
60    public void testRSTermnationAfterRegisteringToMasterBeforeCreatingEphemeralNod() throws Exception {
61  
62      final int NUM_MASTERS = 1;
63      final int NUM_RS = 2;
64      firstRS.set(true);
65      // Create config to use for this cluster
66      Configuration conf = HBaseConfiguration.create();
67      conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
68  
69      // Start the cluster
70      final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(conf);
71      TEST_UTIL.startMiniDFSCluster(3);
72      TEST_UTIL.startMiniZKCluster();
73      TEST_UTIL.createRootDir();
74      final LocalHBaseCluster cluster =
75          new LocalHBaseCluster(conf, NUM_MASTERS, NUM_RS, HMaster.class, MockedRegionServer.class);
76      final MasterThread master = cluster.getMasters().get(0);
77      master.start();
78      try {
79        long startTime = System.currentTimeMillis();
80        while (!master.getMaster().isActiveMaster()) {
81          try {
82            Thread.sleep(100);
83          } catch (InterruptedException ignored) {
84          }
85          if (System.currentTimeMillis() > startTime + 30000) {
86            throw new RuntimeException("Master not active after 30 seconds");
87          }
88        }
89        masterActive = true;
90        cluster.getRegionServers().get(0).start();
91        cluster.getRegionServers().get(1).start();
92        Thread.sleep(10000);
93        List<ServerName> onlineServersList =
94            master.getMaster().getServerManager().getOnlineServersList();
95        while (onlineServersList.size() > 1) {
96          Thread.sleep(100);
97          onlineServersList = master.getMaster().getServerManager().getOnlineServersList();
98        }
99        assertEquals(onlineServersList.size(), 1);
100       cluster.shutdown();
101     } finally {
102       masterActive = false;
103       firstRS.set(true);
104       TEST_UTIL.shutdownMiniCluster();
105     }
106   }
107 
108   public static class MockedRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
109 
110     public MockedRegionServer(Configuration conf, CoordinatedStateManager cp)
111       throws IOException, InterruptedException {
112       super(conf, cp);
113     }
114 
115     @Override
116     protected void handleReportForDutyResponse(RegionServerStartupResponse c) throws IOException {
117       if (firstRS.getAndSet(false)) {
118         InetSocketAddress address = super.getRpcServer().getListenerAddress();
119         if (address == null) {
120           throw new IOException("Listener channel is closed");
121         }
122         for (NameStringPair e : c.getMapEntriesList()) {
123           String key = e.getName();
124           // The hostname the master sees us as.
125           if (key.equals(HConstants.KEY_FOR_HOSTNAME_SEEN_BY_MASTER)) {
126             String hostnameFromMasterPOV = e.getValue();
127             assertEquals(address.getHostName(), hostnameFromMasterPOV);
128           }
129         }
130         while (!masterActive) {
131           Threads.sleep(100);
132         }
133         super.kill();
134       } else {
135         super.handleReportForDutyResponse(c);
136       }
137     }
138   }
139 }