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