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 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotSame;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.concurrent.atomic.AtomicBoolean;
32
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.hbase.CellScannable;
35 import org.apache.hadoop.hbase.CellUtil;
36 import org.apache.hadoop.hbase.CoordinatedStateException;
37 import org.apache.hadoop.hbase.CoordinatedStateManager;
38 import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
39 import org.apache.hadoop.hbase.DoNotRetryIOException;
40 import org.apache.hadoop.hbase.HBaseConfiguration;
41 import org.apache.hadoop.hbase.HBaseTestingUtility;
42 import org.apache.hadoop.hbase.HConstants;
43 import org.apache.hadoop.hbase.HRegionInfo;
44 import org.apache.hadoop.hbase.MetaMockingUtil;
45 import org.apache.hadoop.hbase.RegionException;
46 import org.apache.hadoop.hbase.RegionTransition;
47 import org.apache.hadoop.hbase.Server;
48 import org.apache.hadoop.hbase.ServerLoad;
49 import org.apache.hadoop.hbase.ServerName;
50 import org.apache.hadoop.hbase.TableName;
51 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
52 import org.apache.hadoop.hbase.client.ClusterConnection;
53 import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
54 import org.apache.hadoop.hbase.client.Result;
55 import org.apache.hadoop.hbase.coordination.BaseCoordinatedStateManager;
56 import org.apache.hadoop.hbase.coordination.OpenRegionCoordination;
57 import org.apache.hadoop.hbase.coordination.ZkCoordinatedStateManager;
58 import org.apache.hadoop.hbase.coordination.ZkOpenRegionCoordination;
59 import org.apache.hadoop.hbase.exceptions.DeserializationException;
60 import org.apache.hadoop.hbase.executor.EventType;
61 import org.apache.hadoop.hbase.executor.ExecutorService;
62 import org.apache.hadoop.hbase.executor.ExecutorType;
63 import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
64 import org.apache.hadoop.hbase.master.RegionState.State;
65 import org.apache.hadoop.hbase.master.TableLockManager.NullTableLockManager;
66 import org.apache.hadoop.hbase.master.balancer.LoadBalancerFactory;
67 import org.apache.hadoop.hbase.master.balancer.SimpleLoadBalancer;
68 import org.apache.hadoop.hbase.master.handler.EnableTableHandler;
69 import org.apache.hadoop.hbase.master.handler.ServerShutdownHandler;
70 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
71 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
72 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
73 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetResponse;
74 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
75 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
76 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.SplitLogTask.RecoveryMode;
77 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.Table;
78 import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
79 import org.apache.hadoop.hbase.testclassification.MediumTests;
80 import org.apache.hadoop.hbase.util.Bytes;
81 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
82 import org.apache.hadoop.hbase.util.Threads;
83 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
84 import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
85 import org.apache.hadoop.hbase.zookeeper.ZKAssign;
86 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
87 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
88 import org.apache.zookeeper.KeeperException;
89 import org.apache.zookeeper.KeeperException.NodeExistsException;
90 import org.apache.zookeeper.Watcher;
91 import org.junit.After;
92 import org.junit.AfterClass;
93 import org.junit.Before;
94 import org.junit.BeforeClass;
95 import org.junit.Test;
96 import org.junit.experimental.categories.Category;
97 import org.mockito.Mockito;
98 import org.mockito.internal.util.reflection.Whitebox;
99 import org.mockito.invocation.InvocationOnMock;
100 import org.mockito.stubbing.Answer;
101
102 import com.google.protobuf.RpcController;
103 import com.google.protobuf.ServiceException;
104
105
106
107
108
109 @Category(MediumTests.class)
110 public class TestAssignmentManager {
111 private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
112 private static final ServerName SERVERNAME_A =
113 ServerName.valueOf("example.org", 1234, 5678);
114 private static final ServerName SERVERNAME_B =
115 ServerName.valueOf("example.org", 0, 5678);
116 private static final HRegionInfo REGIONINFO =
117 new HRegionInfo(TableName.valueOf("t"),
118 HConstants.EMPTY_START_ROW, HConstants.EMPTY_START_ROW);
119 private static int assignmentCount;
120 private static boolean enabling = false;
121
122
123 private Server server;
124 private ServerManager serverManager;
125 private ZooKeeperWatcher watcher;
126 private CoordinatedStateManager cp;
127 private MetaTableLocator mtl;
128 private LoadBalancer balancer;
129 private HMaster master;
130 private ClusterConnection connection;
131
132 @BeforeClass
133 public static void beforeClass() throws Exception {
134 HTU.getConfiguration().setBoolean("hbase.assignment.usezk", true);
135 HTU.startMiniZKCluster();
136 }
137
138 @AfterClass
139 public static void afterClass() throws IOException {
140 HTU.shutdownMiniZKCluster();
141 }
142
143 @Before
144 public void before() throws ZooKeeperConnectionException, IOException {
145
146
147
148
149
150
151 this.server = Mockito.mock(Server.class);
152 Mockito.when(server.getServerName()).thenReturn(ServerName.valueOf("master,1,1"));
153 Mockito.when(server.getConfiguration()).thenReturn(HTU.getConfiguration());
154 this.watcher =
155 new ZooKeeperWatcher(HTU.getConfiguration(), "mockedServer", this.server, true);
156 Mockito.when(server.getZooKeeper()).thenReturn(this.watcher);
157 Mockito.doThrow(new RuntimeException("Aborted")).
158 when(server).abort(Mockito.anyString(), (Throwable)Mockito.anyObject());
159
160 cp = new ZkCoordinatedStateManager();
161 cp.initialize(this.server);
162 cp.start();
163
164 mtl = Mockito.mock(MetaTableLocator.class);
165
166 Mockito.when(server.getCoordinatedStateManager()).thenReturn(cp);
167 Mockito.when(server.getMetaTableLocator()).thenReturn(mtl);
168
169
170 this.connection =
171 (ClusterConnection)HConnectionTestingUtility.getMockedConnection(HTU.getConfiguration());
172
173
174
175 Mockito.when(server.getConnection()).thenReturn(connection);
176
177
178
179 this.serverManager = Mockito.mock(ServerManager.class);
180 Mockito.when(this.serverManager.isServerOnline(SERVERNAME_A)).thenReturn(true);
181 Mockito.when(this.serverManager.isServerOnline(SERVERNAME_B)).thenReturn(true);
182 Mockito.when(this.serverManager.getDeadServers()).thenReturn(new DeadServer());
183 final Map<ServerName, ServerLoad> onlineServers = new HashMap<ServerName, ServerLoad>();
184 onlineServers.put(SERVERNAME_B, ServerLoad.EMPTY_SERVERLOAD);
185 onlineServers.put(SERVERNAME_A, ServerLoad.EMPTY_SERVERLOAD);
186 Mockito.when(this.serverManager.getOnlineServersList()).thenReturn(
187 new ArrayList<ServerName>(onlineServers.keySet()));
188 Mockito.when(this.serverManager.getOnlineServers()).thenReturn(onlineServers);
189
190 List<ServerName> avServers = new ArrayList<ServerName>();
191 avServers.addAll(onlineServers.keySet());
192 Mockito.when(this.serverManager.createDestinationServersList()).thenReturn(avServers);
193 Mockito.when(this.serverManager.createDestinationServersList(null)).thenReturn(avServers);
194
195 Mockito.when(this.serverManager.sendRegionClose(SERVERNAME_A, REGIONINFO, -1)).
196 thenReturn(true);
197 Mockito.when(this.serverManager.sendRegionClose(SERVERNAME_B, REGIONINFO, -1)).
198 thenReturn(true);
199
200 Mockito.when(this.serverManager.sendRegionOpen(SERVERNAME_A, REGIONINFO, -1, null)).
201 thenReturn(RegionOpeningState.OPENED);
202 Mockito.when(this.serverManager.sendRegionOpen(SERVERNAME_B, REGIONINFO, -1, null)).
203 thenReturn(RegionOpeningState.OPENED);
204 this.master = Mockito.mock(HMaster.class);
205
206 Mockito.when(this.master.getServerManager()).thenReturn(serverManager);
207 }
208
209 @After public void after() throws KeeperException, IOException {
210 if (this.watcher != null) {
211
212 ZKAssign.deleteAllNodes(this.watcher);
213 this.watcher.close();
214 this.cp.stop();
215 }
216 if (this.connection != null) this.connection.close();
217 }
218
219
220
221
222
223
224
225
226
227 @Test(timeout = 60000)
228 public void testBalanceOnMasterFailoverScenarioWithOpenedNode()
229 throws IOException, KeeperException, InterruptedException, ServiceException,
230 DeserializationException, CoordinatedStateException {
231 AssignmentManagerWithExtrasForTesting am =
232 setUpMockedAssignmentManager(this.server, this.serverManager);
233 try {
234 createRegionPlanAndBalance(am, SERVERNAME_A, SERVERNAME_B, REGIONINFO);
235 startFakeFailedOverMasterAssignmentManager(am, this.watcher);
236 while (!am.processRITInvoked) Thread.sleep(1);
237
238
239
240 am.addPlan(REGIONINFO.getEncodedName(), new RegionPlan(REGIONINFO, null, SERVERNAME_B));
241
242 Mocking.waitForRegionFailedToCloseAndSetToPendingClose(am, REGIONINFO);
243
244
245
246
247
248
249 int versionid =
250 ZKAssign.transitionNodeClosed(this.watcher, REGIONINFO, SERVERNAME_A, -1);
251 assertNotSame(versionid, -1);
252 Mocking.waitForRegionPendingOpenInRIT(am, REGIONINFO.getEncodedName());
253
254
255
256 versionid = ZKAssign.getVersion(this.watcher, REGIONINFO);
257 assertNotSame(-1, versionid);
258
259 versionid = ZKAssign.transitionNode(server.getZooKeeper(), REGIONINFO,
260 SERVERNAME_B, EventType.M_ZK_REGION_OFFLINE,
261 EventType.RS_ZK_REGION_OPENING, versionid);
262 assertNotSame(-1, versionid);
263
264 versionid = ZKAssign.transitionNodeOpened(this.watcher, REGIONINFO,
265 SERVERNAME_B, versionid);
266 assertNotSame(-1, versionid);
267 am.gate.set(false);
268
269 ZKAssign.blockUntilNoRIT(watcher);
270 } finally {
271 am.getExecutorService().shutdown();
272 am.shutdown();
273 }
274 }
275
276 @Test(timeout = 60000)
277 public void testBalanceOnMasterFailoverScenarioWithClosedNode()
278 throws IOException, KeeperException, InterruptedException, ServiceException,
279 DeserializationException, CoordinatedStateException {
280 AssignmentManagerWithExtrasForTesting am =
281 setUpMockedAssignmentManager(this.server, this.serverManager);
282 try {
283 createRegionPlanAndBalance(am, SERVERNAME_A, SERVERNAME_B, REGIONINFO);
284 startFakeFailedOverMasterAssignmentManager(am, this.watcher);
285 while (!am.processRITInvoked) Thread.sleep(1);
286
287
288
289 am.addPlan(REGIONINFO.getEncodedName(), new RegionPlan(REGIONINFO, null, SERVERNAME_B));
290
291 Mocking.waitForRegionFailedToCloseAndSetToPendingClose(am, REGIONINFO);
292
293
294
295
296
297
298 int versionid =
299 ZKAssign.transitionNodeClosed(this.watcher, REGIONINFO, SERVERNAME_A, -1);
300 assertNotSame(versionid, -1);
301 am.gate.set(false);
302 Mocking.waitForRegionPendingOpenInRIT(am, REGIONINFO.getEncodedName());
303
304
305
306 versionid = ZKAssign.getVersion(this.watcher, REGIONINFO);
307 assertNotSame(-1, versionid);
308
309 versionid = ZKAssign.transitionNode(server.getZooKeeper(), REGIONINFO,
310 SERVERNAME_B, EventType.M_ZK_REGION_OFFLINE,
311 EventType.RS_ZK_REGION_OPENING, versionid);
312 assertNotSame(-1, versionid);
313
314 versionid = ZKAssign.transitionNodeOpened(this.watcher, REGIONINFO,
315 SERVERNAME_B, versionid);
316 assertNotSame(-1, versionid);
317
318
319 ZKAssign.blockUntilNoRIT(watcher);
320 } finally {
321 am.getExecutorService().shutdown();
322 am.shutdown();
323 }
324 }
325
326 @Test(timeout = 60000)
327 public void testBalanceOnMasterFailoverScenarioWithOfflineNode()
328 throws IOException, KeeperException, InterruptedException, ServiceException,
329 DeserializationException, CoordinatedStateException {
330 AssignmentManagerWithExtrasForTesting am =
331 setUpMockedAssignmentManager(this.server, this.serverManager);
332 try {
333 createRegionPlanAndBalance(am, SERVERNAME_A, SERVERNAME_B, REGIONINFO);
334 startFakeFailedOverMasterAssignmentManager(am, this.watcher);
335 while (!am.processRITInvoked) Thread.sleep(1);
336
337
338
339 am.addPlan(REGIONINFO.getEncodedName(), new RegionPlan(REGIONINFO, null, SERVERNAME_B));
340
341 Mocking.waitForRegionFailedToCloseAndSetToPendingClose(am, REGIONINFO);
342
343
344
345
346
347
348 int versionid =
349 ZKAssign.transitionNodeClosed(this.watcher, REGIONINFO, SERVERNAME_A, -1);
350 assertNotSame(versionid, -1);
351 Mocking.waitForRegionPendingOpenInRIT(am, REGIONINFO.getEncodedName());
352
353 am.gate.set(false);
354
355
356 versionid = ZKAssign.getVersion(this.watcher, REGIONINFO);
357 assertNotSame(-1, versionid);
358
359 versionid = ZKAssign.transitionNode(server.getZooKeeper(), REGIONINFO,
360 SERVERNAME_B, EventType.M_ZK_REGION_OFFLINE,
361 EventType.RS_ZK_REGION_OPENING, versionid);
362 assertNotSame(-1, versionid);
363
364 versionid = ZKAssign.transitionNodeOpened(this.watcher, REGIONINFO,
365 SERVERNAME_B, versionid);
366 assertNotSame(-1, versionid);
367
368 ZKAssign.blockUntilNoRIT(watcher);
369 } finally {
370 am.getExecutorService().shutdown();
371 am.shutdown();
372 }
373 }
374
375 private void createRegionPlanAndBalance(
376 final AssignmentManager am, final ServerName from,
377 final ServerName to, final HRegionInfo hri) throws RegionException {
378
379
380 am.regionOnline(hri, from);
381
382
383 am.balance(new RegionPlan(hri, from, to));
384 }
385
386
387
388
389
390
391
392
393 @Test (timeout=180000)
394 public void testBalance() throws IOException, KeeperException, DeserializationException,
395 InterruptedException, CoordinatedStateException {
396
397
398 ExecutorService executor = startupMasterExecutor("testBalanceExecutor");
399
400
401 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(server
402 .getConfiguration());
403
404 AssignmentManager am = new AssignmentManager(this.server,
405 this.serverManager, balancer, executor, null, master.getTableLockManager());
406 am.failoverCleanupDone.set(true);
407 try {
408
409
410 this.watcher.registerListenerFirst(am);
411
412
413 am.regionOnline(REGIONINFO, SERVERNAME_A);
414
415 RegionPlan plan = new RegionPlan(REGIONINFO, SERVERNAME_A, SERVERNAME_B);
416 am.balance(plan);
417
418 RegionStates regionStates = am.getRegionStates();
419
420 assertTrue(regionStates.isRegionInTransition(REGIONINFO)
421 && regionStates.isRegionInState(REGIONINFO, State.FAILED_CLOSE));
422
423 regionStates.updateRegionState(REGIONINFO, State.PENDING_CLOSE);
424
425
426
427
428
429
430 int versionid =
431 ZKAssign.transitionNodeClosed(this.watcher, REGIONINFO, SERVERNAME_A, -1);
432 assertNotSame(versionid, -1);
433
434
435
436
437
438 Mocking.waitForRegionPendingOpenInRIT(am, REGIONINFO.getEncodedName());
439
440
441 versionid = ZKAssign.getVersion(this.watcher, REGIONINFO);
442 assertNotSame(-1, versionid);
443
444 versionid = ZKAssign.transitionNode(server.getZooKeeper(), REGIONINFO,
445 SERVERNAME_B, EventType.M_ZK_REGION_OFFLINE,
446 EventType.RS_ZK_REGION_OPENING, versionid);
447 assertNotSame(-1, versionid);
448
449 versionid =
450 ZKAssign.transitionNodeOpened(this.watcher, REGIONINFO, SERVERNAME_B, versionid);
451 assertNotSame(-1, versionid);
452
453 while(regionStates.isRegionInTransition(REGIONINFO)) Threads.sleep(1);
454 } finally {
455 executor.shutdown();
456 am.shutdown();
457
458 ZKAssign.deleteAllNodes(this.watcher);
459 }
460 }
461
462
463
464
465
466
467 @Test (timeout=180000)
468 public void testShutdownHandler()
469 throws KeeperException, IOException, CoordinatedStateException, ServiceException {
470
471
472 ExecutorService executor = startupMasterExecutor("testShutdownHandler");
473
474
475 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(
476 this.server, this.serverManager);
477 try {
478 processServerShutdownHandler(am, false);
479 } finally {
480 executor.shutdown();
481 am.shutdown();
482
483 ZKAssign.deleteAllNodes(this.watcher);
484 }
485 }
486
487
488
489
490
491
492
493
494
495
496 @Test (timeout=180000)
497 public void testSSHWhenDisableTableInProgress() throws KeeperException, IOException,
498 CoordinatedStateException, ServiceException {
499 testCaseWithPartiallyDisabledState(Table.State.DISABLING);
500 testCaseWithPartiallyDisabledState(Table.State.DISABLED);
501 }
502
503
504
505
506
507
508
509
510
511 @Test (timeout=180000)
512 public void testSSHWhenSplitRegionInProgress() throws KeeperException, IOException, Exception {
513
514 testCaseWithSplitRegionPartial(true);
515
516 testCaseWithSplitRegionPartial(false);
517 }
518
519 private void testCaseWithSplitRegionPartial(boolean regionSplitDone) throws KeeperException,
520 IOException, InterruptedException,
521 CoordinatedStateException, ServiceException {
522
523
524 ExecutorService executor = startupMasterExecutor("testSSHWhenSplitRegionInProgress");
525
526 ZKAssign.deleteAllNodes(this.watcher);
527
528
529 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(
530 this.server, this.serverManager);
531
532 am.regionOnline(REGIONINFO, SERVERNAME_A);
533
534 am.getRegionStates().updateRegionState(
535 REGIONINFO, State.SPLITTING, SERVERNAME_A);
536 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
537 Table.State.ENABLED);
538 RegionTransition data = RegionTransition.createRegionTransition(EventType.RS_ZK_REGION_SPLITTING,
539 REGIONINFO.getRegionName(), SERVERNAME_A);
540 String node = ZKAssign.getNodeName(this.watcher, REGIONINFO.getEncodedName());
541
542 ZKUtil.createAndWatch(this.watcher, node, data.toByteArray());
543
544 try {
545 processServerShutdownHandler(am, regionSplitDone);
546
547
548
549 if (regionSplitDone) {
550 assertFalse("Region state of region in SPLITTING should be removed from rit.",
551 am.getRegionStates().isRegionsInTransition());
552 } else {
553 while (!am.assignInvoked) {
554 Thread.sleep(1);
555 }
556 assertTrue("Assign should be invoked.", am.assignInvoked);
557 }
558 } finally {
559 REGIONINFO.setOffline(false);
560 REGIONINFO.setSplit(false);
561 executor.shutdown();
562 am.shutdown();
563
564 ZKAssign.deleteAllNodes(this.watcher);
565 }
566 }
567
568 private void testCaseWithPartiallyDisabledState(Table.State state) throws KeeperException,
569 IOException, CoordinatedStateException, ServiceException {
570
571
572 ExecutorService executor = startupMasterExecutor("testSSHWhenDisableTableInProgress");
573 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(server.getConfiguration());
574 ZKAssign.deleteAllNodes(this.watcher);
575
576
577 AssignmentManager am = new AssignmentManager(this.server,
578 this.serverManager, balancer, executor, null, master.getTableLockManager());
579
580 am.regionOnline(REGIONINFO, SERVERNAME_A);
581
582 am.getRegionStates().updateRegionState(REGIONINFO, State.PENDING_CLOSE);
583 if (state == Table.State.DISABLING) {
584 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
585 Table.State.DISABLING);
586 } else {
587 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
588 Table.State.DISABLED);
589 }
590 RegionTransition data = RegionTransition.createRegionTransition(EventType.M_ZK_REGION_CLOSING,
591 REGIONINFO.getRegionName(), SERVERNAME_A);
592
593
594
595 String node = ZKAssign.getNodeName(this.watcher, REGIONINFO.getEncodedName());
596
597 ZKUtil.createAndWatch(this.watcher, node, data.toByteArray());
598
599 try {
600 processServerShutdownHandler(am, false);
601
602
603 assertTrue("The znode should be deleted.", ZKUtil.checkExists(this.watcher, node) == -1);
604
605
606
607 if (state == Table.State.DISABLED) {
608 assertFalse("Region state of region in pending close should be removed from rit.",
609 am.getRegionStates().isRegionsInTransition());
610 }
611 } finally {
612 am.setEnabledTable(REGIONINFO.getTable());
613 executor.shutdown();
614 am.shutdown();
615
616 ZKAssign.deleteAllNodes(this.watcher);
617 }
618 }
619
620 private void processServerShutdownHandler(AssignmentManager am, boolean splitRegion)
621 throws IOException, ServiceException {
622
623
624 this.watcher.registerListenerFirst(am);
625
626
627
628 ClientProtos.ClientService.BlockingInterface implementation =
629 Mockito.mock(ClientProtos.ClientService.BlockingInterface.class);
630
631
632 Result r;
633 if (splitRegion) {
634 r = MetaMockingUtil.getMetaTableRowResultAsSplitRegion(REGIONINFO, SERVERNAME_A);
635 } else {
636 r = MetaMockingUtil.getMetaTableRowResult(REGIONINFO, SERVERNAME_A);
637 }
638
639 final ScanResponse.Builder builder = ScanResponse.newBuilder();
640 builder.setMoreResults(true);
641 builder.addCellsPerResult(r.size());
642 final List<CellScannable> cellScannables = new ArrayList<CellScannable>(1);
643 cellScannables.add(r);
644 Mockito.when(implementation.scan(
645 (RpcController)Mockito.any(), (ScanRequest)Mockito.any())).
646 thenAnswer(new Answer<ScanResponse>() {
647 @Override
648 public ScanResponse answer(InvocationOnMock invocation) throws Throwable {
649 PayloadCarryingRpcController controller = (PayloadCarryingRpcController) invocation
650 .getArguments()[0];
651 if (controller != null) {
652 controller.setCellScanner(CellUtil.createCellScanner(cellScannables));
653 }
654 return builder.build();
655 }
656 });
657
658
659 ClusterConnection connection =
660 HConnectionTestingUtility.getMockedConnectionAndDecorate(HTU.getConfiguration(),
661 null, implementation, SERVERNAME_B, REGIONINFO);
662
663
664
665
666
667 Mockito.when(connection.isManaged()).thenReturn(true);
668 try {
669
670
671 Mockito.when(this.server.getConnection()).thenReturn(connection);
672
673
674
675 DeadServer deadServers = new DeadServer();
676 deadServers.add(SERVERNAME_A);
677
678 MasterFileSystem fs = Mockito.mock(MasterFileSystem.class);
679 Mockito.doNothing().when(fs).setLogRecoveryMode();
680 Mockito.when(fs.getLogRecoveryMode()).thenReturn(RecoveryMode.LOG_REPLAY);
681 MasterServices services = Mockito.mock(MasterServices.class);
682 Mockito.when(services.getAssignmentManager()).thenReturn(am);
683 Mockito.when(services.getServerManager()).thenReturn(this.serverManager);
684 Mockito.when(services.getZooKeeper()).thenReturn(this.watcher);
685 Mockito.when(services.getMasterFileSystem()).thenReturn(fs);
686 Mockito.when(services.getConnection()).thenReturn(connection);
687 Configuration conf = server.getConfiguration();
688 Mockito.when(services.getConfiguration()).thenReturn(conf);
689 ServerShutdownHandler handler = new ServerShutdownHandler(this.server,
690 services, deadServers, SERVERNAME_A, false);
691 am.failoverCleanupDone.set(true);
692 handler.process();
693
694 } finally {
695 if (connection != null) connection.close();
696 }
697 }
698
699
700
701
702
703
704
705 private ExecutorService startupMasterExecutor(final String name) {
706
707 ExecutorService executor = new ExecutorService(name);
708 executor.startExecutorService(ExecutorType.MASTER_OPEN_REGION, 3);
709 executor.startExecutorService(ExecutorType.MASTER_CLOSE_REGION, 3);
710 executor.startExecutorService(ExecutorType.MASTER_SERVER_OPERATIONS, 3);
711 executor.startExecutorService(ExecutorType.MASTER_META_SERVER_OPERATIONS, 3);
712 return executor;
713 }
714
715 @Test (timeout=180000)
716 public void testUnassignWithSplitAtSameTime() throws KeeperException,
717 IOException, CoordinatedStateException {
718
719 final HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
720
721
722
723 Mockito.when(this.serverManager.sendRegionClose(SERVERNAME_A, hri, -1)).thenReturn(true);
724
725 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(server
726 .getConfiguration());
727
728 AssignmentManager am = new AssignmentManager(this.server,
729 this.serverManager, balancer, null, null, master.getTableLockManager());
730 try {
731
732 unassign(am, SERVERNAME_A, hri);
733
734 ZKAssign.deleteClosingNode(this.watcher, hri, SERVERNAME_A);
735
736
737
738 int version = createNodeSplitting(this.watcher, hri, SERVERNAME_A);
739
740
741
742
743 unassign(am, SERVERNAME_A, hri);
744
745 ZKAssign.transitionNode(this.watcher, hri, SERVERNAME_A,
746 EventType.RS_ZK_REGION_SPLITTING, EventType.RS_ZK_REGION_SPLITTING, version);
747 assertFalse(am.getRegionStates().isRegionInTransition(hri));
748 } finally {
749 am.shutdown();
750 }
751 }
752
753
754
755
756
757
758
759 @Test(timeout = 60000)
760 public void testProcessDeadServersAndRegionsInTransitionShouldNotFailWithNPE()
761 throws IOException, KeeperException, CoordinatedStateException,
762 InterruptedException, ServiceException {
763 final RecoverableZooKeeper recoverableZk = Mockito
764 .mock(RecoverableZooKeeper.class);
765 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(
766 this.server, this.serverManager);
767 Watcher zkw = new ZooKeeperWatcher(HBaseConfiguration.create(), "unittest",
768 null) {
769 @Override
770 public RecoverableZooKeeper getRecoverableZooKeeper() {
771 return recoverableZk;
772 }
773 };
774 ((ZooKeeperWatcher) zkw).registerListener(am);
775 Mockito.doThrow(new InterruptedException()).when(recoverableZk)
776 .getChildren("/hbase/region-in-transition", null);
777 am.setWatcher((ZooKeeperWatcher) zkw);
778 try {
779 am.processDeadServersAndRegionsInTransition(null);
780 fail("Expected to abort");
781 } catch (NullPointerException e) {
782 fail("Should not throw NPE");
783 } catch (RuntimeException e) {
784 assertEquals("Aborted", e.getLocalizedMessage());
785 } finally {
786 am.shutdown();
787 }
788 }
789
790
791
792
793 @Test(timeout = 60000)
794 public void testRegionPlanIsUpdatedWhenRegionFailsToOpen() throws IOException, KeeperException,
795 ServiceException, InterruptedException, CoordinatedStateException {
796 this.server.getConfiguration().setClass(
797 HConstants.HBASE_MASTER_LOADBALANCER_CLASS, MockedLoadBalancer.class,
798 LoadBalancer.class);
799 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(
800 this.server, this.serverManager);
801 try {
802
803
804
805 AtomicBoolean gate = new AtomicBoolean(false);
806 if (balancer instanceof MockedLoadBalancer) {
807 ((MockedLoadBalancer) balancer).setGateVariable(gate);
808 }
809 ZKAssign.createNodeOffline(this.watcher, REGIONINFO, SERVERNAME_A);
810 int v = ZKAssign.getVersion(this.watcher, REGIONINFO);
811 ZKAssign.transitionNode(this.watcher, REGIONINFO, SERVERNAME_A,
812 EventType.M_ZK_REGION_OFFLINE, EventType.RS_ZK_REGION_FAILED_OPEN, v);
813 String path = ZKAssign.getNodeName(this.watcher, REGIONINFO
814 .getEncodedName());
815 am.getRegionStates().updateRegionState(
816 REGIONINFO, State.OPENING, SERVERNAME_A);
817
818
819 am.regionPlans.put(REGIONINFO.getEncodedName(), new RegionPlan(
820 REGIONINFO, null, SERVERNAME_A));
821 RegionPlan regionPlan = am.regionPlans.get(REGIONINFO.getEncodedName());
822 List<ServerName> serverList = new ArrayList<ServerName>(2);
823 serverList.add(SERVERNAME_B);
824 Mockito.when(
825 this.serverManager.createDestinationServersList(SERVERNAME_A))
826 .thenReturn(serverList);
827 am.nodeDataChanged(path);
828
829
830 while (!gate.get()) {
831 Thread.sleep(10);
832 }
833
834
835
836 RegionPlan newRegionPlan = am.regionPlans
837 .get(REGIONINFO.getEncodedName());
838 while (newRegionPlan == null) {
839 Thread.sleep(10);
840 newRegionPlan = am.regionPlans.get(REGIONINFO.getEncodedName());
841 }
842
843
844
845 assertNotSame("Same region plan should not come", regionPlan,
846 newRegionPlan);
847 assertTrue("Destination servers should be different.", !(regionPlan
848 .getDestination().equals(newRegionPlan.getDestination())));
849
850 Mocking.waitForRegionPendingOpenInRIT(am, REGIONINFO.getEncodedName());
851 } finally {
852 this.server.getConfiguration().setClass(
853 HConstants.HBASE_MASTER_LOADBALANCER_CLASS, SimpleLoadBalancer.class,
854 LoadBalancer.class);
855 am.getExecutorService().shutdown();
856 am.shutdown();
857 }
858 }
859
860
861
862
863
864 public static class MockedLoadBalancer extends SimpleLoadBalancer {
865 private AtomicBoolean gate;
866
867 public void setGateVariable(AtomicBoolean gate) {
868 this.gate = gate;
869 }
870
871 @Override
872 public ServerName randomAssignment(HRegionInfo regionInfo, List<ServerName> servers) {
873 ServerName randomServerName = super.randomAssignment(regionInfo, servers);
874 this.gate.set(true);
875 return randomServerName;
876 }
877
878 @Override
879 public Map<ServerName, List<HRegionInfo>> retainAssignment(
880 Map<HRegionInfo, ServerName> regions, List<ServerName> servers) {
881 this.gate.set(true);
882 return super.retainAssignment(regions, servers);
883 }
884 }
885
886
887
888
889
890
891 @Test(timeout = 60000)
892 public void testRegionInOpeningStateOnDeadRSWhileMasterFailover() throws IOException,
893 KeeperException, ServiceException, CoordinatedStateException, InterruptedException {
894 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(
895 this.server, this.serverManager);
896 ZKAssign.createNodeOffline(this.watcher, REGIONINFO, SERVERNAME_A);
897 int version = ZKAssign.getVersion(this.watcher, REGIONINFO);
898 ZKAssign.transitionNode(this.watcher, REGIONINFO, SERVERNAME_A, EventType.M_ZK_REGION_OFFLINE,
899 EventType.RS_ZK_REGION_OPENING, version);
900 RegionTransition rt = RegionTransition.createRegionTransition(EventType.RS_ZK_REGION_OPENING,
901 REGIONINFO.getRegionName(), SERVERNAME_A, HConstants.EMPTY_BYTE_ARRAY);
902 version = ZKAssign.getVersion(this.watcher, REGIONINFO);
903 Mockito.when(this.serverManager.isServerOnline(SERVERNAME_A)).thenReturn(false);
904 am.getRegionStates().logSplit(SERVERNAME_A);
905 am.getRegionStates().createRegionState(REGIONINFO);
906 am.gate.set(false);
907
908 BaseCoordinatedStateManager cp = new ZkCoordinatedStateManager();
909 cp.initialize(server);
910 cp.start();
911
912 OpenRegionCoordination orc = cp.getOpenRegionCoordination();
913 ZkOpenRegionCoordination.ZkOpenRegionDetails zkOrd =
914 new ZkOpenRegionCoordination.ZkOpenRegionDetails();
915 zkOrd.setServerName(server.getServerName());
916 zkOrd.setVersion(version);
917
918 assertFalse(am.processRegionsInTransition(rt, REGIONINFO, orc, zkOrd));
919 am.getTableStateManager().setTableState(REGIONINFO.getTable(), Table.State.ENABLED);
920 processServerShutdownHandler(am, false);
921
922 while (!am.gate.get()) {
923 Thread.sleep(10);
924 }
925 assertTrue("The region should be assigned immediately.", null != am.regionPlans.get(REGIONINFO
926 .getEncodedName()));
927 am.shutdown();
928 }
929
930
931
932
933
934
935
936
937
938 @Test(timeout = 60000)
939 public void testDisablingTableRegionsAssignmentDuringCleanClusterStartup()
940 throws KeeperException, IOException, Exception {
941 this.server.getConfiguration().setClass(HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
942 MockedLoadBalancer.class, LoadBalancer.class);
943 Mockito.when(this.serverManager.getOnlineServers()).thenReturn(
944 new HashMap<ServerName, ServerLoad>(0));
945 List<ServerName> destServers = new ArrayList<ServerName>(1);
946 destServers.add(SERVERNAME_A);
947 Mockito.when(this.serverManager.createDestinationServersList()).thenReturn(destServers);
948
949 HTU.getConfiguration().setInt(HConstants.MASTER_PORT, 0);
950
951 CoordinatedStateManager csm = CoordinatedStateManagerFactory.getCoordinatedStateManager(
952 HTU.getConfiguration());
953 Server server = new HMaster(HTU.getConfiguration(), csm);
954 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(server,
955 this.serverManager);
956
957 Whitebox.setInternalState(server, "metaTableLocator", Mockito.mock(MetaTableLocator.class));
958
959
960
961 Whitebox.setInternalState(server, "clusterConnection", am.getConnection());
962
963 AtomicBoolean gate = new AtomicBoolean(false);
964 if (balancer instanceof MockedLoadBalancer) {
965 ((MockedLoadBalancer) balancer).setGateVariable(gate);
966 }
967 try{
968
969 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
970 Table.State.DISABLING);
971 am.joinCluster();
972
973 assertFalse(
974 "Assign should not be invoked for disabling table regions during clean cluster startup.",
975 gate.get());
976
977 assertTrue("Table should be disabled.",
978 am.getTableStateManager().isTableState(REGIONINFO.getTable(),
979 Table.State.DISABLED));
980 } finally {
981 this.server.getConfiguration().setClass(
982 HConstants.HBASE_MASTER_LOADBALANCER_CLASS, SimpleLoadBalancer.class,
983 LoadBalancer.class);
984 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
985 Table.State.ENABLED);
986 am.shutdown();
987 }
988 }
989
990
991
992
993
994
995
996
997 @Test (timeout=180000)
998 public void testMasterRestartWhenTableInEnabling() throws KeeperException, IOException, Exception {
999 enabling = true;
1000 List<ServerName> destServers = new ArrayList<ServerName>(1);
1001 destServers.add(SERVERNAME_A);
1002 Mockito.when(this.serverManager.createDestinationServersList()).thenReturn(destServers);
1003 Mockito.when(this.serverManager.isServerOnline(SERVERNAME_A)).thenReturn(true);
1004 HTU.getConfiguration().setInt(HConstants.MASTER_PORT, 0);
1005 CoordinatedStateManager csm = CoordinatedStateManagerFactory.getCoordinatedStateManager(
1006 HTU.getConfiguration());
1007 Server server = new HMaster(HTU.getConfiguration(), csm);
1008 Whitebox.setInternalState(server, "serverManager", this.serverManager);
1009 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(server,
1010 this.serverManager);
1011
1012 Whitebox.setInternalState(server, "metaTableLocator", Mockito.mock(MetaTableLocator.class));
1013
1014
1015
1016 Whitebox.setInternalState(server, "clusterConnection", am.getConnection());
1017
1018 try {
1019
1020 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
1021 Table.State.ENABLING);
1022 new EnableTableHandler(server, REGIONINFO.getTable(),
1023 am, new NullTableLockManager(), true).prepare()
1024 .process();
1025 assertEquals("Number of assignments should be 1.", 1, assignmentCount);
1026 assertTrue("Table should be enabled.",
1027 am.getTableStateManager().isTableState(REGIONINFO.getTable(),
1028 Table.State.ENABLED));
1029 } finally {
1030 enabling = false;
1031 assignmentCount = 0;
1032 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
1033 Table.State.ENABLED);
1034 am.shutdown();
1035 ZKAssign.deleteAllNodes(this.watcher);
1036 }
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046 @Test (timeout=180000)
1047 public void testMasterRestartShouldRemoveStaleZnodesOfUnknownTableAsForMeta()
1048 throws Exception {
1049 List<ServerName> destServers = new ArrayList<ServerName>(1);
1050 destServers.add(SERVERNAME_A);
1051 Mockito.when(this.serverManager.createDestinationServersList()).thenReturn(destServers);
1052 Mockito.when(this.serverManager.isServerOnline(SERVERNAME_A)).thenReturn(true);
1053 HTU.getConfiguration().setInt(HConstants.MASTER_PORT, 0);
1054 CoordinatedStateManager csm = CoordinatedStateManagerFactory.getCoordinatedStateManager(
1055 HTU.getConfiguration());
1056 Server server = new HMaster(HTU.getConfiguration(), csm);
1057 Whitebox.setInternalState(server, "serverManager", this.serverManager);
1058 AssignmentManagerWithExtrasForTesting am = setUpMockedAssignmentManager(server,
1059 this.serverManager);
1060
1061 Whitebox.setInternalState(server, "metaTableLocator", Mockito.mock(MetaTableLocator.class));
1062
1063
1064
1065 Whitebox.setInternalState(server, "clusterConnection", am.getConnection());
1066
1067 try {
1068 TableName tableName = TableName.valueOf("dummyTable");
1069
1070 am.getTableStateManager().setTableState(tableName,
1071 Table.State.ENABLING);
1072 am.joinCluster();
1073 assertFalse("Table should not be present in zookeeper.",
1074 am.getTableStateManager().isTablePresent(tableName));
1075 } finally {
1076 am.shutdown();
1077 }
1078 }
1079
1080
1081
1082
1083
1084 @Test (timeout=180000)
1085 public void testSSHTimesOutOpeningRegionTransition()
1086 throws KeeperException, IOException, CoordinatedStateException, ServiceException {
1087
1088 AssignmentManagerWithExtrasForTesting am =
1089 setUpMockedAssignmentManager(this.server, this.serverManager);
1090
1091 RegionState state = new RegionState(REGIONINFO,
1092 State.OPENING, System.currentTimeMillis(), SERVERNAME_A);
1093 am.getRegionStates().regionOnline(REGIONINFO, SERVERNAME_B);
1094 am.getRegionStates().regionsInTransition.put(REGIONINFO.getEncodedName(), state);
1095
1096 am.regionPlans.put(REGIONINFO.getEncodedName(),
1097 new RegionPlan(REGIONINFO, SERVERNAME_B, SERVERNAME_A));
1098 am.getTableStateManager().setTableState(REGIONINFO.getTable(),
1099 Table.State.ENABLED);
1100
1101 try {
1102 am.assignInvoked = false;
1103 processServerShutdownHandler(am, false);
1104 assertTrue(am.assignInvoked);
1105 } finally {
1106 am.getRegionStates().regionsInTransition.remove(REGIONINFO.getEncodedName());
1107 am.regionPlans.remove(REGIONINFO.getEncodedName());
1108 am.shutdown();
1109 }
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 @Test (timeout=180000)
1122 public void testClosingFailureDuringRecovery() throws Exception {
1123
1124 AssignmentManagerWithExtrasForTesting am =
1125 setUpMockedAssignmentManager(this.server, this.serverManager);
1126 ZKAssign.createNodeClosing(this.watcher, REGIONINFO, SERVERNAME_A);
1127 try {
1128 am.getRegionStates().createRegionState(REGIONINFO);
1129
1130 assertFalse( am.getRegionStates().isRegionsInTransition() );
1131
1132 am.processRegionInTransition(REGIONINFO.getEncodedName(), REGIONINFO);
1133
1134 assertTrue( am.getRegionStates().isRegionsInTransition() );
1135 } finally {
1136 am.shutdown();
1137 }
1138 }
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 private static int createNodeSplitting(final ZooKeeperWatcher zkw,
1157 final HRegionInfo region, final ServerName serverName)
1158 throws KeeperException, IOException {
1159 RegionTransition rt =
1160 RegionTransition.createRegionTransition(EventType.RS_ZK_REGION_SPLITTING,
1161 region.getRegionName(), serverName);
1162
1163 String node = ZKAssign.getNodeName(zkw, region.getEncodedName());
1164 if (!ZKUtil.createEphemeralNodeAndWatch(zkw, node, rt.toByteArray())) {
1165 throw new IOException("Failed create of ephemeral " + node);
1166 }
1167
1168
1169 return transitionNodeSplitting(zkw, region, serverName, -1);
1170 }
1171
1172
1173
1174 private static int transitionNodeSplitting(final ZooKeeperWatcher zkw,
1175 final HRegionInfo parent,
1176 final ServerName serverName, final int version)
1177 throws KeeperException, IOException {
1178 return ZKAssign.transitionNode(zkw, parent, serverName,
1179 EventType.RS_ZK_REGION_SPLITTING, EventType.RS_ZK_REGION_SPLITTING, version);
1180 }
1181
1182 private void unassign(final AssignmentManager am, final ServerName sn,
1183 final HRegionInfo hri) throws RegionException {
1184
1185 am.regionOnline(hri, sn);
1186
1187 am.unassign(hri);
1188 }
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199 private AssignmentManagerWithExtrasForTesting setUpMockedAssignmentManager(final Server server,
1200 final ServerManager manager) throws IOException, KeeperException,
1201 ServiceException, CoordinatedStateException {
1202
1203
1204
1205
1206
1207 ClientProtos.ClientService.BlockingInterface ri =
1208 Mockito.mock(ClientProtos.ClientService.BlockingInterface.class);
1209
1210 Result r = MetaMockingUtil.getMetaTableRowResult(REGIONINFO, SERVERNAME_A);
1211 final ScanResponse.Builder builder = ScanResponse.newBuilder();
1212 builder.setMoreResults(true);
1213 builder.addCellsPerResult(r.size());
1214 final List<CellScannable> rows = new ArrayList<CellScannable>(1);
1215 rows.add(r);
1216 Answer<ScanResponse> ans = new Answer<ClientProtos.ScanResponse>() {
1217 @Override
1218 public ScanResponse answer(InvocationOnMock invocation) throws Throwable {
1219 PayloadCarryingRpcController controller = (PayloadCarryingRpcController) invocation
1220 .getArguments()[0];
1221 if (controller != null) {
1222 controller.setCellScanner(CellUtil.createCellScanner(rows));
1223 }
1224 return builder.build();
1225 }
1226 };
1227 if (enabling) {
1228 Mockito.when(ri.scan((RpcController) Mockito.any(), (ScanRequest) Mockito.any()))
1229 .thenAnswer(ans).thenAnswer(ans).thenAnswer(ans).thenAnswer(ans).thenAnswer(ans)
1230 .thenReturn(ScanResponse.newBuilder().setMoreResults(false).build());
1231 } else {
1232 Mockito.when(ri.scan((RpcController) Mockito.any(), (ScanRequest) Mockito.any())).thenAnswer(
1233 ans);
1234 }
1235
1236 GetResponse.Builder getBuilder = GetResponse.newBuilder();
1237 getBuilder.setResult(ProtobufUtil.toResult(r));
1238 Mockito.when(ri.get((RpcController)Mockito.any(), (GetRequest) Mockito.any())).
1239 thenReturn(getBuilder.build());
1240
1241 ClusterConnection connection = (ClusterConnection)HConnectionTestingUtility.
1242 getMockedConnectionAndDecorate(HTU.getConfiguration(), null,
1243 ri, SERVERNAME_B, REGIONINFO);
1244
1245
1246
1247
1248
1249 Mockito.when(connection.isManaged()).thenReturn(true);
1250
1251
1252 ExecutorService executor = startupMasterExecutor("mockedAMExecutor");
1253 this.balancer = LoadBalancerFactory.getLoadBalancer(server.getConfiguration());
1254 AssignmentManagerWithExtrasForTesting am = new AssignmentManagerWithExtrasForTesting(
1255 server, connection, manager, this.balancer, executor, new NullTableLockManager());
1256 return am;
1257 }
1258
1259
1260
1261
1262 class AssignmentManagerWithExtrasForTesting extends AssignmentManager {
1263
1264 private final ExecutorService es;
1265 boolean processRITInvoked = false;
1266 boolean assignInvoked = false;
1267 AtomicBoolean gate = new AtomicBoolean(true);
1268 private ClusterConnection connection;
1269
1270 public AssignmentManagerWithExtrasForTesting(
1271 final Server master, ClusterConnection connection, final ServerManager serverManager,
1272 final LoadBalancer balancer,
1273 final ExecutorService service, final TableLockManager tableLockManager)
1274 throws KeeperException, IOException, CoordinatedStateException {
1275 super(master, serverManager, balancer, service, null, tableLockManager);
1276 this.es = service;
1277 this.connection = connection;
1278 }
1279
1280 @Override
1281 boolean processRegionInTransition(String encodedRegionName,
1282 HRegionInfo regionInfo) throws KeeperException, IOException {
1283 this.processRITInvoked = true;
1284 return super.processRegionInTransition(encodedRegionName, regionInfo);
1285 }
1286
1287 @Override
1288 public void assign(HRegionInfo region, boolean setOfflineInZK, boolean forceNewPlan) {
1289 if (enabling) {
1290 assignmentCount++;
1291 this.regionOnline(region, SERVERNAME_A);
1292 } else {
1293 super.assign(region, setOfflineInZK, forceNewPlan);
1294 this.gate.set(true);
1295 }
1296 }
1297
1298 @Override
1299 boolean assign(ServerName destination, List<HRegionInfo> regions)
1300 throws InterruptedException {
1301 if (enabling) {
1302 for (HRegionInfo region : regions) {
1303 assignmentCount++;
1304 this.regionOnline(region, SERVERNAME_A);
1305 }
1306 return true;
1307 }
1308 return super.assign(destination, regions);
1309 }
1310
1311 @Override
1312 public void assign(List<HRegionInfo> regions)
1313 throws IOException, InterruptedException {
1314 assignInvoked = (regions != null && regions.size() > 0);
1315 super.assign(regions);
1316 this.gate.set(true);
1317 }
1318
1319
1320 void setWatcher(ZooKeeperWatcher watcher) {
1321 this.watcher = watcher;
1322 }
1323
1324
1325
1326
1327 ExecutorService getExecutorService() {
1328 return this.es;
1329 }
1330
1331
1332
1333
1334 ClusterConnection getConnection() {
1335 return this.connection;
1336 }
1337
1338 @Override
1339 public void shutdown() {
1340 super.shutdown();
1341 if (this.connection != null)
1342 try {
1343 this.connection.close();
1344 } catch (IOException e) {
1345 fail("Failed to close connection");
1346 }
1347 }
1348 }
1349
1350
1351
1352
1353
1354
1355
1356 private void startFakeFailedOverMasterAssignmentManager(final AssignmentManager am,
1357 final ZooKeeperWatcher watcher) {
1358
1359
1360 watcher.registerListenerFirst(am);
1361 Thread t = new Thread("RunAmJoinCluster") {
1362 @Override
1363 public void run() {
1364
1365
1366
1367
1368
1369 am.getRegionStates().regionsInTransition.clear();
1370 am.regionPlans.clear();
1371 try {
1372 am.joinCluster();
1373 } catch (IOException e) {
1374 throw new RuntimeException(e);
1375 } catch (KeeperException e) {
1376 throw new RuntimeException(e);
1377 } catch (InterruptedException e) {
1378 throw new RuntimeException(e);
1379 } catch (CoordinatedStateException e) {
1380 throw new RuntimeException(e);
1381 }
1382 }
1383 };
1384 t.start();
1385 while (!t.isAlive()) Threads.sleep(1);
1386 }
1387
1388 @Test (timeout=180000)
1389 public void testForceAssignMergingRegion() throws Exception {
1390
1391 final HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
1392
1393 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(
1394 server.getConfiguration());
1395
1396 AssignmentManager am = new AssignmentManager(this.server,
1397 this.serverManager, balancer, null, null, master.getTableLockManager());
1398 RegionStates regionStates = am.getRegionStates();
1399 try {
1400
1401 regionStates.updateRegionState(hri, RegionState.State.MERGING);
1402
1403 am.assign(hri, true, true);
1404 assertEquals("The region should be still in merging state",
1405 RegionState.State.MERGING, regionStates.getRegionState(hri).getState());
1406 } finally {
1407 am.shutdown();
1408 }
1409 }
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421 @Test (timeout=180000)
1422 public void testAssignmentEventIgnoredIfNotExpected() throws KeeperException, IOException,
1423 CoordinatedStateException {
1424
1425 final HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
1426 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(
1427 server.getConfiguration());
1428 final AtomicBoolean zkEventProcessed = new AtomicBoolean(false);
1429
1430 AssignmentManager am = new AssignmentManager(this.server,
1431 this.serverManager, balancer, null, null, master.getTableLockManager()) {
1432
1433 @Override
1434 void handleRegion(final RegionTransition rt, OpenRegionCoordination coordination,
1435 OpenRegionCoordination.OpenRegionDetails ord) {
1436 super.handleRegion(rt, coordination, ord);
1437 if (rt != null && Bytes.equals(hri.getRegionName(),
1438 rt.getRegionName()) && rt.getEventType() == EventType.RS_ZK_REGION_OPENING) {
1439 zkEventProcessed.set(true);
1440 }
1441 }
1442 };
1443 try {
1444
1445 am.getRegionStates().regionOffline(hri);
1446 zkEventProcessed.set(false);
1447 this.watcher.registerListenerFirst(am);
1448 assertFalse("The region should not be in transition",
1449 am.getRegionStates().isRegionInTransition(hri));
1450 ZKAssign.createNodeOffline(this.watcher, hri, SERVERNAME_A);
1451
1452 ZKAssign.transitionNodeOpening(this.watcher, hri, SERVERNAME_A);
1453 long startTime = EnvironmentEdgeManager.currentTime();
1454 while (!zkEventProcessed.get()) {
1455 assertTrue("Timed out in waiting for ZK event to be processed",
1456 EnvironmentEdgeManager.currentTime() - startTime < 30000);
1457 Threads.sleepWithoutInterrupt(100);
1458 }
1459 assertFalse(am.getRegionStates().isRegionInTransition(hri));
1460 } finally {
1461 am.shutdown();
1462 }
1463 }
1464
1465
1466
1467
1468
1469
1470 @Test (timeout=180000)
1471 public void testBalanceRegionOfDeletedTable() throws Exception {
1472 AssignmentManager am = new AssignmentManager(this.server, this.serverManager,
1473 balancer, null, null, master.getTableLockManager());
1474 RegionStates regionStates = am.getRegionStates();
1475 HRegionInfo hri = REGIONINFO;
1476 regionStates.createRegionState(hri);
1477 assertFalse(regionStates.isRegionInTransition(hri));
1478 RegionPlan plan = new RegionPlan(hri, SERVERNAME_A, SERVERNAME_B);
1479
1480 regionStates.tableDeleted(hri.getTable());
1481 am.balance(plan);
1482 assertFalse("The region should not in transition",
1483 regionStates.isRegionInTransition(hri));
1484 am.shutdown();
1485 }
1486
1487
1488
1489
1490
1491 @SuppressWarnings("unchecked")
1492 @Test (timeout=180000)
1493 public void testOpenCloseRegionRPCIntendedForPreviousServer() throws Exception {
1494 Mockito.when(this.serverManager.sendRegionOpen(Mockito.eq(SERVERNAME_B), Mockito.eq(REGIONINFO),
1495 Mockito.anyInt(), (List<ServerName>)Mockito.any()))
1496 .thenThrow(new DoNotRetryIOException());
1497 this.server.getConfiguration().setInt("hbase.assignment.maximum.attempts", 100);
1498
1499 HRegionInfo hri = REGIONINFO;
1500 LoadBalancer balancer = LoadBalancerFactory.getLoadBalancer(
1501 server.getConfiguration());
1502
1503 AssignmentManager am = new AssignmentManager(this.server,
1504 this.serverManager, balancer, null, null, master.getTableLockManager());
1505 RegionStates regionStates = am.getRegionStates();
1506 try {
1507 am.regionPlans.put(REGIONINFO.getEncodedName(),
1508 new RegionPlan(REGIONINFO, null, SERVERNAME_B));
1509
1510
1511 am.assign(hri, true, false);
1512 } finally {
1513 assertEquals(SERVERNAME_A, regionStates.getRegionState(REGIONINFO).getServerName());
1514 am.shutdown();
1515 }
1516 }
1517 }