blob: 0a47d4d043d8f28d3b80ec0ef39eae07b5cb395e [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.util, class: TestRegionMover3, class: MockRackManager">
<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.util;</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.util.ArrayList;</span>
<span class="source-line-no">021</span><span id="line-21">import java.util.List;</span>
<span class="source-line-no">022</span><span id="line-22">import java.util.stream.Collectors;</span>
<span class="source-line-no">023</span><span id="line-23">import java.util.stream.IntStream;</span>
<span class="source-line-no">024</span><span id="line-24">import org.apache.hadoop.hbase.HBaseClassTestRule;</span>
<span class="source-line-no">025</span><span id="line-25">import org.apache.hadoop.hbase.HBaseTestingUtil;</span>
<span class="source-line-no">026</span><span id="line-26">import org.apache.hadoop.hbase.ServerName;</span>
<span class="source-line-no">027</span><span id="line-27">import org.apache.hadoop.hbase.SingleProcessHBaseCluster;</span>
<span class="source-line-no">028</span><span id="line-28">import org.apache.hadoop.hbase.TableName;</span>
<span class="source-line-no">029</span><span id="line-29">import org.apache.hadoop.hbase.client.Admin;</span>
<span class="source-line-no">030</span><span id="line-30">import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;</span>
<span class="source-line-no">031</span><span id="line-31">import org.apache.hadoop.hbase.client.Put;</span>
<span class="source-line-no">032</span><span id="line-32">import org.apache.hadoop.hbase.client.Table;</span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hadoop.hbase.client.TableDescriptor;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hadoop.hbase.client.TableDescriptorBuilder;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hadoop.hbase.master.RackManager;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hadoop.hbase.regionserver.HRegion;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hadoop.hbase.regionserver.HRegionServer;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.hadoop.hbase.testclassification.LargeTests;</span>
<span class="source-line-no">039</span><span id="line-39">import org.apache.hadoop.hbase.testclassification.MiscTests;</span>
<span class="source-line-no">040</span><span id="line-40">import org.junit.AfterClass;</span>
<span class="source-line-no">041</span><span id="line-41">import org.junit.Assert;</span>
<span class="source-line-no">042</span><span id="line-42">import org.junit.Before;</span>
<span class="source-line-no">043</span><span id="line-43">import org.junit.BeforeClass;</span>
<span class="source-line-no">044</span><span id="line-44">import org.junit.ClassRule;</span>
<span class="source-line-no">045</span><span id="line-45">import org.junit.Rule;</span>
<span class="source-line-no">046</span><span id="line-46">import org.junit.Test;</span>
<span class="source-line-no">047</span><span id="line-47">import org.junit.experimental.categories.Category;</span>
<span class="source-line-no">048</span><span id="line-48">import org.junit.rules.TestName;</span>
<span class="source-line-no">049</span><span id="line-49"></span>
<span class="source-line-no">050</span><span id="line-50">@Category({ MiscTests.class, LargeTests.class })</span>
<span class="source-line-no">051</span><span id="line-51">public class TestRegionMover3 {</span>
<span class="source-line-no">052</span><span id="line-52"></span>
<span class="source-line-no">053</span><span id="line-53"> @ClassRule</span>
<span class="source-line-no">054</span><span id="line-54"> public static final HBaseClassTestRule CLASS_RULE =</span>
<span class="source-line-no">055</span><span id="line-55"> HBaseClassTestRule.forClass(TestRegionMover3.class);</span>
<span class="source-line-no">056</span><span id="line-56"></span>
<span class="source-line-no">057</span><span id="line-57"> @Rule</span>
<span class="source-line-no">058</span><span id="line-58"> public TestName name = new TestName();</span>
<span class="source-line-no">059</span><span id="line-59"></span>
<span class="source-line-no">060</span><span id="line-60"> private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();</span>
<span class="source-line-no">061</span><span id="line-61"> private static ServerName rs0;</span>
<span class="source-line-no">062</span><span id="line-62"> private static ServerName rs1;</span>
<span class="source-line-no">063</span><span id="line-63"> private static ServerName rs2;</span>
<span class="source-line-no">064</span><span id="line-64"></span>
<span class="source-line-no">065</span><span id="line-65"> @BeforeClass</span>
<span class="source-line-no">066</span><span id="line-66"> public static void setUpBeforeClass() throws Exception {</span>
<span class="source-line-no">067</span><span id="line-67"> TEST_UTIL.startMiniCluster(3);</span>
<span class="source-line-no">068</span><span id="line-68"> SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();</span>
<span class="source-line-no">069</span><span id="line-69"> rs0 = cluster.getRegionServer(0).getServerName();</span>
<span class="source-line-no">070</span><span id="line-70"> rs1 = cluster.getRegionServer(1).getServerName();</span>
<span class="source-line-no">071</span><span id="line-71"> rs2 = cluster.getRegionServer(2).getServerName();</span>
<span class="source-line-no">072</span><span id="line-72"> TEST_UTIL.getAdmin().balancerSwitch(false, true);</span>
<span class="source-line-no">073</span><span id="line-73"> }</span>
<span class="source-line-no">074</span><span id="line-74"></span>
<span class="source-line-no">075</span><span id="line-75"> @AfterClass</span>
<span class="source-line-no">076</span><span id="line-76"> public static void tearDownAfterClass() throws Exception {</span>
<span class="source-line-no">077</span><span id="line-77"> TEST_UTIL.shutdownMiniCluster();</span>
<span class="source-line-no">078</span><span id="line-78"> }</span>
<span class="source-line-no">079</span><span id="line-79"></span>
<span class="source-line-no">080</span><span id="line-80"> @Before</span>
<span class="source-line-no">081</span><span id="line-81"> public void setUp() throws Exception {</span>
<span class="source-line-no">082</span><span id="line-82"> final TableName tableName = TableName.valueOf(name.getMethodName());</span>
<span class="source-line-no">083</span><span id="line-83"> TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)</span>
<span class="source-line-no">084</span><span id="line-84"> .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build();</span>
<span class="source-line-no">085</span><span id="line-85"> int startKey = 0;</span>
<span class="source-line-no">086</span><span id="line-86"> int endKey = 80000;</span>
<span class="source-line-no">087</span><span id="line-87"> TEST_UTIL.getAdmin().createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);</span>
<span class="source-line-no">088</span><span id="line-88"> }</span>
<span class="source-line-no">089</span><span id="line-89"></span>
<span class="source-line-no">090</span><span id="line-90"> @Test</span>
<span class="source-line-no">091</span><span id="line-91"> public void testRegionUnloadWithRack() throws Exception {</span>
<span class="source-line-no">092</span><span id="line-92"> final TableName tableName = TableName.valueOf(name.getMethodName());</span>
<span class="source-line-no">093</span><span id="line-93"> SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();</span>
<span class="source-line-no">094</span><span id="line-94"> Admin admin = TEST_UTIL.getAdmin();</span>
<span class="source-line-no">095</span><span id="line-95"> Table table = TEST_UTIL.getConnection().getTable(tableName);</span>
<span class="source-line-no">096</span><span id="line-96"> List&lt;Put&gt; puts = IntStream.range(10, 50000).mapToObj(i -&gt; new Put(Bytes.toBytes(i))</span>
<span class="source-line-no">097</span><span id="line-97"> .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i)))</span>
<span class="source-line-no">098</span><span id="line-98"> .collect(Collectors.toList());</span>
<span class="source-line-no">099</span><span id="line-99"> table.put(puts);</span>
<span class="source-line-no">100</span><span id="line-100"> admin.flush(tableName);</span>
<span class="source-line-no">101</span><span id="line-101"> admin.compact(tableName);</span>
<span class="source-line-no">102</span><span id="line-102"> Thread.sleep(3000);</span>
<span class="source-line-no">103</span><span id="line-103"> HRegionServer hRegionServer0 = cluster.getRegionServer(0);</span>
<span class="source-line-no">104</span><span id="line-104"> HRegionServer hRegionServer1 = cluster.getRegionServer(1);</span>
<span class="source-line-no">105</span><span id="line-105"> HRegionServer hRegionServer2 = cluster.getRegionServer(2);</span>
<span class="source-line-no">106</span><span id="line-106"> int numRegions0 = hRegionServer0.getNumberOfOnlineRegions();</span>
<span class="source-line-no">107</span><span id="line-107"> int numRegions1 = hRegionServer1.getNumberOfOnlineRegions();</span>
<span class="source-line-no">108</span><span id="line-108"> int numRegions2 = hRegionServer2.getNumberOfOnlineRegions();</span>
<span class="source-line-no">109</span><span id="line-109"></span>
<span class="source-line-no">110</span><span id="line-110"> Assert.assertTrue(numRegions0 &gt;= 3);</span>
<span class="source-line-no">111</span><span id="line-111"> Assert.assertTrue(numRegions1 &gt;= 3);</span>
<span class="source-line-no">112</span><span id="line-112"> Assert.assertTrue(numRegions2 &gt;= 3);</span>
<span class="source-line-no">113</span><span id="line-113"> int totalRegions = numRegions0 + numRegions1 + numRegions2;</span>
<span class="source-line-no">114</span><span id="line-114"></span>
<span class="source-line-no">115</span><span id="line-115"> // source RS: rs0</span>
<span class="source-line-no">116</span><span id="line-116"> String sourceRSName = rs0.getAddress().toString();</span>
<span class="source-line-no">117</span><span id="line-117"></span>
<span class="source-line-no">118</span><span id="line-118"> // move all regions from rs1 to rs0</span>
<span class="source-line-no">119</span><span id="line-119"> for (HRegion region : hRegionServer1.getRegions()) {</span>
<span class="source-line-no">120</span><span id="line-120"> TEST_UTIL.getAdmin().move(region.getRegionInfo().getEncodedNameAsBytes(), rs0);</span>
<span class="source-line-no">121</span><span id="line-121"> }</span>
<span class="source-line-no">122</span><span id="line-122"> TEST_UTIL.waitFor(5000, () -&gt; {</span>
<span class="source-line-no">123</span><span id="line-123"> int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();</span>
<span class="source-line-no">124</span><span id="line-124"> int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();</span>
<span class="source-line-no">125</span><span id="line-125"> return newNumRegions1 == 0 &amp;&amp; newNumRegions0 == (numRegions0 + numRegions1);</span>
<span class="source-line-no">126</span><span id="line-126"> });</span>
<span class="source-line-no">127</span><span id="line-127"></span>
<span class="source-line-no">128</span><span id="line-128"> // regionMover obj on rs0. While unloading regions from rs0</span>
<span class="source-line-no">129</span><span id="line-129"> // with default rackManager, which resolves "/default-rack" for each server, no region</span>
<span class="source-line-no">130</span><span id="line-130"> // is moved while using unloadFromRack() as all rs belong to same rack.</span>
<span class="source-line-no">131</span><span id="line-131"> RegionMover.RegionMoverBuilder rmBuilder =</span>
<span class="source-line-no">132</span><span id="line-132"> new RegionMover.RegionMoverBuilder(sourceRSName, TEST_UTIL.getConfiguration()).ack(true)</span>
<span class="source-line-no">133</span><span id="line-133"> .maxthreads(8);</span>
<span class="source-line-no">134</span><span id="line-134"> try (RegionMover regionMover = rmBuilder.build()) {</span>
<span class="source-line-no">135</span><span id="line-135"> regionMover.unloadFromRack();</span>
<span class="source-line-no">136</span><span id="line-136"> int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();</span>
<span class="source-line-no">137</span><span id="line-137"> int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();</span>
<span class="source-line-no">138</span><span id="line-138"> int newNumRegions2 = hRegionServer2.getNumberOfOnlineRegions();</span>
<span class="source-line-no">139</span><span id="line-139"> Assert.assertEquals(0, newNumRegions1);</span>
<span class="source-line-no">140</span><span id="line-140"> Assert.assertEquals(totalRegions, newNumRegions0 + newNumRegions2);</span>
<span class="source-line-no">141</span><span id="line-141"> }</span>
<span class="source-line-no">142</span><span id="line-142"></span>
<span class="source-line-no">143</span><span id="line-143"> // use custom rackManager, which resolves "rack-1" for rs0 and rs1,</span>
<span class="source-line-no">144</span><span id="line-144"> // while "rack-2" for rs2. Hence, unloadFromRack() from rs0 should move all</span>
<span class="source-line-no">145</span><span id="line-145"> // regions that belong to rs0 to rs2 only, and nothing should be moved to rs1</span>
<span class="source-line-no">146</span><span id="line-146"> // as rs0 and rs1 belong to same rack.</span>
<span class="source-line-no">147</span><span id="line-147"> rmBuilder.rackManager(new MockRackManager());</span>
<span class="source-line-no">148</span><span id="line-148"> try (RegionMover regionMover = rmBuilder.build()) {</span>
<span class="source-line-no">149</span><span id="line-149"> regionMover.unloadFromRack();</span>
<span class="source-line-no">150</span><span id="line-150"> int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();</span>
<span class="source-line-no">151</span><span id="line-151"> int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();</span>
<span class="source-line-no">152</span><span id="line-152"> int newNumRegions2 = hRegionServer2.getNumberOfOnlineRegions();</span>
<span class="source-line-no">153</span><span id="line-153"> Assert.assertEquals(0, newNumRegions0);</span>
<span class="source-line-no">154</span><span id="line-154"> Assert.assertEquals(0, newNumRegions1);</span>
<span class="source-line-no">155</span><span id="line-155"> Assert.assertEquals(totalRegions, newNumRegions2);</span>
<span class="source-line-no">156</span><span id="line-156"> }</span>
<span class="source-line-no">157</span><span id="line-157"></span>
<span class="source-line-no">158</span><span id="line-158"> }</span>
<span class="source-line-no">159</span><span id="line-159"></span>
<span class="source-line-no">160</span><span id="line-160"> private static class MockRackManager extends RackManager {</span>
<span class="source-line-no">161</span><span id="line-161"></span>
<span class="source-line-no">162</span><span id="line-162"> private static final String RACK_2 = "rack-2";</span>
<span class="source-line-no">163</span><span id="line-163"> private static final String RACK_1 = "rack-1";</span>
<span class="source-line-no">164</span><span id="line-164"></span>
<span class="source-line-no">165</span><span id="line-165"> @Override</span>
<span class="source-line-no">166</span><span id="line-166"> public String getRack(ServerName server) {</span>
<span class="source-line-no">167</span><span id="line-167"> return rs2.equals(server) ? RACK_2 : RACK_1;</span>
<span class="source-line-no">168</span><span id="line-168"> }</span>
<span class="source-line-no">169</span><span id="line-169"></span>
<span class="source-line-no">170</span><span id="line-170"> @Override</span>
<span class="source-line-no">171</span><span id="line-171"> public List&lt;String&gt; getRack(List&lt;ServerName&gt; servers) {</span>
<span class="source-line-no">172</span><span id="line-172"> List&lt;String&gt; racks = new ArrayList&lt;&gt;();</span>
<span class="source-line-no">173</span><span id="line-173"> servers.forEach(serverName -&gt; {</span>
<span class="source-line-no">174</span><span id="line-174"> if (rs2.equals(serverName)) {</span>
<span class="source-line-no">175</span><span id="line-175"> racks.add(RACK_2);</span>
<span class="source-line-no">176</span><span id="line-176"> } else {</span>
<span class="source-line-no">177</span><span id="line-177"> racks.add(RACK_1);</span>
<span class="source-line-no">178</span><span id="line-178"> }</span>
<span class="source-line-no">179</span><span id="line-179"> });</span>
<span class="source-line-no">180</span><span id="line-180"> return racks;</span>
<span class="source-line-no">181</span><span id="line-181"> }</span>
<span class="source-line-no">182</span><span id="line-182"> }</span>
<span class="source-line-no">183</span><span id="line-183"></span>
<span class="source-line-no">184</span><span id="line-184">}</span>
</pre>
</div>
</main>
</body>
</html>