1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
77
78
79 @Test (timeout=180000)
80 public void testReportForDutyWithMasterChange() throws Exception {
81
82
83
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
96 cluster.getConfiguration().set(HConstants.REGION_SERVER_IMPL, MyRegionServer.class.getName());
97 rs2 = cluster.addRegionServer();
98
99
100
101 LOG.debug("Starting 2nd region server: " + rs2.getRegionServer().getServerName());
102 rs2.start();
103
104 waitForSecondRsStarted();
105
106
107 master.getMaster().stop("Stopping master");
108
109
110
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
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
149
150
151
152
153 public static class MyRegionServer extends MiniHBaseClusterRegionServer {
154
155 private ServerName sn;
156
157
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
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 }