HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestRemoteBackup.java
blob4150d3fd2fc5786c09ec2d31ff9c6515720c374c
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.backup;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import java.util.concurrent.CountDownLatch;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtil;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.backup.util.BackupUtils;
29 import org.apache.hadoop.hbase.client.Admin;
30 import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
31 import org.apache.hadoop.hbase.client.Connection;
32 import org.apache.hadoop.hbase.client.ConnectionFactory;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.client.TableDescriptor;
36 import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
37 import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
38 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
39 import org.apache.hadoop.hbase.testclassification.LargeTests;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.junit.Assert;
42 import org.junit.BeforeClass;
43 import org.junit.ClassRule;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
51 @Category(LargeTests.class)
52 public class TestRemoteBackup extends TestBackupBase {
54 @ClassRule
55 public static final HBaseClassTestRule CLASS_RULE =
56 HBaseClassTestRule.forClass(TestRemoteBackup.class);
58 private static final Logger LOG = LoggerFactory.getLogger(TestRemoteBackup.class);
60 /**
61 * Setup Cluster with appropriate configurations before running tests.
63 * @throws Exception if starting the mini cluster or setting up the tables fails
65 @BeforeClass
66 public static void setUp() throws Exception {
67 TEST_UTIL = new HBaseTestingUtil();
68 conf1 = TEST_UTIL.getConfiguration();
69 conf1.setInt(HConstants.REGION_SERVER_HANDLER_COUNT, 10);
70 useSecondCluster = true;
71 setUpHelper();
74 /**
75 * Verify that a remote full backup is created on a single table with data correctly.
77 * @throws Exception if an operation on the table fails
79 @Test
80 public void testFullBackupRemote() throws Exception {
81 LOG.info("test remote full backup on a single table");
82 final CountDownLatch latch = new CountDownLatch(1);
83 final int NB_ROWS_IN_FAM3 = 6;
84 final byte[] fam3Name = Bytes.toBytes("f3");
85 final byte[] fam2Name = Bytes.toBytes("f2");
86 final Connection conn = ConnectionFactory.createConnection(conf1);
87 Thread t = new Thread(() -> {
88 try {
89 latch.await();
90 } catch (InterruptedException ie) {
92 try {
93 Table t1 = conn.getTable(table1);
94 Put p1;
95 for (int i = 0; i < NB_ROWS_IN_FAM3; i++) {
96 p1 = new Put(Bytes.toBytes("row-t1" + i));
97 p1.addColumn(fam3Name, qualName, Bytes.toBytes("val" + i));
98 t1.put(p1);
100 LOG.debug("Wrote " + NB_ROWS_IN_FAM3 + " rows into family3");
101 t1.close();
102 } catch (IOException ioe) {
103 throw new RuntimeException(ioe);
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);
115 SnapshotTestingUtils.loadData(TEST_UTIL, table1, 50, fam2Name);
116 Table t1 = conn.getTable(table1);
117 int rows0 = MobSnapshotTestingUtils.countMobRows(t1, fam2Name);
119 latch.countDown();
120 String backupId =
121 backupTables(BackupType.FULL, Lists.newArrayList(table1), BACKUP_REMOTE_ROOT_DIR);
122 assertTrue(checkSucceeded(backupId));
124 LOG.info("backup complete " + backupId);
125 Assert.assertEquals(TEST_UTIL.countRows(t1, famName), NB_ROWS_IN_BATCH);
127 t.join();
128 Assert.assertEquals(TEST_UTIL.countRows(t1, fam3Name), NB_ROWS_IN_FAM3);
129 t1.close();
131 TableName[] tablesRestoreFull = new TableName[] { table1 };
133 TableName[] tablesMapFull = new TableName[] { table1_restore };
135 BackupAdmin client = getBackupAdmin();
136 client.restore(BackupUtils.createRestoreRequest(BACKUP_REMOTE_ROOT_DIR, backupId, false,
137 tablesRestoreFull, tablesMapFull, false));
139 // check tables for full restore
140 Admin hAdmin = TEST_UTIL.getAdmin();
141 assertTrue(hAdmin.tableExists(table1_restore));
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);
149 int rows1 = MobSnapshotTestingUtils.countMobRows(t1, fam2Name);
150 Assert.assertEquals(rows0, rows1);
151 hTable.close();
153 hAdmin.close();