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