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 */
018
019package org.apache.hadoop.hbase;
020
021import java.io.IOException;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.util.ReflectionUtils;
024
025/**
026 * Facility for <strong>integration/system</strong> tests. This extends {@link HBaseTestingUtility}
027 * and adds-in the functionality needed by integration and system tests. This class understands
028 * distributed and pseudo-distributed/local cluster deployments, and abstracts those from the tests
029 * in this module.
030 * <p>
031 * IntegrationTestingUtility is constructed and used by the integration tests, but the tests
032 * themselves should not assume a particular deployment. They can rely on the methods in this
033 * class and HBaseCluster. Before the testing begins, the test should initialize the cluster by
034 * calling {@link #initializeCluster(int)}.
035 * <p>
036 * The cluster that is used defaults to a mini cluster, but it can be forced to use a distributed
037 * cluster by calling {@link #setUseDistributedCluster(Configuration)}. This method is invoked by
038 * test drivers (maven, IntegrationTestsDriver, etc) before initializing the cluster
039 * via {@link #initializeCluster(int)}. Individual tests should not directly call
040 * {@link #setUseDistributedCluster(Configuration)}.
041 */
042public class IntegrationTestingUtility extends HBaseTestingUtility {
043
044  public IntegrationTestingUtility() {
045    this(HBaseConfiguration.create());
046  }
047
048  public IntegrationTestingUtility(Configuration conf) {
049    super(conf);
050  }
051
052  /**
053   * Configuration that controls whether this utility assumes a running/deployed cluster.
054   * This is different than "hbase.cluster.distributed" since that parameter indicates whether the
055   * cluster is in an actual distributed environment, while this shows that there is a
056   * deployed (distributed or pseudo-distributed) cluster running, and we do not need to
057   * start a mini-cluster for tests.
058   */
059  public static final String IS_DISTRIBUTED_CLUSTER = "hbase.test.cluster.distributed";
060
061  /** Config for pluggable hbase cluster manager */
062  private static final String HBASE_CLUSTER_MANAGER_CLASS = "hbase.it.clustermanager.class";
063  private static final Class<? extends ClusterManager> DEFAULT_HBASE_CLUSTER_MANAGER_CLASS =
064    HBaseClusterManager.class;
065
066  /**
067   * Initializes the state of the cluster. It starts a new in-process mini cluster, OR
068   * if we are given an already deployed distributed cluster it initializes the state.
069   * @param numSlaves Number of slaves to start up if we are booting a mini cluster. Otherwise
070   * we check whether this many nodes are available and throw an exception if not.
071   */
072  public void initializeCluster(int numSlaves) throws Exception {
073    if (isDistributedCluster()) {
074      createDistributedHBaseCluster();
075      checkNodeCount(numSlaves);
076    } else {
077      startMiniCluster(numSlaves);
078    }
079  }
080
081  /**
082   * Checks whether we have more than numSlaves nodes. Throws an
083   * exception otherwise.
084   */
085  public void checkNodeCount(int numSlaves) throws Exception {
086    HBaseCluster cluster = getHBaseClusterInterface();
087    if (cluster.getClusterMetrics().getLiveServerMetrics().size() < numSlaves) {
088      throw new Exception("Cluster does not have enough nodes:" + numSlaves);
089    }
090  }
091
092  /**
093   * Restores the cluster to the initial state if it is a distributed cluster, otherwise, shutdowns the
094   * mini cluster.
095   */
096  public void restoreCluster() throws IOException {
097    if (isDistributedCluster()) {
098      getHBaseClusterInterface().restoreInitialStatus();
099    } else {
100      try {
101        shutdownMiniCluster();
102      } catch (Exception e) {
103        // re-wrap into IOException
104        throw new IOException(e);
105      }
106    }
107  }
108
109  /**
110   * Sets the configuration property to use a distributed cluster for the integration tests. Test drivers
111   * should use this to enforce cluster deployment.
112   */
113  public static void setUseDistributedCluster(Configuration conf) {
114    conf.setBoolean(IS_DISTRIBUTED_CLUSTER, true);
115    System.setProperty(IS_DISTRIBUTED_CLUSTER, "true");
116  }
117
118  /**
119   * @return whether we are interacting with a distributed cluster as opposed to and in-process mini
120   * cluster or a local cluster.
121   * @see IntegrationTestingUtility#setUseDistributedCluster(Configuration)
122   */
123  public boolean isDistributedCluster() {
124    Configuration conf = getConfiguration();
125    boolean isDistributedCluster = false;
126    isDistributedCluster = Boolean.parseBoolean(System.getProperty(IS_DISTRIBUTED_CLUSTER, "false"));
127    if (!isDistributedCluster) {
128      isDistributedCluster = conf.getBoolean(IS_DISTRIBUTED_CLUSTER, false);
129    }
130    return isDistributedCluster;
131  }
132
133  public void createDistributedHBaseCluster() throws IOException {
134    //if it is a distributed HBase cluster, use the conf provided by classpath
135    //to set hbase dir and fs.defaultFS.
136    //Since when the super class HBaseTestingUtility initializing, it will
137    //change hbase.rootdir to a local test dir.
138    //we use "original.defaultFS" and "original.hbase.dir" to restore them.
139    Configuration conf = getConfiguration();
140    if (conf.get("original.defaultFS") != null) {
141      conf.set("fs.defaultFS", conf.get("original.defaultFS"));
142    }
143    if (conf.get("original.hbase.dir") != null) {
144      conf.set(HConstants.HBASE_DIR, conf.get("original.hbase.dir"));
145    }
146    LOG.debug("Setting {} to {} since it is a distributed cluster",
147        HConstants.HBASE_DIR, conf.get(HConstants.HBASE_DIR));
148    Class<? extends ClusterManager> clusterManagerClass = conf.getClass(HBASE_CLUSTER_MANAGER_CLASS,
149      DEFAULT_HBASE_CLUSTER_MANAGER_CLASS, ClusterManager.class);
150    LOG.info("Instantiating {}", clusterManagerClass.getName());
151    ClusterManager clusterManager = ReflectionUtils.newInstance(
152      clusterManagerClass, conf);
153    setHBaseCluster(new DistributedHBaseCluster(conf, clusterManager));
154    getAdmin();
155  }
156
157}