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.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.backup.util.BackupUtils;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.util.ToolRunner;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
038
039@Category(LargeTests.class)
040public class TestBackupShowHistory extends TestBackupBase {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestBackupShowHistory.class);
045
046  private static final Logger LOG = LoggerFactory.getLogger(TestBackupShowHistory.class);
047
048  private boolean findBackup(List<BackupInfo> history, String backupId) {
049    assertTrue(history.size() > 0);
050    boolean success = false;
051    for (BackupInfo info : history) {
052      if (info.getBackupId().equals(backupId)) {
053        success = true;
054        break;
055      }
056    }
057    return success;
058  }
059
060  /**
061   * Verify that full backup is created on a single table with data correctly. Verify that history
062   * works as expected.
063   *
064   * @throws Exception if doing the backup or an operation on the tables fails
065   */
066  @Test
067  public void testBackupHistory() throws Exception {
068
069    LOG.info("test backup history on a single table with data");
070
071    List<TableName> tableList = Lists.newArrayList(table1);
072    String backupId = fullTableBackup(tableList);
073    assertTrue(checkSucceeded(backupId));
074    LOG.info("backup complete");
075
076    List<BackupInfo> history = getBackupAdmin().getHistory(10);
077    assertTrue(findBackup(history, backupId));
078    BackupInfo.Filter nullFilter = info -> true;
079    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), nullFilter);
080    assertTrue(findBackup(history, backupId));
081
082    ByteArrayOutputStream baos = new ByteArrayOutputStream();
083    System.setOut(new PrintStream(baos));
084
085    String[] args = new String[] { "history", "-n", "10", "-p", BACKUP_ROOT_DIR };
086    // Run backup
087    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
088    assertTrue(ret == 0);
089    LOG.info("show_history");
090    String output = baos.toString();
091    LOG.info(output);
092    baos.close();
093    assertTrue(output.indexOf(backupId) > 0);
094
095    tableList = Lists.newArrayList(table2);
096    String backupId2 = fullTableBackup(tableList);
097    assertTrue(checkSucceeded(backupId2));
098    LOG.info("backup complete: " + table2);
099    BackupInfo.Filter tableNameFilter = image -> {
100      if (table1 == null) {
101        return true;
102      }
103
104      List<TableName> names = image.getTableNames();
105      return names.contains(table1);
106    };
107    BackupInfo.Filter tableSetFilter = info -> {
108      String backupId1 = info.getBackupId();
109      return backupId1.startsWith("backup");
110    };
111
112    history = getBackupAdmin().getHistory(10, tableNameFilter, tableSetFilter);
113    assertTrue(history.size() > 0);
114    boolean success = true;
115    for (BackupInfo info : history) {
116      if (!info.getTableNames().contains(table1)) {
117        success = false;
118        break;
119      }
120    }
121    assertTrue(success);
122
123    history =
124        BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), tableNameFilter,
125          tableSetFilter);
126    assertTrue(history.size() > 0);
127    success = true;
128    for (BackupInfo info : history) {
129      if (!info.getTableNames().contains(table1)) {
130        success = false;
131        break;
132      }
133    }
134    assertTrue(success);
135
136    args =
137        new String[] { "history", "-n", "10", "-p", BACKUP_ROOT_DIR,
138          "-t", "table1", "-s", "backup" };
139    // Run backup
140    ret = ToolRunner.run(conf1, new BackupDriver(), args);
141    assertTrue(ret == 0);
142    LOG.info("show_history");
143  }
144}