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