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.util.BackupUtils;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.Delete;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
037
038@Tag(LargeTests.TAG)
039public class TestBackupDeleteRestore extends TestBackupBase {
040
041  private static final Logger LOG = LoggerFactory.getLogger(TestBackupDeleteRestore.class);
042
043  /**
044   * Verify that load data- backup - delete some data - restore works as expected - deleted data get
045   * restored.
046   * @throws Exception if doing the backup or an operation on the tables fails
047   */
048  @Test
049  public void testBackupDeleteRestore() throws Exception {
050    LOG.info("test full restore on a single table empty table");
051
052    List<TableName> tables = Lists.newArrayList(table1);
053    String backupId = fullTableBackup(tables);
054    assertTrue(checkSucceeded(backupId));
055    LOG.info("backup complete");
056    int numRows = TEST_UTIL.countRows(table1);
057    Admin hba = TEST_UTIL.getAdmin();
058    // delete row
059    try (Table table = TEST_UTIL.getConnection().getTable(table1)) {
060      Delete delete = new Delete(Bytes.toBytes("row0"));
061      table.delete(delete);
062      hba.flush(table1);
063    }
064
065    TableName[] tableset = new TableName[] { table1 };
066    TableName[] tablemap = null;// new TableName[] { table1_restore };
067    BackupAdmin client = getBackupAdmin();
068    client.restore(
069      BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupId, false, tableset, tablemap, true));
070
071    int numRowsAfterRestore = TEST_UTIL.countRows(table1);
072    assertEquals(numRows, numRowsAfterRestore);
073    hba.close();
074  }
075}