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.janitor;
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;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.client.RegionInfoBuilder;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.Pair;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039/**
040 * Test small utility methods inside {@link MetaFixer}. For cluster tests see {@link TestMetaFixer}
041 */
042@Category({ MasterTests.class, SmallTests.class })
043public class TestMetaFixerNoCluster {
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestMetaFixerNoCluster.class);
047  private static byte[] A = Bytes.toBytes("a");
048  private static byte[] B = Bytes.toBytes("b");
049  private static byte[] C = Bytes.toBytes("c");
050  private static byte[] D = Bytes.toBytes("d");
051  private static RegionInfo ALL = RegionInfoBuilder.FIRST_META_REGIONINFO;
052  private static RegionInfo _ARI =
053    RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).setEndKey(A).build();
054  private static RegionInfo _BRI =
055    RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).setEndKey(B).build();
056  private static RegionInfo ABRI =
057    RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).setStartKey(A).setEndKey(B).build();
058  private static RegionInfo ACRI = org.apache.hadoop.hbase.client.RegionInfoBuilder
059    .newBuilder(TableName.META_TABLE_NAME).setStartKey(A).setEndKey(C).build();
060  private static RegionInfo CDRI = org.apache.hadoop.hbase.client.RegionInfoBuilder
061    .newBuilder(TableName.META_TABLE_NAME).setStartKey(C).setEndKey(D).build();
062  private static RegionInfo ADRI = org.apache.hadoop.hbase.client.RegionInfoBuilder
063    .newBuilder(TableName.META_TABLE_NAME).setStartKey(A).setEndKey(D).build();
064  private static RegionInfo D_RI = org.apache.hadoop.hbase.client.RegionInfoBuilder
065    .newBuilder(TableName.META_TABLE_NAME).setStartKey(D).build();
066  private static RegionInfo C_RI = org.apache.hadoop.hbase.client.RegionInfoBuilder
067    .newBuilder(TableName.META_TABLE_NAME).setStartKey(C).build();
068
069  @Test
070  public void testGetRegionInfoWithLargestEndKey() {
071    assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(_ARI, _BRI).equals(_BRI));
072    assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(C_RI, D_RI).equals(C_RI));
073    assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(ABRI, CDRI).equals(CDRI));
074    assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, CDRI).equals(CDRI));
075    assertTrue(MetaFixer.getRegionInfoWithLargestEndKey(null, null) == null);
076  }
077
078  @Test
079  public void testIsOverlap() {
080    assertTrue(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(ABRI, ACRI)));
081    assertFalse(MetaFixer.isOverlap(_ARI, new Pair<RegionInfo, RegionInfo>(C_RI, D_RI)));
082    assertTrue(MetaFixer.isOverlap(ADRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI)));
083    assertFalse(MetaFixer.isOverlap(_BRI, new Pair<RegionInfo, RegionInfo>(CDRI, C_RI)));
084  }
085
086  @Test
087  public void testCalculateMergesNoAggregation() {
088    List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
089    overlaps.add(new Pair<RegionInfo, RegionInfo>(_ARI, _BRI));
090    overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
091    List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
092    assertEquals(2, merges.size());
093    assertEquals(2, merges.get(0).size());
094    assertEquals(2, merges.get(1).size());
095  }
096
097  @Test
098  public void testCalculateMergesAggregation() {
099    List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
100    overlaps.add(new Pair<RegionInfo, RegionInfo>(ALL, D_RI));
101    overlaps.add(new Pair<RegionInfo, RegionInfo>(_ARI, _BRI));
102    overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
103    List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
104    assertEquals(1, merges.size());
105    assertEquals(5, merges.get(0).size());
106  }
107
108  @Test
109  public void testCalculateMergesNoRepeatOfRegionNames() {
110    List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
111    overlaps.add(new Pair<RegionInfo, RegionInfo>(_BRI, ABRI));
112    overlaps.add(new Pair<RegionInfo, RegionInfo>(ABRI, ADRI));
113    List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(10, overlaps);
114    assertEquals(1, merges.size());
115    // There should be three regions to merge, not four.
116    assertEquals(3, merges.get(0).size());
117  }
118
119  @Test
120  public void testCalculateMergesRespectsMax() {
121    List<Pair<RegionInfo, RegionInfo>> overlaps = new ArrayList<>();
122    overlaps.add(new Pair<RegionInfo, RegionInfo>(_BRI, ABRI));
123    overlaps.add(new Pair<RegionInfo, RegionInfo>(ABRI, ADRI));
124    overlaps.add(new Pair<RegionInfo, RegionInfo>(C_RI, D_RI));
125    List<SortedSet<RegionInfo>> merges = MetaFixer.calculateMerges(3, overlaps);
126    assertEquals(2, merges.size());
127    // There should be three regions to merge, not four.
128    assertEquals(3, merges.get(0).size());
129    assertEquals(2, merges.get(1).size());
130  }
131}