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  /**
44   * 
45   *  1. Create table t1, t2
46   *  2. Load data to t1, t2
47   *  3  Full backup t1, t2
48   *  4  Delete t2
49   *  5  Load data to t1
50   *  6  Incremental backup t1
51   */
52  @Category(LargeTests.class)
53  public class TestIncrementalBackupDeleteTable extends TestBackupBase {
54    private static final Log LOG = LogFactory.getLog(TestIncrementalBackupDeleteTable.class);
55    @Test
56    public void TestIncBackupDeleteTable() throws Exception {
57      // #1 - create full backup for all tables
58      LOG.info("create full backup image for all tables");
59  
60      List<TableName> tables = Lists.newArrayList(table1, table2);
61      HBaseAdmin admin = null;
62      Connection conn = ConnectionFactory.createConnection(conf1);
63      admin = (HBaseAdmin) conn.getAdmin();
64  
65      BackupRequest request = new BackupRequest();
66      request.setBackupType(BackupType.FULL).setTableList(tables).setTargetRootDir(BACKUP_ROOT_DIR);
67      String backupIdFull = admin.getBackupAdmin().backupTables(request);
68  
69      assertTrue(checkSucceeded(backupIdFull));
70  
71      // #2 - insert some data to table table1
72      HTable t1 = (HTable) conn.getTable(table1);
73      Put p1;
74      for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
75        p1 = new Put(Bytes.toBytes("row-t1" + i));
76        p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
77        t1.put(p1);
78      }
79  
80      Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
81      t1.close();
82  
83      // Delete table table2
84      admin.disableTable(table2);
85      admin.deleteTable(table2);
86      
87      // #3 - incremental backup for table1
88      tables = Lists.newArrayList(table1);
89      request = new BackupRequest();
90      request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
91      .setTargetRootDir(BACKUP_ROOT_DIR);
92      String backupIdIncMultiple = admin.getBackupAdmin().backupTables(request);
93      assertTrue(checkSucceeded(backupIdIncMultiple));
94  
95      // #4 - restore full backup for all tables, without overwrite
96      TableName[] tablesRestoreFull =
97          new TableName[] { table1, table2};
98  
99      TableName[] tablesMapFull =
100         new TableName[] { table1_restore, table2_restore };
101 
102     BackupAdmin client = getBackupAdmin();
103     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
104       tablesRestoreFull,
105       tablesMapFull, false));
106 
107     // #5.1 - check tables for full restore
108     HBaseAdmin hAdmin = TEST_UTIL.getHBaseAdmin();
109     assertTrue(hAdmin.tableExists(table1_restore));
110     assertTrue(hAdmin.tableExists(table2_restore));
111 
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 
125     // #6 - restore incremental backup for table1
126     TableName[] tablesRestoreIncMultiple =
127         new TableName[] { table1 };
128     TableName[] tablesMapIncMultiple =
129         new TableName[] { table1_restore };
130     client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple, false,
131       tablesRestoreIncMultiple, tablesMapIncMultiple, true));
132 
133     hTable = (HTable) conn.getTable(table1_restore);
134     Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
135     hTable.close();
136     admin.close();
137     conn.close();
138   }
139 
140 }