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.master; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.util.ArrayList; 025import java.util.List; 026import java.util.SortedSet; 027 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.client.RegionInfoBuilder; 032import org.apache.hadoop.hbase.testclassification.MasterTests; 033import org.apache.hadoop.hbase.testclassification.SmallTests; 034import org.apache.hadoop.hbase.util.Bytes; 035import org.apache.hadoop.hbase.util.Pair; 036 037import org.junit.ClassRule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040 041 042 043/** 044 * Test small utility methods inside {@link MetaFixer}. 045 * For cluster tests see {@link TestMetaFixer} 046 */ 047@Category({MasterTests.class, SmallTests.class}) 048public class TestMetaFixerNoCluster { 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestMetaFixerNoCluster.class); 052 private static byte [] A = Bytes.toBytes("a"); 053 private static byte [] B = Bytes.toBytes("b"); 054 private static byte [] C = Bytes.toBytes("c"); 055 private static byte [] D = Bytes.toBytes("d"); 056 private static RegionInfo ALL = RegionInfoBuilder.FIRST_META_REGIONINFO; 057 private static RegionInfo _ARI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 058 setEndKey(A).build(); 059 private static RegionInfo _BRI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 060 setEndKey(B).build(); 061 private static RegionInfo ABRI = RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 062 setStartKey(A).setEndKey(B).build(); 063 private static RegionInfo ACRI = 064 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 065 setStartKey(A).setEndKey(C).build(); 066 private static RegionInfo CDRI = 067 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 068 setStartKey(C).setEndKey(D).build(); 069 private static RegionInfo ADRI = 070 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 071 setStartKey(A).setEndKey(D).build(); 072 private static RegionInfo D_RI = 073 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 074 setStartKey(D).build(); 075 private static RegionInfo C_RI = 076 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME). 077 setStartKey(C).build(); 078 079 @Test 080 public void testGetRegionInfoWithLargestEndKey() { 081 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(_ARI, _BRI).equals(_BRI)); 082 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(C_RI, D_RI).equals(C_RI)); 083 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(ABRI, CDRI).equals(CDRI)); 084 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, CDRI).equals(CDRI)); 085 assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, null) == null); 086 } 087 088 @Test 089 public void testIsOverlap() { 090 assertTrue(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(ABRI, ACRI))); 091 assertFalse(MetaFixer.isOverlap(_ARI, new Pair<RegionInfo, RegionInfo>(C_RI, D_RI))); 092 assertTrue(MetaFixer.isOverlap(ADRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI))); 093 assertFalse(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI))); 094 } 095 096 @Test 097 public void testCalculateMergesNoAggregation() { 098 List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>(); 099 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()); 105 } 106 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()); 116 } 117 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()); 127 } 128 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()); 140 } 141}