001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.List;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.backup.util.BackupUtils;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.util.ToolRunner;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
039
040@Category(LargeTests.class)
041public class TestFullRestore extends TestBackupBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestFullRestore.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestFullRestore.class);
048
049  /**
050   * Verify that a single table is restored to a new table.
051   *
052   * @throws Exception if doing the backup, restoring it or an operation on the tables fails
053   */
054  @Test
055  public void testFullRestoreSingle() throws Exception {
056    LOG.info("test full restore on a single table empty table");
057
058    List<TableName> tables = Lists.newArrayList(table1);
059    String backupId = fullTableBackup(tables);
060    assertTrue(checkSucceeded(backupId));
061
062    LOG.info("backup complete");
063
064    TableName[] tableset = new TableName[] { table1 };
065    TableName[] tablemap = new TableName[] { table1_restore };
066    BackupAdmin client = getBackupAdmin();
067    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
068      tableset, tablemap, false));
069    Admin hba = TEST_UTIL.getAdmin();
070    assertTrue(hba.tableExists(table1_restore));
071    TEST_UTIL.deleteTable(table1_restore);
072    hba.close();
073  }
074
075  @Test
076  public void testFullRestoreSingleCommand() throws Exception {
077    LOG.info("test full restore on a single table empty table: command-line");
078
079    List<TableName> tables = Lists.newArrayList(table1);
080    String backupId = fullTableBackup(tables);
081    LOG.info("backup complete");
082    assertTrue(checkSucceeded(backupId));
083    // restore <backup_root_path> <backup_id> <tables> [tableMapping]
084    String[] args =
085        new String[] { BACKUP_ROOT_DIR, backupId, "-t", table1.getNameAsString(), "-m",
086            table1_restore.getNameAsString() };
087    // Run backup
088    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
089
090    assertTrue(ret == 0);
091    Admin hba = TEST_UTIL.getAdmin();
092    assertTrue(hba.tableExists(table1_restore));
093    TEST_UTIL.deleteTable(table1_restore);
094    hba.close();
095  }
096
097  @Test
098  public void testFullRestoreCheckCommand() throws Exception {
099    LOG.info("test full restore on a single table: command-line, check only");
100
101    List<TableName> tables = Lists.newArrayList(table1);
102    String backupId = fullTableBackup(tables);
103    LOG.info("backup complete");
104    assertTrue(checkSucceeded(backupId));
105    // restore <backup_root_path> <backup_id> <tables> [tableMapping]
106    String[] args =
107        new String[] { BACKUP_ROOT_DIR, backupId, "-t", table1.getNameAsString(), "-m",
108            table1_restore.getNameAsString(), "-c" };
109    // Run backup
110    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
111    assertTrue(ret == 0);
112    //Verify that table has not been restored
113    Admin hba = TEST_UTIL.getAdmin();
114    assertFalse(hba.tableExists(table1_restore));
115  }
116
117  /**
118   * Verify that multiple tables are restored to new tables.
119   *
120   * @throws Exception if doing the backup, restoring it or an operation on the tables fails
121   */
122  @Test
123  public void testFullRestoreMultiple() throws Exception {
124    LOG.info("create full backup image on multiple tables");
125    List<TableName> tables = Lists.newArrayList(table2, table3);
126    String backupId = fullTableBackup(tables);
127    assertTrue(checkSucceeded(backupId));
128
129    TableName[] restore_tableset = new TableName[] { table2, table3 };
130    TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
131    BackupAdmin client = getBackupAdmin();
132    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
133      restore_tableset, tablemap, false));
134    Admin hba = TEST_UTIL.getAdmin();
135    assertTrue(hba.tableExists(table2_restore));
136    assertTrue(hba.tableExists(table3_restore));
137    TEST_UTIL.deleteTable(table2_restore);
138    TEST_UTIL.deleteTable(table3_restore);
139    hba.close();
140  }
141
142  /**
143   * Verify that multiple tables are restored to new tables.
144   *
145   * @throws Exception if doing the backup, restoring it or an operation on the tables fails
146   */
147  @Test
148  public void testFullRestoreMultipleCommand() throws Exception {
149    LOG.info("create full backup image on multiple tables: command-line");
150    List<TableName> tables = Lists.newArrayList(table2, table3);
151    String backupId = fullTableBackup(tables);
152    assertTrue(checkSucceeded(backupId));
153
154    TableName[] restore_tableset = new TableName[] { table2, table3 };
155    TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
156
157    // restore <backup_root_path> <backup_id> <tables> [tableMapping]
158    String[] args =
159        new String[] { BACKUP_ROOT_DIR, backupId, "-t", StringUtils.join(restore_tableset, ","),
160          "-m", StringUtils.join(tablemap, ",") };
161    // Run backup
162    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
163
164    assertTrue(ret == 0);
165    Admin hba = TEST_UTIL.getAdmin();
166    assertTrue(hba.tableExists(table2_restore));
167    assertTrue(hba.tableExists(table3_restore));
168    TEST_UTIL.deleteTable(table2_restore);
169    TEST_UTIL.deleteTable(table3_restore);
170    hba.close();
171  }
172
173  /**
174   * Verify that a single table is restored using overwrite.
175   *
176   * @throws Exception if doing the backup or restoring it fails
177   */
178  @Test
179  public void testFullRestoreSingleOverwrite() throws Exception {
180    LOG.info("test full restore on a single table empty table");
181    List<TableName> tables = Lists.newArrayList(table1);
182    String backupId = fullTableBackup(tables);
183    assertTrue(checkSucceeded(backupId));
184
185    LOG.info("backup complete");
186
187    TableName[] tableset = new TableName[] { table1 };
188    BackupAdmin client = getBackupAdmin();
189    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
190      tableset, null, true));
191  }
192
193  /**
194   * Verify that a single table is restored using overwrite.
195   *
196   * @throws Exception if doing the backup or an operation on the tables fails
197   */
198  @Test
199  public void testFullRestoreSingleOverwriteCommand() throws Exception {
200    LOG.info("test full restore on a single table empty table: command-line");
201    List<TableName> tables = Lists.newArrayList(table1);
202    String backupId = fullTableBackup(tables);
203    assertTrue(checkSucceeded(backupId));
204    LOG.info("backup complete");
205    TableName[] tableset = new TableName[] { table1 };
206    // restore <backup_root_path> <backup_id> <tables> [tableMapping]
207    String[] args =
208        new String[] { BACKUP_ROOT_DIR, backupId, "-t", StringUtils.join(tableset, ","), "-o" };
209    // Run restore
210    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
211    assertTrue(ret == 0);
212
213    Admin hba = TEST_UTIL.getAdmin();
214    assertTrue(hba.tableExists(table1));
215    hba.close();
216  }
217
218  /**
219   * Verify that multiple tables are restored to new tables using overwrite.
220   *
221   * @throws Exception if doing the backup or restoring it fails
222   */
223  @Test
224  public void testFullRestoreMultipleOverwrite() throws Exception {
225    LOG.info("create full backup image on multiple tables");
226
227    List<TableName> tables = Lists.newArrayList(table2, table3);
228    String backupId = fullTableBackup(tables);
229    assertTrue(checkSucceeded(backupId));
230
231    TableName[] restore_tableset = new TableName[] { table2, table3 };
232    BackupAdmin client = getBackupAdmin();
233    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
234      restore_tableset, null, true));
235  }
236
237  /**
238   * Verify that multiple tables are restored to new tables using overwrite.
239   *
240   * @throws Exception if doing the backup or an operation on the tables fails
241   */
242  @Test
243  public void testFullRestoreMultipleOverwriteCommand() throws Exception {
244    LOG.info("create full backup image on multiple tables: command-line");
245
246    List<TableName> tables = Lists.newArrayList(table2, table3);
247    String backupId = fullTableBackup(tables);
248    assertTrue(checkSucceeded(backupId));
249
250    TableName[] restore_tableset = new TableName[] { table2, table3 };
251    // restore <backup_root_path> <backup_id> <tables> [tableMapping]
252    String[] args =
253        new String[] { BACKUP_ROOT_DIR, backupId, "-t",
254        StringUtils.join(restore_tableset, ","), "-o" };
255    // Run backup
256    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
257
258    assertTrue(ret == 0);
259    Admin hba = TEST_UTIL.getAdmin();
260    assertTrue(hba.tableExists(table2));
261    assertTrue(hba.tableExists(table3));
262    hba.close();
263  }
264
265  /**
266   * Verify that restore fails on a single table that does not exist.
267   *
268   * @throws Exception if doing the backup or restoring it fails
269   */
270  @Test(expected = IOException.class)
271  public void testFullRestoreSingleDNE() throws Exception {
272    LOG.info("test restore fails on a single table that does not exist");
273    List<TableName> tables = Lists.newArrayList(table1);
274    String backupId = fullTableBackup(tables);
275    assertTrue(checkSucceeded(backupId));
276
277    LOG.info("backup complete");
278
279    TableName[] tableset = new TableName[] { TableName.valueOf("faketable") };
280    TableName[] tablemap = new TableName[] { table1_restore };
281    BackupAdmin client = getBackupAdmin();
282    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
283      tableset, tablemap, false));
284  }
285
286  /**
287   * Verify that restore fails on a single table that does not exist.
288   *
289   * @throws Exception if doing the backup or restoring it fails
290   */
291  @Test
292  public void testFullRestoreSingleDNECommand() throws Exception {
293    LOG.info("test restore fails on a single table that does not exist: command-line");
294    List<TableName> tables = Lists.newArrayList(table1);
295    String backupId = fullTableBackup(tables);
296    assertTrue(checkSucceeded(backupId));
297
298    LOG.info("backup complete");
299
300    TableName[] tableset = new TableName[] { TableName.valueOf("faketable") };
301    TableName[] tablemap = new TableName[] { table1_restore };
302    String[] args =
303        new String[] { BACKUP_ROOT_DIR, backupId, StringUtils.join(tableset, ","), "-m",
304            StringUtils.join(tablemap, ",") };
305    // Run restore
306    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
307    assertTrue(ret != 0);
308  }
309
310  /**
311   * Verify that restore fails on multiple tables that do not exist.
312   *
313   * @throws Exception if doing the backup or restoring it fails
314   */
315  @Test(expected = IOException.class)
316  public void testFullRestoreMultipleDNE() throws Exception {
317    LOG.info("test restore fails on multiple tables that do not exist");
318
319    List<TableName> tables = Lists.newArrayList(table2, table3);
320    String backupId = fullTableBackup(tables);
321    assertTrue(checkSucceeded(backupId));
322
323    TableName[] restore_tableset =
324        new TableName[] { TableName.valueOf("faketable1"), TableName.valueOf("faketable2") };
325    TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
326    BackupAdmin client = getBackupAdmin();
327    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
328      restore_tableset, tablemap, false));
329  }
330
331  /**
332   * Verify that restore fails on multiple tables that do not exist.
333   *
334   * @throws Exception if doing the backup or restoring it fails
335   */
336  @Test
337  public void testFullRestoreMultipleDNECommand() throws Exception {
338    LOG.info("test restore fails on multiple tables that do not exist: command-line");
339
340    List<TableName> tables = Lists.newArrayList(table2, table3);
341    String backupId = fullTableBackup(tables);
342    assertTrue(checkSucceeded(backupId));
343
344    TableName[] restore_tableset =
345        new TableName[] { TableName.valueOf("faketable1"), TableName.valueOf("faketable2") };
346    TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
347    String[] args =
348        new String[] { BACKUP_ROOT_DIR, backupId, StringUtils.join(restore_tableset, ","), "-m",
349            StringUtils.join(tablemap, ",") };
350    // Run restore
351    int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
352    assertTrue(ret != 0);
353  }
354}