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.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import java.util.Set;
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.backup.impl.TableBackupClient;
029import org.apache.hadoop.hbase.backup.impl.TableBackupClient.Stage;
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
038@Category(LargeTests.class)
039public class TestBackupRepair extends TestBackupBase {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestBackupRepair.class);
044
045  private static final Logger LOG = LoggerFactory.getLogger(TestBackupRepair.class);
046
047  @Test
048  public void testFullBackupWithFailuresAndRestore() throws Exception {
049
050    autoRestoreOnFailure = false;
051
052    conf1.set(TableBackupClient.BACKUP_CLIENT_IMPL_CLASS,
053      FullTableBackupClientForTest.class.getName());
054    int maxStage = Stage.values().length - 1;
055    // Fail stage in loop between 0 and 4 inclusive
056    for (int stage = 0; stage < maxStage; stage++) {
057      LOG.info("Running stage " + stage);
058      runBackupAndFailAtStageWithRestore(stage);
059    }
060  }
061
062  public void runBackupAndFailAtStageWithRestore(int stage) throws Exception {
063
064    conf1.setInt(FullTableBackupClientForTest.BACKUP_TEST_MODE_STAGE, stage);
065    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
066      int before = table.getBackupHistory().size();
067      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-t",
068        table1.getNameAsString() + "," + table2.getNameAsString() };
069      // Run backup
070      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
071      assertFalse(ret == 0);
072
073      // Now run restore
074      args = new String[] { "repair" };
075
076      ret = ToolRunner.run(conf1, new BackupDriver(), args);
077      assertTrue(ret == 0);
078
079      List<BackupInfo> backups = table.getBackupHistory();
080      int after = table.getBackupHistory().size();
081
082      assertTrue(after == before + 1);
083      for (BackupInfo data : backups) {
084        String backupId = data.getBackupId();
085        assertFalse(checkSucceeded(backupId));
086      }
087      Set<TableName> tables = table.getIncrementalBackupTableSet(BACKUP_ROOT_DIR);
088      assertTrue(tables.size() == 0);
089    }
090  }
091
092}