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.util.ArrayList; 023import java.util.Collection; 024import java.util.List; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.MiniHBaseCluster; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.backup.impl.BackupAdminImpl; 029import org.apache.hadoop.hbase.backup.util.BackupUtils; 030import org.apache.hadoop.hbase.client.Admin; 031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 032import org.apache.hadoop.hbase.client.Connection; 033import org.apache.hadoop.hbase.client.ConnectionFactory; 034import org.apache.hadoop.hbase.client.Put; 035import org.apache.hadoop.hbase.client.Table; 036import org.apache.hadoop.hbase.client.TableDescriptor; 037import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 038import org.apache.hadoop.hbase.regionserver.HRegion; 039import org.apache.hadoop.hbase.testclassification.LargeTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 042import org.junit.Assert; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.junit.runner.RunWith; 047import org.junit.runners.Parameterized; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 052 053@Category(LargeTests.class) 054@RunWith(Parameterized.class) 055public class TestIncrementalBackup extends TestBackupBase { 056 057 @ClassRule 058 public static final HBaseClassTestRule CLASS_RULE = 059 HBaseClassTestRule.forClass(TestIncrementalBackup.class); 060 061 private static final Logger LOG = LoggerFactory.getLogger(TestIncrementalBackup.class); 062 063 @Parameterized.Parameters 064 public static Collection<Object[]> data() { 065 provider = "multiwal"; 066 List<Object[]> params = new ArrayList<>(); 067 params.add(new Object[] { Boolean.TRUE }); 068 return params; 069 } 070 071 public TestIncrementalBackup(Boolean b) { 072 } 073 074 // implement all test cases in 1 test since incremental 075 // backup/restore has dependencies 076 @Test 077 public void TestIncBackupRestore() throws Exception { 078 int ADD_ROWS = 99; 079 080 // #1 - create full backup for all tables 081 LOG.info("create full backup image for all tables"); 082 List<TableName> tables = Lists.newArrayList(table1, table2); 083 final byte[] fam3Name = Bytes.toBytes("f3"); 084 final byte[] mobName = Bytes.toBytes("mob"); 085 086 TableDescriptor newTable1Desc = TableDescriptorBuilder.newBuilder(table1Desc) 087 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam3Name)) 088 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(mobName).setMobEnabled(true) 089 .setMobThreshold(5L).build()) 090 .build(); 091 TEST_UTIL.getAdmin().modifyTable(newTable1Desc); 092 093 try (Connection conn = ConnectionFactory.createConnection(conf1)) { 094 int NB_ROWS_FAM3 = 6; 095 insertIntoTable(conn, table1, fam3Name, 3, NB_ROWS_FAM3).close(); 096 insertIntoTable(conn, table1, mobName, 3, NB_ROWS_FAM3).close(); 097 Admin admin = conn.getAdmin(); 098 BackupAdminImpl client = new BackupAdminImpl(conn); 099 BackupRequest request = createBackupRequest(BackupType.FULL, tables, BACKUP_ROOT_DIR); 100 String backupIdFull = client.backupTables(request); 101 assertTrue(checkSucceeded(backupIdFull)); 102 103 // #2 - insert some data to table 104 Table t1 = insertIntoTable(conn, table1, famName, 1, ADD_ROWS); 105 LOG.debug("writing " + ADD_ROWS + " rows to " + table1); 106 Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH + ADD_ROWS + NB_ROWS_FAM3); 107 LOG.debug("written " + ADD_ROWS + " rows to " + table1); 108 // additionally, insert rows to MOB cf 109 int NB_ROWS_MOB = 111; 110 insertIntoTable(conn, table1, mobName, 3, NB_ROWS_MOB); 111 LOG.debug("written " + NB_ROWS_MOB + " rows to " + table1 + " to Mob enabled CF"); 112 t1.close(); 113 Assert.assertEquals(TEST_UTIL.countRows(t1), NB_ROWS_IN_BATCH + ADD_ROWS + NB_ROWS_MOB); 114 Table t2 = conn.getTable(table2); 115 Put p2; 116 for (int i = 0; i < 5; i++) { 117 p2 = new Put(Bytes.toBytes("row-t2" + i)); 118 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i)); 119 t2.put(p2); 120 } 121 Assert.assertEquals(NB_ROWS_IN_BATCH + 5, TEST_UTIL.countRows(t2)); 122 t2.close(); 123 LOG.debug("written " + 5 + " rows to " + table2); 124 // split table1 125 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 126 List<HRegion> regions = cluster.getRegions(table1); 127 byte[] name = regions.get(0).getRegionInfo().getRegionName(); 128 long startSplitTime = EnvironmentEdgeManager.currentTime(); 129 try { 130 admin.splitRegionAsync(name).get(); 131 } catch (Exception e) { 132 // although split fail, this may not affect following check in current API, 133 // exception will be thrown. 134 LOG.debug("region is not splittable, because " + e); 135 } 136 while (!admin.isTableAvailable(table1)) { 137 Thread.sleep(100); 138 } 139 long endSplitTime = EnvironmentEdgeManager.currentTime(); 140 // split finished 141 LOG.debug("split finished in =" + (endSplitTime - startSplitTime)); 142 143 // #3 - incremental backup for multiple tables 144 tables = Lists.newArrayList(table1, table2); 145 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR); 146 String backupIdIncMultiple = client.backupTables(request); 147 assertTrue(checkSucceeded(backupIdIncMultiple)); 148 149 // add column family f2 to table1 150 // drop column family f3 151 final byte[] fam2Name = Bytes.toBytes("f2"); 152 newTable1Desc = TableDescriptorBuilder.newBuilder(newTable1Desc) 153 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam2Name)).removeColumnFamily(fam3Name) 154 .build(); 155 TEST_UTIL.getAdmin().modifyTable(newTable1Desc); 156 157 int NB_ROWS_FAM2 = 7; 158 Table t3 = insertIntoTable(conn, table1, fam2Name, 2, NB_ROWS_FAM2); 159 t3.close(); 160 161 // Wait for 5 sec to make sure that old WALs were deleted 162 Thread.sleep(5000); 163 164 // #4 - additional incremental backup for multiple tables 165 request = createBackupRequest(BackupType.INCREMENTAL, tables, BACKUP_ROOT_DIR); 166 String backupIdIncMultiple2 = client.backupTables(request); 167 assertTrue(checkSucceeded(backupIdIncMultiple2)); 168 169 // #5 - restore full backup for all tables 170 TableName[] tablesRestoreFull = new TableName[] { table1, table2 }; 171 TableName[] tablesMapFull = new TableName[] { table1_restore, table2_restore }; 172 173 LOG.debug("Restoring full " + backupIdFull); 174 client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false, 175 tablesRestoreFull, tablesMapFull, true)); 176 177 // #6.1 - check tables for full restore 178 Admin hAdmin = TEST_UTIL.getAdmin(); 179 assertTrue(hAdmin.tableExists(table1_restore)); 180 assertTrue(hAdmin.tableExists(table2_restore)); 181 hAdmin.close(); 182 183 // #6.2 - checking row count of tables for full restore 184 Table hTable = conn.getTable(table1_restore); 185 Assert.assertEquals(TEST_UTIL.countRows(hTable), NB_ROWS_IN_BATCH + NB_ROWS_FAM3); 186 hTable.close(); 187 188 hTable = conn.getTable(table2_restore); 189 Assert.assertEquals(NB_ROWS_IN_BATCH, TEST_UTIL.countRows(hTable)); 190 hTable.close(); 191 192 // #7 - restore incremental backup for multiple tables, with overwrite 193 TableName[] tablesRestoreIncMultiple = new TableName[] { table1, table2 }; 194 TableName[] tablesMapIncMultiple = new TableName[] { table1_restore, table2_restore }; 195 client.restore(BackupUtils.createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple2, false, 196 tablesRestoreIncMultiple, tablesMapIncMultiple, true)); 197 hTable = conn.getTable(table1_restore); 198 199 LOG.debug("After incremental restore: " + hTable.getDescriptor()); 200 int countFamName = TEST_UTIL.countRows(hTable, famName); 201 LOG.debug("f1 has " + countFamName + " rows"); 202 Assert.assertEquals(countFamName, NB_ROWS_IN_BATCH + ADD_ROWS); 203 204 int countFam2Name = TEST_UTIL.countRows(hTable, fam2Name); 205 LOG.debug("f2 has " + countFam2Name + " rows"); 206 Assert.assertEquals(countFam2Name, NB_ROWS_FAM2); 207 208 int countMobName = TEST_UTIL.countRows(hTable, mobName); 209 LOG.debug("mob has " + countMobName + " rows"); 210 Assert.assertEquals(countMobName, NB_ROWS_MOB); 211 hTable.close(); 212 213 hTable = conn.getTable(table2_restore); 214 Assert.assertEquals(NB_ROWS_IN_BATCH + 5, TEST_UTIL.countRows(hTable)); 215 hTable.close(); 216 admin.close(); 217 } 218 } 219}