|
| 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 |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 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. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.snapshot; |
| 19 | + |
| 20 | +import static org.awaitility.Awaitility.await; |
| 21 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 22 | +import static org.hamcrest.Matchers.hasSize; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.List; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.apache.hadoop.fs.Path; |
| 30 | +import org.apache.hadoop.hbase.TableName; |
| 31 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 32 | +import org.apache.hadoop.hbase.client.Put; |
| 33 | +import org.apache.hadoop.hbase.client.RegionInfo; |
| 34 | +import org.apache.hadoop.hbase.client.Table; |
| 35 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 36 | +import org.apache.hadoop.hbase.tool.BulkLoadHFilesTool; |
| 37 | +import org.apache.hadoop.hbase.util.Bytes; |
| 38 | +import org.apache.hadoop.hbase.util.HFileTestUtil; |
| 39 | +import org.junit.jupiter.api.TestTemplate; |
| 40 | + |
| 41 | +import org.apache.hbase.thirdparty.com.google.common.collect.Lists; |
| 42 | + |
| 43 | +public class ExportFileSystemStateWithMergeOrSplitRegionTestBase extends ExportSnapshotTestBase { |
| 44 | + |
| 45 | + protected ExportFileSystemStateWithMergeOrSplitRegionTestBase(boolean mob) { |
| 46 | + super(mob); |
| 47 | + } |
| 48 | + |
| 49 | + @TestTemplate |
| 50 | + public void testExportFileSystemStateWithMergeRegion() throws Exception { |
| 51 | + // disable compaction |
| 52 | + admin.compactionSwitch(false, |
| 53 | + admin.getRegionServers().stream().map(a -> a.getServerName()).collect(Collectors.toList())); |
| 54 | + // create Table |
| 55 | + String suffix = mob ? methodName + "-mob" : methodName; |
| 56 | + TableName tableName0 = TableName.valueOf("testtb-" + suffix + "-1"); |
| 57 | + String snapshotName0 = "snaptb0-" + suffix + "-1"; |
| 58 | + admin.createTable( |
| 59 | + TableDescriptorBuilder.newBuilder(tableName0) |
| 60 | + .setColumnFamilies( |
| 61 | + Lists.newArrayList(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build())) |
| 62 | + .build(), |
| 63 | + new byte[][] { Bytes.toBytes("2") }); |
| 64 | + // put some data |
| 65 | + try (Table table = admin.getConnection().getTable(tableName0)) { |
| 66 | + table.put(new Put(Bytes.toBytes("1")).addColumn(FAMILY, null, Bytes.toBytes("1"))); |
| 67 | + table.put(new Put(Bytes.toBytes("2")).addColumn(FAMILY, null, Bytes.toBytes("2"))); |
| 68 | + } |
| 69 | + List<RegionInfo> regions = admin.getRegions(tableName0); |
| 70 | + assertEquals(2, regions.size()); |
| 71 | + tableNumFiles = regions.size(); |
| 72 | + // merge region |
| 73 | + admin.mergeRegionsAsync(new byte[][] { regions.get(0).getEncodedNameAsBytes(), |
| 74 | + regions.get(1).getEncodedNameAsBytes() }, true).get(); |
| 75 | + await().atMost(Duration.ofSeconds(30)) |
| 76 | + .untilAsserted(() -> assertThat(admin.getRegions(tableName0), hasSize(1))); |
| 77 | + // take a snapshot |
| 78 | + admin.snapshot(snapshotName0, tableName0); |
| 79 | + // export snapshot and verify |
| 80 | + testExportFileSystemState(tableName0, snapshotName0, snapshotName0, tableNumFiles); |
| 81 | + } |
| 82 | + |
| 83 | + @TestTemplate |
| 84 | + public void testExportFileSystemStateWithSplitRegion() throws Exception { |
| 85 | + // disable compaction |
| 86 | + admin.compactionSwitch(false, |
| 87 | + admin.getRegionServers().stream().map(a -> a.getServerName()).collect(Collectors.toList())); |
| 88 | + // create Table |
| 89 | + String suffix = mob ? methodName + "-mob" : methodName; |
| 90 | + TableName splitTableName = TableName.valueOf(suffix); |
| 91 | + String splitTableSnap = "snapshot-" + suffix; |
| 92 | + admin.createTable(TableDescriptorBuilder.newBuilder(splitTableName).setColumnFamilies( |
| 93 | + Lists.newArrayList(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build())).build()); |
| 94 | + |
| 95 | + Path output = TEST_UTIL.getDataTestDir("output/cf"); |
| 96 | + TEST_UTIL.getTestFileSystem().mkdirs(output); |
| 97 | + // Create and load a large hfile to ensure the execution time of MR job. |
| 98 | + HFileTestUtil.createHFile(TEST_UTIL.getConfiguration(), TEST_UTIL.getTestFileSystem(), |
| 99 | + new Path(output, "test_file"), FAMILY, Bytes.toBytes("q"), Bytes.toBytes("1"), |
| 100 | + Bytes.toBytes("9"), 9999999); |
| 101 | + BulkLoadHFilesTool tool = new BulkLoadHFilesTool(TEST_UTIL.getConfiguration()); |
| 102 | + tool.run(new String[] { output.getParent().toString(), splitTableName.getNameAsString() }); |
| 103 | + |
| 104 | + List<RegionInfo> regions = admin.getRegions(splitTableName); |
| 105 | + assertEquals(1, regions.size()); |
| 106 | + tableNumFiles = regions.size(); |
| 107 | + |
| 108 | + // split region |
| 109 | + admin.split(splitTableName, Bytes.toBytes("5")); |
| 110 | + await().atMost(Duration.ofSeconds(30)) |
| 111 | + .untilAsserted(() -> assertThat(admin.getRegions(splitTableName), hasSize(2))); |
| 112 | + |
| 113 | + // take a snapshot |
| 114 | + admin.snapshot(splitTableSnap, splitTableName); |
| 115 | + // export snapshot and verify |
| 116 | + Configuration tmpConf = TEST_UTIL.getConfiguration(); |
| 117 | + // Decrease the buffer size of copier to avoid the export task finished shortly |
| 118 | + tmpConf.setInt("snapshot.export.buffer.size", 1); |
| 119 | + // Decrease the maximum files of each mapper to ensure the three files(1 hfile + 2 reference |
| 120 | + // files) copied in different mappers concurrently. |
| 121 | + tmpConf.setInt("snapshot.export.default.map.group", 1); |
| 122 | + testExportFileSystemState(tmpConf, splitTableName, splitTableSnap, splitTableSnap, |
| 123 | + tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), getHdfsDestinationDir(), false, false, |
| 124 | + getBypassRegionPredicate(), true, false); |
| 125 | + } |
| 126 | +} |
0 commit comments