blob: e20d589f0fe75a57903b980998302ff4c9038928 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>Source code</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="source: package: org.apache.hadoop.hbase, class: IntegrationTestingUtility">
<meta name="generator" content="javadoc/SourceToHTMLConverter">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body class="source-page">
<main role="main">
<div class="source-container">
<pre><span class="source-line-no">001</span><span id="line-1">/*</span>
<span class="source-line-no">002</span><span id="line-2"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span class="source-line-no">003</span><span id="line-3"> * or more contributor license agreements. See the NOTICE file</span>
<span class="source-line-no">004</span><span id="line-4"> * distributed with this work for additional information</span>
<span class="source-line-no">005</span><span id="line-5"> * regarding copyright ownership. The ASF licenses this file</span>
<span class="source-line-no">006</span><span id="line-6"> * to you under the Apache License, Version 2.0 (the</span>
<span class="source-line-no">007</span><span id="line-7"> * "License"); you may not use this file except in compliance</span>
<span class="source-line-no">008</span><span id="line-8"> * with the License. You may obtain a copy of the License at</span>
<span class="source-line-no">009</span><span id="line-9"> *</span>
<span class="source-line-no">010</span><span id="line-10"> * http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="source-line-no">011</span><span id="line-11"> *</span>
<span class="source-line-no">012</span><span id="line-12"> * Unless required by applicable law or agreed to in writing, software</span>
<span class="source-line-no">013</span><span id="line-13"> * distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="source-line-no">014</span><span id="line-14"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="source-line-no">015</span><span id="line-15"> * See the License for the specific language governing permissions and</span>
<span class="source-line-no">016</span><span id="line-16"> * limitations under the License.</span>
<span class="source-line-no">017</span><span id="line-17"> */</span>
<span class="source-line-no">018</span><span id="line-18">package org.apache.hadoop.hbase;</span>
<span class="source-line-no">019</span><span id="line-19"></span>
<span class="source-line-no">020</span><span id="line-20">import java.io.IOException;</span>
<span class="source-line-no">021</span><span id="line-21">import org.apache.hadoop.conf.Configuration;</span>
<span class="source-line-no">022</span><span id="line-22">import org.apache.hadoop.util.ReflectionUtils;</span>
<span class="source-line-no">023</span><span id="line-23"></span>
<span class="source-line-no">024</span><span id="line-24">/**</span>
<span class="source-line-no">025</span><span id="line-25"> * Facility for &lt;strong&gt;integration/system&lt;/strong&gt; tests. This extends {@link HBaseTestingUtil} and</span>
<span class="source-line-no">026</span><span id="line-26"> * adds-in the functionality needed by integration and system tests. This class understands</span>
<span class="source-line-no">027</span><span id="line-27"> * distributed and pseudo-distributed/local cluster deployments, and abstracts those from the tests</span>
<span class="source-line-no">028</span><span id="line-28"> * in this module.</span>
<span class="source-line-no">029</span><span id="line-29"> * &lt;p&gt;</span>
<span class="source-line-no">030</span><span id="line-30"> * IntegrationTestingUtility is constructed and used by the integration tests, but the tests</span>
<span class="source-line-no">031</span><span id="line-31"> * themselves should not assume a particular deployment. They can rely on the methods in this class</span>
<span class="source-line-no">032</span><span id="line-32"> * and HBaseCluster. Before the testing begins, the test should initialize the cluster by calling</span>
<span class="source-line-no">033</span><span id="line-33"> * {@link #initializeCluster(int)}.</span>
<span class="source-line-no">034</span><span id="line-34"> * &lt;p&gt;</span>
<span class="source-line-no">035</span><span id="line-35"> * The cluster that is used defaults to a mini cluster, but it can be forced to use a distributed</span>
<span class="source-line-no">036</span><span id="line-36"> * cluster by calling {@link #setUseDistributedCluster(Configuration)}. This method is invoked by</span>
<span class="source-line-no">037</span><span id="line-37"> * test drivers (maven, IntegrationTestsDriver, etc) before initializing the cluster via</span>
<span class="source-line-no">038</span><span id="line-38"> * {@link #initializeCluster(int)}. Individual tests should not directly call</span>
<span class="source-line-no">039</span><span id="line-39"> * {@link #setUseDistributedCluster(Configuration)}.</span>
<span class="source-line-no">040</span><span id="line-40"> */</span>
<span class="source-line-no">041</span><span id="line-41">public class IntegrationTestingUtility extends HBaseTestingUtil {</span>
<span class="source-line-no">042</span><span id="line-42"></span>
<span class="source-line-no">043</span><span id="line-43"> public IntegrationTestingUtility() {</span>
<span class="source-line-no">044</span><span id="line-44"> this(HBaseConfiguration.create());</span>
<span class="source-line-no">045</span><span id="line-45"> }</span>
<span class="source-line-no">046</span><span id="line-46"></span>
<span class="source-line-no">047</span><span id="line-47"> public IntegrationTestingUtility(Configuration conf) {</span>
<span class="source-line-no">048</span><span id="line-48"> super(conf);</span>
<span class="source-line-no">049</span><span id="line-49"> }</span>
<span class="source-line-no">050</span><span id="line-50"></span>
<span class="source-line-no">051</span><span id="line-51"> /**</span>
<span class="source-line-no">052</span><span id="line-52"> * Configuration that controls whether this utility assumes a running/deployed cluster. This is</span>
<span class="source-line-no">053</span><span id="line-53"> * different than "hbase.cluster.distributed" since that parameter indicates whether the cluster</span>
<span class="source-line-no">054</span><span id="line-54"> * is in an actual distributed environment, while this shows that there is a deployed (distributed</span>
<span class="source-line-no">055</span><span id="line-55"> * or pseudo-distributed) cluster running, and we do not need to start a mini-cluster for tests.</span>
<span class="source-line-no">056</span><span id="line-56"> */</span>
<span class="source-line-no">057</span><span id="line-57"> public static final String IS_DISTRIBUTED_CLUSTER = "hbase.test.cluster.distributed";</span>
<span class="source-line-no">058</span><span id="line-58"></span>
<span class="source-line-no">059</span><span id="line-59"> /**</span>
<span class="source-line-no">060</span><span id="line-60"> * Config for pluggable hbase cluster manager. Pass fully-qualified class name as property value.</span>
<span class="source-line-no">061</span><span id="line-61"> * Drop the '.class' suffix.</span>
<span class="source-line-no">062</span><span id="line-62"> */</span>
<span class="source-line-no">063</span><span id="line-63"> public static final String HBASE_CLUSTER_MANAGER_CLASS = "hbase.it.clustermanager.class";</span>
<span class="source-line-no">064</span><span id="line-64"> private static final Class&lt;? extends ClusterManager&gt; DEFAULT_HBASE_CLUSTER_MANAGER_CLASS =</span>
<span class="source-line-no">065</span><span id="line-65"> HBaseClusterManager.class;</span>
<span class="source-line-no">066</span><span id="line-66"></span>
<span class="source-line-no">067</span><span id="line-67"> /**</span>
<span class="source-line-no">068</span><span id="line-68"> * Initializes the state of the cluster. It starts a new in-process mini cluster, OR if we are</span>
<span class="source-line-no">069</span><span id="line-69"> * given an already deployed distributed cluster it initializes the state.</span>
<span class="source-line-no">070</span><span id="line-70"> * @param numSlaves Number of slaves to start up if we are booting a mini cluster. Otherwise we</span>
<span class="source-line-no">071</span><span id="line-71"> * check whether this many nodes are available and throw an exception if not.</span>
<span class="source-line-no">072</span><span id="line-72"> */</span>
<span class="source-line-no">073</span><span id="line-73"> public void initializeCluster(int numSlaves) throws Exception {</span>
<span class="source-line-no">074</span><span id="line-74"> if (isDistributedCluster()) {</span>
<span class="source-line-no">075</span><span id="line-75"> createDistributedHBaseCluster();</span>
<span class="source-line-no">076</span><span id="line-76"> checkNodeCount(numSlaves);</span>
<span class="source-line-no">077</span><span id="line-77"> } else {</span>
<span class="source-line-no">078</span><span id="line-78"> startMiniCluster(numSlaves);</span>
<span class="source-line-no">079</span><span id="line-79"> }</span>
<span class="source-line-no">080</span><span id="line-80"> }</span>
<span class="source-line-no">081</span><span id="line-81"></span>
<span class="source-line-no">082</span><span id="line-82"> /**</span>
<span class="source-line-no">083</span><span id="line-83"> * Checks whether we have more than numSlaves nodes. Throws an exception otherwise.</span>
<span class="source-line-no">084</span><span id="line-84"> */</span>
<span class="source-line-no">085</span><span id="line-85"> public void checkNodeCount(int numSlaves) throws Exception {</span>
<span class="source-line-no">086</span><span id="line-86"> HBaseClusterInterface cluster = getHBaseClusterInterface();</span>
<span class="source-line-no">087</span><span id="line-87"> if (cluster.getClusterMetrics().getLiveServerMetrics().size() &lt; numSlaves) {</span>
<span class="source-line-no">088</span><span id="line-88"> throw new Exception("Cluster does not have enough nodes:" + numSlaves);</span>
<span class="source-line-no">089</span><span id="line-89"> }</span>
<span class="source-line-no">090</span><span id="line-90"> }</span>
<span class="source-line-no">091</span><span id="line-91"></span>
<span class="source-line-no">092</span><span id="line-92"> /**</span>
<span class="source-line-no">093</span><span id="line-93"> * Restores the cluster to the initial state if it is a distributed cluster, otherwise, shutdowns</span>
<span class="source-line-no">094</span><span id="line-94"> * the mini cluster.</span>
<span class="source-line-no">095</span><span id="line-95"> */</span>
<span class="source-line-no">096</span><span id="line-96"> public void restoreCluster() throws IOException {</span>
<span class="source-line-no">097</span><span id="line-97"> if (isDistributedCluster()) {</span>
<span class="source-line-no">098</span><span id="line-98"> getHBaseClusterInterface().restoreInitialStatus();</span>
<span class="source-line-no">099</span><span id="line-99"> } else {</span>
<span class="source-line-no">100</span><span id="line-100"> try {</span>
<span class="source-line-no">101</span><span id="line-101"> shutdownMiniCluster();</span>
<span class="source-line-no">102</span><span id="line-102"> } catch (Exception e) {</span>
<span class="source-line-no">103</span><span id="line-103"> // re-wrap into IOException</span>
<span class="source-line-no">104</span><span id="line-104"> throw new IOException(e);</span>
<span class="source-line-no">105</span><span id="line-105"> }</span>
<span class="source-line-no">106</span><span id="line-106"> }</span>
<span class="source-line-no">107</span><span id="line-107"> }</span>
<span class="source-line-no">108</span><span id="line-108"></span>
<span class="source-line-no">109</span><span id="line-109"> /**</span>
<span class="source-line-no">110</span><span id="line-110"> * Sets the configuration property to use a distributed cluster for the integration tests. Test</span>
<span class="source-line-no">111</span><span id="line-111"> * drivers should use this to enforce cluster deployment.</span>
<span class="source-line-no">112</span><span id="line-112"> */</span>
<span class="source-line-no">113</span><span id="line-113"> public static void setUseDistributedCluster(Configuration conf) {</span>
<span class="source-line-no">114</span><span id="line-114"> conf.setBoolean(IS_DISTRIBUTED_CLUSTER, true);</span>
<span class="source-line-no">115</span><span id="line-115"> System.setProperty(IS_DISTRIBUTED_CLUSTER, "true");</span>
<span class="source-line-no">116</span><span id="line-116"> }</span>
<span class="source-line-no">117</span><span id="line-117"></span>
<span class="source-line-no">118</span><span id="line-118"> /**</span>
<span class="source-line-no">119</span><span id="line-119"> * Returns whether we are interacting with a distributed cluster as opposed to and in-process mini</span>
<span class="source-line-no">120</span><span id="line-120"> * cluster or a local cluster.</span>
<span class="source-line-no">121</span><span id="line-121"> * @see IntegrationTestingUtility#setUseDistributedCluster(Configuration)</span>
<span class="source-line-no">122</span><span id="line-122"> */</span>
<span class="source-line-no">123</span><span id="line-123"> public boolean isDistributedCluster() {</span>
<span class="source-line-no">124</span><span id="line-124"> Configuration conf = getConfiguration();</span>
<span class="source-line-no">125</span><span id="line-125"> boolean isDistributedCluster =</span>
<span class="source-line-no">126</span><span id="line-126"> Boolean.parseBoolean(System.getProperty(IS_DISTRIBUTED_CLUSTER, "false"));</span>
<span class="source-line-no">127</span><span id="line-127"> if (!isDistributedCluster) {</span>
<span class="source-line-no">128</span><span id="line-128"> isDistributedCluster = conf.getBoolean(IS_DISTRIBUTED_CLUSTER, false);</span>
<span class="source-line-no">129</span><span id="line-129"> }</span>
<span class="source-line-no">130</span><span id="line-130"> return isDistributedCluster;</span>
<span class="source-line-no">131</span><span id="line-131"> }</span>
<span class="source-line-no">132</span><span id="line-132"></span>
<span class="source-line-no">133</span><span id="line-133"> public void createDistributedHBaseCluster() throws IOException {</span>
<span class="source-line-no">134</span><span id="line-134"> // if it is a distributed HBase cluster, use the conf provided by classpath</span>
<span class="source-line-no">135</span><span id="line-135"> // to set hbase dir and fs.defaultFS.</span>
<span class="source-line-no">136</span><span id="line-136"> // Since when the super class HBaseTestingUtility initializing, it will</span>
<span class="source-line-no">137</span><span id="line-137"> // change hbase.rootdir to a local test dir.</span>
<span class="source-line-no">138</span><span id="line-138"> // we use "original.defaultFS" and "original.hbase.dir" to restore them.</span>
<span class="source-line-no">139</span><span id="line-139"> Configuration conf = getConfiguration();</span>
<span class="source-line-no">140</span><span id="line-140"> if (conf.get("original.defaultFS") != null) {</span>
<span class="source-line-no">141</span><span id="line-141"> conf.set("fs.defaultFS", conf.get("original.defaultFS"));</span>
<span class="source-line-no">142</span><span id="line-142"> }</span>
<span class="source-line-no">143</span><span id="line-143"> if (conf.get("original.hbase.dir") != null) {</span>
<span class="source-line-no">144</span><span id="line-144"> conf.set(HConstants.HBASE_DIR, conf.get("original.hbase.dir"));</span>
<span class="source-line-no">145</span><span id="line-145"> }</span>
<span class="source-line-no">146</span><span id="line-146"> LOG.debug("Setting {} to {} since it is a distributed cluster", HConstants.HBASE_DIR,</span>
<span class="source-line-no">147</span><span id="line-147"> conf.get(HConstants.HBASE_DIR));</span>
<span class="source-line-no">148</span><span id="line-148"> Class&lt;? extends ClusterManager&gt; clusterManagerClass = conf.getClass(HBASE_CLUSTER_MANAGER_CLASS,</span>
<span class="source-line-no">149</span><span id="line-149"> DEFAULT_HBASE_CLUSTER_MANAGER_CLASS, ClusterManager.class);</span>
<span class="source-line-no">150</span><span id="line-150"> LOG.info("Instantiating {}", clusterManagerClass.getName());</span>
<span class="source-line-no">151</span><span id="line-151"> ClusterManager clusterManager = ReflectionUtils.newInstance(clusterManagerClass, conf);</span>
<span class="source-line-no">152</span><span id="line-152"> setHBaseCluster(new DistributedHBaseCluster(conf, clusterManager));</span>
<span class="source-line-no">153</span><span id="line-153"> getAdmin();</span>
<span class="source-line-no">154</span><span id="line-154"> }</span>
<span class="source-line-no">155</span><span id="line-155">}</span>
</pre>
</div>
</main>
</body>
</html>