1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  import org.apache.hadoop.hbase.HRegionInfo;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.SmallTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.util.MD5Hash;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(SmallTests.class)
36  public class TestHRegionInfo {
37    @Test
38    public void testCreateHRegionInfoName() throws Exception {
39      String tableName = "tablename";
40      final byte [] tn = Bytes.toBytes(tableName);
41      String startKey = "startkey";
42      final byte [] sk = Bytes.toBytes(startKey);
43      String id = "id";
44  
45      // old format region name
46      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
47      String nameStr = Bytes.toString(name);
48      assertEquals(tableName + "," + startKey + "," + id, nameStr);
49  
50  
51      // new format region name.
52      String md5HashInHex = MD5Hash.getMD5AsHex(name);
53      assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
54      name = HRegionInfo.createRegionName(tn, sk, id, true);
55      nameStr = Bytes.toString(name);
56      assertEquals(tableName + "," + startKey + ","
57                   + id + "." + md5HashInHex + ".",
58                   nameStr);
59    }
60  
61    @Test
62    public void testContainsRange() {
63      HTableDescriptor tableDesc = new HTableDescriptor("testtable");
64      HRegionInfo hri = new HRegionInfo(
65          tableDesc.getName(), Bytes.toBytes("a"), Bytes.toBytes("g"));
66      // Single row range at start of region
67      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
68      // Fully contained range
69      assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
70      // Range overlapping start of region
71      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
72      // Fully contained single-row range
73      assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
74      // Range that overlaps end key and hence doesn't fit
75      assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
76      // Single row range on end key
77      assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
78      // Single row range entirely outside
79      assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
80  
81      // Degenerate range
82      try {
83        hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
84        fail("Invalid range did not throw IAE");
85      } catch (IllegalArgumentException iae) {
86      }
87    }
88  
89    @Test
90    public void testLastRegionCompare() {
91      HTableDescriptor tableDesc = new HTableDescriptor("testtable");
92      HRegionInfo hrip = new HRegionInfo(
93          tableDesc.getName(), Bytes.toBytes("a"), new byte[0]);
94      HRegionInfo hric = new HRegionInfo(
95          tableDesc.getName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
96      assertTrue(hrip.compareTo(hric) > 0);
97    }
98  
99    @Test
100   public void testMetaTables() {
101     assertTrue(HRegionInfo.ROOT_REGIONINFO.isMetaTable());
102     assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
103   }
104 
105   @Test
106   public void testComparator() {
107     byte[] tablename = Bytes.toBytes("comparatorTablename");
108     byte[] empty = new byte[0];
109     HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L);
110     HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L);
111     assertTrue(older.compareTo(newer) < 0);
112     assertTrue(newer.compareTo(older) > 0);
113     assertTrue(older.compareTo(older) == 0);
114     assertTrue(newer.compareTo(newer) == 0);
115   }
116 
117   @org.junit.Rule
118   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
119     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
120 }
121