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