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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.List;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.testclassification.LargeTests;
029import org.apache.hadoop.util.ToolRunner;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Tag(LargeTests.TAG)
036public class TestFullBackupSetRestoreSet extends TestBackupBase {
037
038  private static final Logger LOG = LoggerFactory.getLogger(TestFullBackupSetRestoreSet.class);
039
040  @Test
041  public void testFullRestoreSetToOtherTable() throws Exception {
042
043    LOG.info("Test full restore set");
044
045    // Create set
046    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
047      String name = "name";
048      table.addToBackupSet(name, new String[] { table1.getNameAsString() });
049      List<TableName> names = table.describeBackupSet(name);
050
051      assertNotNull(names);
052      assertEquals(1, names.size());
053      assertTrue(names.get(0).equals(table1));
054
055      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
056      // Run backup
057      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
058      assertEquals(0, ret);
059      List<BackupInfo> backups = table.getBackupHistory();
060      assertEquals(1, backups.size());
061      String backupId = backups.get(0).getBackupId();
062      assertTrue(checkSucceeded(backupId));
063
064      LOG.info("backup complete");
065
066      // Restore from set into other table
067      args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-m",
068        table1_restore.getNameAsString(), "-o" };
069      // Run backup
070      ret = ToolRunner.run(conf1, new RestoreDriver(), args);
071      assertEquals(0, ret);
072      Admin hba = TEST_UTIL.getAdmin();
073      assertTrue(hba.tableExists(table1_restore));
074      // Verify number of rows in both tables
075      assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore));
076      TEST_UTIL.deleteTable(table1_restore);
077      LOG.info("restore into other table is complete");
078      hba.close();
079    }
080  }
081
082  @Test
083  public void testFullRestoreSetToSameTable() throws Exception {
084
085    LOG.info("Test full restore set to same table");
086
087    // Create set
088    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
089      String name = "name1";
090      table.addToBackupSet(name, new String[] { table1.getNameAsString() });
091      List<TableName> names = table.describeBackupSet(name);
092
093      assertNotNull(names);
094      assertEquals(1, names.size());
095      assertTrue(names.get(0).equals(table1));
096
097      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-s", name };
098      // Run backup
099      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
100      assertEquals(0, ret);
101      List<BackupInfo> backups = table.getBackupHistory();
102      String backupId = backups.get(0).getBackupId();
103      assertTrue(checkSucceeded(backupId));
104
105      LOG.info("backup complete");
106      int count = TEST_UTIL.countRows(table1);
107      TEST_UTIL.deleteTable(table1);
108
109      // Restore from set into other table
110      args = new String[] { BACKUP_ROOT_DIR, backupId, "-s", name, "-o" };
111      // Run backup
112      ret = ToolRunner.run(conf1, new RestoreDriver(), args);
113      assertEquals(0, ret);
114      Admin hba = TEST_UTIL.getAdmin();
115      assertTrue(hba.tableExists(table1));
116      // Verify number of rows in both tables
117      assertEquals(count, TEST_UTIL.countRows(table1));
118      LOG.info("restore into same table is complete");
119      hba.close();
120
121    }
122
123  }
124
125}