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.Path;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.hadoop.hbase.client.ConnectionFactory;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
037
038@Category(LargeTests.class)
039public class TestIncrementalBackupWithDataLoss extends TestBackupBase {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestIncrementalBackupWithDataLoss.class);
044
045  private static final Logger LOG =
046    LoggerFactory.getLogger(TestIncrementalBackupWithDataLoss.class);
047
048  @Test
049  public void testFullBackupBreaksDependencyOnOlderBackups() throws Exception {
050    LOG.info("test creation of backups after backup data was lost");
051
052    try (Connection conn = ConnectionFactory.createConnection(conf1)) {
053      BackupAdminImpl client = new BackupAdminImpl(conn);
054      List<TableName> tables = Lists.newArrayList(table1);
055
056      insertIntoTable(conn, table1, famName, 1, 1).close();
057      String backup1 =
058        client.backupTables(createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR));
059      insertIntoTable(conn, table1, famName, 2, 1).close();
060      String backup2 =
061        client.backupTables(createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR));
062
063      assertTrue(checkSucceeded(backup1));
064      assertTrue(checkSucceeded(backup2));
065
066      // Simulate data loss on the backup storage
067      TEST_UTIL.getTestFileSystem().delete(new Path(BACKUP_ROOT_DIR, backup2), true);
068
069      insertIntoTable(conn, table1, famName, 4, 1).close();
070      String backup4 =
071        client.backupTables(createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR));
072      insertIntoTable(conn, table1, famName, 5, 1).close();
073      String backup5 =
074        client.backupTables(createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR));
075      insertIntoTable(conn, table1, famName, 6, 1).close();
076      String backup6 =
077        client.backupTables(createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR));
078
079      assertTrue(checkSucceeded(backup4));
080      assertTrue(checkSucceeded(backup5));
081      assertTrue(checkSucceeded(backup6));
082    }
083  }
084
085}