001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase; 020 021import java.io.IOException; 022import java.security.PrivilegedAction; 023import java.util.ArrayList; 024import java.util.HashSet; 025import java.util.List; 026import java.util.Set; 027 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.fs.FileSystem; 030import org.apache.hadoop.hbase.master.HMaster; 031import org.apache.hadoop.hbase.regionserver.HRegion; 032import org.apache.hadoop.hbase.regionserver.HRegion.FlushResult; 033import org.apache.hadoop.hbase.regionserver.HRegionServer; 034import org.apache.hadoop.hbase.regionserver.Region; 035import org.apache.hadoop.hbase.security.User; 036import org.apache.hadoop.hbase.test.MetricsAssertHelper; 037import org.apache.hadoop.hbase.util.JVMClusterUtil; 038import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; 039import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread; 040import org.apache.hadoop.hbase.util.Threads; 041import org.apache.yetus.audience.InterfaceAudience; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MasterService; 048import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse; 049 050/** 051 * This class creates a single process HBase cluster. 052 * each server. The master uses the 'default' FileSystem. The RegionServers, 053 * if we are running on DistributedFilesystem, create a FileSystem instance 054 * each and will close down their instance on the way out. 055 */ 056@InterfaceAudience.Public 057public class MiniHBaseCluster extends HBaseCluster { 058 private static final Logger LOG = LoggerFactory.getLogger(MiniHBaseCluster.class.getName()); 059 public LocalHBaseCluster hbaseCluster; 060 private static int index; 061 062 /** 063 * Start a MiniHBaseCluster. 064 * @param conf Configuration to be used for cluster 065 * @param numRegionServers initial number of region servers to start. 066 * @throws IOException 067 */ 068 public MiniHBaseCluster(Configuration conf, int numRegionServers) 069 throws IOException, InterruptedException { 070 this(conf, 1, numRegionServers); 071 } 072 073 /** 074 * Start a MiniHBaseCluster. 075 * @param conf Configuration to be used for cluster 076 * @param numMasters initial number of masters to start. 077 * @param numRegionServers initial number of region servers to start. 078 * @throws IOException 079 */ 080 public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers) 081 throws IOException, InterruptedException { 082 this(conf, numMasters, numRegionServers, null, null); 083 } 084 085 /** 086 * Start a MiniHBaseCluster. 087 * @param conf Configuration to be used for cluster 088 * @param numMasters initial number of masters to start. 089 * @param numRegionServers initial number of region servers to start. 090 */ 091 public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers, 092 Class<? extends HMaster> masterClass, 093 Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass) 094 throws IOException, InterruptedException { 095 this(conf, numMasters, numRegionServers, null, masterClass, regionserverClass); 096 } 097 098 /** 099 * @param rsPorts Ports that RegionServer should use; pass ports if you want to test cluster 100 * restart where for sure the regionservers come up on same address+port (but 101 * just with different startcode); by default mini hbase clusters choose new 102 * arbitrary ports on each cluster start. 103 * @throws IOException 104 * @throws InterruptedException 105 */ 106 public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers, 107 List<Integer> rsPorts, 108 Class<? extends HMaster> masterClass, 109 Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass) 110 throws IOException, InterruptedException { 111 super(conf); 112 113 // Hadoop 2 114 CompatibilityFactory.getInstance(MetricsAssertHelper.class).init(); 115 116 init(numMasters, numRegionServers, rsPorts, masterClass, regionserverClass); 117 this.initialClusterStatus = getClusterStatus(); 118 } 119 120 public Configuration getConfiguration() { 121 return this.conf; 122 } 123 124 /** 125 * Subclass so can get at protected methods (none at moment). Also, creates 126 * a FileSystem instance per instantiation. Adds a shutdown own FileSystem 127 * on the way out. Shuts down own Filesystem only, not All filesystems as 128 * the FileSystem system exit hook does. 129 */ 130 public static class MiniHBaseClusterRegionServer extends HRegionServer { 131 private Thread shutdownThread = null; 132 private User user = null; 133 /** 134 * List of RegionServers killed so far. ServerName also comprises startCode of a server, 135 * so any restarted instances of the same server will have different ServerName and will not 136 * coincide with past dead ones. So there's no need to cleanup this list. 137 */ 138 static Set<ServerName> killedServers = new HashSet<>(); 139 140 public MiniHBaseClusterRegionServer(Configuration conf) 141 throws IOException, InterruptedException { 142 super(conf); 143 this.user = User.getCurrent(); 144 } 145 146 /* 147 * @param c 148 * @param currentfs We return this if we did not make a new one. 149 * @param uniqueName Same name used to help identify the created fs. 150 * @return A new fs instance if we are up on DistributeFileSystem. 151 * @throws IOException 152 */ 153 154 @Override 155 protected void handleReportForDutyResponse( 156 final RegionServerStartupResponse c) throws IOException { 157 super.handleReportForDutyResponse(c); 158 // Run this thread to shutdown our filesystem on way out. 159 this.shutdownThread = new SingleFileSystemShutdownThread(getFileSystem()); 160 } 161 162 @Override 163 public void run() { 164 try { 165 this.user.runAs(new PrivilegedAction<Object>() { 166 @Override 167 public Object run() { 168 runRegionServer(); 169 return null; 170 } 171 }); 172 } catch (Throwable t) { 173 LOG.error("Exception in run", t); 174 } finally { 175 // Run this on the way out. 176 if (this.shutdownThread != null) { 177 this.shutdownThread.start(); 178 Threads.shutdown(this.shutdownThread, 30000); 179 } 180 } 181 } 182 183 private void runRegionServer() { 184 super.run(); 185 } 186 187 @Override 188 protected void kill() { 189 killedServers.add(getServerName()); 190 super.kill(); 191 } 192 193 @Override 194 public void abort(final String reason, final Throwable cause) { 195 this.user.runAs(new PrivilegedAction<Object>() { 196 @Override 197 public Object run() { 198 abortRegionServer(reason, cause); 199 return null; 200 } 201 }); 202 } 203 204 private void abortRegionServer(String reason, Throwable cause) { 205 super.abort(reason, cause); 206 } 207 } 208 209 /** 210 * Alternate shutdown hook. 211 * Just shuts down the passed fs, not all as default filesystem hook does. 212 */ 213 static class SingleFileSystemShutdownThread extends Thread { 214 private final FileSystem fs; 215 SingleFileSystemShutdownThread(final FileSystem fs) { 216 super("Shutdown of " + fs); 217 this.fs = fs; 218 } 219 @Override 220 public void run() { 221 try { 222 LOG.info("Hook closing fs=" + this.fs); 223 this.fs.close(); 224 } catch (NullPointerException npe) { 225 LOG.debug("Need to fix these: " + npe.toString()); 226 } catch (IOException e) { 227 LOG.warn("Running hook", e); 228 } 229 } 230 } 231 232 private void init(final int nMasterNodes, final int nRegionNodes, List<Integer> rsPorts, 233 Class<? extends HMaster> masterClass, 234 Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass) 235 throws IOException, InterruptedException { 236 try { 237 if (masterClass == null){ 238 masterClass = HMaster.class; 239 } 240 if (regionserverClass == null){ 241 regionserverClass = MiniHBaseCluster.MiniHBaseClusterRegionServer.class; 242 } 243 244 // start up a LocalHBaseCluster 245 hbaseCluster = new LocalHBaseCluster(conf, nMasterNodes, 0, 246 masterClass, regionserverClass); 247 248 // manually add the regionservers as other users 249 for (int i = 0; i < nRegionNodes; i++) { 250 Configuration rsConf = HBaseConfiguration.create(conf); 251 if (rsPorts != null) { 252 rsConf.setInt(HConstants.REGIONSERVER_PORT, rsPorts.get(i)); 253 } 254 User user = HBaseTestingUtility.getDifferentUser(rsConf, 255 ".hfs."+index++); 256 hbaseCluster.addRegionServer(rsConf, i, user); 257 } 258 259 hbaseCluster.startup(); 260 } catch (IOException e) { 261 shutdown(); 262 throw e; 263 } catch (Throwable t) { 264 LOG.error("Error starting cluster", t); 265 shutdown(); 266 throw new IOException("Shutting down", t); 267 } 268 } 269 270 @Override 271 public void startRegionServer(String hostname, int port) throws IOException { 272 final Configuration newConf = HBaseConfiguration.create(conf); 273 newConf.setInt(HConstants.REGIONSERVER_PORT, port); 274 startRegionServer(newConf); 275 } 276 277 @Override 278 public void killRegionServer(ServerName serverName) throws IOException { 279 HRegionServer server = getRegionServer(getRegionServerIndex(serverName)); 280 if (server instanceof MiniHBaseClusterRegionServer) { 281 LOG.info("Killing " + server.toString()); 282 ((MiniHBaseClusterRegionServer) server).kill(); 283 } else { 284 abortRegionServer(getRegionServerIndex(serverName)); 285 } 286 } 287 288 @Override 289 public boolean isKilledRS(ServerName serverName) { 290 return MiniHBaseClusterRegionServer.killedServers.contains(serverName); 291 } 292 293 @Override 294 public void stopRegionServer(ServerName serverName) throws IOException { 295 stopRegionServer(getRegionServerIndex(serverName)); 296 } 297 298 @Override 299 public void waitForRegionServerToStop(ServerName serverName, long timeout) throws IOException { 300 //ignore timeout for now 301 waitOnRegionServer(getRegionServerIndex(serverName)); 302 } 303 304 @Override 305 public void startZkNode(String hostname, int port) throws IOException { 306 LOG.warn("Starting zookeeper nodes on mini cluster is not supported"); 307 } 308 309 @Override 310 public void killZkNode(ServerName serverName) throws IOException { 311 LOG.warn("Aborting zookeeper nodes on mini cluster is not supported"); 312 } 313 314 @Override 315 public void stopZkNode(ServerName serverName) throws IOException { 316 LOG.warn("Stopping zookeeper nodes on mini cluster is not supported"); 317 } 318 319 @Override 320 public void waitForZkNodeToStart(ServerName serverName, long timeout) throws IOException { 321 LOG.warn("Waiting for zookeeper nodes to start on mini cluster is not supported"); 322 } 323 324 @Override 325 public void waitForZkNodeToStop(ServerName serverName, long timeout) throws IOException { 326 LOG.warn("Waiting for zookeeper nodes to stop on mini cluster is not supported"); 327 } 328 329 @Override 330 public void startDataNode(ServerName serverName) throws IOException { 331 LOG.warn("Starting datanodes on mini cluster is not supported"); 332 } 333 334 @Override 335 public void killDataNode(ServerName serverName) throws IOException { 336 LOG.warn("Aborting datanodes on mini cluster is not supported"); 337 } 338 339 @Override 340 public void stopDataNode(ServerName serverName) throws IOException { 341 LOG.warn("Stopping datanodes on mini cluster is not supported"); 342 } 343 344 @Override 345 public void waitForDataNodeToStart(ServerName serverName, long timeout) throws IOException { 346 LOG.warn("Waiting for datanodes to start on mini cluster is not supported"); 347 } 348 349 @Override 350 public void waitForDataNodeToStop(ServerName serverName, long timeout) throws IOException { 351 LOG.warn("Waiting for datanodes to stop on mini cluster is not supported"); 352 } 353 354 @Override 355 public void startNameNode(ServerName serverName) throws IOException { 356 LOG.warn("Starting namenodes on mini cluster is not supported"); 357 } 358 359 @Override 360 public void killNameNode(ServerName serverName) throws IOException { 361 LOG.warn("Aborting namenodes on mini cluster is not supported"); 362 } 363 364 @Override 365 public void stopNameNode(ServerName serverName) throws IOException { 366 LOG.warn("Stopping namenodes on mini cluster is not supported"); 367 } 368 369 @Override 370 public void waitForNameNodeToStart(ServerName serverName, long timeout) throws IOException { 371 LOG.warn("Waiting for namenodes to start on mini cluster is not supported"); 372 } 373 374 @Override 375 public void waitForNameNodeToStop(ServerName serverName, long timeout) throws IOException { 376 LOG.warn("Waiting for namenodes to stop on mini cluster is not supported"); 377 } 378 379 @Override 380 public void startMaster(String hostname, int port) throws IOException { 381 this.startMaster(); 382 } 383 384 @Override 385 public void killMaster(ServerName serverName) throws IOException { 386 abortMaster(getMasterIndex(serverName)); 387 } 388 389 @Override 390 public void stopMaster(ServerName serverName) throws IOException { 391 stopMaster(getMasterIndex(serverName)); 392 } 393 394 @Override 395 public void waitForMasterToStop(ServerName serverName, long timeout) throws IOException { 396 //ignore timeout for now 397 waitOnMaster(getMasterIndex(serverName)); 398 } 399 400 /** 401 * Starts a region server thread running 402 * 403 * @throws IOException 404 * @return New RegionServerThread 405 */ 406 public JVMClusterUtil.RegionServerThread startRegionServer() 407 throws IOException { 408 final Configuration newConf = HBaseConfiguration.create(conf); 409 return startRegionServer(newConf); 410 } 411 412 private JVMClusterUtil.RegionServerThread startRegionServer(Configuration configuration) 413 throws IOException { 414 User rsUser = 415 HBaseTestingUtility.getDifferentUser(configuration, ".hfs."+index++); 416 JVMClusterUtil.RegionServerThread t = null; 417 try { 418 t = hbaseCluster.addRegionServer( 419 configuration, hbaseCluster.getRegionServers().size(), rsUser); 420 t.start(); 421 t.waitForServerOnline(); 422 } catch (InterruptedException ie) { 423 throw new IOException("Interrupted adding regionserver to cluster", ie); 424 } 425 return t; 426 } 427 428 /** 429 * Starts a region server thread and waits until its processed by master. Throws an exception 430 * when it can't start a region server or when the region server is not processed by master 431 * within the timeout. 432 * 433 * @return New RegionServerThread 434 */ 435 public JVMClusterUtil.RegionServerThread startRegionServerAndWait(long timeout) 436 throws IOException { 437 438 JVMClusterUtil.RegionServerThread t = startRegionServer(); 439 ServerName rsServerName = t.getRegionServer().getServerName(); 440 441 long start = System.currentTimeMillis(); 442 ClusterStatus clusterStatus = getClusterStatus(); 443 while ((System.currentTimeMillis() - start) < timeout) { 444 if (clusterStatus != null && clusterStatus.getServers().contains(rsServerName)) { 445 return t; 446 } 447 Threads.sleep(100); 448 } 449 if (t.getRegionServer().isOnline()) { 450 throw new IOException("RS: " + rsServerName + " online, but not processed by master"); 451 } else { 452 throw new IOException("RS: " + rsServerName + " is offline"); 453 } 454 } 455 456 /** 457 * Cause a region server to exit doing basic clean up only on its way out. 458 * @param serverNumber Used as index into a list. 459 */ 460 public String abortRegionServer(int serverNumber) { 461 HRegionServer server = getRegionServer(serverNumber); 462 LOG.info("Aborting " + server.toString()); 463 server.abort("Aborting for tests", new Exception("Trace info")); 464 return server.toString(); 465 } 466 467 /** 468 * Shut down the specified region server cleanly 469 * 470 * @param serverNumber Used as index into a list. 471 * @return the region server that was stopped 472 */ 473 public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber) { 474 return stopRegionServer(serverNumber, true); 475 } 476 477 /** 478 * Shut down the specified region server cleanly 479 * 480 * @param serverNumber Used as index into a list. 481 * @param shutdownFS True is we are to shutdown the filesystem as part of this 482 * regionserver's shutdown. Usually we do but you do not want to do this if 483 * you are running multiple regionservers in a test and you shut down one 484 * before end of the test. 485 * @return the region server that was stopped 486 */ 487 public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber, 488 final boolean shutdownFS) { 489 JVMClusterUtil.RegionServerThread server = 490 hbaseCluster.getRegionServers().get(serverNumber); 491 LOG.info("Stopping " + server.toString()); 492 server.getRegionServer().stop("Stopping rs " + serverNumber); 493 return server; 494 } 495 496 /** 497 * Wait for the specified region server to stop. Removes this thread from list 498 * of running threads. 499 * @param serverNumber 500 * @return Name of region server that just went down. 501 */ 502 public String waitOnRegionServer(final int serverNumber) { 503 return this.hbaseCluster.waitOnRegionServer(serverNumber); 504 } 505 506 507 /** 508 * Starts a master thread running 509 * 510 * @return New RegionServerThread 511 */ 512 public JVMClusterUtil.MasterThread startMaster() throws IOException { 513 Configuration c = HBaseConfiguration.create(conf); 514 User user = 515 HBaseTestingUtility.getDifferentUser(c, ".hfs."+index++); 516 517 JVMClusterUtil.MasterThread t = null; 518 try { 519 t = hbaseCluster.addMaster(c, hbaseCluster.getMasters().size(), user); 520 t.start(); 521 } catch (InterruptedException ie) { 522 throw new IOException("Interrupted adding master to cluster", ie); 523 } 524 return t; 525 } 526 527 /** 528 * Returns the current active master, if available. 529 * @return the active HMaster, null if none is active. 530 */ 531 @Override 532 public MasterService.BlockingInterface getMasterAdminService() { 533 return this.hbaseCluster.getActiveMaster().getMasterRpcServices(); 534 } 535 536 /** 537 * Returns the current active master, if available. 538 * @return the active HMaster, null if none is active. 539 */ 540 public HMaster getMaster() { 541 return this.hbaseCluster.getActiveMaster(); 542 } 543 544 /** 545 * Returns the current active master thread, if available. 546 * @return the active MasterThread, null if none is active. 547 */ 548 public MasterThread getMasterThread() { 549 for (MasterThread mt: hbaseCluster.getLiveMasters()) { 550 if (mt.getMaster().isActiveMaster()) { 551 return mt; 552 } 553 } 554 return null; 555 } 556 557 /** 558 * Returns the master at the specified index, if available. 559 * @return the active HMaster, null if none is active. 560 */ 561 public HMaster getMaster(final int serverNumber) { 562 return this.hbaseCluster.getMaster(serverNumber); 563 } 564 565 /** 566 * Cause a master to exit without shutting down entire cluster. 567 * @param serverNumber Used as index into a list. 568 */ 569 public String abortMaster(int serverNumber) { 570 HMaster server = getMaster(serverNumber); 571 LOG.info("Aborting " + server.toString()); 572 server.abort("Aborting for tests", new Exception("Trace info")); 573 return server.toString(); 574 } 575 576 /** 577 * Shut down the specified master cleanly 578 * 579 * @param serverNumber Used as index into a list. 580 * @return the region server that was stopped 581 */ 582 public JVMClusterUtil.MasterThread stopMaster(int serverNumber) { 583 return stopMaster(serverNumber, true); 584 } 585 586 /** 587 * Shut down the specified master cleanly 588 * 589 * @param serverNumber Used as index into a list. 590 * @param shutdownFS True is we are to shutdown the filesystem as part of this 591 * master's shutdown. Usually we do but you do not want to do this if 592 * you are running multiple master in a test and you shut down one 593 * before end of the test. 594 * @return the master that was stopped 595 */ 596 public JVMClusterUtil.MasterThread stopMaster(int serverNumber, 597 final boolean shutdownFS) { 598 JVMClusterUtil.MasterThread server = 599 hbaseCluster.getMasters().get(serverNumber); 600 LOG.info("Stopping " + server.toString()); 601 server.getMaster().stop("Stopping master " + serverNumber); 602 return server; 603 } 604 605 /** 606 * Wait for the specified master to stop. Removes this thread from list 607 * of running threads. 608 * @param serverNumber 609 * @return Name of master that just went down. 610 */ 611 public String waitOnMaster(final int serverNumber) { 612 return this.hbaseCluster.waitOnMaster(serverNumber); 613 } 614 615 /** 616 * Blocks until there is an active master and that master has completed 617 * initialization. 618 * 619 * @return true if an active master becomes available. false if there are no 620 * masters left. 621 * @throws InterruptedException 622 */ 623 @Override 624 public boolean waitForActiveAndReadyMaster(long timeout) throws IOException { 625 List<JVMClusterUtil.MasterThread> mts; 626 long start = System.currentTimeMillis(); 627 while (!(mts = getMasterThreads()).isEmpty() 628 && (System.currentTimeMillis() - start) < timeout) { 629 for (JVMClusterUtil.MasterThread mt : mts) { 630 if (mt.getMaster().isActiveMaster() && mt.getMaster().isInitialized()) { 631 return true; 632 } 633 } 634 635 Threads.sleep(100); 636 } 637 return false; 638 } 639 640 /** 641 * @return List of master threads. 642 */ 643 public List<JVMClusterUtil.MasterThread> getMasterThreads() { 644 return this.hbaseCluster.getMasters(); 645 } 646 647 /** 648 * @return List of live master threads (skips the aborted and the killed) 649 */ 650 public List<JVMClusterUtil.MasterThread> getLiveMasterThreads() { 651 return this.hbaseCluster.getLiveMasters(); 652 } 653 654 /** 655 * Wait for Mini HBase Cluster to shut down. 656 */ 657 public void join() { 658 this.hbaseCluster.join(); 659 } 660 661 /** 662 * Shut down the mini HBase cluster 663 */ 664 @Override 665 public void shutdown() throws IOException { 666 if (this.hbaseCluster != null) { 667 this.hbaseCluster.shutdown(); 668 } 669 } 670 671 @Override 672 public void close() throws IOException { 673 } 674 675 /** 676 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 677 * Use {@link #getClusterMetrics()} instead. 678 */ 679 @Deprecated 680 public ClusterStatus getClusterStatus() throws IOException { 681 HMaster master = getMaster(); 682 return master == null ? null : new ClusterStatus(master.getClusterMetrics()); 683 } 684 685 @Override 686 public ClusterMetrics getClusterMetrics() throws IOException { 687 HMaster master = getMaster(); 688 return master == null ? null : master.getClusterMetrics(); 689 } 690 691 private void executeFlush(HRegion region) throws IOException { 692 // retry 5 times if we can not flush 693 for (int i = 0; i < 5; i++) { 694 FlushResult result = region.flush(true); 695 if (result.getResult() != FlushResult.Result.CANNOT_FLUSH) { 696 return; 697 } 698 Threads.sleep(1000); 699 } 700 } 701 702 /** 703 * Call flushCache on all regions on all participating regionservers. 704 */ 705 public void flushcache() throws IOException { 706 for (JVMClusterUtil.RegionServerThread t : this.hbaseCluster.getRegionServers()) { 707 for (HRegion r : t.getRegionServer().getOnlineRegionsLocalContext()) { 708 executeFlush(r); 709 } 710 } 711 } 712 713 /** 714 * Call flushCache on all regions of the specified table. 715 */ 716 public void flushcache(TableName tableName) throws IOException { 717 for (JVMClusterUtil.RegionServerThread t : this.hbaseCluster.getRegionServers()) { 718 for (HRegion r : t.getRegionServer().getOnlineRegionsLocalContext()) { 719 if (r.getTableDescriptor().getTableName().equals(tableName)) { 720 executeFlush(r); 721 } 722 } 723 } 724 } 725 726 /** 727 * Call flushCache on all regions on all participating regionservers. 728 * @throws IOException 729 */ 730 public void compact(boolean major) throws IOException { 731 for (JVMClusterUtil.RegionServerThread t: 732 this.hbaseCluster.getRegionServers()) { 733 for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) { 734 r.compact(major); 735 } 736 } 737 } 738 739 /** 740 * Call flushCache on all regions of the specified table. 741 * @throws IOException 742 */ 743 public void compact(TableName tableName, boolean major) throws IOException { 744 for (JVMClusterUtil.RegionServerThread t: 745 this.hbaseCluster.getRegionServers()) { 746 for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) { 747 if(r.getTableDescriptor().getTableName().equals(tableName)) { 748 r.compact(major); 749 } 750 } 751 } 752 } 753 754 /** 755 * @return List of region server threads. Does not return the master even though it is also 756 * a region server. 757 */ 758 public List<JVMClusterUtil.RegionServerThread> getRegionServerThreads() { 759 return this.hbaseCluster.getRegionServers(); 760 } 761 762 /** 763 * @return List of live region server threads (skips the aborted and the killed) 764 */ 765 public List<JVMClusterUtil.RegionServerThread> getLiveRegionServerThreads() { 766 return this.hbaseCluster.getLiveRegionServers(); 767 } 768 769 /** 770 * Grab a numbered region server of your choice. 771 * @param serverNumber 772 * @return region server 773 */ 774 public HRegionServer getRegionServer(int serverNumber) { 775 return hbaseCluster.getRegionServer(serverNumber); 776 } 777 778 public HRegionServer getRegionServer(ServerName serverName) { 779 return hbaseCluster.getRegionServers().stream() 780 .map(t -> t.getRegionServer()) 781 .filter(r -> r.getServerName().equals(serverName)) 782 .findFirst().orElse(null); 783 } 784 785 public List<HRegion> getRegions(byte[] tableName) { 786 return getRegions(TableName.valueOf(tableName)); 787 } 788 789 public List<HRegion> getRegions(TableName tableName) { 790 List<HRegion> ret = new ArrayList<>(); 791 for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) { 792 HRegionServer hrs = rst.getRegionServer(); 793 for (Region region : hrs.getOnlineRegionsLocalContext()) { 794 if (region.getTableDescriptor().getTableName().equals(tableName)) { 795 ret.add((HRegion)region); 796 } 797 } 798 } 799 return ret; 800 } 801 802 /** 803 * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()} 804 * of HRS carrying regionName. Returns -1 if none found. 805 */ 806 public int getServerWithMeta() { 807 return getServerWith(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); 808 } 809 810 /** 811 * Get the location of the specified region 812 * @param regionName Name of the region in bytes 813 * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()} 814 * of HRS carrying hbase:meta. Returns -1 if none found. 815 */ 816 public int getServerWith(byte[] regionName) { 817 int index = -1; 818 int count = 0; 819 for (JVMClusterUtil.RegionServerThread rst: getRegionServerThreads()) { 820 HRegionServer hrs = rst.getRegionServer(); 821 if (!hrs.isStopped()) { 822 Region region = hrs.getOnlineRegion(regionName); 823 if (region != null) { 824 index = count; 825 break; 826 } 827 } 828 count++; 829 } 830 return index; 831 } 832 833 @Override 834 public ServerName getServerHoldingRegion(final TableName tn, byte[] regionName) 835 throws IOException { 836 // Assume there is only one master thread which is the active master. 837 // If there are multiple master threads, the backup master threads 838 // should hold some regions. Please refer to #countServedRegions 839 // to see how we find out all regions. 840 HMaster master = getMaster(); 841 Region region = master.getOnlineRegion(regionName); 842 if (region != null) { 843 return master.getServerName(); 844 } 845 int index = getServerWith(regionName); 846 if (index < 0) { 847 return null; 848 } 849 return getRegionServer(index).getServerName(); 850 } 851 852 /** 853 * Counts the total numbers of regions being served by the currently online 854 * region servers by asking each how many regions they have. Does not look 855 * at hbase:meta at all. Count includes catalog tables. 856 * @return number of regions being served by all region servers 857 */ 858 public long countServedRegions() { 859 long count = 0; 860 for (JVMClusterUtil.RegionServerThread rst : getLiveRegionServerThreads()) { 861 count += rst.getRegionServer().getNumberOfOnlineRegions(); 862 } 863 for (JVMClusterUtil.MasterThread mt : getLiveMasterThreads()) { 864 count += mt.getMaster().getNumberOfOnlineRegions(); 865 } 866 return count; 867 } 868 869 /** 870 * Do a simulated kill all masters and regionservers. Useful when it is 871 * impossible to bring the mini-cluster back for clean shutdown. 872 */ 873 public void killAll() { 874 // Do backups first. 875 MasterThread activeMaster = null; 876 for (MasterThread masterThread : getMasterThreads()) { 877 if (!masterThread.getMaster().isActiveMaster()) { 878 masterThread.getMaster().abort("killAll"); 879 } else { 880 activeMaster = masterThread; 881 } 882 } 883 // Do active after. 884 if (activeMaster != null) { 885 activeMaster.getMaster().abort("killAll"); 886 } 887 for (RegionServerThread rst : getRegionServerThreads()) { 888 rst.getRegionServer().abort("killAll"); 889 } 890 } 891 892 @Override 893 public void waitUntilShutDown() { 894 this.hbaseCluster.join(); 895 } 896 897 public List<HRegion> findRegionsForTable(TableName tableName) { 898 ArrayList<HRegion> ret = new ArrayList<>(); 899 for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) { 900 HRegionServer hrs = rst.getRegionServer(); 901 for (Region region : hrs.getRegions(tableName)) { 902 if (region.getTableDescriptor().getTableName().equals(tableName)) { 903 ret.add((HRegion)region); 904 } 905 } 906 } 907 return ret; 908 } 909 910 911 protected int getRegionServerIndex(ServerName serverName) { 912 //we have a small number of region servers, this should be fine for now. 913 List<RegionServerThread> servers = getRegionServerThreads(); 914 for (int i=0; i < servers.size(); i++) { 915 if (servers.get(i).getRegionServer().getServerName().equals(serverName)) { 916 return i; 917 } 918 } 919 return -1; 920 } 921 922 protected int getMasterIndex(ServerName serverName) { 923 List<MasterThread> masters = getMasterThreads(); 924 for (int i = 0; i < masters.size(); i++) { 925 if (masters.get(i).getMaster().getServerName().equals(serverName)) { 926 return i; 927 } 928 } 929 return -1; 930 } 931 932 @Override 933 public AdminService.BlockingInterface getAdminProtocol(ServerName serverName) throws IOException { 934 return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices(); 935 } 936 937 @Override 938 public ClientService.BlockingInterface getClientProtocol(ServerName serverName) 939 throws IOException { 940 return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices(); 941 } 942}