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.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertThrows;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024import static org.junit.jupiter.api.Assertions.fail;
025
026import java.io.ByteArrayOutputStream;
027import java.io.IOException;
028import java.io.PrintStream;
029import java.util.List;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.fs.FileSystem;
032import org.apache.hadoop.fs.Path;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl;
035import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
036import org.apache.hadoop.hbase.client.Connection;
037import org.apache.hadoop.hbase.client.ConnectionFactory;
038import org.apache.hadoop.hbase.testclassification.LargeTests;
039import org.apache.hadoop.hbase.util.EnvironmentEdge;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.apache.hadoop.util.ToolRunner;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.Test;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
048import org.apache.hbase.thirdparty.com.google.common.collect.Sets;
049
050@Tag(LargeTests.TAG)
051public class TestBackupDelete extends TestBackupBase {
052
053  private static final Logger LOG = LoggerFactory.getLogger(TestBackupDelete.class);
054
055  /**
056   * Verify that full backup is created on a single table with data correctly. Verify that history
057   * works as expected.
058   * @throws Exception if doing the backup or an operation on the tables fails
059   */
060  @Test
061  public void testBackupDelete() throws Exception {
062    LOG.info("test backup delete on a single table with data");
063    List<TableName> tableList = Lists.newArrayList(table1);
064    String backupId = fullTableBackup(tableList);
065    assertTrue(checkSucceeded(backupId));
066    LOG.info("backup complete");
067    String[] backupIds = new String[] { backupId };
068    BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection());
069    BackupInfo info = table.readBackupInfo(backupId);
070    Path path = new Path(info.getBackupRootDir(), backupId);
071    FileSystem fs = FileSystem.get(path.toUri(), conf1);
072    assertTrue(fs.exists(path));
073    TestBackupUtils.assertBackupCompleteFileExists(conf1, info.getBackupRootDir(), backupId);
074    int deleted = getBackupAdmin().deleteBackups(backupIds);
075
076    TestBackupUtils.assertBackupCompleteFileDoesNotExist(conf1, info.getBackupRootDir(), backupId);
077    assertTrue(!fs.exists(path));
078    assertTrue(fs.exists(new Path(info.getBackupRootDir())));
079    assertTrue(1 == deleted);
080    table.close();
081    LOG.info("delete_backup");
082  }
083
084  /**
085   * Verify that if the completion marker cannot be removed, then the backup delete fails before
086   * removing the backup's data or its system table record. Also verify retrying the backup delete
087   * succeeds.
088   * @throws Exception if doing the backup or an operation on the tables fails
089   */
090  @Test
091  public void testBackupDeleteFailsWhenMarkerDeleteFails() throws Exception {
092    String backupId = fullTableBackup(Lists.newArrayList(table1));
093    assertTrue(checkSucceeded(backupId));
094    String[] backupIds = new String[] { backupId };
095    Path path = new Path(BACKUP_ROOT_DIR, backupId);
096    FileSystem fs = FileSystem.get(path.toUri(), conf1);
097
098    Configuration markerFailConf = BackupTestUtil.createMarkerDeletionFailConf(conf1);
099
100    // Attempt a backup delete that results in a failure due to not being able to remove
101    // the completion marker file
102    try (Connection markerFailConn = ConnectionFactory.createConnection(markerFailConf);
103      BackupAdmin markerFailAdmin = new BackupAdminImpl(markerFailConn)) {
104      assertThrows(IOException.class, () -> markerFailAdmin.deleteBackups(backupIds));
105    }
106
107    // Nothing was deleted, so the backup is still complete and consistent
108    try (BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection())) {
109      assertNotNull(table.readBackupInfo(backupId));
110    }
111    assertTrue(fs.exists(path));
112    TestBackupUtils.assertBackupCompleteFileExists(conf1, BACKUP_ROOT_DIR, backupId);
113
114    // Retry a successful delete this time and expect the marker file to be deleted
115    assertEquals(1, getBackupAdmin().deleteBackups(backupIds));
116    assertTrue(!fs.exists(path));
117  }
118
119  /**
120   * Verify that full backup is created on a single table with data correctly. Verify that history
121   * works as expected.
122   * @throws Exception if doing the backup or an operation on the tables fails
123   */
124  @Test
125  public void testBackupDeleteCommand() throws Exception {
126    LOG.info("test backup delete on a single table with data: command-line");
127    List<TableName> tableList = Lists.newArrayList(table1);
128    String backupId = fullTableBackup(tableList);
129    assertTrue(checkSucceeded(backupId));
130    LOG.info("backup complete");
131    ByteArrayOutputStream baos = new ByteArrayOutputStream();
132    System.setOut(new PrintStream(baos));
133
134    String[] args = new String[] { "delete", "-l", backupId };
135    // Run backup
136
137    try {
138      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
139      assertTrue(ret == 0);
140    } catch (Exception e) {
141      LOG.error("failed", e);
142    }
143    LOG.info("delete_backup");
144    String output = baos.toString();
145    LOG.info(baos.toString());
146    assertTrue(output.indexOf("Deleted 1 backups") >= 0);
147  }
148
149  @Test
150  public void testBackupPurgeOldBackupsCommand() throws Exception {
151    LOG.info("test backup delete (purge old backups) on a single table with data: command-line");
152    List<TableName> tableList = Lists.newArrayList(table1);
153    EnvironmentEdgeManager.injectEdge(new EnvironmentEdge() {
154      // time - 2 days
155      @Override
156      public long currentTime() {
157        return System.currentTimeMillis() - 2 * 24 * 3600 * 1000;
158      }
159    });
160    String backupId = fullTableBackup(tableList);
161    assertTrue(checkSucceeded(backupId));
162
163    EnvironmentEdgeManager.reset();
164
165    LOG.info("backup complete");
166    ByteArrayOutputStream baos = new ByteArrayOutputStream();
167    System.setOut(new PrintStream(baos));
168
169    // Purge all backups which are older than 3 days
170    // Must return 0 (no backups were purged)
171    String[] args = new String[] { "delete", "-k", "3" };
172    // Run backup
173
174    try {
175      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
176      assertTrue(ret == 0);
177    } catch (Exception e) {
178      LOG.error("failed", e);
179      fail(e.getMessage());
180    }
181    String output = baos.toString();
182    LOG.info(baos.toString());
183    assertTrue(output.indexOf("Deleted 0 backups") >= 0);
184
185    // Purge all backups which are older than 1 days
186    // Must return 1 deleted backup
187    args = new String[] { "delete", "-k", "1" };
188    // Run backup
189    baos.reset();
190    try {
191      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
192      assertTrue(ret == 0);
193    } catch (Exception e) {
194      LOG.error("failed", e);
195      fail(e.getMessage());
196    }
197    output = baos.toString();
198    LOG.info(baos.toString());
199    assertTrue(output.indexOf("Deleted 1 backups") >= 0);
200  }
201
202  /**
203   * Verify that backup deletion updates the incremental-backup-set.
204   */
205  @Test
206  public void testBackupDeleteUpdatesIncrementalBackupSet() throws Exception {
207    LOG.info("Test backup delete updates the incremental backup set");
208    BackupSystemTable backupSystemTable = new BackupSystemTable(TEST_UTIL.getConnection());
209
210    String backupId1 = fullTableBackup(Lists.newArrayList(table1, table2));
211    assertTrue(checkSucceeded(backupId1));
212    assertEquals(Sets.newHashSet(table1, table2),
213      backupSystemTable.getIncrementalBackupTableSet(BACKUP_ROOT_DIR));
214
215    String backupId2 = fullTableBackup(Lists.newArrayList(table3));
216    assertTrue(checkSucceeded(backupId2));
217    assertEquals(Sets.newHashSet(table1, table2, table3),
218      backupSystemTable.getIncrementalBackupTableSet(BACKUP_ROOT_DIR));
219
220    getBackupAdmin().deleteBackups(new String[] { backupId1 });
221    assertEquals(Sets.newHashSet(table3),
222      backupSystemTable.getIncrementalBackupTableSet(BACKUP_ROOT_DIR));
223  }
224}