View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.backup;
20  
21  import static org.junit.Assert.*;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
30  import org.apache.hadoop.hbase.client.HBaseAdmin;
31  import org.apache.hadoop.hbase.testclassification.LargeTests;
32  import org.apache.hadoop.util.ToolRunner;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(LargeTests.class)
37  public class TestFullBackupSetRestoreSet extends TestBackupBase {
38  
39    private static final Log LOG = LogFactory.getLog(TestFullBackupSetRestoreSet.class);
40  
41    @Test
42    public void testFullRestoreSetToOtherTable() throws Exception {
43  
44      LOG.info("Test full restore set");
45  
46      // Create set
47      try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
48        String name = "name";
49        table.addToBackupSet(name, new String[] { table1.getNameAsString() });
50        List<TableName> names = table.describeBackupSet(name);
51  
52        assertNotNull(names);
53        assertTrue(names.size() == 1);
54        assertTrue(names.get(0).equals(table1));
55  
56        String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-set", name };
57        // Run backup
58        int ret = ToolRunner.run(conf1, new BackupDriver(), args);
59        assertTrue(ret == 0);
60        ArrayList<BackupInfo> backups = table.getBackupHistory();
61        assertTrue(backups.size() == 1);
62        String backupId = backups.get(0).getBackupId();
63        assertTrue(checkSucceeded(backupId));
64  
65        LOG.info("backup complete");
66  
67        // Restore from set into other table
68        args =
69            new String[] { BACKUP_ROOT_DIR, backupId, "-set", name, table1_restore.getNameAsString(),
70                "-overwrite" };
71        // Run backup
72        ret = ToolRunner.run(conf1, new RestoreDriver(), args);
73        assertTrue(ret == 0);
74        HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
75        assertTrue(hba.tableExists(table1_restore));
76        // Verify number of rows in both tables
77        assertEquals(TEST_UTIL.countRows(table1), TEST_UTIL.countRows(table1_restore));
78        TEST_UTIL.deleteTable(table1_restore);
79        LOG.info("restore into other table is complete");
80        hba.close();
81      }
82    }
83  
84    @Test
85    public void testFullRestoreSetToSameTable() throws Exception {
86  
87      LOG.info("Test full restore set to same table");
88  
89      // Create set
90      try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
91        String name = "name1";
92        table.addToBackupSet(name, new String[] { table1.getNameAsString() });
93        List<TableName> names = table.describeBackupSet(name);
94  
95        assertNotNull(names);
96        assertTrue(names.size() == 1);
97        assertTrue(names.get(0).equals(table1));
98  
99        String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-set", name };
100       // Run backup
101       int ret = ToolRunner.run(conf1, new BackupDriver(), args);
102       assertTrue(ret == 0);
103       ArrayList<BackupInfo> backups = table.getBackupHistory();
104       String backupId = backups.get(0).getBackupId();
105       assertTrue(checkSucceeded(backupId));
106 
107       LOG.info("backup complete");
108       int count = TEST_UTIL.countRows(table1);
109       TEST_UTIL.deleteTable(table1);
110 
111       // Restore from set into other table
112       args = new String[] { BACKUP_ROOT_DIR, backupId, "-set", name, "-overwrite" };
113       // Run backup
114       ret = ToolRunner.run(conf1, new RestoreDriver(), args);
115       assertTrue(ret == 0);
116       HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
117       assertTrue(hba.tableExists(table1));
118       // Verify number of rows in both tables
119       assertEquals(count, TEST_UTIL.countRows(table1));
120       LOG.info("restore into same table is complete");
121       hba.close();
122 
123     }
124 
125   }
126 
127 }