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.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.LocalHBaseCluster;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
33  import org.apache.hadoop.hbase.master.HMaster;
34  import org.apache.hadoop.hbase.master.ServerManager;
35  import org.apache.hadoop.hbase.testclassification.MediumTests;
36  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
37  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
38  import org.apache.zookeeper.KeeperException;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(MediumTests.class)
45  public class TestRegionServerReportForDuty {
46  
47    private static final Log LOG = LogFactory.getLog(TestRegionServerReportForDuty.class);
48  
49    private static final long SLEEP_INTERVAL = 500;
50  
51    private HBaseTestingUtility testUtil;
52    private LocalHBaseCluster cluster;
53    private RegionServerThread rs;
54    private RegionServerThread rs2;
55    private MasterThread master;
56    private MasterThread backupMaster;
57  
58    @Before
59    public void setUp() throws Exception {
60      testUtil = new HBaseTestingUtility();
61      testUtil.startMiniDFSCluster(1);
62      testUtil.startMiniZKCluster(1);
63      testUtil.createRootDir();
64      cluster = new LocalHBaseCluster(testUtil.getConfiguration(), 0, 0);
65    }
66  
67    @After
68    public void tearDown() throws Exception {
69      cluster.shutdown();
70      cluster.join();
71      testUtil.shutdownMiniZKCluster();
72      testUtil.shutdownMiniDFSCluster();
73    }
74  
75    /**
76     * Tests region sever reportForDuty with backup master becomes primary master after
77     * the first master goes away.
78     */
79    @Test (timeout=180000)
80    public void testReportForDutyWithMasterChange() throws Exception {
81  
82      // Start a master and wait for it to become the active/primary master.
83      // Use a random unique port
84      cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
85      cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
86      cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 1);
87      master = cluster.addMaster();
88      rs = cluster.addRegionServer();
89      LOG.debug("Starting master: " + master.getMaster().getServerName());
90      master.start();
91      rs.start();
92  
93      waitForClusterOnline(master);
94  
95      // Add a 2nd region server
96      cluster.getConfiguration().set(HConstants.REGION_SERVER_IMPL, MyRegionServer.class.getName());
97      rs2 = cluster.addRegionServer();
98      // Start the region server. This region server will refresh RPC connection
99      // from the current active master to the next active master before completing
100     // reportForDuty
101     LOG.debug("Starting 2nd region server: " + rs2.getRegionServer().getServerName());
102     rs2.start();
103 
104     waitForSecondRsStarted();
105 
106     // Stop the current master.
107     master.getMaster().stop("Stopping master");
108 
109     // Start a new master and use another random unique port
110     // Also let it wait for exactly 2 region severs to report in.
111     cluster.getConfiguration().setInt(HConstants.MASTER_PORT, HBaseTestingUtility.randomFreePort());
112     cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 2);
113     cluster.getConfiguration().setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 2);
114     backupMaster = cluster.addMaster();
115     LOG.debug("Starting new master: " + backupMaster.getMaster().getServerName());
116     backupMaster.start();
117 
118     waitForClusterOnline(backupMaster);
119 
120     // Do some checking/asserts here.
121     assertTrue(backupMaster.getMaster().isActiveMaster());
122     assertTrue(backupMaster.getMaster().isInitialized());
123     assertEquals(backupMaster.getMaster().getServerManager().getOnlineServersList().size(), 2);
124 
125   }
126 
127   private void waitForClusterOnline(MasterThread master) throws InterruptedException {
128     while (true) {
129       if (master.getMaster().isInitialized()) {
130         break;
131       }
132       Thread.sleep(SLEEP_INTERVAL);
133       LOG.debug("Waiting for master to come online ...");
134     }
135     rs.waitForServerOnline();
136   }
137 
138   private void waitForSecondRsStarted() throws InterruptedException {
139     while (true) {
140       if (((MyRegionServer) rs2.getRegionServer()).getRpcStubCreatedFlag() == true) {
141         break;
142       }
143       Thread.sleep(SLEEP_INTERVAL);
144       LOG.debug("Waiting 2nd RS to be started ...");
145     }
146   }
147 
148   // Create a Region Server that provide a hook so that we can wait for the master switch over
149   // before continuing reportForDuty to the mater.
150   // The idea is that we get a RPC connection to the first active master, then we wait.
151   // The first master goes down, the second master becomes the active master. The region
152   // server continues reportForDuty. It should succeed with the new master.
153   public static class MyRegionServer extends MiniHBaseClusterRegionServer {
154 
155     private ServerName sn;
156     // This flag is to make sure this rs has obtained the rpcStub to the first master.
157     // The first master will go down after this.
158     private boolean rpcStubCreatedFlag = false;
159     private boolean masterChanged = false;
160 
161     public MyRegionServer(Configuration conf)
162       throws IOException, KeeperException,
163         InterruptedException {
164       super(conf);
165     }
166 
167     @Override
168     protected synchronized ServerName createRegionServerStatusStub() {
169       sn = super.createRegionServerStatusStub();
170       rpcStubCreatedFlag = true;
171 
172       // Wait for master switch over. Only do this for the second region server.
173       while (!masterChanged) {
174         ServerName newSn = super.getMasterAddressTracker().getMasterAddress(true);
175         if (newSn != null && !newSn.equals(sn)) {
176           masterChanged = true;
177           break;
178         }
179         try {
180           Thread.sleep(SLEEP_INTERVAL);
181         } catch (InterruptedException e) {
182           return null;
183         }
184         LOG.debug("Waiting for master switch over ... ");
185       }
186       return sn;
187     }
188 
189     public boolean getRpcStubCreatedFlag() {
190       return rpcStubCreatedFlag;
191     }
192   }
193 }