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.io.ByteArrayOutputStream;
023import java.io.PrintStream;
024import java.util.List;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.util.EnvironmentEdge;
032import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
033import org.apache.hadoop.util.ToolRunner;
034import org.junit.Assert;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
042
043@Category(LargeTests.class)
044public class TestBackupDelete extends TestBackupBase {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestBackupDelete.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestBackupDelete.class);
051
052  /**
053   * Verify that full backup is created on a single table with data correctly. Verify that history
054   * works as expected.
055   * @throws Exception if doing the backup or an operation on the tables fails
056   */
057  @Test
058  public void testBackupDelete() throws Exception {
059    LOG.info("test backup delete on a single table with data");
060    List<TableName> tableList = Lists.newArrayList(table1);
061    String backupId = fullTableBackup(tableList);
062    assertTrue(checkSucceeded(backupId));
063    LOG.info("backup complete");
064    String[] backupIds = new String[] { backupId };
065    BackupSystemTable table = new BackupSystemTable(TEST_UTIL.getConnection());
066    BackupInfo info = table.readBackupInfo(backupId);
067    Path path = new Path(info.getBackupRootDir(), backupId);
068    FileSystem fs = FileSystem.get(path.toUri(), conf1);
069    assertTrue(fs.exists(path));
070    int deleted = getBackupAdmin().deleteBackups(backupIds);
071
072    assertTrue(!fs.exists(path));
073    assertTrue(fs.exists(new Path(info.getBackupRootDir())));
074    assertTrue(1 == deleted);
075    table.close();
076    LOG.info("delete_backup");
077  }
078
079  /**
080   * Verify that full backup is created on a single table with data correctly. Verify that history
081   * works as expected.
082   * @throws Exception if doing the backup or an operation on the tables fails
083   */
084  @Test
085  public void testBackupDeleteCommand() throws Exception {
086    LOG.info("test backup delete on a single table with data: command-line");
087    List<TableName> tableList = Lists.newArrayList(table1);
088    String backupId = fullTableBackup(tableList);
089    assertTrue(checkSucceeded(backupId));
090    LOG.info("backup complete");
091    ByteArrayOutputStream baos = new ByteArrayOutputStream();
092    System.setOut(new PrintStream(baos));
093
094    String[] args = new String[] { "delete", "-l", backupId };
095    // Run backup
096
097    try {
098      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
099      assertTrue(ret == 0);
100    } catch (Exception e) {
101      LOG.error("failed", e);
102    }
103    LOG.info("delete_backup");
104    String output = baos.toString();
105    LOG.info(baos.toString());
106    assertTrue(output.indexOf("Deleted 1 backups") >= 0);
107  }
108
109  @Test
110  public void testBackupPurgeOldBackupsCommand() throws Exception {
111    LOG.info("test backup delete (purge old backups) on a single table with data: command-line");
112    List<TableName> tableList = Lists.newArrayList(table1);
113    EnvironmentEdgeManager.injectEdge(new EnvironmentEdge() {
114      // time - 2 days
115      @Override
116      public long currentTime() {
117        return System.currentTimeMillis() - 2 * 24 * 3600 * 1000;
118      }
119    });
120    String backupId = fullTableBackup(tableList);
121    assertTrue(checkSucceeded(backupId));
122
123    EnvironmentEdgeManager.reset();
124
125    LOG.info("backup complete");
126    ByteArrayOutputStream baos = new ByteArrayOutputStream();
127    System.setOut(new PrintStream(baos));
128
129    // Purge all backups which are older than 3 days
130    // Must return 0 (no backups were purged)
131    String[] args = new String[] { "delete", "-k", "3" };
132    // Run backup
133
134    try {
135      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
136      assertTrue(ret == 0);
137    } catch (Exception e) {
138      LOG.error("failed", e);
139      Assert.fail(e.getMessage());
140    }
141    String output = baos.toString();
142    LOG.info(baos.toString());
143    assertTrue(output.indexOf("Deleted 0 backups") >= 0);
144
145    // Purge all backups which are older than 1 days
146    // Must return 1 deleted backup
147    args = new String[] { "delete", "-k", "1" };
148    // Run backup
149    baos.reset();
150    try {
151      int ret = ToolRunner.run(conf1, new BackupDriver(), args);
152      assertTrue(ret == 0);
153    } catch (Exception e) {
154      LOG.error("failed", e);
155      Assert.fail(e.getMessage());
156    }
157    output = baos.toString();
158    LOG.info(baos.toString());
159    assertTrue(output.indexOf("Deleted 1 backups") >= 0);
160  }
161}