1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master;
19
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.Abortable;
34 import org.apache.hadoop.hbase.TableName;
35 import org.apache.hadoop.hbase.exceptions.DeserializationException;
36 import org.apache.hadoop.hbase.HBaseTestingUtility;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.HRegionInfo;
39 import org.apache.hadoop.hbase.ServerLoad;
40 import org.apache.hadoop.hbase.Server;
41 import org.apache.hadoop.hbase.ServerName;
42 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
43 import org.apache.hadoop.hbase.catalog.CatalogTracker;
44 import org.apache.hadoop.hbase.catalog.MetaMockingUtil;
45 import org.apache.hadoop.hbase.client.HConnection;
46 import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
47 import org.apache.hadoop.hbase.client.Result;
48 import org.apache.hadoop.hbase.master.RegionState.State;
49 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
50 import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
51 import org.apache.hadoop.hbase.util.FSUtils;
52 import org.apache.hadoop.hbase.util.Threads;
53 import org.apache.hadoop.hbase.zookeeper.MetaRegionTracker;
54 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
55 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
56 import org.apache.hadoop.hbase.testclassification.MediumTests;
57 import org.apache.zookeeper.KeeperException;
58 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
59 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
60 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerReportRequest;
61 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupRequest;
62 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
63 import com.google.protobuf.ServiceException;
64 import org.junit.After;
65 import org.junit.AfterClass;
66 import org.junit.BeforeClass;
67 import org.junit.Test;
68 import org.mockito.Mockito;
69 import org.junit.experimental.categories.Category;
70 import org.apache.commons.logging.Log;
71 import org.apache.commons.logging.LogFactory;
72
73
74
75
76
77
78
79
80
81 @Category(MediumTests.class)
82 public class TestMasterNoCluster {
83 private static final Log LOG = LogFactory.getLog(TestMasterNoCluster.class);
84 private static final HBaseTestingUtility TESTUTIL = new HBaseTestingUtility();
85
86 @BeforeClass
87 public static void setUpBeforeClass() throws Exception {
88 Configuration c = TESTUTIL.getConfiguration();
89
90 FSUtils.setRootDir(c, TESTUTIL.getDataTestDir());
91
92 TESTUTIL.startMiniZKCluster();
93 }
94
95 @AfterClass
96 public static void tearDownAfterClass() throws Exception {
97 TESTUTIL.shutdownMiniZKCluster();
98 }
99
100 @After
101 public void tearDown()
102 throws KeeperException, ZooKeeperConnectionException, IOException {
103
104 ZooKeeperWatcher zkw = new ZooKeeperWatcher(TESTUTIL.getConfiguration(),
105 "@Before", new Abortable() {
106 @Override
107 public void abort(String why, Throwable e) {
108 throw new RuntimeException(why, e);
109 }
110
111 @Override
112 public boolean isAborted() {
113 return false;
114 }
115 });
116 ZKUtil.deleteNodeRecursively(zkw, zkw.baseZNode);
117 zkw.close();
118 }
119
120
121
122
123
124
125
126 @Test (timeout=30000)
127 public void testStopDuringStart()
128 throws IOException, KeeperException, InterruptedException {
129 HMaster master = new HMaster(TESTUTIL.getConfiguration());
130 master.start();
131
132 master.stopMaster();
133 master.join();
134 }
135
136
137
138
139
140
141
142
143 @Test (timeout=30000)
144 public void testFailover()
145 throws IOException, KeeperException, InterruptedException, ServiceException {
146 final long now = System.currentTimeMillis();
147
148
149 final ServerName sn0 = ServerName.valueOf("0.example.org", 0, now);
150 final ServerName sn1 = ServerName.valueOf("1.example.org", 1, now);
151 final ServerName sn2 = ServerName.valueOf("2.example.org", 2, now);
152 final ServerName [] sns = new ServerName [] {sn0, sn1, sn2};
153
154 final Configuration conf = TESTUTIL.getConfiguration();
155 final MockRegionServer rs0 = new MockRegionServer(conf, sn0);
156 final MockRegionServer rs1 = new MockRegionServer(conf, sn1);
157 final MockRegionServer rs2 = new MockRegionServer(conf, sn2);
158
159
160 MetaRegionTracker.setMetaLocation(rs0.getZooKeeper(), rs0.getServerName(), State.OPEN);
161 final TableName tableName = TableName.valueOf("t");
162 Result [] results = new Result [] {
163 MetaMockingUtil.getMetaTableRowResult(
164 new HRegionInfo(tableName, HConstants.EMPTY_START_ROW, HBaseTestingUtility.KEYS[1]),
165 rs2.getServerName()),
166 MetaMockingUtil.getMetaTableRowResult(
167 new HRegionInfo(tableName, HBaseTestingUtility.KEYS[1], HBaseTestingUtility.KEYS[2]),
168 rs2.getServerName()),
169 MetaMockingUtil.getMetaTableRowResult(new HRegionInfo(tableName, HBaseTestingUtility.KEYS[2],
170 HConstants.EMPTY_END_ROW),
171 rs2.getServerName())
172 };
173 rs1.setNextResults(HRegionInfo.FIRST_META_REGIONINFO.getRegionName(), results);
174
175
176
177
178
179 HMaster master = new HMaster(conf) {
180 InetAddress getRemoteInetAddress(final int port, final long serverStartCode)
181 throws UnknownHostException {
182
183 ServerName sn = sns[port];
184 return InetAddress.getByAddress(sn.getHostname(),
185 new byte [] {10, 0, 0, (byte)sn.getPort()});
186 }
187
188 @Override
189 ServerManager createServerManager(Server master, MasterServices services)
190 throws IOException {
191 ServerManager sm = super.createServerManager(master, services);
192
193 ServerManager spy = Mockito.spy(sm);
194
195 Mockito.doReturn(RegionOpeningState.OPENED).when(spy).
196 sendRegionOpen((ServerName)Mockito.any(), (HRegionInfo)Mockito.any(),
197 Mockito.anyInt(), Mockito.anyListOf(ServerName.class));
198 return spy;
199 }
200
201 @Override
202 CatalogTracker createCatalogTracker(ZooKeeperWatcher zk,
203 Configuration conf, Abortable abortable)
204 throws IOException {
205
206
207
208
209 HConnection connection =
210 HConnectionTestingUtility.getMockedConnectionAndDecorate(TESTUTIL.getConfiguration(),
211 rs0, rs0, rs0.getServerName(), HRegionInfo.FIRST_META_REGIONINFO);
212 return new CatalogTracker(zk, conf, connection, abortable);
213 }
214
215 @Override
216 void initNamespace() {
217 }
218 };
219 master.start();
220
221 try {
222
223 while (!master.isRpcServerOpen()) Threads.sleep(10);
224
225 for (int i = 0; i < sns.length; i++) {
226 RegionServerReportRequest.Builder request = RegionServerReportRequest.newBuilder();;
227 ServerName sn = ServerName.parseVersionedServerName(sns[i].getVersionedBytes());
228 request.setServer(ProtobufUtil.toServerName(sn));
229 request.setLoad(ServerLoad.EMPTY_SERVERLOAD.obtainServerLoadPB());
230 master.regionServerReport(null, request.build());
231 }
232
233 while (!master.isInitialized()) {Threads.sleep(10);}
234 assertTrue(master.isInitialized());
235 } finally {
236 rs0.stop("Test is done");
237 rs1.stop("Test is done");
238 rs2.stop("Test is done");
239 master.stopMaster();
240 master.join();
241 }
242 }
243
244
245
246
247
248
249
250
251
252 @Test (timeout=60000)
253 public void testCatalogDeploys()
254 throws Exception {
255 final Configuration conf = TESTUTIL.getConfiguration();
256 conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
257 conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 1);
258
259 final long now = System.currentTimeMillis();
260
261 final ServerName sn = ServerName.valueOf("0.example.org", 0, now);
262
263
264 final MockRegionServer rs0 = new MockRegionServer(conf, sn);
265
266
267
268
269
270 HMaster master = new HMaster(conf) {
271 InetAddress getRemoteInetAddress(final int port, final long serverStartCode)
272 throws UnknownHostException {
273
274 return InetAddress.getByAddress(rs0.getServerName().getHostname(),
275 new byte [] {10, 0, 0, 0});
276 }
277
278 @Override
279 ServerManager createServerManager(Server master, MasterServices services)
280 throws IOException {
281 ServerManager sm = super.createServerManager(master, services);
282
283 ServerManager spy = Mockito.spy(sm);
284
285 Mockito.doReturn(RegionOpeningState.OPENED).when(spy).
286 sendRegionOpen((ServerName)Mockito.any(), (HRegionInfo)Mockito.any(),
287 Mockito.anyInt(), Mockito.anyListOf(ServerName.class));
288 return spy;
289 }
290
291 @Override
292 CatalogTracker createCatalogTracker(ZooKeeperWatcher zk,
293 Configuration conf, Abortable abortable)
294 throws IOException {
295
296
297
298
299 HConnection connection =
300 HConnectionTestingUtility.getMockedConnectionAndDecorate(TESTUTIL.getConfiguration(),
301 rs0, rs0, rs0.getServerName(), HRegionInfo.FIRST_META_REGIONINFO);
302 return new CatalogTracker(zk, conf, connection, abortable);
303 }
304
305 @Override
306 void initNamespace() {
307 }
308 };
309 master.start();
310 LOG.info("Master has started");
311
312 try {
313
314 while (!master.isRpcServerOpen()) Threads.sleep(10);
315 LOG.info("RpcServerOpen has started");
316
317
318 RegionServerStartupRequest.Builder request = RegionServerStartupRequest.newBuilder();
319 request.setPort(rs0.getServerName().getPort());
320 request.setServerStartCode(rs0.getServerName().getStartcode());
321 request.setServerCurrentTime(now);
322 RegionServerStartupResponse result =
323 master.regionServerStartup(null, request.build());
324 String rshostname = new String();
325 for (NameStringPair e : result.getMapEntriesList()) {
326 if (e.getName().toString().equals(HConstants.KEY_FOR_HOSTNAME_SEEN_BY_MASTER)) {
327 rshostname = e.getValue();
328 }
329 }
330
331 assertEquals(rs0.getServerName().getHostname(), rshostname);
332
333
334
335
336
337
338
339 Mocking.fakeRegionServerRegionOpenInZK(master, rs0.getZooKeeper(),
340 rs0.getServerName(), HRegionInfo.FIRST_META_REGIONINFO);
341 LOG.info("fakeRegionServerRegionOpenInZK has started");
342
343
344
345
346
347 MetaRegionTracker.setMetaLocation(rs0.getZooKeeper(), rs0.getServerName(), State.OPEN);
348
349 while (!master.isInitialized()) {Threads.sleep(10);}
350 assertTrue(master.isInitialized());
351 } finally {
352 rs0.stop("Test is done");
353 master.stopMaster();
354 master.join();
355 }
356 }
357
358 @Test
359 public void testNotPullingDeadRegionServerFromZK()
360 throws IOException, KeeperException, InterruptedException {
361 final Configuration conf = TESTUTIL.getConfiguration();
362 final ServerName newServer = ServerName.valueOf("test.sample", 1, 101);
363 final ServerName deadServer = ServerName.valueOf("test.sample", 1, 100);
364 final MockRegionServer rs0 = new MockRegionServer(conf, newServer);
365
366 HMaster master = new HMaster(conf) {
367 @Override
368 void assignMeta(MonitoredTask status, Set<ServerName> previouslyFailedMeatRSs) {
369 }
370
371 @Override
372 void initializeZKBasedSystemTrackers() throws IOException,
373 InterruptedException, KeeperException {
374 super.initializeZKBasedSystemTrackers();
375
376 serverManager.recordNewServerWithLock(newServer, ServerLoad.EMPTY_SERVERLOAD);
377
378 List<ServerName> onlineServers = new ArrayList<ServerName>();
379 onlineServers.add(deadServer);
380 onlineServers.add(newServer);
381
382 regionServerTracker = Mockito.spy(regionServerTracker);
383 Mockito.doReturn(onlineServers).when(
384 regionServerTracker).getOnlineServers();
385 }
386
387 @Override
388 CatalogTracker createCatalogTracker(ZooKeeperWatcher zk,
389 Configuration conf, Abortable abortable)
390 throws IOException {
391
392
393
394
395 HConnection connection =
396 HConnectionTestingUtility.getMockedConnectionAndDecorate(TESTUTIL.getConfiguration(),
397 rs0, rs0, rs0.getServerName(), HRegionInfo.FIRST_META_REGIONINFO);
398 return new CatalogTracker(zk, conf, connection, abortable);
399 }
400
401 @Override
402 void initNamespace() {
403 }
404 };
405 master.start();
406
407 try {
408
409 while (!master.initialized) Threads.sleep(10);
410 LOG.info("Master is initialized");
411
412 assertFalse("The dead server should not be pulled in",
413 master.serverManager.isServerOnline(deadServer));
414 } finally {
415 master.stopMaster();
416 master.join();
417 }
418 }
419 }