View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * This class creates a single process HBase cluster.
49   * each server.  The master uses the 'default' FileSystem.  The RegionServers,
50   * if we are running on DistributedFilesystem, create a FileSystem instance
51   * each and will close down their instance on the way out.
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     * Start a MiniHBaseCluster.
62     * @param conf Configuration to be used for cluster
63     * @param numRegionServers initial number of region servers to start.
64     * @throws IOException
65     */
66    public MiniHBaseCluster(Configuration conf, int numRegionServers)
67    throws IOException, InterruptedException {
68      this(conf, 1, numRegionServers);
69    }
70  
71    /**
72     * Start a MiniHBaseCluster.
73     * @param conf Configuration to be used for cluster
74     * @param numMasters initial number of masters to start.
75     * @param numRegionServers initial number of region servers to start.
76     * @throws IOException
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      // Hadoop 2
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    * Subclass so can get at protected methods (none at moment).  Also, creates
107    * a FileSystem instance per instantiation.  Adds a shutdown own FileSystem
108    * on the way out. Shuts down own Filesystem only, not All filesystems as
109    * the FileSystem system exit hook does.
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      * @param c
124      * @param currentfs We return this if we did not make a new one.
125      * @param uniqueName Same name used to help identify the created fs.
126      * @return A new fs instance if we are up on DistributeFileSystem.
127      * @throws IOException
128      */
129 
130     @Override
131     protected void handleReportForDutyResponse(
132         final RegionServerStartupResponse c) throws IOException {
133       super.handleReportForDutyResponse(c);
134       // Run this thread to shutdown our filesystem on way out.
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         // Run this on the way out.
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    * Alternate shutdown hook.
183    * Just shuts down the passed fs, not all as default filesystem hook does.
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       // start up a LocalHBaseCluster
217       hbaseCluster = new LocalHBaseCluster(conf, nMasterNodes, 0,
218           masterClass, regionserverClass);
219 
220       // manually add the regionservers as other users
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     //ignore timeout for now
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     //ignore timeout for now
334     waitOnMaster(getMasterIndex(serverName));
335   }
336 
337   /**
338    * Starts a region server thread running
339    *
340    * @throws IOException
341    * @return New RegionServerThread
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    * Cause a region server to exit doing basic clean up only on its way out.
362    * @param serverNumber  Used as index into a list.
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    * Shut down the specified region server cleanly
373    *
374    * @param serverNumber  Used as index into a list.
375    * @return the region server that was stopped
376    */
377   public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber) {
378     return stopRegionServer(serverNumber, true);
379   }
380 
381   /**
382    * Shut down the specified region server cleanly
383    *
384    * @param serverNumber  Used as index into a list.
385    * @param shutdownFS True is we are to shutdown the filesystem as part of this
386    * regionserver's shutdown.  Usually we do but you do not want to do this if
387    * you are running multiple regionservers in a test and you shut down one
388    * before end of the test.
389    * @return the region server that was stopped
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    * Wait for the specified region server to stop. Removes this thread from list
402    * of running threads.
403    * @param serverNumber
404    * @return Name of region server that just went down.
405    */
406   public String waitOnRegionServer(final int serverNumber) {
407     return this.hbaseCluster.waitOnRegionServer(serverNumber);
408   }
409 
410 
411   /**
412    * Starts a master thread running
413    *
414    * @throws IOException
415    * @return New RegionServerThread
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    * Returns the current active master, if available.
434    * @return the active HMaster, null if none is active.
435    */
436   public MasterService.BlockingInterface getMasterAdminService() {
437     return this.hbaseCluster.getActiveMaster().getMasterRpcServices();
438   }
439 
440   /**
441    * Returns the current active master, if available.
442    * @return the active HMaster, null if none is active.
443    */
444   public HMaster getMaster() {
445     return this.hbaseCluster.getActiveMaster();
446   }
447 
448   /**
449    * Returns the current active master thread, if available.
450    * @return the active MasterThread, null if none is active.
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    * Returns the master at the specified index, if available.
463    * @return the active HMaster, null if none is active.
464    */
465   public HMaster getMaster(final int serverNumber) {
466     return this.hbaseCluster.getMaster(serverNumber);
467   }
468 
469   /**
470    * Cause a master to exit without shutting down entire cluster.
471    * @param serverNumber  Used as index into a list.
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    * Shut down the specified master cleanly
482    *
483    * @param serverNumber  Used as index into a list.
484    * @return the region server that was stopped
485    */
486   public JVMClusterUtil.MasterThread stopMaster(int serverNumber) {
487     return stopMaster(serverNumber, true);
488   }
489 
490   /**
491    * Shut down the specified master cleanly
492    *
493    * @param serverNumber  Used as index into a list.
494    * @param shutdownFS True is we are to shutdown the filesystem as part of this
495    * master's shutdown.  Usually we do but you do not want to do this if
496    * you are running multiple master in a test and you shut down one
497    * before end of the test.
498    * @return the master that was stopped
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    * Wait for the specified master to stop. Removes this thread from list
511    * of running threads.
512    * @param serverNumber
513    * @return Name of master that just went down.
514    */
515   public String waitOnMaster(final int serverNumber) {
516     return this.hbaseCluster.waitOnMaster(serverNumber);
517   }
518 
519   /**
520    * Blocks until there is an active master and that master has completed
521    * initialization.
522    *
523    * @return true if an active master becomes available.  false if there are no
524    *         masters left.
525    * @throws InterruptedException
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    * @return List of master threads.
545    */
546   public List<JVMClusterUtil.MasterThread> getMasterThreads() {
547     return this.hbaseCluster.getMasters();
548   }
549 
550   /**
551    * @return List of live master threads (skips the aborted and the killed)
552    */
553   public List<JVMClusterUtil.MasterThread> getLiveMasterThreads() {
554     return this.hbaseCluster.getLiveMasters();
555   }
556 
557   /**
558    * Wait for Mini HBase Cluster to shut down.
559    */
560   public void join() {
561     this.hbaseCluster.join();
562   }
563 
564   /**
565    * Shut down the mini HBase cluster
566    * @throws IOException
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    * Call flushCache on all regions on all participating regionservers.
587    * @throws IOException
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    * Call flushCache on all regions of the specified table.
600    * @throws IOException
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    * Call flushCache on all regions on all participating regionservers.
615    * @throws IOException
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    * Call flushCache on all regions of the specified table.
628    * @throws IOException
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    * @return List of region server threads.
643    */
644   public List<JVMClusterUtil.RegionServerThread> getRegionServerThreads() {
645     return this.hbaseCluster.getRegionServers();
646   }
647 
648   /**
649    * @return List of live region server threads (skips the aborted and the killed)
650    */
651   public List<JVMClusterUtil.RegionServerThread> getLiveRegionServerThreads() {
652     return this.hbaseCluster.getLiveRegionServers();
653   }
654 
655   /**
656    * Grab a numbered region server of your choice.
657    * @param serverNumber
658    * @return region server
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    * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()}
683    * of HRS carrying regionName. Returns -1 if none found.
684    */
685   public int getServerWithMeta() {
686     return getServerWith(HRegionInfo.FIRST_META_REGIONINFO.getRegionName());
687   }
688 
689   /**
690    * Get the location of the specified region
691    * @param regionName Name of the region in bytes
692    * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()}
693    * of HRS carrying hbase:meta. Returns -1 if none found.
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     // Assume there is only one master thread which is the active master.
716     // If there are multiple master threads, the backup master threads
717     // should hold some regions. Please refer to #countServedRegions
718     // to see how we find out all regions.
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    * Counts the total numbers of regions being served by the currently online
733    * region servers by asking each how many regions they have.  Does not look
734    * at hbase:meta at all.  Count includes catalog tables.
735    * @return number of regions being served by all region servers
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    * Do a simulated kill all masters and regionservers. Useful when it is
750    * impossible to bring the mini-cluster back for clean shutdown.
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     //we have a small number of region servers, this should be fine for now.
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 }