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.security.User;
40  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
41  import org.apache.hadoop.hbase.util.JVMClusterUtil;
42  import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
43  import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
44  import org.apache.hadoop.hbase.util.Threads;
45  
46  /**
47   * This class creates a single process HBase cluster.
48   * each server.  The master uses the 'default' FileSystem.  The RegionServers,
49   * if we are running on DistributedFilesystem, create a FileSystem instance
50   * each and will close down their instance on the way out.
51   */
52  @InterfaceAudience.Public
53  @InterfaceStability.Evolving
54  public class MiniHBaseCluster extends HBaseCluster {
55    static final Log LOG = LogFactory.getLog(MiniHBaseCluster.class.getName());
56    public LocalHBaseCluster hbaseCluster;
57    private static int index;
58  
59    /**
60     * Start a MiniHBaseCluster.
61     * @param conf Configuration to be used for cluster
62     * @param numRegionServers initial number of region servers to start.
63     * @throws IOException
64     */
65    public MiniHBaseCluster(Configuration conf, int numRegionServers)
66    throws IOException, InterruptedException {
67      this(conf, 1, numRegionServers);
68    }
69  
70    /**
71     * Start a MiniHBaseCluster.
72     * @param conf Configuration to be used for cluster
73     * @param numMasters initial number of masters to start.
74     * @param numRegionServers initial number of region servers to start.
75     * @throws IOException
76     */
77    public MiniHBaseCluster(Configuration conf, int numMasters,
78                               int numRegionServers)
79        throws IOException, InterruptedException {
80      this(conf, numMasters, numRegionServers, null, null);
81    }
82  
83    public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers,
84           Class<? extends HMaster> masterClass,
85           Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
86        throws IOException, InterruptedException {
87      super(conf);
88      conf.set(HConstants.MASTER_PORT, "0");
89  
90      // Hadoop 2
91      CompatibilityFactory.getInstance(MetricsAssertHelper.class).init();
92  
93      init(numMasters, numRegionServers, masterClass, regionserverClass);
94      this.initialClusterStatus = getClusterStatus();
95    }
96  
97    public Configuration getConfiguration() {
98      return this.conf;
99    }
100 
101   /**
102    * Subclass so can get at protected methods (none at moment).  Also, creates
103    * a FileSystem instance per instantiation.  Adds a shutdown own FileSystem
104    * on the way out. Shuts down own Filesystem only, not All filesystems as
105    * the FileSystem system exit hook does.
106    */
107   public static class MiniHBaseClusterRegionServer extends HRegionServer {
108     private Thread shutdownThread = null;
109     private User user = null;
110     public static boolean TEST_SKIP_CLOSE = false;
111 
112     public MiniHBaseClusterRegionServer(Configuration conf, CoordinatedStateManager cp)
113         throws IOException, InterruptedException {
114       super(conf, cp);
115       this.user = User.getCurrent();
116     }
117 
118     /*
119      * @param c
120      * @param currentfs We return this if we did not make a new one.
121      * @param uniqueName Same name used to help identify the created fs.
122      * @return A new fs instance if we are up on DistributeFileSystem.
123      * @throws IOException
124      */
125 
126     @Override
127     protected void handleReportForDutyResponse(
128         final RegionServerStartupResponse c) throws IOException {
129       super.handleReportForDutyResponse(c);
130       // Run this thread to shutdown our filesystem on way out.
131       this.shutdownThread = new SingleFileSystemShutdownThread(getFileSystem());
132     }
133 
134     @Override
135     public void run() {
136       try {
137         this.user.runAs(new PrivilegedAction<Object>(){
138           public Object run() {
139             runRegionServer();
140             return null;
141           }
142         });
143       } catch (Throwable t) {
144         LOG.error("Exception in run", t);
145       } finally {
146         // Run this on the way out.
147         if (this.shutdownThread != null) {
148           this.shutdownThread.start();
149           Threads.shutdown(this.shutdownThread, 30000);
150         }
151       }
152     }
153 
154     private void runRegionServer() {
155       super.run();
156     }
157 
158     @Override
159     public void kill() {
160       super.kill();
161     }
162 
163     public void abort(final String reason, final Throwable cause) {
164       this.user.runAs(new PrivilegedAction<Object>() {
165         public Object run() {
166           abortRegionServer(reason, cause);
167           return null;
168         }
169       });
170     }
171 
172     private void abortRegionServer(String reason, Throwable cause) {
173       super.abort(reason, cause);
174     }
175   }
176 
177   /**
178    * Alternate shutdown hook.
179    * Just shuts down the passed fs, not all as default filesystem hook does.
180    */
181   static class SingleFileSystemShutdownThread extends Thread {
182     private final FileSystem fs;
183     SingleFileSystemShutdownThread(final FileSystem fs) {
184       super("Shutdown of " + fs);
185       this.fs = fs;
186     }
187     @Override
188     public void run() {
189       try {
190         LOG.info("Hook closing fs=" + this.fs);
191         this.fs.close();
192       } catch (NullPointerException npe) {
193         LOG.debug("Need to fix these: " + npe.toString());
194       } catch (IOException e) {
195         LOG.warn("Running hook", e);
196       }
197     }
198   }
199 
200   private void init(final int nMasterNodes, final int nRegionNodes,
201                  Class<? extends HMaster> masterClass,
202                  Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
203   throws IOException, InterruptedException {
204     try {
205       if (masterClass == null){
206         masterClass =  HMaster.class;
207       }
208       if (regionserverClass == null){
209         regionserverClass = MiniHBaseCluster.MiniHBaseClusterRegionServer.class;
210       }
211 
212       // start up a LocalHBaseCluster
213       hbaseCluster = new LocalHBaseCluster(conf, nMasterNodes, 0,
214           masterClass, regionserverClass);
215 
216       // manually add the regionservers as other users
217       for (int i=0; i<nRegionNodes; i++) {
218         Configuration rsConf = HBaseConfiguration.create(conf);
219         User user = HBaseTestingUtility.getDifferentUser(rsConf,
220             ".hfs."+index++);
221         hbaseCluster.addRegionServer(rsConf, i, user);
222       }
223 
224       hbaseCluster.startup();
225     } catch (IOException e) {
226       shutdown();
227       throw e;
228     } catch (Throwable t) {
229       LOG.error("Error starting cluster", t);
230       shutdown();
231       throw new IOException("Shutting down", t);
232     }
233   }
234 
235   @Override
236   public void startRegionServer(String hostname) throws IOException {
237     this.startRegionServer();
238   }
239 
240   @Override
241   public void killRegionServer(ServerName serverName) throws IOException {
242     HRegionServer server = getRegionServer(getRegionServerIndex(serverName));
243     if (server instanceof MiniHBaseClusterRegionServer) {
244       LOG.info("Killing " + server.toString());
245       ((MiniHBaseClusterRegionServer) server).kill();
246     } else {
247       abortRegionServer(getRegionServerIndex(serverName));
248     }
249   }
250 
251   @Override
252   public void stopRegionServer(ServerName serverName) throws IOException {
253     stopRegionServer(getRegionServerIndex(serverName));
254   }
255 
256   @Override
257   public void waitForRegionServerToStop(ServerName serverName, long timeout) throws IOException {
258     //ignore timeout for now
259     waitOnRegionServer(getRegionServerIndex(serverName));
260   }
261 
262   @Override
263   public void startMaster(String hostname) throws IOException {
264     this.startMaster();
265   }
266 
267   @Override
268   public void killMaster(ServerName serverName) throws IOException {
269     abortMaster(getMasterIndex(serverName));
270   }
271 
272   @Override
273   public void stopMaster(ServerName serverName) throws IOException {
274     stopMaster(getMasterIndex(serverName));
275   }
276 
277   @Override
278   public void waitForMasterToStop(ServerName serverName, long timeout) throws IOException {
279     //ignore timeout for now
280     waitOnMaster(getMasterIndex(serverName));
281   }
282 
283   /**
284    * Starts a region server thread running
285    *
286    * @throws IOException
287    * @return New RegionServerThread
288    */
289   public JVMClusterUtil.RegionServerThread startRegionServer()
290       throws IOException {
291     final Configuration newConf = HBaseConfiguration.create(conf);
292     User rsUser =
293         HBaseTestingUtility.getDifferentUser(newConf, ".hfs."+index++);
294     JVMClusterUtil.RegionServerThread t =  null;
295     try {
296       t = hbaseCluster.addRegionServer(
297           newConf, hbaseCluster.getRegionServers().size(), rsUser);
298       t.start();
299       t.waitForServerOnline();
300     } catch (InterruptedException ie) {
301       throw new IOException("Interrupted adding regionserver to cluster", ie);
302     }
303     return t;
304   }
305 
306   /**
307    * Cause a region server to exit doing basic clean up only on its way out.
308    * @param serverNumber  Used as index into a list.
309    */
310   public String abortRegionServer(int serverNumber) {
311     HRegionServer server = getRegionServer(serverNumber);
312     LOG.info("Aborting " + server.toString());
313     server.abort("Aborting for tests", new Exception("Trace info"));
314     return server.toString();
315   }
316 
317   /**
318    * Shut down the specified region server cleanly
319    *
320    * @param serverNumber  Used as index into a list.
321    * @return the region server that was stopped
322    */
323   public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber) {
324     return stopRegionServer(serverNumber, true);
325   }
326 
327   /**
328    * Shut down the specified region server cleanly
329    *
330    * @param serverNumber  Used as index into a list.
331    * @param shutdownFS True is we are to shutdown the filesystem as part of this
332    * regionserver's shutdown.  Usually we do but you do not want to do this if
333    * you are running multiple regionservers in a test and you shut down one
334    * before end of the test.
335    * @return the region server that was stopped
336    */
337   public JVMClusterUtil.RegionServerThread stopRegionServer(int serverNumber,
338       final boolean shutdownFS) {
339     JVMClusterUtil.RegionServerThread server =
340       hbaseCluster.getRegionServers().get(serverNumber);
341     LOG.info("Stopping " + server.toString());
342     server.getRegionServer().stop("Stopping rs " + serverNumber);
343     return server;
344   }
345 
346   /**
347    * Wait for the specified region server to stop. Removes this thread from list
348    * of running threads.
349    * @param serverNumber
350    * @return Name of region server that just went down.
351    */
352   public String waitOnRegionServer(final int serverNumber) {
353     return this.hbaseCluster.waitOnRegionServer(serverNumber);
354   }
355 
356 
357   /**
358    * Starts a master thread running
359    *
360    * @throws IOException
361    * @return New RegionServerThread
362    */
363   public JVMClusterUtil.MasterThread startMaster() throws IOException {
364     Configuration c = HBaseConfiguration.create(conf);
365     User user =
366         HBaseTestingUtility.getDifferentUser(c, ".hfs."+index++);
367 
368     JVMClusterUtil.MasterThread t = null;
369     try {
370       t = hbaseCluster.addMaster(c, hbaseCluster.getMasters().size(), user);
371       t.start();
372     } catch (InterruptedException ie) {
373       throw new IOException("Interrupted adding master to cluster", ie);
374     }
375     return t;
376   }
377 
378   /**
379    * Returns the current active master, if available.
380    * @return the active HMaster, null if none is active.
381    */
382   public MasterService.BlockingInterface getMasterAdminService() {
383     return this.hbaseCluster.getActiveMaster().getMasterRpcServices();
384   }
385 
386   /**
387    * Returns the current active master, if available.
388    * @return the active HMaster, null if none is active.
389    */
390   public HMaster getMaster() {
391     return this.hbaseCluster.getActiveMaster();
392   }
393 
394   /**
395    * Returns the current active master thread, if available.
396    * @return the active MasterThread, null if none is active.
397    */
398   public MasterThread getMasterThread() {
399     for (MasterThread mt: hbaseCluster.getLiveMasters()) {
400       if (mt.getMaster().isActiveMaster()) {
401         return mt;
402       }
403     }
404     return null;
405   }
406 
407   /**
408    * Returns the master at the specified index, if available.
409    * @return the active HMaster, null if none is active.
410    */
411   public HMaster getMaster(final int serverNumber) {
412     return this.hbaseCluster.getMaster(serverNumber);
413   }
414 
415   /**
416    * Cause a master to exit without shutting down entire cluster.
417    * @param serverNumber  Used as index into a list.
418    */
419   public String abortMaster(int serverNumber) {
420     HMaster server = getMaster(serverNumber);
421     LOG.info("Aborting " + server.toString());
422     server.abort("Aborting for tests", new Exception("Trace info"));
423     return server.toString();
424   }
425 
426   /**
427    * Shut down the specified master cleanly
428    *
429    * @param serverNumber  Used as index into a list.
430    * @return the region server that was stopped
431    */
432   public JVMClusterUtil.MasterThread stopMaster(int serverNumber) {
433     return stopMaster(serverNumber, true);
434   }
435 
436   /**
437    * Shut down the specified master cleanly
438    *
439    * @param serverNumber  Used as index into a list.
440    * @param shutdownFS True is we are to shutdown the filesystem as part of this
441    * master's shutdown.  Usually we do but you do not want to do this if
442    * you are running multiple master in a test and you shut down one
443    * before end of the test.
444    * @return the master that was stopped
445    */
446   public JVMClusterUtil.MasterThread stopMaster(int serverNumber,
447       final boolean shutdownFS) {
448     JVMClusterUtil.MasterThread server =
449       hbaseCluster.getMasters().get(serverNumber);
450     LOG.info("Stopping " + server.toString());
451     server.getMaster().stop("Stopping master " + serverNumber);
452     return server;
453   }
454 
455   /**
456    * Wait for the specified master to stop. Removes this thread from list
457    * of running threads.
458    * @param serverNumber
459    * @return Name of master that just went down.
460    */
461   public String waitOnMaster(final int serverNumber) {
462     return this.hbaseCluster.waitOnMaster(serverNumber);
463   }
464 
465   /**
466    * Blocks until there is an active master and that master has completed
467    * initialization.
468    *
469    * @return true if an active master becomes available.  false if there are no
470    *         masters left.
471    * @throws InterruptedException
472    */
473   public boolean waitForActiveAndReadyMaster(long timeout) throws IOException {
474     List<JVMClusterUtil.MasterThread> mts;
475     long start = System.currentTimeMillis();
476     while (!(mts = getMasterThreads()).isEmpty()
477         && (System.currentTimeMillis() - start) < timeout) {
478       for (JVMClusterUtil.MasterThread mt : mts) {
479         if (mt.getMaster().isActiveMaster() && mt.getMaster().isInitialized()) {
480           return true;
481         }
482       }
483 
484       Threads.sleep(100);
485     }
486     return false;
487   }
488 
489   /**
490    * @return List of master threads.
491    */
492   public List<JVMClusterUtil.MasterThread> getMasterThreads() {
493     return this.hbaseCluster.getMasters();
494   }
495 
496   /**
497    * @return List of live master threads (skips the aborted and the killed)
498    */
499   public List<JVMClusterUtil.MasterThread> getLiveMasterThreads() {
500     return this.hbaseCluster.getLiveMasters();
501   }
502 
503   /**
504    * Wait for Mini HBase Cluster to shut down.
505    */
506   public void join() {
507     this.hbaseCluster.join();
508   }
509 
510   /**
511    * Shut down the mini HBase cluster
512    * @throws IOException
513    */
514   @SuppressWarnings("deprecation")
515   public void shutdown() throws IOException {
516     if (this.hbaseCluster != null) {
517       this.hbaseCluster.shutdown();
518     }
519   }
520 
521   @Override
522   public void close() throws IOException {
523   }
524 
525   @Override
526   public ClusterStatus getClusterStatus() throws IOException {
527     HMaster master = getMaster();
528     return master == null ? null : master.getClusterStatus();
529   }
530 
531   /**
532    * Call flushCache on all regions on all participating regionservers.
533    * @throws IOException
534    */
535   public void flushcache() throws IOException {
536     for (JVMClusterUtil.RegionServerThread t:
537         this.hbaseCluster.getRegionServers()) {
538       for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) {
539         r.flushcache();
540       }
541     }
542   }
543 
544   /**
545    * Call flushCache on all regions of the specified table.
546    * @throws IOException
547    */
548   public void flushcache(TableName tableName) throws IOException {
549     for (JVMClusterUtil.RegionServerThread t:
550         this.hbaseCluster.getRegionServers()) {
551       for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) {
552         if(r.getTableDesc().getTableName().equals(tableName)) {
553           r.flushcache();
554         }
555       }
556     }
557   }
558 
559   /**
560    * Call flushCache on all regions on all participating regionservers.
561    * @throws IOException
562    */
563   public void compact(boolean major) throws IOException {
564     for (JVMClusterUtil.RegionServerThread t:
565         this.hbaseCluster.getRegionServers()) {
566       for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) {
567         r.compactStores(major);
568       }
569     }
570   }
571 
572   /**
573    * Call flushCache on all regions of the specified table.
574    * @throws IOException
575    */
576   public void compact(TableName tableName, boolean major) throws IOException {
577     for (JVMClusterUtil.RegionServerThread t:
578         this.hbaseCluster.getRegionServers()) {
579       for(HRegion r: t.getRegionServer().getOnlineRegionsLocalContext()) {
580         if(r.getTableDesc().getTableName().equals(tableName)) {
581           r.compactStores(major);
582         }
583       }
584     }
585   }
586 
587   /**
588    * @return List of region server threads.
589    */
590   public List<JVMClusterUtil.RegionServerThread> getRegionServerThreads() {
591     return this.hbaseCluster.getRegionServers();
592   }
593 
594   /**
595    * @return List of live region server threads (skips the aborted and the killed)
596    */
597   public List<JVMClusterUtil.RegionServerThread> getLiveRegionServerThreads() {
598     return this.hbaseCluster.getLiveRegionServers();
599   }
600 
601   /**
602    * Grab a numbered region server of your choice.
603    * @param serverNumber
604    * @return region server
605    */
606   public HRegionServer getRegionServer(int serverNumber) {
607     return hbaseCluster.getRegionServer(serverNumber);
608   }
609 
610   public List<HRegion> getRegions(byte[] tableName) {
611     return getRegions(TableName.valueOf(tableName));
612   }
613 
614   public List<HRegion> getRegions(TableName tableName) {
615     List<HRegion> ret = new ArrayList<HRegion>();
616     for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) {
617       HRegionServer hrs = rst.getRegionServer();
618       for (HRegion region : hrs.getOnlineRegionsLocalContext()) {
619         if (region.getTableDesc().getTableName().equals(tableName)) {
620           ret.add(region);
621         }
622       }
623     }
624     return ret;
625   }
626 
627   /**
628    * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()}
629    * of HRS carrying regionName. Returns -1 if none found.
630    */
631   public int getServerWithMeta() {
632     return getServerWith(HRegionInfo.FIRST_META_REGIONINFO.getRegionName());
633   }
634 
635   /**
636    * Get the location of the specified region
637    * @param regionName Name of the region in bytes
638    * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()}
639    * of HRS carrying hbase:meta. Returns -1 if none found.
640    */
641   public int getServerWith(byte[] regionName) {
642     int index = -1;
643     int count = 0;
644     for (JVMClusterUtil.RegionServerThread rst: getRegionServerThreads()) {
645       HRegionServer hrs = rst.getRegionServer();
646       HRegion metaRegion =
647         hrs.getOnlineRegion(regionName);
648       if (metaRegion != null) {
649         index = count;
650         break;
651       }
652       count++;
653     }
654     return index;
655   }
656 
657   @Override
658   public ServerName getServerHoldingRegion(final TableName tn, byte[] regionName)
659   throws IOException {
660     // Assume there is only one master thread which is the active master.
661     // If there are multiple master threads, the backup master threads
662     // should hold some regions. Please refer to #countServedRegions
663     // to see how we find out all regions.
664     HMaster master = getMaster();
665     HRegion region = master.getOnlineRegion(regionName);
666     if (region != null) {
667       return master.getServerName();
668     }
669     int index = getServerWith(regionName);
670     if (index < 0) {
671       return null;
672     }
673     return getRegionServer(index).getServerName();
674   }
675 
676   /**
677    * Counts the total numbers of regions being served by the currently online
678    * region servers by asking each how many regions they have.  Does not look
679    * at hbase:meta at all.  Count includes catalog tables.
680    * @return number of regions being served by all region servers
681    */
682   public long countServedRegions() {
683     long count = 0;
684     for (JVMClusterUtil.RegionServerThread rst : getLiveRegionServerThreads()) {
685       count += rst.getRegionServer().getNumberOfOnlineRegions();
686     }
687     for (JVMClusterUtil.MasterThread mt : getLiveMasterThreads()) {
688       count += mt.getMaster().getNumberOfOnlineRegions();
689     }
690     return count;
691   }
692 
693   /**
694    * Do a simulated kill all masters and regionservers. Useful when it is
695    * impossible to bring the mini-cluster back for clean shutdown.
696    */
697   public void killAll() {
698     for (RegionServerThread rst : getRegionServerThreads()) {
699       rst.getRegionServer().abort("killAll");
700     }
701     for (MasterThread masterThread : getMasterThreads()) {
702       masterThread.getMaster().abort("killAll", new Throwable());
703     }
704   }
705 
706   @Override
707   public void waitUntilShutDown() {
708     this.hbaseCluster.join();
709   }
710 
711   public List<HRegion> findRegionsForTable(TableName tableName) {
712     ArrayList<HRegion> ret = new ArrayList<HRegion>();
713     for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) {
714       HRegionServer hrs = rst.getRegionServer();
715       for (HRegion region : hrs.getOnlineRegions(tableName)) {
716         if (region.getTableDesc().getTableName().equals(tableName)) {
717           ret.add(region);
718         }
719       }
720     }
721     return ret;
722   }
723 
724 
725   protected int getRegionServerIndex(ServerName serverName) {
726     //we have a small number of region servers, this should be fine for now.
727     List<RegionServerThread> servers = getRegionServerThreads();
728     for (int i=0; i < servers.size(); i++) {
729       if (servers.get(i).getRegionServer().getServerName().equals(serverName)) {
730         return i;
731       }
732     }
733     return -1;
734   }
735 
736   protected int getMasterIndex(ServerName serverName) {
737     List<MasterThread> masters = getMasterThreads();
738     for (int i = 0; i < masters.size(); i++) {
739       if (masters.get(i).getMaster().getServerName().equals(serverName)) {
740         return i;
741       }
742     }
743     return -1;
744   }
745 
746   @Override
747   public AdminService.BlockingInterface getAdminProtocol(ServerName serverName) throws IOException {
748     return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices();
749   }
750 
751   @Override
752   public ClientService.BlockingInterface getClientProtocol(ServerName serverName)
753   throws IOException {
754     return getRegionServer(getRegionServerIndex(serverName)).getRSRpcServices();
755   }
756 }