blob: 1720dada097ed37c873db0be95818a45dcdb147d [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.coprocessor, class: TestRegionObserverScannerOpenHook, class: CompactionCompletionNotifyingRegion">
<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.coprocessor;</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 static org.junit.Assert.assertEquals;</span>
<span class="source-line-no">021</span><span id="line-21">import static org.junit.Assert.assertNull;</span>
<span class="source-line-no">022</span><span id="line-22"></span>
<span class="source-line-no">023</span><span id="line-23">import java.io.IOException;</span>
<span class="source-line-no">024</span><span id="line-24">import java.util.Collections;</span>
<span class="source-line-no">025</span><span id="line-25">import java.util.List;</span>
<span class="source-line-no">026</span><span id="line-26">import java.util.Optional;</span>
<span class="source-line-no">027</span><span id="line-27">import java.util.concurrent.CountDownLatch;</span>
<span class="source-line-no">028</span><span id="line-28">import org.apache.hadoop.conf.Configuration;</span>
<span class="source-line-no">029</span><span id="line-29">import org.apache.hadoop.fs.FileSystem;</span>
<span class="source-line-no">030</span><span id="line-30">import org.apache.hadoop.fs.Path;</span>
<span class="source-line-no">031</span><span id="line-31">import org.apache.hadoop.hbase.Cell;</span>
<span class="source-line-no">032</span><span id="line-32">import org.apache.hadoop.hbase.Coprocessor;</span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hadoop.hbase.HBaseClassTestRule;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hadoop.hbase.HBaseTestingUtil;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hadoop.hbase.HConstants;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hadoop.hbase.TableName;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hadoop.hbase.client.Admin;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;</span>
<span class="source-line-no">039</span><span id="line-39">import org.apache.hadoop.hbase.client.CoprocessorDescriptorBuilder;</span>
<span class="source-line-no">040</span><span id="line-40">import org.apache.hadoop.hbase.client.Get;</span>
<span class="source-line-no">041</span><span id="line-41">import org.apache.hadoop.hbase.client.Put;</span>
<span class="source-line-no">042</span><span id="line-42">import org.apache.hadoop.hbase.client.RegionInfo;</span>
<span class="source-line-no">043</span><span id="line-43">import org.apache.hadoop.hbase.client.RegionInfoBuilder;</span>
<span class="source-line-no">044</span><span id="line-44">import org.apache.hadoop.hbase.client.Result;</span>
<span class="source-line-no">045</span><span id="line-45">import org.apache.hadoop.hbase.client.Scan;</span>
<span class="source-line-no">046</span><span id="line-46">import org.apache.hadoop.hbase.client.Table;</span>
<span class="source-line-no">047</span><span id="line-47">import org.apache.hadoop.hbase.client.TableDescriptor;</span>
<span class="source-line-no">048</span><span id="line-48">import org.apache.hadoop.hbase.client.TableDescriptorBuilder;</span>
<span class="source-line-no">049</span><span id="line-49">import org.apache.hadoop.hbase.filter.FilterBase;</span>
<span class="source-line-no">050</span><span id="line-50">import org.apache.hadoop.hbase.regionserver.ChunkCreator;</span>
<span class="source-line-no">051</span><span id="line-51">import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker;</span>
<span class="source-line-no">052</span><span id="line-52">import org.apache.hadoop.hbase.regionserver.HRegion;</span>
<span class="source-line-no">053</span><span id="line-53">import org.apache.hadoop.hbase.regionserver.HRegionServer;</span>
<span class="source-line-no">054</span><span id="line-54">import org.apache.hadoop.hbase.regionserver.HStore;</span>
<span class="source-line-no">055</span><span id="line-55">import org.apache.hadoop.hbase.regionserver.InternalScanner;</span>
<span class="source-line-no">056</span><span id="line-56">import org.apache.hadoop.hbase.regionserver.MemStoreLAB;</span>
<span class="source-line-no">057</span><span id="line-57">import org.apache.hadoop.hbase.regionserver.Region;</span>
<span class="source-line-no">058</span><span id="line-58">import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;</span>
<span class="source-line-no">059</span><span id="line-59">import org.apache.hadoop.hbase.regionserver.RegionServerServices;</span>
<span class="source-line-no">060</span><span id="line-60">import org.apache.hadoop.hbase.regionserver.ScanType;</span>
<span class="source-line-no">061</span><span id="line-61">import org.apache.hadoop.hbase.regionserver.ScannerContext;</span>
<span class="source-line-no">062</span><span id="line-62">import org.apache.hadoop.hbase.regionserver.Store;</span>
<span class="source-line-no">063</span><span id="line-63">import org.apache.hadoop.hbase.regionserver.StoreScanner;</span>
<span class="source-line-no">064</span><span id="line-64">import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;</span>
<span class="source-line-no">065</span><span id="line-65">import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;</span>
<span class="source-line-no">066</span><span id="line-66">import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;</span>
<span class="source-line-no">067</span><span id="line-67">import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;</span>
<span class="source-line-no">068</span><span id="line-68">import org.apache.hadoop.hbase.security.User;</span>
<span class="source-line-no">069</span><span id="line-69">import org.apache.hadoop.hbase.testclassification.CoprocessorTests;</span>
<span class="source-line-no">070</span><span id="line-70">import org.apache.hadoop.hbase.testclassification.MediumTests;</span>
<span class="source-line-no">071</span><span id="line-71">import org.apache.hadoop.hbase.util.Bytes;</span>
<span class="source-line-no">072</span><span id="line-72">import org.apache.hadoop.hbase.wal.WAL;</span>
<span class="source-line-no">073</span><span id="line-73">import org.junit.ClassRule;</span>
<span class="source-line-no">074</span><span id="line-74">import org.junit.Rule;</span>
<span class="source-line-no">075</span><span id="line-75">import org.junit.Test;</span>
<span class="source-line-no">076</span><span id="line-76">import org.junit.experimental.categories.Category;</span>
<span class="source-line-no">077</span><span id="line-77">import org.junit.rules.TestName;</span>
<span class="source-line-no">078</span><span id="line-78"></span>
<span class="source-line-no">079</span><span id="line-79">@Category({ CoprocessorTests.class, MediumTests.class })</span>
<span class="source-line-no">080</span><span id="line-80">public class TestRegionObserverScannerOpenHook {</span>
<span class="source-line-no">081</span><span id="line-81"></span>
<span class="source-line-no">082</span><span id="line-82"> @ClassRule</span>
<span class="source-line-no">083</span><span id="line-83"> public static final HBaseClassTestRule CLASS_RULE =</span>
<span class="source-line-no">084</span><span id="line-84"> HBaseClassTestRule.forClass(TestRegionObserverScannerOpenHook.class);</span>
<span class="source-line-no">085</span><span id="line-85"></span>
<span class="source-line-no">086</span><span id="line-86"> private static HBaseTestingUtil UTIL = new HBaseTestingUtil();</span>
<span class="source-line-no">087</span><span id="line-87"> static final Path DIR = UTIL.getDataTestDir();</span>
<span class="source-line-no">088</span><span id="line-88"></span>
<span class="source-line-no">089</span><span id="line-89"> @Rule</span>
<span class="source-line-no">090</span><span id="line-90"> public TestName name = new TestName();</span>
<span class="source-line-no">091</span><span id="line-91"></span>
<span class="source-line-no">092</span><span id="line-92"> public static class NoDataFilter extends FilterBase {</span>
<span class="source-line-no">093</span><span id="line-93"></span>
<span class="source-line-no">094</span><span id="line-94"> @Override</span>
<span class="source-line-no">095</span><span id="line-95"> public ReturnCode filterCell(final Cell ignored) {</span>
<span class="source-line-no">096</span><span id="line-96"> return ReturnCode.SKIP;</span>
<span class="source-line-no">097</span><span id="line-97"> }</span>
<span class="source-line-no">098</span><span id="line-98"></span>
<span class="source-line-no">099</span><span id="line-99"> @Override</span>
<span class="source-line-no">100</span><span id="line-100"> public boolean filterAllRemaining() throws IOException {</span>
<span class="source-line-no">101</span><span id="line-101"> return true;</span>
<span class="source-line-no">102</span><span id="line-102"> }</span>
<span class="source-line-no">103</span><span id="line-103"></span>
<span class="source-line-no">104</span><span id="line-104"> @Override</span>
<span class="source-line-no">105</span><span id="line-105"> public boolean filterRow() throws IOException {</span>
<span class="source-line-no">106</span><span id="line-106"> return true;</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"> /**</span>
<span class="source-line-no">111</span><span id="line-111"> * Do the default logic in {@link RegionObserver} interface.</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 class EmptyRegionObsever implements RegionCoprocessor, RegionObserver {</span>
<span class="source-line-no">114</span><span id="line-114"> @Override</span>
<span class="source-line-no">115</span><span id="line-115"> public Optional&lt;RegionObserver&gt; getRegionObserver() {</span>
<span class="source-line-no">116</span><span id="line-116"> return Optional.of(this);</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"></span>
<span class="source-line-no">120</span><span id="line-120"> /**</span>
<span class="source-line-no">121</span><span id="line-121"> * Don't return any data from a scan by creating a custom {@link StoreScanner}.</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 static class NoDataFromScan implements RegionCoprocessor, RegionObserver {</span>
<span class="source-line-no">124</span><span id="line-124"> @Override</span>
<span class="source-line-no">125</span><span id="line-125"> public Optional&lt;RegionObserver&gt; getRegionObserver() {</span>
<span class="source-line-no">126</span><span id="line-126"> return Optional.of(this);</span>
<span class="source-line-no">127</span><span id="line-127"> }</span>
<span class="source-line-no">128</span><span id="line-128"></span>
<span class="source-line-no">129</span><span id="line-129"> @Override</span>
<span class="source-line-no">130</span><span id="line-130"> public void preGetOp(ObserverContext&lt;RegionCoprocessorEnvironment&gt; c, Get get,</span>
<span class="source-line-no">131</span><span id="line-131"> List&lt;Cell&gt; result) throws IOException {</span>
<span class="source-line-no">132</span><span id="line-132"> c.bypass();</span>
<span class="source-line-no">133</span><span id="line-133"> }</span>
<span class="source-line-no">134</span><span id="line-134"></span>
<span class="source-line-no">135</span><span id="line-135"> @Override</span>
<span class="source-line-no">136</span><span id="line-136"> public void preScannerOpen(ObserverContext&lt;RegionCoprocessorEnvironment&gt; c, Scan scan)</span>
<span class="source-line-no">137</span><span id="line-137"> throws IOException {</span>
<span class="source-line-no">138</span><span id="line-138"> scan.setFilter(new NoDataFilter());</span>
<span class="source-line-no">139</span><span id="line-139"> }</span>
<span class="source-line-no">140</span><span id="line-140"> }</span>
<span class="source-line-no">141</span><span id="line-141"></span>
<span class="source-line-no">142</span><span id="line-142"> private static final InternalScanner NO_DATA = new InternalScanner() {</span>
<span class="source-line-no">143</span><span id="line-143"></span>
<span class="source-line-no">144</span><span id="line-144"> @Override</span>
<span class="source-line-no">145</span><span id="line-145"> public boolean next(List&lt;Cell&gt; result, ScannerContext scannerContext) throws IOException {</span>
<span class="source-line-no">146</span><span id="line-146"> return false;</span>
<span class="source-line-no">147</span><span id="line-147"> }</span>
<span class="source-line-no">148</span><span id="line-148"></span>
<span class="source-line-no">149</span><span id="line-149"> @Override</span>
<span class="source-line-no">150</span><span id="line-150"> public void close() throws IOException {</span>
<span class="source-line-no">151</span><span id="line-151"> }</span>
<span class="source-line-no">152</span><span id="line-152"> };</span>
<span class="source-line-no">153</span><span id="line-153"></span>
<span class="source-line-no">154</span><span id="line-154"> /**</span>
<span class="source-line-no">155</span><span id="line-155"> * Don't allow any data in a flush by creating a custom {@link StoreScanner}.</span>
<span class="source-line-no">156</span><span id="line-156"> */</span>
<span class="source-line-no">157</span><span id="line-157"> public static class NoDataFromFlush implements RegionCoprocessor, RegionObserver {</span>
<span class="source-line-no">158</span><span id="line-158"> @Override</span>
<span class="source-line-no">159</span><span id="line-159"> public Optional&lt;RegionObserver&gt; getRegionObserver() {</span>
<span class="source-line-no">160</span><span id="line-160"> return Optional.of(this);</span>
<span class="source-line-no">161</span><span id="line-161"> }</span>
<span class="source-line-no">162</span><span id="line-162"></span>
<span class="source-line-no">163</span><span id="line-163"> @Override</span>
<span class="source-line-no">164</span><span id="line-164"> public InternalScanner preFlush(ObserverContext&lt;RegionCoprocessorEnvironment&gt; c, Store store,</span>
<span class="source-line-no">165</span><span id="line-165"> InternalScanner scanner, FlushLifeCycleTracker tracker) throws IOException {</span>
<span class="source-line-no">166</span><span id="line-166"> return NO_DATA;</span>
<span class="source-line-no">167</span><span id="line-167"> }</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"> /**</span>
<span class="source-line-no">171</span><span id="line-171"> * Don't allow any data to be written out in the compaction by creating a custom</span>
<span class="source-line-no">172</span><span id="line-172"> * {@link StoreScanner}.</span>
<span class="source-line-no">173</span><span id="line-173"> */</span>
<span class="source-line-no">174</span><span id="line-174"> public static class NoDataFromCompaction implements RegionCoprocessor, RegionObserver {</span>
<span class="source-line-no">175</span><span id="line-175"> @Override</span>
<span class="source-line-no">176</span><span id="line-176"> public Optional&lt;RegionObserver&gt; getRegionObserver() {</span>
<span class="source-line-no">177</span><span id="line-177"> return Optional.of(this);</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"> @Override</span>
<span class="source-line-no">181</span><span id="line-181"> public InternalScanner preCompact(ObserverContext&lt;RegionCoprocessorEnvironment&gt; c, Store store,</span>
<span class="source-line-no">182</span><span id="line-182"> InternalScanner scanner, ScanType scanType, CompactionLifeCycleTracker tracker,</span>
<span class="source-line-no">183</span><span id="line-183"> CompactionRequest request) throws IOException {</span>
<span class="source-line-no">184</span><span id="line-184"> return NO_DATA;</span>
<span class="source-line-no">185</span><span id="line-185"> }</span>
<span class="source-line-no">186</span><span id="line-186"> }</span>
<span class="source-line-no">187</span><span id="line-187"></span>
<span class="source-line-no">188</span><span id="line-188"> HRegion initHRegion(byte[] tableName, String callingMethod, Configuration conf,</span>
<span class="source-line-no">189</span><span id="line-189"> byte[]... families) throws IOException {</span>
<span class="source-line-no">190</span><span id="line-190"> TableDescriptorBuilder builder =</span>
<span class="source-line-no">191</span><span id="line-191"> TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));</span>
<span class="source-line-no">192</span><span id="line-192"> for (byte[] family : families) {</span>
<span class="source-line-no">193</span><span id="line-193"> builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family));</span>
<span class="source-line-no">194</span><span id="line-194"> }</span>
<span class="source-line-no">195</span><span id="line-195"> TableDescriptor tableDescriptor = builder.build();</span>
<span class="source-line-no">196</span><span id="line-196"> ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,</span>
<span class="source-line-no">197</span><span id="line-197"> MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);</span>
<span class="source-line-no">198</span><span id="line-198"> RegionInfo info = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();</span>
<span class="source-line-no">199</span><span id="line-199"> Path path = new Path(DIR + callingMethod);</span>
<span class="source-line-no">200</span><span id="line-200"> WAL wal = HBaseTestingUtil.createWal(conf, path, info);</span>
<span class="source-line-no">201</span><span id="line-201"> HRegion r = HRegion.createHRegion(info, path, conf, tableDescriptor, wal);</span>
<span class="source-line-no">202</span><span id="line-202"> // this following piece is a hack. currently a coprocessorHost</span>
<span class="source-line-no">203</span><span id="line-203"> // is secretly loaded at OpenRegionHandler. we don't really</span>
<span class="source-line-no">204</span><span id="line-204"> // start a region server here, so just manually create cphost</span>
<span class="source-line-no">205</span><span id="line-205"> // and set it to region.</span>
<span class="source-line-no">206</span><span id="line-206"> RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);</span>
<span class="source-line-no">207</span><span id="line-207"> r.setCoprocessorHost(host);</span>
<span class="source-line-no">208</span><span id="line-208"> return r;</span>
<span class="source-line-no">209</span><span id="line-209"> }</span>
<span class="source-line-no">210</span><span id="line-210"></span>
<span class="source-line-no">211</span><span id="line-211"> @Test</span>
<span class="source-line-no">212</span><span id="line-212"> public void testRegionObserverScanTimeStacking() throws Exception {</span>
<span class="source-line-no">213</span><span id="line-213"> byte[] ROW = Bytes.toBytes("testRow");</span>
<span class="source-line-no">214</span><span id="line-214"> byte[] TABLE = Bytes.toBytes(getClass().getName());</span>
<span class="source-line-no">215</span><span id="line-215"> byte[] A = Bytes.toBytes("A");</span>
<span class="source-line-no">216</span><span id="line-216"> byte[][] FAMILIES = new byte[][] { A };</span>
<span class="source-line-no">217</span><span id="line-217"></span>
<span class="source-line-no">218</span><span id="line-218"> // Use new HTU to not overlap with the DFS cluster started in #CompactionStacking</span>
<span class="source-line-no">219</span><span id="line-219"> Configuration conf = new HBaseTestingUtil().getConfiguration();</span>
<span class="source-line-no">220</span><span id="line-220"> HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);</span>
<span class="source-line-no">221</span><span id="line-221"> RegionCoprocessorHost h = region.getCoprocessorHost();</span>
<span class="source-line-no">222</span><span id="line-222"> h.load(NoDataFromScan.class, Coprocessor.PRIORITY_HIGHEST, conf);</span>
<span class="source-line-no">223</span><span id="line-223"> h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);</span>
<span class="source-line-no">224</span><span id="line-224"></span>
<span class="source-line-no">225</span><span id="line-225"> Put put = new Put(ROW);</span>
<span class="source-line-no">226</span><span id="line-226"> put.addColumn(A, A, A);</span>
<span class="source-line-no">227</span><span id="line-227"> region.put(put);</span>
<span class="source-line-no">228</span><span id="line-228"></span>
<span class="source-line-no">229</span><span id="line-229"> Get get = new Get(ROW);</span>
<span class="source-line-no">230</span><span id="line-230"> Result r = region.get(get);</span>
<span class="source-line-no">231</span><span id="line-231"> assertNull(</span>
<span class="source-line-no">232</span><span id="line-232"> "Got an unexpected number of rows - "</span>
<span class="source-line-no">233</span><span id="line-233"> + "no data should be returned with the NoDataFromScan coprocessor. Found: " + r,</span>
<span class="source-line-no">234</span><span id="line-234"> r.listCells());</span>
<span class="source-line-no">235</span><span id="line-235"> HBaseTestingUtil.closeRegionAndWAL(region);</span>
<span class="source-line-no">236</span><span id="line-236"> }</span>
<span class="source-line-no">237</span><span id="line-237"></span>
<span class="source-line-no">238</span><span id="line-238"> @Test</span>
<span class="source-line-no">239</span><span id="line-239"> public void testRegionObserverFlushTimeStacking() throws Exception {</span>
<span class="source-line-no">240</span><span id="line-240"> byte[] ROW = Bytes.toBytes("testRow");</span>
<span class="source-line-no">241</span><span id="line-241"> byte[] TABLE = Bytes.toBytes(getClass().getName());</span>
<span class="source-line-no">242</span><span id="line-242"> byte[] A = Bytes.toBytes("A");</span>
<span class="source-line-no">243</span><span id="line-243"> byte[][] FAMILIES = new byte[][] { A };</span>
<span class="source-line-no">244</span><span id="line-244"></span>
<span class="source-line-no">245</span><span id="line-245"> // Use new HTU to not overlap with the DFS cluster started in #CompactionStacking</span>
<span class="source-line-no">246</span><span id="line-246"> Configuration conf = new HBaseTestingUtil().getConfiguration();</span>
<span class="source-line-no">247</span><span id="line-247"> HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);</span>
<span class="source-line-no">248</span><span id="line-248"> RegionCoprocessorHost h = region.getCoprocessorHost();</span>
<span class="source-line-no">249</span><span id="line-249"> h.load(NoDataFromFlush.class, Coprocessor.PRIORITY_HIGHEST, conf);</span>
<span class="source-line-no">250</span><span id="line-250"> h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);</span>
<span class="source-line-no">251</span><span id="line-251"></span>
<span class="source-line-no">252</span><span id="line-252"> // put a row and flush it to disk</span>
<span class="source-line-no">253</span><span id="line-253"> Put put = new Put(ROW);</span>
<span class="source-line-no">254</span><span id="line-254"> put.addColumn(A, A, A);</span>
<span class="source-line-no">255</span><span id="line-255"> region.put(put);</span>
<span class="source-line-no">256</span><span id="line-256"> region.flush(true);</span>
<span class="source-line-no">257</span><span id="line-257"> Get get = new Get(ROW);</span>
<span class="source-line-no">258</span><span id="line-258"> Result r = region.get(get);</span>
<span class="source-line-no">259</span><span id="line-259"> assertNull(</span>
<span class="source-line-no">260</span><span id="line-260"> "Got an unexpected number of rows - "</span>
<span class="source-line-no">261</span><span id="line-261"> + "no data should be returned with the NoDataFromScan coprocessor. Found: " + r,</span>
<span class="source-line-no">262</span><span id="line-262"> r.listCells());</span>
<span class="source-line-no">263</span><span id="line-263"> HBaseTestingUtil.closeRegionAndWAL(region);</span>
<span class="source-line-no">264</span><span id="line-264"> }</span>
<span class="source-line-no">265</span><span id="line-265"></span>
<span class="source-line-no">266</span><span id="line-266"> /*</span>
<span class="source-line-no">267</span><span id="line-267"> * Custom HRegion which uses CountDownLatch to signal the completion of compaction</span>
<span class="source-line-no">268</span><span id="line-268"> */</span>
<span class="source-line-no">269</span><span id="line-269"> public static class CompactionCompletionNotifyingRegion extends HRegion {</span>
<span class="source-line-no">270</span><span id="line-270"> private static volatile CountDownLatch compactionStateChangeLatch = null;</span>
<span class="source-line-no">271</span><span id="line-271"></span>
<span class="source-line-no">272</span><span id="line-272"> @SuppressWarnings("deprecation")</span>
<span class="source-line-no">273</span><span id="line-273"> public CompactionCompletionNotifyingRegion(Path tableDir, WAL log, FileSystem fs,</span>
<span class="source-line-no">274</span><span id="line-274"> Configuration confParam, RegionInfo info, TableDescriptor htd,</span>
<span class="source-line-no">275</span><span id="line-275"> RegionServerServices rsServices) {</span>
<span class="source-line-no">276</span><span id="line-276"> super(tableDir, log, fs, confParam, info, htd, rsServices);</span>
<span class="source-line-no">277</span><span id="line-277"> }</span>
<span class="source-line-no">278</span><span id="line-278"></span>
<span class="source-line-no">279</span><span id="line-279"> public CountDownLatch getCompactionStateChangeLatch() {</span>
<span class="source-line-no">280</span><span id="line-280"> if (compactionStateChangeLatch == null) {</span>
<span class="source-line-no">281</span><span id="line-281"> compactionStateChangeLatch = new CountDownLatch(1);</span>
<span class="source-line-no">282</span><span id="line-282"> }</span>
<span class="source-line-no">283</span><span id="line-283"> return compactionStateChangeLatch;</span>
<span class="source-line-no">284</span><span id="line-284"> }</span>
<span class="source-line-no">285</span><span id="line-285"></span>
<span class="source-line-no">286</span><span id="line-286"> @Override</span>
<span class="source-line-no">287</span><span id="line-287"> public boolean compact(CompactionContext compaction, HStore store,</span>
<span class="source-line-no">288</span><span id="line-288"> ThroughputController throughputController) throws IOException {</span>
<span class="source-line-no">289</span><span id="line-289"> boolean ret = super.compact(compaction, store, throughputController);</span>
<span class="source-line-no">290</span><span id="line-290"> if (ret) {</span>
<span class="source-line-no">291</span><span id="line-291"> compactionStateChangeLatch.countDown();</span>
<span class="source-line-no">292</span><span id="line-292"> }</span>
<span class="source-line-no">293</span><span id="line-293"> return ret;</span>
<span class="source-line-no">294</span><span id="line-294"> }</span>
<span class="source-line-no">295</span><span id="line-295"></span>
<span class="source-line-no">296</span><span id="line-296"> @Override</span>
<span class="source-line-no">297</span><span id="line-297"> public boolean compact(CompactionContext compaction, HStore store,</span>
<span class="source-line-no">298</span><span id="line-298"> ThroughputController throughputController, User user) throws IOException {</span>
<span class="source-line-no">299</span><span id="line-299"> boolean ret = super.compact(compaction, store, throughputController, user);</span>
<span class="source-line-no">300</span><span id="line-300"> if (ret) compactionStateChangeLatch.countDown();</span>
<span class="source-line-no">301</span><span id="line-301"> return ret;</span>
<span class="source-line-no">302</span><span id="line-302"> }</span>
<span class="source-line-no">303</span><span id="line-303"> }</span>
<span class="source-line-no">304</span><span id="line-304"></span>
<span class="source-line-no">305</span><span id="line-305"> /**</span>
<span class="source-line-no">306</span><span id="line-306"> * Unfortunately, the easiest way to test this is to spin up a mini-cluster since we want to do</span>
<span class="source-line-no">307</span><span id="line-307"> * the usual compaction mechanism on the region, rather than going through the backdoor to the</span>
<span class="source-line-no">308</span><span id="line-308"> * region</span>
<span class="source-line-no">309</span><span id="line-309"> */</span>
<span class="source-line-no">310</span><span id="line-310"> @Test</span>
<span class="source-line-no">311</span><span id="line-311"> public void testRegionObserverCompactionTimeStacking() throws Exception {</span>
<span class="source-line-no">312</span><span id="line-312"> // setup a mini cluster so we can do a real compaction on a region</span>
<span class="source-line-no">313</span><span id="line-313"> Configuration conf = UTIL.getConfiguration();</span>
<span class="source-line-no">314</span><span id="line-314"> conf.setClass(HConstants.REGION_IMPL, CompactionCompletionNotifyingRegion.class, HRegion.class);</span>
<span class="source-line-no">315</span><span id="line-315"> conf.setInt("hbase.hstore.compaction.min", 2);</span>
<span class="source-line-no">316</span><span id="line-316"> UTIL.startMiniCluster();</span>
<span class="source-line-no">317</span><span id="line-317"> byte[] ROW = Bytes.toBytes("testRow");</span>
<span class="source-line-no">318</span><span id="line-318"> byte[] A = Bytes.toBytes("A");</span>
<span class="source-line-no">319</span><span id="line-319"> TableDescriptor tableDescriptor =</span>
<span class="source-line-no">320</span><span id="line-320"> TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))</span>
<span class="source-line-no">321</span><span id="line-321"> .setColumnFamily(ColumnFamilyDescriptorBuilder.of(A))</span>
<span class="source-line-no">322</span><span id="line-322"> .setCoprocessor(CoprocessorDescriptorBuilder</span>
<span class="source-line-no">323</span><span id="line-323"> .newBuilder(EmptyRegionObsever.class.getName()).setJarPath(null)</span>
<span class="source-line-no">324</span><span id="line-324"> .setPriority(Coprocessor.PRIORITY_USER).setProperties(Collections.emptyMap()).build())</span>
<span class="source-line-no">325</span><span id="line-325"> .setCoprocessor(CoprocessorDescriptorBuilder</span>
<span class="source-line-no">326</span><span id="line-326"> .newBuilder(NoDataFromCompaction.class.getName()).setJarPath(null)</span>
<span class="source-line-no">327</span><span id="line-327"> .setPriority(Coprocessor.PRIORITY_HIGHEST).setProperties(Collections.emptyMap()).build())</span>
<span class="source-line-no">328</span><span id="line-328"> .build();</span>
<span class="source-line-no">329</span><span id="line-329"></span>
<span class="source-line-no">330</span><span id="line-330"> Admin admin = UTIL.getAdmin();</span>
<span class="source-line-no">331</span><span id="line-331"> admin.createTable(tableDescriptor);</span>
<span class="source-line-no">332</span><span id="line-332"></span>
<span class="source-line-no">333</span><span id="line-333"> Table table = UTIL.getConnection().getTable(tableDescriptor.getTableName());</span>
<span class="source-line-no">334</span><span id="line-334"></span>
<span class="source-line-no">335</span><span id="line-335"> // put a row and flush it to disk</span>
<span class="source-line-no">336</span><span id="line-336"> Put put = new Put(ROW);</span>
<span class="source-line-no">337</span><span id="line-337"> put.addColumn(A, A, A);</span>
<span class="source-line-no">338</span><span id="line-338"> table.put(put);</span>
<span class="source-line-no">339</span><span id="line-339"></span>
<span class="source-line-no">340</span><span id="line-340"> HRegionServer rs = UTIL.getRSForFirstRegionInTable(tableDescriptor.getTableName());</span>
<span class="source-line-no">341</span><span id="line-341"> List&lt;HRegion&gt; regions = rs.getRegions(tableDescriptor.getTableName());</span>
<span class="source-line-no">342</span><span id="line-342"> assertEquals("More than 1 region serving test table with 1 row", 1, regions.size());</span>
<span class="source-line-no">343</span><span id="line-343"> Region region = regions.get(0);</span>
<span class="source-line-no">344</span><span id="line-344"> admin.flushRegion(region.getRegionInfo().getRegionName());</span>
<span class="source-line-no">345</span><span id="line-345"> CountDownLatch latch =</span>
<span class="source-line-no">346</span><span id="line-346"> ((CompactionCompletionNotifyingRegion) region).getCompactionStateChangeLatch();</span>
<span class="source-line-no">347</span><span id="line-347"></span>
<span class="source-line-no">348</span><span id="line-348"> // put another row and flush that too</span>
<span class="source-line-no">349</span><span id="line-349"> put = new Put(Bytes.toBytes("anotherrow"));</span>
<span class="source-line-no">350</span><span id="line-350"> put.addColumn(A, A, A);</span>
<span class="source-line-no">351</span><span id="line-351"> table.put(put);</span>
<span class="source-line-no">352</span><span id="line-352"> admin.flushRegion(region.getRegionInfo().getRegionName());</span>
<span class="source-line-no">353</span><span id="line-353"></span>
<span class="source-line-no">354</span><span id="line-354"> // run a compaction, which normally would should get rid of the data</span>
<span class="source-line-no">355</span><span id="line-355"> // wait for the compaction checker to complete</span>
<span class="source-line-no">356</span><span id="line-356"> latch.await();</span>
<span class="source-line-no">357</span><span id="line-357"> // check both rows to ensure that they aren't there</span>
<span class="source-line-no">358</span><span id="line-358"> Get get = new Get(ROW);</span>
<span class="source-line-no">359</span><span id="line-359"> Result r = table.get(get);</span>
<span class="source-line-no">360</span><span id="line-360"> assertNull(</span>
<span class="source-line-no">361</span><span id="line-361"> "Got an unexpected number of rows - "</span>
<span class="source-line-no">362</span><span id="line-362"> + "no data should be returned with the NoDataFromScan coprocessor. Found: " + r,</span>
<span class="source-line-no">363</span><span id="line-363"> r.listCells());</span>
<span class="source-line-no">364</span><span id="line-364"></span>
<span class="source-line-no">365</span><span id="line-365"> get = new Get(Bytes.toBytes("anotherrow"));</span>
<span class="source-line-no">366</span><span id="line-366"> r = table.get(get);</span>
<span class="source-line-no">367</span><span id="line-367"> assertNull(</span>
<span class="source-line-no">368</span><span id="line-368"> "Got an unexpected number of rows - "</span>
<span class="source-line-no">369</span><span id="line-369"> + "no data should be returned with the NoDataFromScan coprocessor Found: " + r,</span>
<span class="source-line-no">370</span><span id="line-370"> r.listCells());</span>
<span class="source-line-no">371</span><span id="line-371"></span>
<span class="source-line-no">372</span><span id="line-372"> table.close();</span>
<span class="source-line-no">373</span><span id="line-373"> UTIL.shutdownMiniCluster();</span>
<span class="source-line-no">374</span><span id="line-374"> }</span>
<span class="source-line-no">375</span><span id="line-375">}</span>
</pre>
</div>
</main>
</body>
</html>