blob: 19bc0aad8c749987285c9bec1160455e4933f930 [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.backup, class: TestIncrementalBackupMergeWithFailures, class: BackupMergeJobWithFailures">
<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.backup;</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.apache.hadoop.hbase.backup.util.BackupUtils.succeeded;</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.commons.lang3.StringUtils;</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.HBaseClassTestRule;</span>
<span class="source-line-no">032</span><span id="line-32">import org.apache.hadoop.hbase.TableName;</span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hadoop.hbase.backup.impl.BackupCommands;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hadoop.hbase.backup.mapreduce.MapReduceBackupMergeJob;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hadoop.hbase.backup.mapreduce.MapReduceHFileSplitterJob;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.hadoop.hbase.backup.util.BackupUtils;</span>
<span class="source-line-no">039</span><span id="line-39">import org.apache.hadoop.hbase.client.Admin;</span>
<span class="source-line-no">040</span><span id="line-40">import org.apache.hadoop.hbase.client.Connection;</span>
<span class="source-line-no">041</span><span id="line-41">import org.apache.hadoop.hbase.client.ConnectionFactory;</span>
<span class="source-line-no">042</span><span id="line-42">import org.apache.hadoop.hbase.client.Table;</span>
<span class="source-line-no">043</span><span id="line-43">import org.apache.hadoop.hbase.testclassification.LargeTests;</span>
<span class="source-line-no">044</span><span id="line-44">import org.apache.hadoop.hbase.util.Pair;</span>
<span class="source-line-no">045</span><span id="line-45">import org.junit.Assert;</span>
<span class="source-line-no">046</span><span id="line-46">import org.junit.ClassRule;</span>
<span class="source-line-no">047</span><span id="line-47">import org.junit.Test;</span>
<span class="source-line-no">048</span><span id="line-48">import org.junit.experimental.categories.Category;</span>
<span class="source-line-no">049</span><span id="line-49">import org.slf4j.Logger;</span>
<span class="source-line-no">050</span><span id="line-50">import org.slf4j.LoggerFactory;</span>
<span class="source-line-no">051</span><span id="line-51"></span>
<span class="source-line-no">052</span><span id="line-52">import org.apache.hbase.thirdparty.com.google.common.collect.Lists;</span>
<span class="source-line-no">053</span><span id="line-53"></span>
<span class="source-line-no">054</span><span id="line-54">@Category(LargeTests.class)</span>
<span class="source-line-no">055</span><span id="line-55">public class TestIncrementalBackupMergeWithFailures extends TestBackupBase {</span>
<span class="source-line-no">056</span><span id="line-56"></span>
<span class="source-line-no">057</span><span id="line-57"> @ClassRule</span>
<span class="source-line-no">058</span><span id="line-58"> public static final HBaseClassTestRule CLASS_RULE =</span>
<span class="source-line-no">059</span><span id="line-59"> HBaseClassTestRule.forClass(TestIncrementalBackupMergeWithFailures.class);</span>
<span class="source-line-no">060</span><span id="line-60"></span>
<span class="source-line-no">061</span><span id="line-61"> private static final Logger LOG =</span>
<span class="source-line-no">062</span><span id="line-62"> LoggerFactory.getLogger(TestIncrementalBackupMergeWithFailures.class);</span>
<span class="source-line-no">063</span><span id="line-63"></span>
<span class="source-line-no">064</span><span id="line-64"> enum FailurePhase {</span>
<span class="source-line-no">065</span><span id="line-65"> PHASE1,</span>
<span class="source-line-no">066</span><span id="line-66"> PHASE2,</span>
<span class="source-line-no">067</span><span id="line-67"> PHASE3,</span>
<span class="source-line-no">068</span><span id="line-68"> PHASE4</span>
<span class="source-line-no">069</span><span id="line-69"> }</span>
<span class="source-line-no">070</span><span id="line-70"></span>
<span class="source-line-no">071</span><span id="line-71"> public final static String FAILURE_PHASE_KEY = "failurePhase";</span>
<span class="source-line-no">072</span><span id="line-72"></span>
<span class="source-line-no">073</span><span id="line-73"> static class BackupMergeJobWithFailures extends MapReduceBackupMergeJob {</span>
<span class="source-line-no">074</span><span id="line-74"> FailurePhase failurePhase;</span>
<span class="source-line-no">075</span><span id="line-75"></span>
<span class="source-line-no">076</span><span id="line-76"> @Override</span>
<span class="source-line-no">077</span><span id="line-77"> public void setConf(Configuration conf) {</span>
<span class="source-line-no">078</span><span id="line-78"> super.setConf(conf);</span>
<span class="source-line-no">079</span><span id="line-79"> String val = conf.get(FAILURE_PHASE_KEY);</span>
<span class="source-line-no">080</span><span id="line-80"> if (val != null) {</span>
<span class="source-line-no">081</span><span id="line-81"> failurePhase = FailurePhase.valueOf(val);</span>
<span class="source-line-no">082</span><span id="line-82"> } else {</span>
<span class="source-line-no">083</span><span id="line-83"> Assert.fail("Failure phase is not set");</span>
<span class="source-line-no">084</span><span id="line-84"> }</span>
<span class="source-line-no">085</span><span id="line-85"> }</span>
<span class="source-line-no">086</span><span id="line-86"></span>
<span class="source-line-no">087</span><span id="line-87"> /**</span>
<span class="source-line-no">088</span><span id="line-88"> * This is the exact copy of parent's run() with injections of different types of failures</span>
<span class="source-line-no">089</span><span id="line-89"> */</span>
<span class="source-line-no">090</span><span id="line-90"> @Override</span>
<span class="source-line-no">091</span><span id="line-91"> public void run(String[] backupIds) throws IOException {</span>
<span class="source-line-no">092</span><span id="line-92"> String bulkOutputConfKey;</span>
<span class="source-line-no">093</span><span id="line-93"></span>
<span class="source-line-no">094</span><span id="line-94"> // TODO : run player on remote cluster</span>
<span class="source-line-no">095</span><span id="line-95"> player = new MapReduceHFileSplitterJob();</span>
<span class="source-line-no">096</span><span id="line-96"> bulkOutputConfKey = MapReduceHFileSplitterJob.BULK_OUTPUT_CONF_KEY;</span>
<span class="source-line-no">097</span><span id="line-97"> // Player reads all files in arbitrary directory structure and creates</span>
<span class="source-line-no">098</span><span id="line-98"> // a Map task for each file</span>
<span class="source-line-no">099</span><span id="line-99"> String bids = StringUtils.join(backupIds, ",");</span>
<span class="source-line-no">100</span><span id="line-100"></span>
<span class="source-line-no">101</span><span id="line-101"> if (LOG.isDebugEnabled()) {</span>
<span class="source-line-no">102</span><span id="line-102"> LOG.debug("Merge backup images " + bids);</span>
<span class="source-line-no">103</span><span id="line-103"> }</span>
<span class="source-line-no">104</span><span id="line-104"></span>
<span class="source-line-no">105</span><span id="line-105"> List&lt;Pair&lt;TableName, Path&gt;&gt; processedTableList = new ArrayList&lt;&gt;();</span>
<span class="source-line-no">106</span><span id="line-106"> boolean finishedTables = false;</span>
<span class="source-line-no">107</span><span id="line-107"> Connection conn = ConnectionFactory.createConnection(getConf());</span>
<span class="source-line-no">108</span><span id="line-108"> BackupSystemTable table = new BackupSystemTable(conn);</span>
<span class="source-line-no">109</span><span id="line-109"> FileSystem fs = FileSystem.get(getConf());</span>
<span class="source-line-no">110</span><span id="line-110"></span>
<span class="source-line-no">111</span><span id="line-111"> try {</span>
<span class="source-line-no">112</span><span id="line-112"> // Start backup exclusive operation</span>
<span class="source-line-no">113</span><span id="line-113"> table.startBackupExclusiveOperation();</span>
<span class="source-line-no">114</span><span id="line-114"> // Start merge operation</span>
<span class="source-line-no">115</span><span id="line-115"> table.startMergeOperation(backupIds);</span>
<span class="source-line-no">116</span><span id="line-116"></span>
<span class="source-line-no">117</span><span id="line-117"> // Select most recent backup id</span>
<span class="source-line-no">118</span><span id="line-118"> String mergedBackupId = BackupUtils.findMostRecentBackupId(backupIds);</span>
<span class="source-line-no">119</span><span id="line-119"></span>
<span class="source-line-no">120</span><span id="line-120"> TableName[] tableNames = getTableNamesInBackupImages(backupIds);</span>
<span class="source-line-no">121</span><span id="line-121"></span>
<span class="source-line-no">122</span><span id="line-122"> BackupInfo bInfo = table.readBackupInfo(backupIds[0]);</span>
<span class="source-line-no">123</span><span id="line-123"> String backupRoot = bInfo.getBackupRootDir();</span>
<span class="source-line-no">124</span><span id="line-124"> // PHASE 1</span>
<span class="source-line-no">125</span><span id="line-125"> checkFailure(FailurePhase.PHASE1);</span>
<span class="source-line-no">126</span><span id="line-126"></span>
<span class="source-line-no">127</span><span id="line-127"> for (int i = 0; i &lt; tableNames.length; i++) {</span>
<span class="source-line-no">128</span><span id="line-128"> LOG.info("Merge backup images for " + tableNames[i]);</span>
<span class="source-line-no">129</span><span id="line-129"></span>
<span class="source-line-no">130</span><span id="line-130"> // Find input directories for table</span>
<span class="source-line-no">131</span><span id="line-131"> Path[] dirPaths = findInputDirectories(fs, backupRoot, tableNames[i], backupIds);</span>
<span class="source-line-no">132</span><span id="line-132"> String dirs = StringUtils.join(dirPaths, ",");</span>
<span class="source-line-no">133</span><span id="line-133"> Path bulkOutputPath = BackupUtils.getBulkOutputDir(</span>
<span class="source-line-no">134</span><span id="line-134"> BackupUtils.getFileNameCompatibleString(tableNames[i]), getConf(), false);</span>
<span class="source-line-no">135</span><span id="line-135"> // Delete content if exists</span>
<span class="source-line-no">136</span><span id="line-136"> if (fs.exists(bulkOutputPath)) {</span>
<span class="source-line-no">137</span><span id="line-137"> if (!fs.delete(bulkOutputPath, true)) {</span>
<span class="source-line-no">138</span><span id="line-138"> LOG.warn("Can not delete: " + bulkOutputPath);</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"> Configuration conf = getConf();</span>
<span class="source-line-no">142</span><span id="line-142"> conf.set(bulkOutputConfKey, bulkOutputPath.toString());</span>
<span class="source-line-no">143</span><span id="line-143"> String[] playerArgs = { dirs, tableNames[i].getNameAsString() };</span>
<span class="source-line-no">144</span><span id="line-144"></span>
<span class="source-line-no">145</span><span id="line-145"> // PHASE 2</span>
<span class="source-line-no">146</span><span id="line-146"> checkFailure(FailurePhase.PHASE2);</span>
<span class="source-line-no">147</span><span id="line-147"> player.setConf(getConf());</span>
<span class="source-line-no">148</span><span id="line-148"> int result = player.run(playerArgs);</span>
<span class="source-line-no">149</span><span id="line-149"> if (succeeded(result)) {</span>
<span class="source-line-no">150</span><span id="line-150"> // Add to processed table list</span>
<span class="source-line-no">151</span><span id="line-151"> processedTableList.add(new Pair&lt;&gt;(tableNames[i], bulkOutputPath));</span>
<span class="source-line-no">152</span><span id="line-152"> } else {</span>
<span class="source-line-no">153</span><span id="line-153"> throw new IOException("Can not merge backup images for " + dirs</span>
<span class="source-line-no">154</span><span id="line-154"> + " (check Hadoop/MR and HBase logs). Player return code =" + result);</span>
<span class="source-line-no">155</span><span id="line-155"> }</span>
<span class="source-line-no">156</span><span id="line-156"> LOG.debug("Merge Job finished:" + result);</span>
<span class="source-line-no">157</span><span id="line-157"> }</span>
<span class="source-line-no">158</span><span id="line-158"> List&lt;TableName&gt; tableList = toTableNameList(processedTableList);</span>
<span class="source-line-no">159</span><span id="line-159"> // PHASE 3</span>
<span class="source-line-no">160</span><span id="line-160"> checkFailure(FailurePhase.PHASE3);</span>
<span class="source-line-no">161</span><span id="line-161"> table.updateProcessedTablesForMerge(tableList);</span>
<span class="source-line-no">162</span><span id="line-162"> finishedTables = true;</span>
<span class="source-line-no">163</span><span id="line-163"></span>
<span class="source-line-no">164</span><span id="line-164"> // (modification of a backup file system)</span>
<span class="source-line-no">165</span><span id="line-165"> // Move existing mergedBackupId data into tmp directory</span>
<span class="source-line-no">166</span><span id="line-166"> // we will need it later in case of a failure</span>
<span class="source-line-no">167</span><span id="line-167"> Path tmpBackupDir =</span>
<span class="source-line-no">168</span><span id="line-168"> HBackupFileSystem.getBackupTmpDirPathForBackupId(backupRoot, mergedBackupId);</span>
<span class="source-line-no">169</span><span id="line-169"> Path backupDirPath = HBackupFileSystem.getBackupPath(backupRoot, mergedBackupId);</span>
<span class="source-line-no">170</span><span id="line-170"> if (!fs.rename(backupDirPath, tmpBackupDir)) {</span>
<span class="source-line-no">171</span><span id="line-171"> throw new IOException("Failed to rename " + backupDirPath + " to " + tmpBackupDir);</span>
<span class="source-line-no">172</span><span id="line-172"> } else {</span>
<span class="source-line-no">173</span><span id="line-173"> LOG.debug("Renamed " + backupDirPath + " to " + tmpBackupDir);</span>
<span class="source-line-no">174</span><span id="line-174"> }</span>
<span class="source-line-no">175</span><span id="line-175"> // Move new data into backup dest</span>
<span class="source-line-no">176</span><span id="line-176"> for (Pair&lt;TableName, Path&gt; tn : processedTableList) {</span>
<span class="source-line-no">177</span><span id="line-177"> moveData(fs, backupRoot, tn.getSecond(), tn.getFirst(), mergedBackupId);</span>
<span class="source-line-no">178</span><span id="line-178"> }</span>
<span class="source-line-no">179</span><span id="line-179"> checkFailure(FailurePhase.PHASE4);</span>
<span class="source-line-no">180</span><span id="line-180"> // Update backup manifest</span>
<span class="source-line-no">181</span><span id="line-181"> List&lt;String&gt; backupsToDelete = getBackupIdsToDelete(backupIds, mergedBackupId);</span>
<span class="source-line-no">182</span><span id="line-182"> updateBackupManifest(tmpBackupDir.getParent().toString(), mergedBackupId, backupsToDelete);</span>
<span class="source-line-no">183</span><span id="line-183"> // Copy meta files back from tmp to backup dir</span>
<span class="source-line-no">184</span><span id="line-184"> copyMetaData(fs, tmpBackupDir, backupDirPath);</span>
<span class="source-line-no">185</span><span id="line-185"> // Delete tmp dir (Rename back during repair)</span>
<span class="source-line-no">186</span><span id="line-186"> if (!fs.delete(tmpBackupDir, true)) {</span>
<span class="source-line-no">187</span><span id="line-187"> // WARN and ignore</span>
<span class="source-line-no">188</span><span id="line-188"> LOG.warn("Could not delete tmp dir: " + tmpBackupDir);</span>
<span class="source-line-no">189</span><span id="line-189"> }</span>
<span class="source-line-no">190</span><span id="line-190"> // Delete old data</span>
<span class="source-line-no">191</span><span id="line-191"> deleteBackupImages(backupsToDelete, conn, fs, backupRoot);</span>
<span class="source-line-no">192</span><span id="line-192"> // Finish merge session</span>
<span class="source-line-no">193</span><span id="line-193"> table.finishMergeOperation();</span>
<span class="source-line-no">194</span><span id="line-194"> // Release lock</span>
<span class="source-line-no">195</span><span id="line-195"> table.finishBackupExclusiveOperation();</span>
<span class="source-line-no">196</span><span id="line-196"> } catch (RuntimeException e) {</span>
<span class="source-line-no">197</span><span id="line-197"> throw e;</span>
<span class="source-line-no">198</span><span id="line-198"> } catch (Exception e) {</span>
<span class="source-line-no">199</span><span id="line-199"> LOG.error(e.toString(), e);</span>
<span class="source-line-no">200</span><span id="line-200"> if (!finishedTables) {</span>
<span class="source-line-no">201</span><span id="line-201"> // cleanup bulk directories and finish merge</span>
<span class="source-line-no">202</span><span id="line-202"> // merge MUST be repeated (no need for repair)</span>
<span class="source-line-no">203</span><span id="line-203"> cleanupBulkLoadDirs(fs, toPathList(processedTableList));</span>
<span class="source-line-no">204</span><span id="line-204"> table.finishMergeOperation();</span>
<span class="source-line-no">205</span><span id="line-205"> table.finishBackupExclusiveOperation();</span>
<span class="source-line-no">206</span><span id="line-206"> throw new IOException("Backup merge operation failed, you should try it again", e);</span>
<span class="source-line-no">207</span><span id="line-207"> } else {</span>
<span class="source-line-no">208</span><span id="line-208"> // backup repair must be run</span>
<span class="source-line-no">209</span><span id="line-209"> throw new IOException(</span>
<span class="source-line-no">210</span><span id="line-210"> "Backup merge operation failed, run backup repair tool to restore system's integrity",</span>
<span class="source-line-no">211</span><span id="line-211"> e);</span>
<span class="source-line-no">212</span><span id="line-212"> }</span>
<span class="source-line-no">213</span><span id="line-213"> } finally {</span>
<span class="source-line-no">214</span><span id="line-214"> table.close();</span>
<span class="source-line-no">215</span><span id="line-215"> conn.close();</span>
<span class="source-line-no">216</span><span id="line-216"> }</span>
<span class="source-line-no">217</span><span id="line-217"> }</span>
<span class="source-line-no">218</span><span id="line-218"></span>
<span class="source-line-no">219</span><span id="line-219"> private void checkFailure(FailurePhase phase) throws IOException {</span>
<span class="source-line-no">220</span><span id="line-220"> if (failurePhase != null &amp;&amp; failurePhase == phase) {</span>
<span class="source-line-no">221</span><span id="line-221"> throw new IOException(phase.toString());</span>
<span class="source-line-no">222</span><span id="line-222"> }</span>
<span class="source-line-no">223</span><span id="line-223"> }</span>
<span class="source-line-no">224</span><span id="line-224"> }</span>
<span class="source-line-no">225</span><span id="line-225"></span>
<span class="source-line-no">226</span><span id="line-226"> @Test</span>
<span class="source-line-no">227</span><span id="line-227"> public void TestIncBackupMergeRestore() throws Exception {</span>
<span class="source-line-no">228</span><span id="line-228"> int ADD_ROWS = 99;</span>
<span class="source-line-no">229</span><span id="line-229"> // #1 - create full backup for all tables</span>
<span class="source-line-no">230</span><span id="line-230"> LOG.info("create full backup image for all tables");</span>
<span class="source-line-no">231</span><span id="line-231"></span>
<span class="source-line-no">232</span><span id="line-232"> List&lt;TableName&gt; tables = Lists.newArrayList(table1, table2);</span>
<span class="source-line-no">233</span><span id="line-233"> // Set custom Merge Job implementation</span>
<span class="source-line-no">234</span><span id="line-234"> conf1.setClass(BackupRestoreFactory.HBASE_BACKUP_MERGE_IMPL_CLASS,</span>
<span class="source-line-no">235</span><span id="line-235"> BackupMergeJobWithFailures.class, BackupMergeJob.class);</span>
<span class="source-line-no">236</span><span id="line-236"></span>
<span class="source-line-no">237</span><span id="line-237"> Connection conn = ConnectionFactory.createConnection(conf1);</span>
<span class="source-line-no">238</span><span id="line-238"></span>
<span class="source-line-no">239</span><span id="line-239"> Admin admin = conn.getAdmin();</span>
<span class="source-line-no">240</span><span id="line-240"> BackupAdminImpl client = new BackupAdminImpl(conn);</span>
<span class="source-line-no">241</span><span id="line-241"></span>
<span class="source-line-no">242</span><span id="line-242"> BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);</span>
<span class="source-line-no">243</span><span id="line-243"> String backupIdFull = client.backupTables(request);</span>
<span class="source-line-no">244</span><span id="line-244"></span>
<span class="source-line-no">245</span><span id="line-245"> assertTrue(checkSucceeded(backupIdFull));</span>
<span class="source-line-no">246</span><span id="line-246"></span>
<span class="source-line-no">247</span><span id="line-247"> // #2 - insert some data to table1</span>
<span class="source-line-no">248</span><span id="line-248"> Table t1 = insertIntoTable(conn, table1, famName, 1, ADD_ROWS);</span>
<span class="source-line-no">249</span><span id="line-249"> LOG.debug("writing " + ADD_ROWS + " rows to " + table1);</span>
<span class="source-line-no">250</span><span id="line-250"></span>
<span class="source-line-no">251</span><span id="line-251"> Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH + ADD_ROWS);</span>
<span class="source-line-no">252</span><span id="line-252"> t1.close();</span>
<span class="source-line-no">253</span><span id="line-253"> LOG.debug("written " + ADD_ROWS + " rows to " + table1);</span>
<span class="source-line-no">254</span><span id="line-254"></span>
<span class="source-line-no">255</span><span id="line-255"> Table t2 = insertIntoTable(conn, table2, famName, 1, ADD_ROWS);</span>
<span class="source-line-no">256</span><span id="line-256"></span>
<span class="source-line-no">257</span><span id="line-257"> Assert.assertEquals(TEST_UTIL.countRows(t2), NB_ROWS_IN_BATCH + ADD_ROWS);</span>
<span class="source-line-no">258</span><span id="line-258"> t2.close();</span>
<span class="source-line-no">259</span><span id="line-259"> LOG.debug("written " + ADD_ROWS + " rows to " + table2);</span>
<span class="source-line-no">260</span><span id="line-260"></span>
<span class="source-line-no">261</span><span id="line-261"> // #3 - incremental backup for multiple tables</span>
<span class="source-line-no">262</span><span id="line-262"> tables = Lists.newArrayList(table1, table2);</span>
<span class="source-line-no">263</span><span id="line-263"> request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);</span>
<span class="source-line-no">264</span><span id="line-264"> String backupIdIncMultiple = client.backupTables(request);</span>
<span class="source-line-no">265</span><span id="line-265"></span>
<span class="source-line-no">266</span><span id="line-266"> assertTrue(checkSucceeded(backupIdIncMultiple));</span>
<span class="source-line-no">267</span><span id="line-267"></span>
<span class="source-line-no">268</span><span id="line-268"> t1 = insertIntoTable(conn, table1, famName, 2, ADD_ROWS);</span>
<span class="source-line-no">269</span><span id="line-269"> t1.close();</span>
<span class="source-line-no">270</span><span id="line-270"></span>
<span class="source-line-no">271</span><span id="line-271"> t2 = insertIntoTable(conn, table2, famName, 2, ADD_ROWS);</span>
<span class="source-line-no">272</span><span id="line-272"> t2.close();</span>
<span class="source-line-no">273</span><span id="line-273"></span>
<span class="source-line-no">274</span><span id="line-274"> // #3 - incremental backup for multiple tables</span>
<span class="source-line-no">275</span><span id="line-275"> request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);</span>
<span class="source-line-no">276</span><span id="line-276"> String backupIdIncMultiple2 = client.backupTables(request);</span>
<span class="source-line-no">277</span><span id="line-277"> assertTrue(checkSucceeded(backupIdIncMultiple2));</span>
<span class="source-line-no">278</span><span id="line-278"> // #4 Merge backup images with failures</span>
<span class="source-line-no">279</span><span id="line-279"></span>
<span class="source-line-no">280</span><span id="line-280"> for (FailurePhase phase : FailurePhase.values()) {</span>
<span class="source-line-no">281</span><span id="line-281"> Configuration conf = conn.getConfiguration();</span>
<span class="source-line-no">282</span><span id="line-282"></span>
<span class="source-line-no">283</span><span id="line-283"> conf.set(FAILURE_PHASE_KEY, phase.toString());</span>
<span class="source-line-no">284</span><span id="line-284"></span>
<span class="source-line-no">285</span><span id="line-285"> try (BackupAdmin bAdmin = new BackupAdminImpl(conn)) {</span>
<span class="source-line-no">286</span><span id="line-286"> String[] backups = new String[] { backupIdIncMultiple, backupIdIncMultiple2 };</span>
<span class="source-line-no">287</span><span id="line-287"> bAdmin.mergeBackups(backups);</span>
<span class="source-line-no">288</span><span id="line-288"> Assert.fail("Expected IOException");</span>
<span class="source-line-no">289</span><span id="line-289"> } catch (IOException e) {</span>
<span class="source-line-no">290</span><span id="line-290"> BackupSystemTable table = new BackupSystemTable(conn);</span>
<span class="source-line-no">291</span><span id="line-291"> if (phase.ordinal() &lt; FailurePhase.PHASE4.ordinal()) {</span>
<span class="source-line-no">292</span><span id="line-292"> // No need to repair:</span>
<span class="source-line-no">293</span><span id="line-293"> // Both Merge and backup exclusive operations are finished</span>
<span class="source-line-no">294</span><span id="line-294"> assertFalse(table.isMergeInProgress());</span>
<span class="source-line-no">295</span><span id="line-295"> try {</span>
<span class="source-line-no">296</span><span id="line-296"> table.finishBackupExclusiveOperation();</span>
<span class="source-line-no">297</span><span id="line-297"> Assert.fail("IOException is expected");</span>
<span class="source-line-no">298</span><span id="line-298"> } catch (IOException ee) {</span>
<span class="source-line-no">299</span><span id="line-299"> // Expected</span>
<span class="source-line-no">300</span><span id="line-300"> }</span>
<span class="source-line-no">301</span><span id="line-301"> } else {</span>
<span class="source-line-no">302</span><span id="line-302"> // Repair is required</span>
<span class="source-line-no">303</span><span id="line-303"> assertTrue(table.isMergeInProgress());</span>
<span class="source-line-no">304</span><span id="line-304"> try {</span>
<span class="source-line-no">305</span><span id="line-305"> table.startBackupExclusiveOperation();</span>
<span class="source-line-no">306</span><span id="line-306"> Assert.fail("IOException is expected");</span>
<span class="source-line-no">307</span><span id="line-307"> } catch (IOException ee) {</span>
<span class="source-line-no">308</span><span id="line-308"> // Expected - clean up before proceeding</span>
<span class="source-line-no">309</span><span id="line-309"> // table.finishMergeOperation();</span>
<span class="source-line-no">310</span><span id="line-310"> // table.finishBackupExclusiveOperation();</span>
<span class="source-line-no">311</span><span id="line-311"> }</span>
<span class="source-line-no">312</span><span id="line-312"> }</span>
<span class="source-line-no">313</span><span id="line-313"> table.close();</span>
<span class="source-line-no">314</span><span id="line-314"> LOG.debug("Expected :" + e.getMessage());</span>
<span class="source-line-no">315</span><span id="line-315"> }</span>
<span class="source-line-no">316</span><span id="line-316"> }</span>
<span class="source-line-no">317</span><span id="line-317"> // Now merge w/o failures</span>
<span class="source-line-no">318</span><span id="line-318"> Configuration conf = conn.getConfiguration();</span>
<span class="source-line-no">319</span><span id="line-319"> conf.unset(FAILURE_PHASE_KEY);</span>
<span class="source-line-no">320</span><span id="line-320"> conf.unset(BackupRestoreFactory.HBASE_BACKUP_MERGE_IMPL_CLASS);</span>
<span class="source-line-no">321</span><span id="line-321"> // Now run repair</span>
<span class="source-line-no">322</span><span id="line-322"> BackupSystemTable sysTable = new BackupSystemTable(conn);</span>
<span class="source-line-no">323</span><span id="line-323"> BackupCommands.RepairCommand.repairFailedBackupMergeIfAny(conn, sysTable);</span>
<span class="source-line-no">324</span><span id="line-324"> // Now repeat merge</span>
<span class="source-line-no">325</span><span id="line-325"> try (BackupAdmin bAdmin = new BackupAdminImpl(conn)) {</span>
<span class="source-line-no">326</span><span id="line-326"> String[] backups = new String[] { backupIdIncMultiple, backupIdIncMultiple2 };</span>
<span class="source-line-no">327</span><span id="line-327"> bAdmin.mergeBackups(backups);</span>
<span class="source-line-no">328</span><span id="line-328"> }</span>
<span class="source-line-no">329</span><span id="line-329"></span>
<span class="source-line-no">330</span><span id="line-330"> // #6 - restore incremental backup for multiple tables, with overwrite</span>
<span class="source-line-no">331</span><span id="line-331"> TableName[] tablesRestoreIncMultiple = new TableName[] { table1, table2 };</span>
<span class="source-line-no">332</span><span id="line-332"> TableName[] tablesMapIncMultiple = new TableName[] { table1_restore, table2_restore };</span>
<span class="source-line-no">333</span><span id="line-333"> client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple2, false,</span>
<span class="source-line-no">334</span><span id="line-334"> tablesRestoreIncMultiple, tablesMapIncMultiple, true));</span>
<span class="source-line-no">335</span><span id="line-335"></span>
<span class="source-line-no">336</span><span id="line-336"> Table hTable = conn.getTable(table1_restore);</span>
<span class="source-line-no">337</span><span id="line-337"> LOG.debug("After incremental restore: " + hTable.getDescriptor());</span>
<span class="source-line-no">338</span><span id="line-338"> LOG.debug("f1 has " + TEST_UTIL.countRows(hTable, famName) + " rows");</span>
<span class="source-line-no">339</span><span id="line-339"> Assert.assertEquals(TEST_UTIL.countRows(hTable, famName), NB_ROWS_IN_BATCH + 2 * ADD_ROWS);</span>
<span class="source-line-no">340</span><span id="line-340"></span>
<span class="source-line-no">341</span><span id="line-341"> hTable.close();</span>
<span class="source-line-no">342</span><span id="line-342"></span>
<span class="source-line-no">343</span><span id="line-343"> hTable = conn.getTable(table2_restore);</span>
<span class="source-line-no">344</span><span id="line-344"> Assert.assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH + 2 * ADD_ROWS);</span>
<span class="source-line-no">345</span><span id="line-345"> hTable.close();</span>
<span class="source-line-no">346</span><span id="line-346"></span>
<span class="source-line-no">347</span><span id="line-347"> admin.close();</span>
<span class="source-line-no">348</span><span id="line-348"> conn.close();</span>
<span class="source-line-no">349</span><span id="line-349"> }</span>
<span class="source-line-no">350</span><span id="line-350">}</span>
</pre>
</div>
</main>
</body>
</html>