HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMetaFixerNoCluster.java
blob5b80f89bffd317c0ee2d3ecdaa5b7d0fafc5e92d
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.master;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.SortedSet;
28 import org.apache.hadoop.hbase.HBaseClassTestRule;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.client.RegionInfo;
31 import org.apache.hadoop.hbase.client.RegionInfoBuilder;
32 import org.apache.hadoop.hbase.testclassification.MasterTests;
33 import org.apache.hadoop.hbase.testclassification.SmallTests;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.apache.hadoop.hbase.util.Pair;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
43 /**
44 * Test small utility methods inside {@link MetaFixer}.
45 * For cluster tests see {@link TestMetaFixer}
47 @Category({MasterTests.class, SmallTests.class})
48 public class TestMetaFixerNoCluster {
49 @ClassRule
50 public static final HBaseClassTestRule CLASS_RULE =
51 HBaseClassTestRule.forClass(TestMetaFixerNoCluster.class);
52 private static byte [] A = Bytes.toBytes("a");
53 private static byte [] B = Bytes.toBytes("b");
54 private static byte [] C = Bytes.toBytes("c");
55 private static byte [] D = Bytes.toBytes("d");
56 private static RegionInfo ALL = RegionInfoBuilder.FIRST_META_REGIONINFO;
57 private static RegionInfo _ARI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
58 setEndKey(A).build();
59 private static RegionInfo _BRI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
60 setEndKey(B).build();
61 private static RegionInfo ABRI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
62 setStartKey(A).setEndKey(B).build();
63 private static RegionInfo ACRI =
64 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
65 setStartKey(A).setEndKey(C).build();
66 private static RegionInfo CDRI =
67 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
68 setStartKey(C).setEndKey(D).build();
69 private static RegionInfo ADRI =
70 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
71 setStartKey(A).setEndKey(D).build();
72 private static RegionInfo D_RI =
73 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
74 setStartKey(D).build();
75 private static RegionInfo C_RI =
76 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).
77 setStartKey(C).build();
79 @Test
80 public void testGetRegionInfoWithLargestEndKey() {
81 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(_ARI, _BRI).equals(_BRI));
82 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(C_RI, D_RI).equals(C_RI));
83 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(ABRI, CDRI).equals(CDRI));
84 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, CDRI).equals(CDRI));
85 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, null) == null);
88 @Test
89 public void testIsOverlap() {
90 assertTrue(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(ABRI, ACRI)));
91 assertFalse(MetaFixer.isOverlap(_ARI, new Pair<RegionInfo, RegionInfo>(C_RI, D_RI)));
92 assertTrue(MetaFixer.isOverlap(ADRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI)));
93 assertFalse(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI)));
96 @Test
97 public void testCalculateMergesNoAggregation() {
98 List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
99 overlaps.add(new Pair<RegionInfo, RegionInfo>(_ARI, _BRI));
100 overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
101 List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
102 assertEquals(2, merges.size());
103 assertEquals(2, merges.get(0).size());
104 assertEquals(2, merges.get(1).size());
107 @Test
108 public void testCalculateMergesAggregation() {
109 List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
110 overlaps.add(new Pair<RegionInfo, RegionInfo>(ALL, D_RI));
111 overlaps.add(new Pair<RegionInfo, RegionInfo>(_ARI, _BRI));
112 overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
113 List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
114 assertEquals(1, merges.size());
115 assertEquals(5, merges.get(0).size());
118 @Test
119 public void testCalculateMergesNoRepeatOfRegionNames() {
120 List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
121 overlaps.add(new Pair<RegionInfo, RegionInfo>(_BRI, ABRI));
122 overlaps.add(new Pair<RegionInfo, RegionInfo>(ABRI, ADRI));
123 List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
124 assertEquals(1, merges.size());
125 // There should be three regions to merge, not four.
126 assertEquals(3, merges.get(0).size());
129 @Test
130 public void testCalculateMergesRespectsMax() {
131 List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
132 overlaps.add(new Pair<RegionInfo, RegionInfo>(_BRI, ABRI));
133 overlaps.add(new Pair<RegionInfo, RegionInfo>(ABRI, ADRI));
134 overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
135 List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(3, overlaps);
136 assertEquals(2, merges.size());
137 // There should be three regions to merge, not four.
138 assertEquals(3, merges.get(0).size());
139 assertEquals(2, merges.get(1).size());