HBASE-23868 : Replace usages of HColumnDescriptor(byte [] familyName)… (#1222)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestIncrementalBackupDeleteTable.java
blob837de4dd616645b2f09a7cfe62d37a9d7d92ff08
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.backup;
20 import static org.junit.Assert.assertTrue;
22 import java.util.List;
23 import org.apache.hadoop.hbase.HBaseClassTestRule;
24 import org.apache.hadoop.hbase.TableName;
25 import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
26 import org.apache.hadoop.hbase.backup.util.BackupUtils;
27 import org.apache.hadoop.hbase.client.Admin;
28 import org.apache.hadoop.hbase.client.Connection;
29 import org.apache.hadoop.hbase.client.ConnectionFactory;
30 import org.apache.hadoop.hbase.client.Put;
31 import org.apache.hadoop.hbase.client.Table;
32 import org.apache.hadoop.hbase.testclassification.LargeTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.junit.Assert;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
43 /**
44 * 1. Create table t1, t2
45 * 2. Load data to t1, t2
46 * 3 Full backup t1, t2
47 * 4 Delete t2
48 * 5 Load data to t1
49 * 6 Incremental backup t1
51 @Category(LargeTests.class)
52 public class TestIncrementalBackupDeleteTable extends TestBackupBase {
54 @ClassRule
55 public static final HBaseClassTestRule CLASS_RULE =
56 HBaseClassTestRule.forClass(TestIncrementalBackupDeleteTable.class);
58 private static final Logger LOG = LoggerFactory.getLogger(TestIncrementalBackupDeleteTable.class);
60 // implement all test cases in 1 test since incremental backup/restore has dependencies
61 @Test
62 public void testIncBackupDeleteTable() throws Exception {
63 // #1 - create full backup for all tables
64 LOG.info("create full backup image for all tables");
66 List<TableName> tables = Lists.newArrayList(table1, table2);
67 Connection conn = ConnectionFactory.createConnection(conf1);
68 Admin admin = conn.getAdmin();
69 BackupAdminImpl client = new BackupAdminImpl(conn);
71 BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
72 String backupIdFull = client.backupTables(request);
74 assertTrue(checkSucceeded(backupIdFull));
76 // #2 - insert some data to table table1
77 Table t1 = conn.getTable(table1);
78 Put p1;
79 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
80 p1 = new Put(Bytes.toBytes("row-t1" + i));
81 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
82 t1.put(p1);
85 Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH * 2);
86 t1.close();
88 // Delete table table2
89 admin.disableTable(table2);
90 admin.deleteTable(table2);
92 // #3 - incremental backup for table1
93 tables = Lists.newArrayList(table1);
94 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
95 String backupIdIncMultiple = client.backupTables(request);
96 assertTrue(checkSucceeded(backupIdIncMultiple));
98 // #4 - restore full backup for all tables, without overwrite
99 TableName[] tablesRestoreFull = new TableName[] { table1, table2 };
101 TableName[] tablesMapFull = new TableName[] { table1_restore, table2_restore };
103 client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
104 tablesRestoreFull, tablesMapFull, false));
106 // #5.1 - check tables for full restore
107 Admin hAdmin = TEST_UTIL.getAdmin();
108 assertTrue(hAdmin.tableExists(table1_restore));
109 assertTrue(hAdmin.tableExists(table2_restore));
111 // #5.2 - checking row count of tables for full restore
112 Table hTable = conn.getTable(table1_restore);
113 Assert.assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH);
114 hTable.close();
116 hTable = conn.getTable(table2_restore);
117 Assert.assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH);
118 hTable.close();
120 // #6 - restore incremental backup for table1
121 TableName[] tablesRestoreIncMultiple = new TableName[] { table1 };
122 TableName[] tablesMapIncMultiple = new TableName[] { table1_restore };
123 client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple,
124 false, tablesRestoreIncMultiple, tablesMapIncMultiple, true));
126 hTable = conn.getTable(table1_restore);
127 Assert.assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH * 2);
128 hTable.close();
129 admin.close();
130 conn.close();