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.IOException;
023import java.util.concurrent.CountDownLatch;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.backup.util.BackupUtils;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.Connection;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.client.TableDescriptor;
036import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
037import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
038import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
039import org.apache.hadoop.hbase.testclassification.LargeTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.junit.Assert;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
050
051@Category(LargeTests.class)
052public class TestRemoteBackup extends TestBackupBase {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestRemoteBackup.class);
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestRemoteBackup.class);
059
060  /**
061   * Setup Cluster with appropriate configurations before running tests.
062   *
063   * @throws Exception if starting the mini cluster or setting up the tables fails
064   */
065  @BeforeClass
066  public static void setUp() throws Exception {
067    TEST_UTIL = new HBaseTestingUtil();
068    conf1 = TEST_UTIL.getConfiguration();
069    conf1.setInt(HConstants.REGION_SERVER_HANDLER_COUNT, 10);
070    useSecondCluster = true;
071    setUpHelper();
072  }
073
074  /**
075   * Verify that a remote full backup is created on a single table with data correctly.
076   *
077   * @throws Exception if an operation on the table fails
078   */
079  @Test
080  public void testFullBackupRemote() throws Exception {
081    LOG.info("test remote full backup on a single table");
082    final CountDownLatch latch = new CountDownLatch(1);
083    final int NB_ROWS_IN_FAM3 = 6;
084    final byte[] fam3Name = Bytes.toBytes("f3");
085    final byte[] fam2Name = Bytes.toBytes("f2");
086    final Connection conn = ConnectionFactory.createConnection(conf1);
087    Thread t = new Thread(() -> {
088      try {
089        latch.await();
090      } catch (InterruptedException ie) {
091      }
092      try {
093        Table t1 = conn.getTable(table1);
094        Put p1;
095        for (int i = 0; i < NB_ROWS_IN_FAM3; i++) {
096          p1 = new Put(Bytes.toBytes("row-t1" + i));
097          p1.addColumn(fam3Name, qualName, Bytes.toBytes("val" + i));
098          t1.put(p1);
099        }
100        LOG.debug("Wrote " + NB_ROWS_IN_FAM3 + " rows into family3");
101        t1.close();
102      } catch (IOException ioe) {
103        throw new RuntimeException(ioe);
104      }
105    });
106    t.start();
107    // family 2 is MOB enabled
108    TableDescriptor newTable1Desc = TableDescriptorBuilder.newBuilder(table1Desc)
109      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam3Name))
110      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam2Name).setMobEnabled(true)
111        .setMobThreshold(0L).build())
112      .build();
113    TEST_UTIL.getAdmin().modifyTable(newTable1Desc);
114
115    SnapshotTestingUtils.loadData(TEST_UTIL, table1, 50, fam2Name);
116    Table t1 = conn.getTable(table1);
117    int rows0 = MobSnapshotTestingUtils.countMobRows(t1, fam2Name);
118
119    latch.countDown();
120    String backupId =
121        backupTables(BackupType.FULL, Lists.newArrayList(table1), BACKUP_REMOTE_ROOT_DIR);
122    assertTrue(checkSucceeded(backupId));
123
124    LOG.info("backup complete " + backupId);
125    Assert.assertEquals(TEST_UTIL.countRows(t1, famName), NB_ROWS_IN_BATCH);
126
127    t.join();
128    Assert.assertEquals(TEST_UTIL.countRows(t1, fam3Name), NB_ROWS_IN_FAM3);
129    t1.close();
130
131    TableName[] tablesRestoreFull = new TableName[] { table1 };
132
133    TableName[] tablesMapFull = new TableName[] { table1_restore };
134
135    BackupAdmin client = getBackupAdmin();
136    client.restore(BackupUtils.createRestoreRequest(BACKUP_REMOTE_ROOT_DIR, backupId, false,
137      tablesRestoreFull, tablesMapFull, false));
138
139    // check tables for full restore
140    Admin hAdmin = TEST_UTIL.getAdmin();
141    assertTrue(hAdmin.tableExists(table1_restore));
142
143    // #5.2 - checking row count of tables for full restore
144    Table hTable = conn.getTable(table1_restore);
145    Assert.assertEquals(TEST_UTIL.countRows(hTable, famName), NB_ROWS_IN_BATCH);
146    int cnt3 = TEST_UTIL.countRows(hTable, fam3Name);
147    Assert.assertTrue(cnt3 >= 0 && cnt3 <= NB_ROWS_IN_FAM3);
148
149    int rows1 = MobSnapshotTestingUtils.countMobRows(t1, fam2Name);
150    Assert.assertEquals(rows0, rows1);
151    hTable.close();
152
153    hAdmin.close();
154  }
155}