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