1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import java.io.IOException;
22 import java.security.PrivilegedAction;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.fs.FileSystem;
32 import org.apache.hadoop.hbase.master.HMaster;
33 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
34 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
35 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.MasterService;
36 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
37 import org.apache.hadoop.hbase.regionserver.HRegion;
38 import org.apache.hadoop.hbase.regionserver.HRegionServer;
39 import org.apache.hadoop.hbase.regionserver.Region;
40 import org.apache.hadoop.hbase.security.User;
41 import org.apache.hadoop.hbase.test.MetricsAssertHelper;
42 import org.apache.hadoop.hbase.util.JVMClusterUtil;
43 import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
44 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
45 import org.apache.hadoop.hbase.util.Threads;
46
47
48
49
50
51
52
53 @InterfaceAudience.Public
54 @InterfaceStability.Evolving
55 public class MiniHBaseCluster extends HBaseCluster {
56 private static final Log LOG = LogFactory.getLog(MiniHBaseCluster.class.getName());
57 public LocalHBaseCluster hbaseCluster;
58 private static int index;
59
60
61
62
63
64
65
66 public MiniHBaseCluster(Configuration conf, int numRegionServers)
67 throws IOException, InterruptedException {
68 this(conf, 1, numRegionServers);
69 }
70
71
72
73
74
75
76
77
78 public MiniHBaseCluster(Configuration conf, int numMasters,
79 int numRegionServers)
80 throws IOException, InterruptedException {
81 this(conf, numMasters, numRegionServers, null, null);
82 }
83
84 public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers,
85 Class<? extends HMaster> masterClass,
86 Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
87 throws IOException, InterruptedException {
88 super(conf);
89 conf.set(HConstants.MASTER_PORT, "0");
90 if (conf.getInt(HConstants.MASTER_INFO_PORT, 0) != -1) {
91 conf.set(HConstants.MASTER_INFO_PORT, "0");
92 }
93
94
95 CompatibilityFactory.getInstance(MetricsAssertHelper.class).init();
96
97 init(numMasters, numRegionServers, masterClass, regionserverClass);
98 this.initialClusterStatus = getClusterStatus();
99 }
100
101 public Configuration getConfiguration() {
102 return this.conf;
103 }
104
105
106
107
108
109
110
111 public static class MiniHBaseClusterRegionServer extends HRegionServer {
112 private Thread shutdownThread = null;
113 private User user = null;
114 public static boolean TEST_SKIP_CLOSE = false;
115
116 public MiniHBaseClusterRegionServer(Configuration conf, CoordinatedStateManager cp)
117 throws IOException, InterruptedException {
118 super(conf, cp);
119 this.user = User.getCurrent();
120 }
121
122
123
124
125
126
127
128
129
130 @Override
131 protected void handleReportForDutyResponse(
132 final RegionServerStartupResponse c) throws IOException {
133 super.handleReportForDutyResponse(c);
134
135 this.shutdownThread = new SingleFileSystemShutdownThread(getFileSystem());
136 }
137
138 @Override
139 public void run() {
140 try {
141 this.user.runAs(new PrivilegedAction<Object>(){
142 public Object run() {
143 runRegionServer();
144 return null;
145 }
146 });
147 } catch (Throwable t) {
148 LOG.error("Exception in run", t);
149 } finally {
150
151 if (this.shutdownThread != null) {
152 this.shutdownThread.start();
153 Threads.shutdown(this.shutdownThread, 30000);
154 }
155 }
156 }
157
158 private void runRegionServer() {
159 super.run();
160 }
161
162 @Override
163 public void kill() {
164 super.kill();
165 }
166
167 public void abort(final String reason, final Throwable cause) {
168 this.user.runAs(new PrivilegedAction<Object>() {
169 public Object run() {
170 abortRegionServer(reason, cause);
171 return null;
172 }
173 });
174 }
175
176 private void abortRegionServer(String reason, Throwable cause) {
177 super.abort(reason, cause);
178 }
179 }
180
181
182
183
184
185 static class SingleFileSystemShutdownThread extends Thread {
186 private final FileSystem fs;
187 SingleFileSystemShutdownThread(final FileSystem fs) {
188 super("Shutdown of " + fs);
189 this.fs = fs;
190 }
191 @Override
192 public void run() {
193 try {
194 LOG.info("Hook closing fs=" + this.fs);
195 this.fs.close();
196 } catch (NullPointerException npe) {
197 LOG.debug("Need to fix these: " + npe.toString());
198 } catch (IOException e) {
199 LOG.warn("Running hook", e);
200 }
201 }
202 }
203
204 private void init(final int nMasterNodes, final int nRegionNodes,
205 Class<? extends HMaster> masterClass,
206 Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
207 throws IOException, InterruptedException {
208 try {
209 if (masterClass == null){
210 masterClass = HMaster.class;
211 }
212 if (regionserverClass == null){
213 regionserverClass = MiniHBaseCluster.MiniHBaseClusterRegionServer.class;
214 }
215
216
217 hbaseCluster = new LocalHBaseCluster(conf, nMasterNodes, 0,
218 masterClass, regionserverClass);
219
220
221 for (int i=0; i<nRegionNodes; i++) {
222 Configuration rsConf = HBaseConfiguration.create(conf);
223 User user = HBaseTestingUtility.getDifferentUser(rsConf,
224 ".hfs."+index++);
225 hbaseCluster.addRegionServer(rsConf, i, user);
226 }
227
228 hbaseCluster.startup();
229 } catch (IOException e) {
230 shutdown();
231 throw e;
232 } catch (Throwable t) {
233 LOG.error("Error starting cluster", t);
234 shutdown();
235 throw new IOException("Shutting down", t);
236 }
237 }
238
239 @Override
240 public void startRegionServer(String hostname, int port) throws IOException {
241 this.startRegionServer();
242 }
243
244 @Override
245 public void killRegionServer(ServerName serverName) throws IOException {
246 HRegionServer server = getRegionServer(getRegionServerIndex(serverName));
247 if (server instanceof MiniHBaseClusterRegionServer) {
248 LOG.info("Killing " + server.toString());
249 ((MiniHBaseClusterRegionServer) server).kill();
250 } else {
251 abortRegionServer(getRegionServerIndex(serverName));
252 }
253 }
254
255 @Override
256 public void stopRegionServer(ServerName serverName) throws IOException {
257 stopRegionServer(getRegionServerIndex(serverName));
258 }
259
260 @Override
261 public void waitForRegionServerToStop(ServerName serverName, long timeout) throws IOException {
262
263 waitOnRegionServer(getRegionServerIndex(serverName));
264 }
265
266 @Override
267 public void startZkNode(String hostname, int port) throws IOException {
268 LOG.warn("Starting zookeeper nodes on mini cluster is not supported");
269 }
270
271 @Override
272 public void killZkNode(ServerName serverName) throws IOException {
273 LOG.warn("Aborting zookeeper nodes on mini cluster is not supported");
274 }
275
276 @Override
277 public void stopZkNode(ServerName serverName) throws IOException {
278 LOG.warn("Stopping zookeeper nodes on mini cluster is not supported");
279 }
280
281 @Override
282 public void waitForZkNodeToStart(ServerName serverName, long timeout) throws IOException {
283 LOG.warn("Waiting for zookeeper nodes to start on mini cluster is not supported");
284 }
285
286 @Override
287 public void waitForZkNodeToStop(ServerName serverName, long timeout) throws IOException {
288 LOG.warn("Waiting for zookeeper nodes to stop on mini cluster is not supported");
289 }
290
291 @Override
292 public void startDataNode(ServerName serverName) throws IOException {
293 LOG.warn("Starting datanodes on mini cluster is not supported");
294 }
295
296 @Override
297 public void killDataNode(ServerName serverName) throws IOException {
298 LOG.warn("Aborting datanodes on mini cluster is not supported");
299 }
300
301 @Override
302 public void stopDataNode(ServerName serverName) throws IOException {
303 LOG.warn("Stopping datanodes on mini cluster is not supported");
304 }
305
306 @Override
307 public void waitForDataNodeToStart(ServerName serverName, long timeout) throws IOException {
308 LOG.warn("Waiting for datanodes to start on mini cluster is not supported");
309 }
310
311 @Override
312 public void waitForDataNodeToStop(ServerName serverName, long timeout) throws IOException {
313 LOG.warn("Waiting for datanodes to stop on mini cluster is not supported");
314 }
315
316 @Override
317 public void startMaster(String hostname, int port) throws IOException {
318 this.startMaster();
319 }
320
321 @Override
322 public void killMaster(ServerName serverName) throws IOException {
323 abortMaster(getMasterIndex(serverName));
324 }
325
326 @Override
327 public void stopMaster(ServerName serverName) throws IOException {
328 stopMaster(getMasterIndex(serverName));
329 }
330
331 @Override
332 public void waitForMasterToStop(ServerName serverName, long timeout) throws IOException {
333
334 waitOnMaster(getMasterIndex(serverName));
335 }
336
337
338
339
340
341
342
343 public JVMClusterUtil.RegionServerThread startRegionServer()
344 throws IOException {
345 final Configuration newConf = HBaseConfiguration.create(conf);
346 User rsUser =
347 HBaseTestingUtility.getDifferentUser(newConf, ".hfs."+index++);
348 JVMClusterUtil.RegionServerThread t = null;
349 try {
350 t = hbaseCluster.addRegionServer(
351 newConf, hbaseCluster.getRegionServers().size(), rsUser);
352 t.start();
353 t.waitForServerOnline();
354 } catch (InterruptedException ie) {
355 throw new IOException("Interrupted adding regionserver to cluster", ie);
356 }
357 return t;
358 }
359
360
361
362
363
364 public String abortRegionServer(int serverNumber) {
365 HRegionServer server = getRegionServer(serverNumber);
366 LOG.info("Aborting " + server.toString());
367 server.abort("Aborting for tests", new Exception("Trace info"));
368 return server.toString();
369 }
370
371
372
373
374
375
376
377 public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber) {
378 return stopRegionServer(serverNumber, true);
379 }
380
381
382
383
384
385
386
387
388
389
390
391 public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber,
392 final boolean shutdownFS) {
393 JVMClusterUtil.RegionServerThread server =
394 hbaseCluster.getRegionServers().get(serverNumber);
395 LOG.info("Stopping " + server.toString());
396 server.getRegionServer().stop("Stopping rs " + serverNumber);
397 return server;
398 }
399
400
401
402
403
404
405
406 public String waitOnRegionServer(final int serverNumber) {
407 return this.hbaseCluster.waitOnRegionServer(serverNumber);
408 }
409
410
411
412
413
414
415
416
417 public JVMClusterUtil.MasterThread startMaster() throws IOException {
418 Configuration c = HBaseConfiguration.create(conf);
419 User user =
420 HBaseTestingUtility.getDifferentUser(c, ".hfs."+index++);
421
422 JVMClusterUtil.MasterThread t = null;
423 try {
424 t = hbaseCluster.addMaster(c, hbaseCluster.getMasters().size(), user);
425 t.start();
426 } catch (InterruptedException ie) {
427 throw new IOException("Interrupted adding master to cluster", ie);
428 }
429 return t;
430 }
431
432
433
434
435
436 public MasterService.BlockingInterface getMasterAdminService() {
437 return this.hbaseCluster.getActiveMaster().getMasterRpcServices();
438 }
439
440
441
442
443
444 public HMaster getMaster() {
445 return this.hbaseCluster.getActiveMaster();
446 }
447
448
449
450
451
452 public MasterThread getMasterThread() {
453 for (MasterThread mt: hbaseCluster.getLiveMasters()) {
454 if (mt.getMaster().isActiveMaster()) {
455 return mt;
456 }
457 }
458 return null;
459 }
460
461
462
463
464
465 public HMaster getMaster(final int serverNumber) {
466 return this.hbaseCluster.getMaster(serverNumber);
467 }
468
469
470
471
472
473 public String abortMaster(int serverNumber) {
474 HMaster server = getMaster(serverNumber);
475 LOG.info("Aborting " + server.toString());
476 server.abort("Aborting for tests", new Exception("Trace info"));
477 return server.toString();
478 }
479
480
481
482
483
484
485
486 public JVMClusterUtil.MasterThread stopMaster(int serverNumber) {
487 return stopMaster(serverNumber, true);
488 }
489
490
491
492
493
494
495
496
497
498
499
500 public JVMClusterUtil.MasterThread stopMaster(int serverNumber,
501 final boolean shutdownFS) {
502 JVMClusterUtil.MasterThread server =
503 hbaseCluster.getMasters().get(serverNumber);
504 LOG.info("Stopping " + server.toString());
505 server.getMaster().stop("Stopping master " + serverNumber);
506 return server;
507 }
508
509
510
511
512
513
514
515 public String waitOnMaster(final int serverNumber) {
516 return this.hbaseCluster.waitOnMaster(serverNumber);
517 }
518
519
520
521
522
523
524
525
526
527 public boolean waitForActiveAndReadyMaster(long timeout) throws IOException {
528 List<JVMClusterUtil.MasterThread> mts;
529 long start = System.currentTimeMillis();
530 while (!(mts = getMasterThreads()).isEmpty()
531 && (System.currentTimeMillis() - start) < timeout) {
532 for (JVMClusterUtil.MasterThread mt : mts) {
533 if (mt.getMaster().isActiveMaster() && mt.getMaster().isInitialized()) {
534 return true;
535 }
536 }
537
538 Threads.sleep(100);
539 }
540 return false;
541 }
542
543
544
545
546 public List<JVMClusterUtil.MasterThread> getMasterThreads() {
547 return this.hbaseCluster.getMasters();
548 }
549
550
551
552
553 public List<JVMClusterUtil.MasterThread> getLiveMasterThreads() {
554 return this.hbaseCluster.getLiveMasters();
555 }
556
557
558
559
560 public void join() {
561 this.hbaseCluster.join();
562 }
563
564
565
566
567
568 @SuppressWarnings("deprecation")
569 public void shutdown() throws IOException {
570 if (this.hbaseCluster != null) {
571 this.hbaseCluster.shutdown();
572 }
573 }
574
575 @Override
576 public void close() throws IOException {
577 }
578
579 @Override
580 public ClusterStatus getClusterStatus() throws IOException {
581 HMaster master = getMaster();
582 return master == null ? null : master.getClusterStatus();
583 }
584
585
586
587
588
589 public void flushcache() throws IOException {
590 for (JVMClusterUtil.RegionServerThread t:
591 this.hbaseCluster.getRegionServers()) {
592 for(Region r: t.getRegionServer().getOnlineRegionsLocalContext()) {
593 r.flush(true);
594 }
595 }
596 }
597
598
599
600
601
602 public void flushcache(TableName tableName) throws IOException {
603 for (JVMClusterUtil.RegionServerThread t:
604 this.hbaseCluster.getRegionServers()) {
605 for(Region r: t.getRegionServer().getOnlineRegionsLocalContext()) {
606 if(r.getTableDesc().getTableName().equals(tableName)) {
607 r.flush(true);
608 }
609 }
610 }
611 }
612
613
614
615
616
617 public void compact(boolean major) throws IOException {
618 for (JVMClusterUtil.RegionServerThread t:
619 this.hbaseCluster.getRegionServers()) {
620 for(Region r: t.getRegionServer().getOnlineRegionsLocalContext()) {
621 r.compact(major);
622 }
623 }
624 }
625
626
627
628
629
630 public void compact(TableName tableName, boolean major) throws IOException {
631 for (JVMClusterUtil.RegionServerThread t:
632 this.hbaseCluster.getRegionServers()) {
633 for(Region r: t.getRegionServer().getOnlineRegionsLocalContext()) {
634 if(r.getTableDesc().getTableName().equals(tableName)) {
635 r.compact(major);
636 }
637 }
638 }
639 }
640
641
642
643
644 public List<JVMClusterUtil.RegionServerThread> getRegionServerThreads() {
645 return this.hbaseCluster.getRegionServers();
646 }
647
648
649
650
651 public List<JVMClusterUtil.RegionServerThread> getLiveRegionServerThreads() {
652 return this.hbaseCluster.getLiveRegionServers();
653 }
654
655
656
657
658
659
660 public HRegionServer getRegionServer(int serverNumber) {
661 return hbaseCluster.getRegionServer(serverNumber);
662 }
663
664 public List<HRegion> getRegions(byte[] tableName) {
665 return getRegions(TableName.valueOf(tableName));
666 }
667
668 public List<HRegion> getRegions(TableName tableName) {
669 List<HRegion> ret = new ArrayList<HRegion>();
670 for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) {
671 HRegionServer hrs = rst.getRegionServer();
672 for (Region region : hrs.getOnlineRegionsLocalContext()) {
673 if (region.getTableDesc().getTableName().equals(tableName)) {
674 ret.add((HRegion)region);
675 }
676 }
677 }
678 return ret;
679 }
680
681
682
683
684
685 public int getServerWithMeta() {
686 return getServerWith(HRegionInfo.FIRST_META_REGIONINFO.getRegionName());
687 }
688
689
690
691
692
693
694
695 public int getServerWith(byte[] regionName) {
696 int index = -1;
697 int count = 0;
698 for (JVMClusterUtil.RegionServerThread rst: getRegionServerThreads()) {
699 HRegionServer hrs = rst.getRegionServer();
700 if (!hrs.isStopped()) {
701 Region region = hrs.getOnlineRegion(regionName);
702 if (region != null) {
703 index = count;
704 break;
705 }
706 }
707 count++;
708 }
709 return index;
710 }
711
712 @Override
713 public ServerName getServerHoldingRegion(final TableName tn, byte[] regionName)
714 throws IOException {
715
716
717
718
719 HMaster master = getMaster();
720 Region region = master.getOnlineRegion(regionName);
721 if (region != null) {
722 return master.getServerName();
723 }
724 int index = getServerWith(regionName);
725 if (index < 0) {
726 return null;
727 }
728 return getRegionServer(index).getServerName();
729 }
730
731
732
733
734
735
736
737 public long countServedRegions() {
738 long count = 0;
739 for (JVMClusterUtil.RegionServerThread rst : getLiveRegionServerThreads()) {
740 count += rst.getRegionServer().getNumberOfOnlineRegions();
741 }
742 for (JVMClusterUtil.MasterThread mt : getLiveMasterThreads()) {
743 count += mt.getMaster().getNumberOfOnlineRegions();
744 }
745 return count;
746 }
747
748
749
750
751
752 public void killAll() {
753 for (RegionServerThread rst : getRegionServerThreads()) {
754 rst.getRegionServer().abort("killAll");
755 }
756 for (MasterThread masterThread : getMasterThreads()) {
757 masterThread.getMaster().abort("killAll", new Throwable());
758 }
759 }
760
761 @Override
762 public void waitUntilShutDown() {
763 this.hbaseCluster.join();
764 }
765
766 public List<HRegion> findRegionsForTable(TableName tableName) {
767 ArrayList<HRegion> ret = new ArrayList<HRegion>();
768 for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) {
769 HRegionServer hrs = rst.getRegionServer();
770 for (Region region : hrs.getOnlineRegions(tableName)) {
771 if (region.getTableDesc().getTableName().equals(tableName)) {
772 ret.add((HRegion)region);
773 }
774 }
775 }
776 return ret;
777 }
778
779
780 protected int getRegionServerIndex(ServerName serverName) {
781
782 List<RegionServerThread> servers = getRegionServerThreads();
783 for (int i=0; i < servers.size(); i++) {
784 if (servers.get(i).getRegionServer().getServerName().equals(serverName)) {
785 return i;
786 }
787 }
788 return -1;
789 }
790
791 protected int getMasterIndex(ServerName serverName) {
792 List<MasterThread> masters = getMasterThreads();
793 for (int i = 0; i < masters.size(); i++) {
794 if (masters.get(i).getMaster().getServerName().equals(serverName)) {
795 return i;
796 }
797 }
798 return -1;
799 }
800
801 @Override
802 public AdminService.BlockingInterface getAdminProtocol(ServerName serverName) throws IOException {
803 return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices();
804 }
805
806 @Override
807 public ClientService.BlockingInterface getClientProtocol(ServerName serverName)
808 throws IOException {
809 return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices();
810 }
811 }