HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestBackupMultipleDeletes.java
blob538488b4c4e44d6126b769e8d477a40798cd76c4
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.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
29 import org.apache.hadoop.hbase.client.Admin;
30 import org.apache.hadoop.hbase.client.Connection;
31 import org.apache.hadoop.hbase.client.ConnectionFactory;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Table;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.Assert;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
45 /**
46 * Create multiple backups for two tables: table1, table2 then perform 1 delete
48 @Category(LargeTests.class)
49 public class TestBackupMultipleDeletes extends TestBackupBase {
51 @ClassRule
52 public static final HBaseClassTestRule CLASS_RULE =
53 HBaseClassTestRule.forClass(TestBackupMultipleDeletes.class);
55 private static final Logger LOG = LoggerFactory.getLogger(TestBackupMultipleDeletes.class);
57 @Test
58 public void testBackupMultipleDeletes() throws Exception {
59 // #1 - create full backup for all tables
60 LOG.info("create full backup image for all tables");
61 List<TableName> tables = Lists.newArrayList(table1, table2);
62 Connection conn = ConnectionFactory.createConnection(conf1);
63 Admin admin = conn.getAdmin();
64 BackupAdmin client = new BackupAdminImpl(conn);
65 BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
66 String backupIdFull = client.backupTables(request);
67 assertTrue(checkSucceeded(backupIdFull));
68 // #2 - insert some data to table table1
69 Table t1 = conn.getTable(table1);
70 Put p1;
71 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
72 p1 = new Put(Bytes.toBytes("row-t1" + i));
73 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
74 t1.put(p1);
76 Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH * 2);
77 t1.close();
78 // #3 - incremental backup for table1
79 tables = Lists.newArrayList(table1);
80 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
81 String backupIdInc1 = client.backupTables(request);
82 assertTrue(checkSucceeded(backupIdInc1));
83 // #4 - insert some data to table table2
84 Table t2 = conn.getTable(table2);
85 Put p2 = null;
86 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
87 p2 = new Put(Bytes.toBytes("row-t2" + i));
88 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
89 t2.put(p2);
91 // #5 - incremental backup for table1, table2
92 tables = Lists.newArrayList(table1, table2);
93 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
94 String backupIdInc2 = client.backupTables(request);
95 assertTrue(checkSucceeded(backupIdInc2));
96 // #6 - insert some data to table table1
97 t1 = conn.getTable(table1);
98 for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
99 p1 = new Put(Bytes.toBytes("row-t1" + i));
100 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
101 t1.put(p1);
103 // #7 - incremental backup for table1
104 tables = Lists.newArrayList(table1);
105 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
106 String backupIdInc3 = client.backupTables(request);
107 assertTrue(checkSucceeded(backupIdInc3));
108 // #8 - insert some data to table table2
109 t2 = conn.getTable(table2);
110 for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
111 p2 = new Put(Bytes.toBytes("row-t1" + i));
112 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
113 t2.put(p2);
115 // #9 - incremental backup for table1, table2
116 tables = Lists.newArrayList(table1, table2);
117 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
118 String backupIdInc4 = client.backupTables(request);
119 assertTrue(checkSucceeded(backupIdInc4));
120 // #10 full backup for table3
121 tables = Lists.newArrayList(table3);
122 request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
123 String backupIdFull2 = client.backupTables(request);
124 assertTrue(checkSucceeded(backupIdFull2));
125 // #11 - incremental backup for table3
126 tables = Lists.newArrayList(table3);
127 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
128 String backupIdInc5 = client.backupTables(request);
129 assertTrue(checkSucceeded(backupIdInc5));
130 LOG.error("Delete backupIdInc2");
131 client.deleteBackups(new String[] { backupIdInc2 });
132 LOG.error("Delete backupIdInc2 done");
133 List<BackupInfo> list = client.getHistory(100);
134 // First check number of backup images before and after
135 assertEquals(4, list.size());
136 // then verify that no backupIdInc2,3,4
137 Set<String> ids = new HashSet<String>();
138 ids.add(backupIdInc2);
139 ids.add(backupIdInc3);
140 ids.add(backupIdInc4);
141 for (BackupInfo info : list) {
142 String backupId = info.getBackupId();
143 if (ids.contains(backupId)) {
144 assertTrue(false);
147 // Verify that backupInc5 contains only table3
148 boolean found = false;
149 for (BackupInfo info : list) {
150 String backupId = info.getBackupId();
151 if (backupId.equals(backupIdInc5)) {
152 assertTrue(info.getTables().size() == 1);
153 assertEquals(table3, info.getTableNames().get(0));
154 found = true;
157 assertTrue(found);
158 admin.close();
159 conn.close();