View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.backup;
13  
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.IOException;
17  import java.util.List;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.TableName;
23  import org.apache.hadoop.hbase.client.BackupAdmin;
24  import org.apache.hadoop.hbase.client.HBaseAdmin;
25  import org.apache.hadoop.hbase.testclassification.LargeTests;
26  import org.apache.hadoop.util.ToolRunner;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  import com.google.common.collect.Lists;
31  
32  @Category(LargeTests.class)
33  public class TestFullRestore extends TestBackupBase {
34  
35    private static final Log LOG = LogFactory.getLog(TestFullRestore.class);
36  
37    /**
38     * Verify that a single table is restored to a new table
39     * @throws Exception
40     */
41    @Test
42    public void testFullRestoreSingle() throws Exception {
43  
44      LOG.info("test full restore on a single table empty table");
45  
46      List<TableName> tables = Lists.newArrayList(table1);
47      String backupId = fullTableBackup(tables);
48      assertTrue(checkSucceeded(backupId));
49      
50      LOG.info("backup complete");
51  
52      TableName[] tableset = new TableName[] { table1 };
53      TableName[] tablemap = new TableName[] { table1_restore };
54      BackupAdmin client = getBackupAdmin();
55      client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,  tableset, tablemap, false));
56      HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
57      assertTrue(hba.tableExists(table1_restore));
58      TEST_UTIL.deleteTable(table1_restore);
59      hba.close();
60    }
61  
62    
63    @Test
64    public void testFullRestoreSingleCommand() throws Exception {
65  
66      LOG.info("test full restore on a single table empty table: command-line");
67  
68      List<TableName> tables = Lists.newArrayList(table1);
69      String backupId = fullTableBackup(tables);
70      LOG.info("backup complete");
71      assertTrue(checkSucceeded(backupId));
72      //restore <backup_root_path> <backup_id> <tables> [tableMapping]
73      String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
74          table1.getNameAsString(), table1_restore.getNameAsString() }; 
75      // Run backup
76      int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
77  
78      assertTrue(ret==0);
79      HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
80      assertTrue(hba.tableExists(table1_restore));
81      TEST_UTIL.deleteTable(table1_restore);
82      hba.close();
83    }
84    
85    /**
86     * Verify that multiple tables are restored to new tables.
87     * @throws Exception
88     */
89    @Test
90    public void testFullRestoreMultiple() throws Exception {
91      LOG.info("create full backup image on multiple tables");
92      List<TableName> tables = Lists.newArrayList(table2, table3);
93      String backupId = fullTableBackup(tables);
94      assertTrue(checkSucceeded(backupId));
95  
96      TableName[] restore_tableset = new TableName[] { table2, table3 };
97      TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
98      BackupAdmin client = getBackupAdmin();
99      client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
100       restore_tableset, tablemap, false));
101     HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
102     assertTrue(hba.tableExists(table2_restore));
103     assertTrue(hba.tableExists(table3_restore));
104     TEST_UTIL.deleteTable(table2_restore);
105     TEST_UTIL.deleteTable(table3_restore);
106     hba.close();
107   }
108 
109   /**
110    * Verify that multiple tables are restored to new tables.
111    * @throws Exception
112    */
113   @Test
114   public void testFullRestoreMultipleCommand() throws Exception {
115     LOG.info("create full backup image on multiple tables: command-line");
116     List<TableName> tables = Lists.newArrayList(table2, table3);
117     String backupId = fullTableBackup(tables);
118     assertTrue(checkSucceeded(backupId));
119 
120     TableName[] restore_tableset = new TableName[] { table2, table3 };
121     TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
122     
123     
124     //restore <backup_root_path> <backup_id> <tables> [tableMapping]
125     String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
126         StringUtils.join(restore_tableset, ","), 
127         StringUtils.join(tablemap, ",") }; 
128     // Run backup
129     int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
130 
131     assertTrue(ret==0);    
132     HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
133     assertTrue(hba.tableExists(table2_restore));
134     assertTrue(hba.tableExists(table3_restore));
135     TEST_UTIL.deleteTable(table2_restore);
136     TEST_UTIL.deleteTable(table3_restore);
137     hba.close();
138   }
139   
140   
141   /**
142    * Verify that a single table is restored using overwrite
143    * @throws Exception
144    */
145   @Test
146   public void testFullRestoreSingleOverwrite() throws Exception {
147 
148     LOG.info("test full restore on a single table empty table");
149     List<TableName> tables = Lists.newArrayList(table1);
150     String backupId = fullTableBackup(tables);
151     assertTrue(checkSucceeded(backupId));
152     
153     LOG.info("backup complete");
154 
155     TableName[] tableset = new TableName[] { table1 };
156     BackupAdmin client = getBackupAdmin();
157     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false, tableset, null,
158       true));
159   }
160 
161   /**
162    * Verify that a single table is restored using overwrite
163    * @throws Exception
164    */
165   @Test
166   public void testFullRestoreSingleOverwriteCommand() throws Exception {
167 
168     LOG.info("test full restore on a single table empty table: command-line");
169     List<TableName> tables = Lists.newArrayList(table1);
170     String backupId = fullTableBackup(tables);
171     assertTrue(checkSucceeded(backupId));    
172     LOG.info("backup complete");
173     TableName[] tableset = new TableName[] { table1 };
174     //restore <backup_root_path> <backup_id> <tables> [tableMapping]
175     String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
176         StringUtils.join(tableset, ","), "-overwrite" }; 
177     // Run restore
178     int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
179     assertTrue(ret==0);   
180     
181     HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
182     assertTrue(hba.tableExists(table1));
183     hba.close();
184 
185   }  
186   
187   /**
188    * Verify that multiple tables are restored to new tables using overwrite.
189    * @throws Exception
190    */
191   @Test
192   public void testFullRestoreMultipleOverwrite() throws Exception {
193     LOG.info("create full backup image on multiple tables");
194 
195     List<TableName> tables = Lists.newArrayList(table2, table3);
196     String backupId = fullTableBackup(tables);
197     assertTrue(checkSucceeded(backupId));    
198 
199     TableName[] restore_tableset = new TableName[] { table2, table3 };
200     BackupAdmin client = getBackupAdmin();
201     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId,
202       false, restore_tableset, null, true));
203   }
204 
205   /**
206    * Verify that multiple tables are restored to new tables using overwrite.
207    * @throws Exception
208    */
209   @Test
210   public void testFullRestoreMultipleOverwriteCommand() throws Exception {
211     LOG.info("create full backup image on multiple tables: command-line");
212 
213     List<TableName> tables = Lists.newArrayList(table2, table3);
214     String backupId = fullTableBackup(tables);
215     assertTrue(checkSucceeded(backupId));    
216 
217     TableName[] restore_tableset = new TableName[] { table2, table3 };
218     //restore <backup_root_path> <backup_id> <tables> [tableMapping]
219     String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
220         StringUtils.join(restore_tableset, ","), "-overwrite" }; 
221     // Run backup
222     int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
223 
224     assertTrue(ret==0);    
225     HBaseAdmin hba = TEST_UTIL.getHBaseAdmin();
226     assertTrue(hba.tableExists(table2));
227     assertTrue(hba.tableExists(table3));
228     hba.close();
229   }  
230   
231   /**
232    * Verify that restore fails on a single table that does not exist.
233    * @throws Exception
234    */
235   @Test(expected = IOException.class)
236   public void testFullRestoreSingleDNE() throws Exception {
237 
238     LOG.info("test restore fails on a single table that does not exist");
239     List<TableName> tables = Lists.newArrayList(table1);
240     String backupId = fullTableBackup(tables);
241     assertTrue(checkSucceeded(backupId));    
242 
243     LOG.info("backup complete");
244 
245     TableName[] tableset = new TableName[] { TableName.valueOf("faketable") };
246     TableName[] tablemap = new TableName[] { table1_restore };
247     BackupAdmin client = getBackupAdmin();
248     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false, tableset, tablemap,
249       false));
250   }
251 
252   
253   /**
254    * Verify that restore fails on a single table that does not exist.
255    * @throws Exception
256    */
257   @Test
258   public void testFullRestoreSingleDNECommand() throws Exception {
259 
260     LOG.info("test restore fails on a single table that does not exist: command-line");
261     List<TableName> tables = Lists.newArrayList(table1);
262     String backupId = fullTableBackup(tables);
263     assertTrue(checkSucceeded(backupId));    
264 
265     LOG.info("backup complete");
266 
267     TableName[] tableset = new TableName[] { TableName.valueOf("faketable") };
268     TableName[] tablemap = new TableName[] { table1_restore };
269     String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
270         StringUtils.join(tableset, ","), 
271         StringUtils.join(tablemap, ",") }; 
272     // Run restore
273     int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
274     assertTrue(ret != 0);    
275     
276   }  
277   /**
278    * Verify that restore fails on multiple tables that do not exist.
279    * @throws Exception
280    */
281   @Test(expected = IOException.class)
282   public void testFullRestoreMultipleDNE() throws Exception {
283 
284     LOG.info("test restore fails on multiple tables that do not exist");
285 
286     List<TableName> tables = Lists.newArrayList(table2, table3);
287     String backupId = fullTableBackup(tables);
288     assertTrue(checkSucceeded(backupId));    
289 
290     TableName[] restore_tableset
291       = new TableName[] { TableName.valueOf("faketable1"), TableName.valueOf("faketable2") };
292     TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
293     BackupAdmin client = getBackupAdmin();
294     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupId, false,
295       restore_tableset, tablemap, false));
296   }
297   
298   /**
299    * Verify that restore fails on multiple tables that do not exist.
300    * @throws Exception
301    */
302   @Test
303   public void testFullRestoreMultipleDNECommand() throws Exception {
304 
305     LOG.info("test restore fails on multiple tables that do not exist: command-line");
306 
307     List<TableName> tables = Lists.newArrayList(table2, table3);
308     String backupId = fullTableBackup(tables);
309     assertTrue(checkSucceeded(backupId));    
310 
311     TableName[] restore_tableset
312       = new TableName[] { TableName.valueOf("faketable1"), TableName.valueOf("faketable2") };
313     TableName[] tablemap = new TableName[] { table2_restore, table3_restore };
314     String[] args = new String[]{BACKUP_ROOT_DIR, backupId, 
315         StringUtils.join(restore_tableset, ","), 
316         StringUtils.join(tablemap, ",") }; 
317     // Run restore
318     int ret = ToolRunner.run(conf1, new RestoreDriver(), args);
319     assertTrue(ret != 0); 
320   }
321 }