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.HBaseAdmin;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.testclassification.LargeTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.hamcrest.CoreMatchers;
37  import org.junit.Assert;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import com.google.common.collect.Lists;
42  
43  @Category(LargeTests.class)
44  public class TestIncrementalBackup extends TestBackupBase {
45    private static final Log LOG = LogFactory.getLog(TestIncrementalBackup.class);
46    //implement all test cases in 1 test since incremental backup/restore has dependencies
47    @Test
48    public void TestIncBackupRestore() throws Exception {
49      // #1 - create full backup for all tables
50      LOG.info("create full backup image for all tables");
51  
52      List<TableName> tables = Lists.newArrayList(table1, table2, table3, table4);
53      HBaseAdmin admin = null;
54      Connection conn = ConnectionFactory.createConnection(conf1);
55      admin = (HBaseAdmin) conn.getAdmin();
56  
57      BackupRequest request = new BackupRequest();
58      request.setBackupType(BackupType.FULL).setTableList(tables).setTargetRootDir(BACKUP_ROOT_DIR);
59      String backupIdFull = admin.getBackupAdmin().backupTables(request);
60  
61      assertTrue(checkSucceeded(backupIdFull));
62  
63      // #2 - insert some data to table
64      HTable t1 = (HTable) conn.getTable(table1);
65      Put p1;
66      for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
67        p1 = new Put(Bytes.toBytes("row-t1" + i));
68        p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
69        t1.put(p1);
70      }
71  
72      Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
73      t1.close();
74  
75      HTable t2 =  (HTable) conn.getTable(table2);
76      Put p2;
77      for (int i = 0; i < 5; i++) {
78        p2 = new Put(Bytes.toBytes("row-t2" + i));
79        p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
80        t2.put(p2);
81      }
82  
83      Assert.assertThat(TEST_UTIL.countRows(t2), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
84      t2.close();
85  
86      // #3 - incremental backup for multiple tables
87      tables = Lists.newArrayList(table1, table2, table3);
88      request = new BackupRequest();
89      request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
90      .setTargetRootDir(BACKUP_ROOT_DIR);
91      String backupIdIncMultiple = admin.getBackupAdmin().backupTables(request);
92      assertTrue(checkSucceeded(backupIdIncMultiple));
93  
94      // #4 - restore full backup for all tables, without overwrite
95      TableName[] tablesRestoreFull =
96          new TableName[] { table1, table2, table3, table4 };
97  
98      TableName[] tablesMapFull =
99          new TableName[] { table1_restore, table2_restore, table3_restore, table4_restore };
100 
101     BackupAdmin client = getBackupAdmin();
102     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
103       tablesRestoreFull,
104       tablesMapFull, false));
105 
106     // #5.1 - check tables for full restore
107     HBaseAdmin hAdmin = TEST_UTIL.getHBaseAdmin();
108     assertTrue(hAdmin.tableExists(table1_restore));
109     assertTrue(hAdmin.tableExists(table2_restore));
110     assertTrue(hAdmin.tableExists(table3_restore));
111     assertTrue(hAdmin.tableExists(table4_restore));
112 
113     hAdmin.close();
114 
115     // #5.2 - checking row count of tables for full restore
116     HTable hTable = (HTable) conn.getTable(table1_restore);
117     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
118     hTable.close();
119 
120     hTable = (HTable) conn.getTable(table2_restore);
121     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
122     hTable.close();
123 
124     hTable = (HTable) conn.getTable(table3_restore);
125     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
126     hTable.close();
127 
128     hTable = (HTable) conn.getTable(table4_restore);
129     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
130     hTable.close();
131 
132     // #6 - restore incremental backup for multiple tables, with overwrite
133     TableName[] tablesRestoreIncMultiple =
134         new TableName[] { table1, table2, table3 };
135     TableName[] tablesMapIncMultiple =
136         new TableName[] { table1_restore, table2_restore, table3_restore };
137     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple, false,
138       tablesRestoreIncMultiple, tablesMapIncMultiple, true));
139 
140     hTable = (HTable) conn.getTable(table1_restore);
141     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
142     hTable.close();
143 
144     hTable = (HTable) conn.getTable(table2_restore);
145     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
146     hTable.close();
147 
148     hTable = (HTable) conn.getTable(table3_restore);
149     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
150     hTable.close();
151 
152     // #7 - incremental backup for single, empty table
153 
154     tables = toList(table4.getNameAsString());
155     request = new BackupRequest();
156     request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
157     .setTargetRootDir(BACKUP_ROOT_DIR);
158     String backupIdIncEmpty = admin.getBackupAdmin().backupTables(request);
159 
160 
161     // #8 - restore incremental backup for single empty table, with overwrite
162     TableName[] tablesRestoreIncEmpty = new TableName[] { table4 };
163     TableName[] tablesMapIncEmpty = new TableName[] { table4_restore };
164 
165     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncEmpty, false,
166       tablesRestoreIncEmpty,
167       tablesMapIncEmpty, true));
168 
169     hTable = (HTable) conn.getTable(table4_restore);
170     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
171     hTable.close();
172     admin.close();
173     conn.close();
174   }
175 
176 }