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.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.ByteArrayOutputStream;
024import java.io.PrintStream;
025import java.util.List;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
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
037@Category(LargeTests.class)
038public class TestBackupShowHistory extends TestBackupBase {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestBackupShowHistory.class);
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestBackupShowHistory.class);
045
046  /**
047   * Verify that backup history retrieval works as expected.
048   */
049  @Test
050  public void testBackupHistory() throws Exception {
051    LOG.info("test backup history on a single table with data");
052
053    // Test without backups present
054    List<BackupInfo> history = getBackupAdmin().getHistory(10);
055    assertEquals(0, history.size());
056
057    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
058    assertEquals(0, history.size());
059
060    // Create first backup
061    String backupId = fullTableBackup(List.of(table1));
062    assertTrue(checkSucceeded(backupId));
063    LOG.info("backup complete");
064
065    // Tests with one backup present
066    history = getBackupAdmin().getHistory(10);
067    assertEquals(1, history.size());
068    assertEquals(backupId, history.get(0).getBackupId());
069
070    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
071    assertEquals(1, history.size());
072    assertEquals(backupId, history.get(0).getBackupId());
073
074    String output = runHistoryCommand(10);
075    assertTrue(output.indexOf(backupId) > 0);
076
077    // Create second backup
078    String backupId2 = fullTableBackup(List.of(table2));
079    assertTrue(checkSucceeded(backupId2));
080    LOG.info("backup complete: " + table2);
081
082    // Test with multiple backups
083    history = getBackupAdmin().getHistory(10);
084    assertEquals(2, history.size());
085    assertEquals(backupId2, history.get(0).getBackupId());
086    assertEquals(backupId, history.get(1).getBackupId());
087
088    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR));
089    assertEquals(2, history.size());
090    assertEquals(backupId2, history.get(0).getBackupId());
091    assertEquals(backupId, history.get(1).getBackupId());
092
093    output = runHistoryCommand(10);
094    int idx1 = output.indexOf(backupId);
095    int idx2 = output.indexOf(backupId2);
096    assertTrue(idx1 >= 0); // Backup 1 is listed
097    assertTrue(idx2 >= 0); // Backup 2 is listed
098    assertTrue(idx2 < idx1); // Newest backup (Backup 2) comes first
099
100    // Test with multiple backups & n == 1
101    history = getBackupAdmin().getHistory(1);
102    assertEquals(1, history.size());
103    assertEquals(backupId2, history.get(0).getBackupId());
104
105    history = BackupUtils.getHistory(conf1, 1, new Path(BACKUP_ROOT_DIR));
106    assertEquals(1, history.size());
107    assertEquals(backupId2, history.get(0).getBackupId());
108
109    output = runHistoryCommand(1);
110    idx1 = output.indexOf(backupId);
111    idx2 = output.indexOf(backupId2);
112    assertTrue(idx2 > 0); // most recent backup is listed
113    assertEquals(-1, idx1); // second most recent backup isn't listed
114
115    // Test with multiple backups & filtering
116    BackupInfo.Filter tableNameFilter = i -> i.getTableNames().contains(table1);
117
118    history = getBackupAdmin().getHistory(10, tableNameFilter);
119    assertEquals(1, history.size());
120    assertEquals(backupId, history.get(0).getBackupId());
121
122    history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), tableNameFilter);
123    assertEquals(1, history.size());
124    assertEquals(backupId, history.get(0).getBackupId());
125
126  }
127
128  private String runHistoryCommand(int n) throws Exception {
129    String[] args = new String[] { "history", "-n", String.valueOf(n), "-p", BACKUP_ROOT_DIR };
130    ByteArrayOutputStream baos = new ByteArrayOutputStream();
131    System.setOut(new PrintStream(baos));
132
133    LOG.info("Running history command");
134    int ret = ToolRunner.run(conf1, new BackupDriver(), args);
135    assertEquals(0, ret);
136
137    String output = baos.toString();
138    LOG.info(output);
139    baos.close();
140    return output;
141  }
142}