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