View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.CoordinatedStateManager;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.LocalHBaseCluster;
32  import org.apache.hadoop.hbase.ServerName;
33  import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
34  import org.apache.hadoop.hbase.master.HMaster;
35  import org.apache.hadoop.hbase.master.ServerManager;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
38  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
39  import org.apache.zookeeper.KeeperException;
40  import org.junit.After;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(MediumTests.class)
46  public class TestRegionServerReportForDuty {
47  
48    private static final Log LOG = LogFactory.getLog(TestRegionServerReportForDuty.class);
49  
50    private static final long SLEEP_INTERVAL = 500;
51  
52    private HBaseTestingUtility testUtil;
53    private LocalHBaseCluster cluster;
54    private RegionServerThread rs;
55    private RegionServerThread rs2;
56    private MasterThread master;
57    private MasterThread backupMaster;
58  
59    @Before
60    public void setUp() throws Exception {
61      testUtil = new HBaseTestingUtility();
62      testUtil.startMiniDFSCluster(1);
63      testUtil.startMiniZKCluster(1);
64      testUtil.createRootDir();
65      cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
66    }
67  
68    @After
69    public void tearDown() throws Exception {
70      cluster.shutdown();
71      cluster.join();
72      testUtil.shutdownMiniZKCluster();
73      testUtil.shutdownMiniDFSCluster();
74    }
75  
76    /**
77     * Tests region sever reportForDuty with backup master becomes primary master after
78     * the first master goes away.
79     */
80    @Test (timeout=180000)
81    public void testReportForDutyWithMasterChange() throws Exception {
82  
83      // Start a master and wait for it to become the active/primary master.
84      // Use a random unique port
85      cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
86      cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
87      cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 1);
88      master = cluster.addMaster();
89      rs = cluster.addRegionServer();
90      LOG.debug("Starting master: " + master.getMaster().getServerName());
91      master.start();
92      rs.start();
93  
94      // Add a 2nd region server
95      cluster.getConfiguration().set(HConstants.REGION_SERVER_IMPL, MyRegionServer.class.getName());
96      rs2 = cluster.addRegionServer();
97      // Start the region server. This region server will refresh RPC connection
98      // from the current active master to the next active master before completing
99      // reportForDuty
100     LOG.debug("Starting 2nd region server: " + rs2.getRegionServer().getServerName());
101     rs2.start();
102 
103     waitForClusterOnline(master);
104 
105     // Stop the current master.
106     master.getMaster().stop("Stopping master");
107 
108     // Start a new master and use another random unique port
109     // Also let it wait for exactly 2 region severs to report in.
110     cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
111     cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 2);
112     cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 2);
113     backupMaster = cluster.addMaster();
114     LOG.debug("Starting new master: " + backupMaster.getMaster().getServerName());
115     backupMaster.start();
116 
117     waitForClusterOnline(backupMaster);
118 
119     // Do some checking/asserts here.
120     assertTrue(backupMaster.getMaster().isActiveMaster());
121     assertTrue(backupMaster.getMaster().isInitialized());
122     assertEquals(backupMaster.getMaster().getServerManager().getOnlineServersList().size(), 2);
123 
124   }
125 
126   private void waitForClusterOnline(MasterThread master) throws InterruptedException {
127     while (true) {
128       if (master.getMaster().isInitialized()
129           && ((MyRegionServer) rs2.getRegionServer()).getRpcStubCreatedFlag() == true) {
130         break;
131       }
132       Thread.sleep(SLEEP_INTERVAL);
133       LOG.debug("Waiting for master to come online ...");
134     }
135     rs.waitForServerOnline();
136   }
137 
138   // Create a Region Server that provide a hook so that we can wait for the master switch over
139   // before continuing reportForDuty to the mater.
140   // The idea is that we get a RPC connection to the first active master, then we wait.
141   // The first master goes down, the second master becomes the active master. The region
142   // server continues reportForDuty. It should succeed with the new master.
143   public static class MyRegionServer extends MiniHBaseClusterRegionServer {
144 
145     private ServerName sn;
146     // This flag is to make sure this rs has obtained the rpcStub to the first master.
147     // The first master will go down after this.
148     private boolean rpcStubCreatedFlag = false;
149     private boolean masterChanged = false;
150 
151     public MyRegionServer(Configuration conf, CoordinatedStateManager cp)
152       throws IOException, KeeperException,
153         InterruptedException {
154       super(conf, cp);
155     }
156 
157     @Override
158     protected synchronized ServerName createRegionServerStatusStub() {
159       sn = super.createRegionServerStatusStub();
160       rpcStubCreatedFlag = true;
161 
162       // Wait for master switch over. Only do this for the second region server.
163       while (!masterChanged) {
164         ServerName newSn = super.getMasterAddressTracker().getMasterAddress(true);
165         if (newSn != null && !newSn.equals(sn)) {
166           masterChanged = true;
167           break;
168         }
169         try {
170           Thread.sleep(SLEEP_INTERVAL);
171         } catch (InterruptedException e) {
172         }
173         LOG.debug("Waiting for master switch over ... ");
174       }
175       return sn;
176     }
177 
178     public boolean getRpcStubCreatedFlag() {
179       return rpcStubCreatedFlag;
180     }
181   }
182 }