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.apache.hadoop.hbase.backup.TestBackupUtils.assertBackupCompleteFileDoesNotExist;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertFalse;
023import static org.junit.jupiter.api.Assertions.assertNotEquals;
024import static org.junit.jupiter.api.Assertions.assertTrue;
025
026import java.util.List;
027import java.util.Set;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
030import org.apache.hadoop.hbase.backup.impl.TableBackupClient;
031import org.apache.hadoop.hbase.backup.impl.TableBackupClient.Stage;
032import org.apache.hadoop.hbase.testclassification.LargeTests;
033import org.apache.hadoop.util.ToolRunner;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Tag(LargeTests.TAG)
040public class TestFullBackupWithFailures extends TestBackupBase {
041
042  private static final Logger LOG = LoggerFactory.getLogger(TestFullBackupWithFailures.class);
043
044  @Test
045  public void testFullBackupWithFailures() throws Exception {
046    conf1.set(TableBackupClient.BACKUP_CLIENT_IMPL_CLASS,
047      FullTableBackupClientForTest.class.getName());
048    int maxStage = Stage.values().length - 1;
049    // Fail stages between 0 and 4 inclusive
050    for (int stage = 0; stage <= maxStage; stage++) {
051      LOG.info("Running stage " + stage);
052      runBackupAndFailAtStage(stage);
053    }
054  }
055
056  public void runBackupAndFailAtStage(int stage) throws Exception {
057
058    conf1.setInt(FullTableBackupClientForTest.BACKUP_TEST_MODE_STAGE, stage);
059    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
060      int before = table.getBackupHistory().size();
061      String[] args = new String[] { "create", "full", BACKUP_ROOT_DIR, "-t",
062        table1.getNameAsString() + "," + table2.getNameAsString() };
063      // Run backup
064      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
065      assertNotEquals(0, ret);
066      List<BackupInfo> backups = table.getBackupHistory();
067      int after = table.getBackupHistory().size();
068
069      assertEquals(before + 1, after);
070      for (BackupInfo data : backups) {
071        String backupId = data.getBackupId();
072        assertFalse(checkSucceeded(backupId));
073        assertBackupCompleteFileDoesNotExist(conf1, BACKUP_ROOT_DIR, backupId);
074      }
075      Set<TableName> tables = table.getIncrementalBackupTableSet(BACKUP_ROOT_DIR);
076      assertTrue(tables.isEmpty());
077    }
078  }
079
080}