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