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 * @throws Exception if doing the backup or an operation on the tables fails 064 */ 065 @Test 066 public void testBackupHistory() throws Exception { 067 068 LOG.info("test backup history on a single table with data"); 069 070 List<TableName> tableList = Lists.newArrayList(table1); 071 String backupId = fullTableBackup(tableList); 072 assertTrue(checkSucceeded(backupId)); 073 LOG.info("backup complete"); 074 075 List<BackupInfo> history = getBackupAdmin().getHistory(10); 076 assertTrue(findBackup(history, backupId)); 077 BackupInfo.Filter nullFilter = info -> true; 078 history = BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), nullFilter); 079 assertTrue(findBackup(history, backupId)); 080 081 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 082 System.setOut(new PrintStream(baos)); 083 084 String[] args = new String[] { "history", "-n", "10", "-p", BACKUP_ROOT_DIR }; 085 // Run backup 086 int ret = ToolRunner.run(conf1, new BackupDriver(), args); 087 assertTrue(ret == 0); 088 LOG.info("show_history"); 089 String output = baos.toString(); 090 LOG.info(output); 091 baos.close(); 092 assertTrue(output.indexOf(backupId) > 0); 093 094 tableList = Lists.newArrayList(table2); 095 String backupId2 = fullTableBackup(tableList); 096 assertTrue(checkSucceeded(backupId2)); 097 LOG.info("backup complete: " + table2); 098 BackupInfo.Filter tableNameFilter = image -> { 099 if (table1 == null) { 100 return true; 101 } 102 103 List<TableName> names = image.getTableNames(); 104 return names.contains(table1); 105 }; 106 BackupInfo.Filter tableSetFilter = info -> { 107 String backupId1 = info.getBackupId(); 108 return backupId1.startsWith("backup"); 109 }; 110 111 history = getBackupAdmin().getHistory(10, tableNameFilter, tableSetFilter); 112 assertTrue(history.size() > 0); 113 boolean success = true; 114 for (BackupInfo info : history) { 115 if (!info.getTableNames().contains(table1)) { 116 success = false; 117 break; 118 } 119 } 120 assertTrue(success); 121 122 history = 123 BackupUtils.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), tableNameFilter, tableSetFilter); 124 assertTrue(history.size() > 0); 125 success = true; 126 for (BackupInfo info : history) { 127 if (!info.getTableNames().contains(table1)) { 128 success = false; 129 break; 130 } 131 } 132 assertTrue(success); 133 134 args = 135 new String[] { "history", "-n", "10", "-p", BACKUP_ROOT_DIR, "-t", "table1", "-s", "backup" }; 136 // Run backup 137 ret = ToolRunner.run(conf1, new BackupDriver(), args); 138 assertTrue(ret == 0); 139 LOG.info("show_history"); 140 } 141}