blob: 97f992748f80242ffeb81d07ff707b8d97956ad0 [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: TestHBaseFsckCleanReplicationBarriers">
<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 static org.junit.Assert.assertEquals;</span>
<span class="source-line-no">021</span><span id="line-21">import static org.junit.Assert.assertFalse;</span>
<span class="source-line-no">022</span><span id="line-22">import static org.junit.Assert.assertTrue;</span>
<span class="source-line-no">023</span><span id="line-23"></span>
<span class="source-line-no">024</span><span id="line-24">import java.io.IOException;</span>
<span class="source-line-no">025</span><span id="line-25">import java.util.ArrayList;</span>
<span class="source-line-no">026</span><span id="line-26">import java.util.List;</span>
<span class="source-line-no">027</span><span id="line-27">import org.apache.hadoop.hbase.ClientMetaTableAccessor;</span>
<span class="source-line-no">028</span><span id="line-28">import org.apache.hadoop.hbase.HBaseClassTestRule;</span>
<span class="source-line-no">029</span><span id="line-29">import org.apache.hadoop.hbase.HBaseTestingUtil;</span>
<span class="source-line-no">030</span><span id="line-30">import org.apache.hadoop.hbase.HConstants;</span>
<span class="source-line-no">031</span><span id="line-31">import org.apache.hadoop.hbase.MetaTableAccessor;</span>
<span class="source-line-no">032</span><span id="line-32">import org.apache.hadoop.hbase.ServerName;</span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hadoop.hbase.TableName;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hadoop.hbase.client.Put;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hadoop.hbase.client.RegionInfo;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hadoop.hbase.client.RegionInfoBuilder;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.hadoop.hbase.client.Result;</span>
<span class="source-line-no">039</span><span id="line-39">import org.apache.hadoop.hbase.client.ResultScanner;</span>
<span class="source-line-no">040</span><span id="line-40">import org.apache.hadoop.hbase.client.Scan;</span>
<span class="source-line-no">041</span><span id="line-41">import org.apache.hadoop.hbase.client.Table;</span>
<span class="source-line-no">042</span><span id="line-42">import org.apache.hadoop.hbase.client.TableDescriptor;</span>
<span class="source-line-no">043</span><span id="line-43">import org.apache.hadoop.hbase.client.TableDescriptorBuilder;</span>
<span class="source-line-no">044</span><span id="line-44">import org.apache.hadoop.hbase.master.RegionState;</span>
<span class="source-line-no">045</span><span id="line-45">import org.apache.hadoop.hbase.replication.ReplicationBarrierFamilyFormat;</span>
<span class="source-line-no">046</span><span id="line-46">import org.apache.hadoop.hbase.replication.ReplicationException;</span>
<span class="source-line-no">047</span><span id="line-47">import org.apache.hadoop.hbase.replication.ReplicationGroupOffset;</span>
<span class="source-line-no">048</span><span id="line-48">import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;</span>
<span class="source-line-no">049</span><span id="line-49">import org.apache.hadoop.hbase.replication.ReplicationQueueId;</span>
<span class="source-line-no">050</span><span id="line-50">import org.apache.hadoop.hbase.replication.ReplicationQueueStorage;</span>
<span class="source-line-no">051</span><span id="line-51">import org.apache.hadoop.hbase.replication.ReplicationStorageFactory;</span>
<span class="source-line-no">052</span><span id="line-52">import org.apache.hadoop.hbase.testclassification.MediumTests;</span>
<span class="source-line-no">053</span><span id="line-53">import org.apache.hadoop.hbase.testclassification.ReplicationTests;</span>
<span class="source-line-no">054</span><span id="line-54">import org.apache.hadoop.hbase.util.hbck.HbckTestingUtil;</span>
<span class="source-line-no">055</span><span id="line-55">import org.junit.AfterClass;</span>
<span class="source-line-no">056</span><span id="line-56">import org.junit.BeforeClass;</span>
<span class="source-line-no">057</span><span id="line-57">import org.junit.ClassRule;</span>
<span class="source-line-no">058</span><span id="line-58">import org.junit.Test;</span>
<span class="source-line-no">059</span><span id="line-59">import org.junit.experimental.categories.Category;</span>
<span class="source-line-no">060</span><span id="line-60"></span>
<span class="source-line-no">061</span><span id="line-61">import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;</span>
<span class="source-line-no">062</span><span id="line-62"></span>
<span class="source-line-no">063</span><span id="line-63">@Category({ ReplicationTests.class, MediumTests.class })</span>
<span class="source-line-no">064</span><span id="line-64">public class TestHBaseFsckCleanReplicationBarriers {</span>
<span class="source-line-no">065</span><span id="line-65"> @ClassRule</span>
<span class="source-line-no">066</span><span id="line-66"> public static final HBaseClassTestRule CLASS_RULE =</span>
<span class="source-line-no">067</span><span id="line-67"> HBaseClassTestRule.forClass(TestHBaseFsckCleanReplicationBarriers.class);</span>
<span class="source-line-no">068</span><span id="line-68"></span>
<span class="source-line-no">069</span><span id="line-69"> private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();</span>
<span class="source-line-no">070</span><span id="line-70"></span>
<span class="source-line-no">071</span><span id="line-71"> private static String PEER_1 = "1", PEER_2 = "2";</span>
<span class="source-line-no">072</span><span id="line-72"></span>
<span class="source-line-no">073</span><span id="line-73"> private static ReplicationQueueStorage QUEUE_STORAGE;</span>
<span class="source-line-no">074</span><span id="line-74"></span>
<span class="source-line-no">075</span><span id="line-75"> private static String WAL_FILE_NAME = "test.wal";</span>
<span class="source-line-no">076</span><span id="line-76"></span>
<span class="source-line-no">077</span><span id="line-77"> private static String TABLE_NAME = "test";</span>
<span class="source-line-no">078</span><span id="line-78"></span>
<span class="source-line-no">079</span><span id="line-79"> private static String COLUMN_FAMILY = "info";</span>
<span class="source-line-no">080</span><span id="line-80"></span>
<span class="source-line-no">081</span><span id="line-81"> @BeforeClass</span>
<span class="source-line-no">082</span><span id="line-82"> public static void setUp() throws Exception {</span>
<span class="source-line-no">083</span><span id="line-83"> UTIL.startMiniCluster(1);</span>
<span class="source-line-no">084</span><span id="line-84"> QUEUE_STORAGE = ReplicationStorageFactory.getReplicationQueueStorage(UTIL.getConnection(),</span>
<span class="source-line-no">085</span><span id="line-85"> UTIL.getConfiguration());</span>
<span class="source-line-no">086</span><span id="line-86"> createPeer();</span>
<span class="source-line-no">087</span><span id="line-87"> }</span>
<span class="source-line-no">088</span><span id="line-88"></span>
<span class="source-line-no">089</span><span id="line-89"> @AfterClass</span>
<span class="source-line-no">090</span><span id="line-90"> public static void tearDown() throws Exception {</span>
<span class="source-line-no">091</span><span id="line-91"> UTIL.shutdownMiniCluster();</span>
<span class="source-line-no">092</span><span id="line-92"> }</span>
<span class="source-line-no">093</span><span id="line-93"></span>
<span class="source-line-no">094</span><span id="line-94"> @Test</span>
<span class="source-line-no">095</span><span id="line-95"> public void testCleanReplicationBarrierWithNonExistTable()</span>
<span class="source-line-no">096</span><span id="line-96"> throws ClassNotFoundException, IOException {</span>
<span class="source-line-no">097</span><span id="line-97"> TableName tableName = TableName.valueOf(TABLE_NAME + "_non");</span>
<span class="source-line-no">098</span><span id="line-98"> boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);</span>
<span class="source-line-no">099</span><span id="line-99"> assertFalse(cleaned);</span>
<span class="source-line-no">100</span><span id="line-100"> }</span>
<span class="source-line-no">101</span><span id="line-101"></span>
<span class="source-line-no">102</span><span id="line-102"> @Test</span>
<span class="source-line-no">103</span><span id="line-103"> public void testCleanReplicationBarrierWithDeletedTable() throws Exception {</span>
<span class="source-line-no">104</span><span id="line-104"> TableName tableName = TableName.valueOf(TABLE_NAME + "_deleted");</span>
<span class="source-line-no">105</span><span id="line-105"> List&lt;RegionInfo&gt; regionInfos = new ArrayList&lt;&gt;();</span>
<span class="source-line-no">106</span><span id="line-106"> // only write some barriers into meta table</span>
<span class="source-line-no">107</span><span id="line-107"></span>
<span class="source-line-no">108</span><span id="line-108"> for (int i = 0; i &lt; 110; i++) {</span>
<span class="source-line-no">109</span><span id="line-109"> RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes(i))</span>
<span class="source-line-no">110</span><span id="line-110"> .setEndKey(Bytes.toBytes(i + 1)).build();</span>
<span class="source-line-no">111</span><span id="line-111"> regionInfos.add(regionInfo);</span>
<span class="source-line-no">112</span><span id="line-112"> addStateAndBarrier(regionInfo, RegionState.State.OPEN, 10, 100);</span>
<span class="source-line-no">113</span><span id="line-113"> updatePushedSeqId(regionInfo, 10);</span>
<span class="source-line-no">114</span><span id="line-114"> assertEquals("check if there is lastPushedId", 10,</span>
<span class="source-line-no">115</span><span id="line-115"> QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_1));</span>
<span class="source-line-no">116</span><span id="line-116"> assertEquals("check if there is lastPushedId", 10,</span>
<span class="source-line-no">117</span><span id="line-117"> QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_2));</span>
<span class="source-line-no">118</span><span id="line-118"> }</span>
<span class="source-line-no">119</span><span id="line-119"> Scan barrierScan = new Scan();</span>
<span class="source-line-no">120</span><span id="line-120"> barrierScan.setCaching(100);</span>
<span class="source-line-no">121</span><span id="line-121"> barrierScan.addFamily(HConstants.REPLICATION_BARRIER_FAMILY);</span>
<span class="source-line-no">122</span><span id="line-122"> barrierScan</span>
<span class="source-line-no">123</span><span id="line-123"> .withStartRow(ClientMetaTableAccessor.getTableStartRowForMeta(tableName,</span>
<span class="source-line-no">124</span><span id="line-124"> ClientMetaTableAccessor.QueryType.REGION))</span>
<span class="source-line-no">125</span><span id="line-125"> .withStopRow(ClientMetaTableAccessor.getTableStopRowForMeta(tableName,</span>
<span class="source-line-no">126</span><span id="line-126"> ClientMetaTableAccessor.QueryType.REGION));</span>
<span class="source-line-no">127</span><span id="line-127"> Result result;</span>
<span class="source-line-no">128</span><span id="line-128"> try (ResultScanner scanner =</span>
<span class="source-line-no">129</span><span id="line-129"> MetaTableAccessor.getMetaHTable(UTIL.getConnection()).getScanner(barrierScan)) {</span>
<span class="source-line-no">130</span><span id="line-130"> while ((result = scanner.next()) != null) {</span>
<span class="source-line-no">131</span><span id="line-131"> assertTrue(ReplicationBarrierFamilyFormat.getReplicationBarriers(result).length &gt; 0);</span>
<span class="source-line-no">132</span><span id="line-132"> }</span>
<span class="source-line-no">133</span><span id="line-133"> }</span>
<span class="source-line-no">134</span><span id="line-134"> boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);</span>
<span class="source-line-no">135</span><span id="line-135"> assertTrue(cleaned);</span>
<span class="source-line-no">136</span><span id="line-136"> for (RegionInfo regionInfo : regionInfos) {</span>
<span class="source-line-no">137</span><span id="line-137"> assertEquals("check if there is lastPushedId", -1,</span>
<span class="source-line-no">138</span><span id="line-138"> QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_1));</span>
<span class="source-line-no">139</span><span id="line-139"> assertEquals("check if there is lastPushedId", -1,</span>
<span class="source-line-no">140</span><span id="line-140"> QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_2));</span>
<span class="source-line-no">141</span><span id="line-141"> }</span>
<span class="source-line-no">142</span><span id="line-142"> cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);</span>
<span class="source-line-no">143</span><span id="line-143"> assertFalse(cleaned);</span>
<span class="source-line-no">144</span><span id="line-144"> for (RegionInfo region : regionInfos) {</span>
<span class="source-line-no">145</span><span id="line-145"> assertEquals(0, ReplicationBarrierFamilyFormat.getReplicationBarriers(UTIL.getConnection(),</span>
<span class="source-line-no">146</span><span id="line-146"> region.getRegionName()).length);</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"></span>
<span class="source-line-no">150</span><span id="line-150"> @Test</span>
<span class="source-line-no">151</span><span id="line-151"> public void testCleanReplicationBarrierWithExistTable() throws Exception {</span>
<span class="source-line-no">152</span><span id="line-152"> TableName tableName = TableName.valueOf(TABLE_NAME);</span>
<span class="source-line-no">153</span><span id="line-153"> String cf = COLUMN_FAMILY;</span>
<span class="source-line-no">154</span><span id="line-154"> TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)</span>
<span class="source-line-no">155</span><span id="line-155"> .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build())</span>
<span class="source-line-no">156</span><span id="line-156"> .setReplicationScope(HConstants.REPLICATION_SCOPE_LOCAL).build();</span>
<span class="source-line-no">157</span><span id="line-157"> UTIL.createTable(tableDescriptor, Bytes.split(Bytes.toBytes(1), Bytes.toBytes(256), 123));</span>
<span class="source-line-no">158</span><span id="line-158"> assertTrue(UTIL.getAdmin().getRegions(tableName).size() &gt; 0);</span>
<span class="source-line-no">159</span><span id="line-159"> for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {</span>
<span class="source-line-no">160</span><span id="line-160"> addStateAndBarrier(region, RegionState.State.OFFLINE, 10, 100);</span>
<span class="source-line-no">161</span><span id="line-161"> updatePushedSeqId(region, 10);</span>
<span class="source-line-no">162</span><span id="line-162"> assertEquals("check if there is lastPushedId", 10,</span>
<span class="source-line-no">163</span><span id="line-163"> QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_1));</span>
<span class="source-line-no">164</span><span id="line-164"> assertEquals("check if there is lastPushedId", 10,</span>
<span class="source-line-no">165</span><span id="line-165"> QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_2));</span>
<span class="source-line-no">166</span><span id="line-166"> }</span>
<span class="source-line-no">167</span><span id="line-167"> boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);</span>
<span class="source-line-no">168</span><span id="line-168"> assertTrue(cleaned);</span>
<span class="source-line-no">169</span><span id="line-169"> for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {</span>
<span class="source-line-no">170</span><span id="line-170"> assertEquals("check if there is lastPushedId", -1,</span>
<span class="source-line-no">171</span><span id="line-171"> QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_1));</span>
<span class="source-line-no">172</span><span id="line-172"> assertEquals("check if there is lastPushedId", -1,</span>
<span class="source-line-no">173</span><span id="line-173"> QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_2));</span>
<span class="source-line-no">174</span><span id="line-174"> }</span>
<span class="source-line-no">175</span><span id="line-175"> cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);</span>
<span class="source-line-no">176</span><span id="line-176"> assertFalse(cleaned);</span>
<span class="source-line-no">177</span><span id="line-177"> for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {</span>
<span class="source-line-no">178</span><span id="line-178"> assertEquals(0, ReplicationBarrierFamilyFormat.getReplicationBarriers(UTIL.getConnection(),</span>
<span class="source-line-no">179</span><span id="line-179"> region.getRegionName()).length);</span>
<span class="source-line-no">180</span><span id="line-180"> }</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"> public static void createPeer() throws IOException {</span>
<span class="source-line-no">184</span><span id="line-184"> ReplicationPeerConfig rpc = ReplicationPeerConfig.newBuilder()</span>
<span class="source-line-no">185</span><span id="line-185"> .setClusterKey(UTIL.getZkConnectionURI() + "-test").setSerial(true).build();</span>
<span class="source-line-no">186</span><span id="line-186"> UTIL.getAdmin().addReplicationPeer(PEER_1, rpc);</span>
<span class="source-line-no">187</span><span id="line-187"> UTIL.getAdmin().addReplicationPeer(PEER_2, rpc);</span>
<span class="source-line-no">188</span><span id="line-188"> }</span>
<span class="source-line-no">189</span><span id="line-189"></span>
<span class="source-line-no">190</span><span id="line-190"> private void addStateAndBarrier(RegionInfo region, RegionState.State state, long... barriers)</span>
<span class="source-line-no">191</span><span id="line-191"> throws IOException {</span>
<span class="source-line-no">192</span><span id="line-192"> Put put = new Put(region.getRegionName(), EnvironmentEdgeManager.currentTime());</span>
<span class="source-line-no">193</span><span id="line-193"> if (state != null) {</span>
<span class="source-line-no">194</span><span id="line-194"> put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,</span>
<span class="source-line-no">195</span><span id="line-195"> Bytes.toBytes(state.name()));</span>
<span class="source-line-no">196</span><span id="line-196"> }</span>
<span class="source-line-no">197</span><span id="line-197"> for (int i = 0; i &lt; barriers.length; i++) {</span>
<span class="source-line-no">198</span><span id="line-198"> put.addColumn(HConstants.REPLICATION_BARRIER_FAMILY, HConstants.SEQNUM_QUALIFIER,</span>
<span class="source-line-no">199</span><span id="line-199"> put.getTimestamp() - barriers.length + i, Bytes.toBytes(barriers[i]));</span>
<span class="source-line-no">200</span><span id="line-200"> }</span>
<span class="source-line-no">201</span><span id="line-201"> try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {</span>
<span class="source-line-no">202</span><span id="line-202"> table.put(put);</span>
<span class="source-line-no">203</span><span id="line-203"> }</span>
<span class="source-line-no">204</span><span id="line-204"> }</span>
<span class="source-line-no">205</span><span id="line-205"></span>
<span class="source-line-no">206</span><span id="line-206"> private void updatePushedSeqId(RegionInfo region, long seqId) throws ReplicationException {</span>
<span class="source-line-no">207</span><span id="line-207"> ServerName sn = UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName();</span>
<span class="source-line-no">208</span><span id="line-208"> QUEUE_STORAGE.setOffset(new ReplicationQueueId(sn, PEER_1), "",</span>
<span class="source-line-no">209</span><span id="line-209"> new ReplicationGroupOffset(WAL_FILE_NAME, 10),</span>
<span class="source-line-no">210</span><span id="line-210"> ImmutableMap.of(region.getEncodedName(), seqId));</span>
<span class="source-line-no">211</span><span id="line-211"> QUEUE_STORAGE.setOffset(new ReplicationQueueId(sn, PEER_2), "",</span>
<span class="source-line-no">212</span><span id="line-212"> new ReplicationGroupOffset(WAL_FILE_NAME, 10),</span>
<span class="source-line-no">213</span><span id="line-213"> ImmutableMap.of(region.getEncodedName(), seqId));</span>
<span class="source-line-no">214</span><span id="line-214"> }</span>
<span class="source-line-no">215</span><span id="line-215">}</span>
</pre>
</div>
</main>
</body>
</html>