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.List;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
026import org.apache.hadoop.hbase.backup.util.BackupUtils;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ConnectionFactory;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.testclassification.LargeTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
040
041/**
042 * 1. Create table t1, t2 2. Load data to t1, t2 3 Full backup t1, t2 4 Delete t2 5 Load data to t1
043 * 6 Incremental backup t1
044 */
045@Tag(LargeTests.TAG)
046public class TestIncrementalBackupDeleteTable extends TestBackupBase {
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestIncrementalBackupDeleteTable.class);
049
050  // implement all test cases in 1 test since incremental backup/restore has dependencies
051  @Test
052  public void testIncBackupDeleteTable() throws Exception {
053    // #1 - create full backup for all tables
054    LOG.info("create full backup image for all tables");
055
056    List<TableName> tables = Lists.newArrayList(table1, table2);
057    Connection conn = ConnectionFactory.createConnection(conf1);
058    Admin admin = conn.getAdmin();
059    BackupAdminImpl client = new BackupAdminImpl(conn);
060
061    BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR);
062    String backupIdFull = client.backupTables(request);
063
064    assertTrue(checkSucceeded(backupIdFull));
065
066    // #2 - insert some data to table table1
067    Table t1 = conn.getTable(table1);
068    Put p1;
069    for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
070      p1 = new Put(Bytes.toBytes("row-t1" + i));
071      p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
072      t1.put(p1);
073    }
074
075    assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH * 2);
076    t1.close();
077
078    // Delete table table2
079    admin.disableTable(table2);
080    admin.deleteTable(table2);
081
082    // #3 - incremental backup for table1
083    tables = Lists.newArrayList(table1);
084    request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR);
085    String backupIdIncMultiple = client.backupTables(request);
086    assertTrue(checkSucceeded(backupIdIncMultiple));
087
088    // #4 - restore full backup for all tables, without overwrite
089    TableName[] tablesRestoreFull = new TableName[] { table1, table2 };
090
091    TableName[] tablesMapFull = new TableName[] { table1_restore, table2_restore };
092
093    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
094      tablesRestoreFull, tablesMapFull, false));
095
096    // #5.1 - check tables for full restore
097    Admin hAdmin = TEST_UTIL.getAdmin();
098    assertTrue(hAdmin.tableExists(table1_restore));
099    assertTrue(hAdmin.tableExists(table2_restore));
100
101    // #5.2 - checking row count of tables for full restore
102    Table hTable = conn.getTable(table1_restore);
103    assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH);
104    hTable.close();
105
106    hTable = conn.getTable(table2_restore);
107    assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH);
108    hTable.close();
109
110    // #6 - restore incremental backup for table1
111    TableName[] tablesRestoreIncMultiple = new TableName[] { table1 };
112    TableName[] tablesMapIncMultiple = new TableName[] { table1_restore };
113    client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple, false,
114      tablesRestoreIncMultiple, tablesMapIncMultiple, true));
115
116    hTable = conn.getTable(table1_restore);
117    assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH * 2);
118    hTable.close();
119    admin.close();
120    conn.close();
121  }
122
123}