001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.HashSet;
024import java.util.List;
025import java.util.Set;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.client.ConnectionFactory;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.jupiter.api.Tag;
036import org.junit.jupiter.api.Test;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
041
042/**
043 * Create multiple backups for two tables: table1, table2 then perform 1 delete
044 */
045@Tag(LargeTests.TAG)
046public class TestBackupMultipleDeletes extends TestBackupBase {
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestBackupMultipleDeletes.class);
049
050  @Test
051  public void testBackupMultipleDeletes() throws Exception {
052    // #1 - create full backup for all tables
053    LOG.info("create full backup image for all tables");
054    List<TableName> tables = Lists.newArrayList(table1, table2);
055    Connection conn = ConnectionFactory.createConnection(conf1);
056    Admin admin = conn.getAdmin();
057    BackupAdmin client = new BackupAdminImpl(conn);
058    BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
059    String backupIdFull = client.backupTables(request);
060    assertTrue(checkSucceeded(backupIdFull));
061    // #2 - insert some data to table table1
062    Table t1 = conn.getTable(table1);
063    Put p1;
064    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
065      p1 = new Put(Bytes.toBytes("row-t1" + i));
066      p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
067      t1.put(p1);
068    }
069    assertEquals(NB_ROWS_IN_BATCH * 2, TEST_UTIL.countRows(t1));
070    t1.close();
071    // #3 - incremental backup for table1
072    tables = Lists.newArrayList(table1);
073    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
074    String backupIdInc1 = client.backupTables(request);
075    assertTrue(checkSucceeded(backupIdInc1));
076    // #4 - insert some data to table table2
077    Table t2 = conn.getTable(table2);
078    Put p2 = null;
079    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
080      p2 = new Put(Bytes.toBytes("row-t2" + i));
081      p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
082      t2.put(p2);
083    }
084    // #5 - incremental backup for table1, table2
085    tables = Lists.newArrayList(table1, table2);
086    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
087    String backupIdInc2 = client.backupTables(request);
088    assertTrue(checkSucceeded(backupIdInc2));
089    // #6 - insert some data to table table1
090    t1 = conn.getTable(table1);
091    for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
092      p1 = new Put(Bytes.toBytes("row-t1" + i));
093      p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
094      t1.put(p1);
095    }
096    // #7 - incremental backup for table1
097    tables = Lists.newArrayList(table1);
098    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
099    String backupIdInc3 = client.backupTables(request);
100    assertTrue(checkSucceeded(backupIdInc3));
101    // #8 - insert some data to table table2
102    t2 = conn.getTable(table2);
103    for (int i = NB_ROWS_IN_BATCH; i < 2 * NB_ROWS_IN_BATCH; i++) {
104      p2 = new Put(Bytes.toBytes("row-t1" + i));
105      p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
106      t2.put(p2);
107    }
108    // #9 - incremental backup for table1, table2
109    tables = Lists.newArrayList(table1, table2);
110    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
111    String backupIdInc4 = client.backupTables(request);
112    assertTrue(checkSucceeded(backupIdInc4));
113    // #10 full backup for table3
114    tables = Lists.newArrayList(table3);
115    request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
116    String backupIdFull2 = client.backupTables(request);
117    assertTrue(checkSucceeded(backupIdFull2));
118    // #11 - incremental backup for table3
119    tables = Lists.newArrayList(table3);
120    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
121    String backupIdInc5 = client.backupTables(request);
122    assertTrue(checkSucceeded(backupIdInc5));
123    LOG.error("Delete backupIdInc2");
124    client.deleteBackups(new String[] { backupIdInc2 });
125    LOG.error("Delete backupIdInc2 done");
126    List<BackupInfo> list = client.getHistory(100);
127    // First check number of backup images before and after
128    assertEquals(4, list.size());
129    // then verify that no backupIdInc2,3,4
130    Set<String> ids = new HashSet<String>();
131    ids.add(backupIdInc2);
132    ids.add(backupIdInc3);
133    ids.add(backupIdInc4);
134    for (BackupInfo info : list) {
135      String backupId = info.getBackupId();
136      if (ids.contains(backupId)) {
137        assertTrue(false);
138      }
139    }
140    // Verify that backupInc5 contains only table3
141    boolean found = false;
142    for (BackupInfo info : list) {
143      String backupId = info.getBackupId();
144      if (backupId.equals(backupIdInc5)) {
145        assertTrue(info.getTables().size() == 1);
146        assertEquals(table3, info.getTableNames().get(0));
147        found = true;
148      }
149    }
150    assertTrue(found);
151    admin.close();
152    conn.close();
153  }
154
155}