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.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.util.ToolRunner;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
039
040@Category(LargeTests.class)
041public class TestRepairAfterFailedDelete extends TestBackupBase {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestRepairAfterFailedDelete.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestRepairAfterFailedDelete.class);
048
049  @Test
050  public void testRepairBackupDelete() throws Exception {
051    LOG.info("test repair backup delete on a single table with data");
052    List<TableName> tableList = Lists.newArrayList(table1);
053    String backupId = fullTableBackup(tableList);
054    assertTrue(checkSucceeded(backupId));
055    LOG.info("backup complete");
056    String[] backupIds = new String[] { backupId };
057    BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection());
058    BackupInfo info = table.readBackupInfo(backupId);
059    Path path = new Path(info.getBackupRootDir(), backupId);
060    FileSystem fs = FileSystem.get(path.toUri(), conf1);
061    assertTrue(fs.exists(path));
062
063    // Snapshot backup system table before delete
064    String snapshotName = "snapshot-backup";
065    Connection conn = TEST_UTIL.getConnection();
066    Admin admin = conn.getAdmin();
067    admin.snapshot(snapshotName, BackupSystemTable.getTableName(conf1));
068
069    int deleted = getBackupAdmin().deleteBackups(backupIds);
070
071    assertTrue(!fs.exists(path));
072    assertTrue(fs.exists(new Path(info.getBackupRootDir())));
073    assertTrue(1 == deleted);
074
075    // Emulate delete failure
076    // Restore backup system table
077    admin.disableTable(BackupSystemTable.getTableName(conf1));
078    admin.restoreSnapshot(snapshotName);
079    admin.enableTable(BackupSystemTable.getTableName(conf1));
080    // Start backup session
081    table.startBackupExclusiveOperation();
082    // Start delete operation
083    table.startDeleteOperation(backupIds);
084
085    // Now run repair command to repair "failed" delete operation
086    String[] args = new String[] { "repair" };
087    // Run restore
088    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
089    assertTrue(ret == 0);
090    // Verify that history length == 0
091    assertTrue(table.getBackupHistory().size() == 0);
092    table.close();
093    admin.close();
094  }
095}