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