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