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.assertTrue;
22  
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.BackupAdmin;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.client.ConnectionFactory;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.testclassification.LargeTests;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.hamcrest.CoreMatchers;
36  import org.junit.Assert;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  import com.google.common.collect.Lists;
41  
42  @Category(LargeTests.class)
43  public class TestIncrementalBackupNoDataLoss extends TestBackupBase {
44    private static final Log LOG = LogFactory.getLog(TestIncrementalBackupNoDataLoss.class);
45  
46    // implement all test cases in 1 test since incremental backup/restore has dependencies
47    @Test
48    public void TestIncBackupRestore() throws Exception {
49  
50      // #1 - create full backup for all tables
51      LOG.info("create full backup image for all tables");
52      List<TableName> tables = Lists.newArrayList(table1, table2);   
53      String backupIdFull = fullTableBackup(tables);
54      assertTrue(checkSucceeded(backupIdFull));
55      Connection conn = ConnectionFactory.createConnection(conf1);
56      // #2 - insert some data to table
57      HTable t1 = (HTable) conn.getTable(table1);
58      Put p1;
59      for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
60        p1 = new Put(Bytes.toBytes("row-t1" + i));
61        p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
62        t1.put(p1);
63      }
64  
65      Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
66      t1.close();
67  
68      HTable t2 = (HTable) conn.getTable(table2);
69      Put p2;
70      for (int i = 0; i < 5; i++) {
71        p2 = new Put(Bytes.toBytes("row-t2" + i));
72        p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
73        t2.put(p2);
74      }
75  
76      Assert.assertThat(TEST_UTIL.countRows(t2), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
77      t2.close();
78  
79      // #3 - incremental backup for table1
80  
81      tables = Lists.newArrayList(table1);
82      String backupIdInc1 = incrementalTableBackup(tables);
83      assertTrue(checkSucceeded(backupIdInc1));
84  
85      // #4 - incremental backup for table2
86  
87      tables = Lists.newArrayList(table2);
88      String backupIdInc2 = incrementalTableBackup(tables);
89      assertTrue(checkSucceeded(backupIdInc2));
90      // #5 - restore incremental backup for table1
91      TableName[] tablesRestoreInc1 = new TableName[] { table1 };
92      TableName[] tablesMapInc1 = new TableName[] { table1_restore };
93  
94      if (TEST_UTIL.getHBaseAdmin().tableExists(table1_restore)) {
95        TEST_UTIL.deleteTable(table1_restore);
96      }
97      if (TEST_UTIL.getHBaseAdmin().tableExists(table2_restore)) {
98        TEST_UTIL.deleteTable(table2_restore);
99      }
100 
101     BackupAdmin client = getBackupAdmin();
102     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdInc1, false, tablesRestoreInc1,
103       tablesMapInc1, false));
104 
105     HTable hTable = (HTable) conn.getTable(table1_restore);
106     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
107     hTable.close();
108 
109     // #5 - restore incremental backup for table2
110     
111     TableName[] tablesRestoreInc2 = new TableName[] { table2 };
112     TableName[] tablesMapInc2 = new TableName[] { table2_restore };
113 
114     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdInc2, false, tablesRestoreInc2,
115       tablesMapInc2, false));
116 
117     hTable = (HTable) conn.getTable(table2_restore);
118     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
119     hTable.close();
120 
121     conn.close();
122   }
123 
124 }