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.assertNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.IOException;
29  
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.FSTableDescriptors;
33  import org.apache.hadoop.hbase.util.FSUtils;
34  import org.apache.hadoop.hbase.util.MD5Hash;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  @Category(SmallTests.class)
39  public class TestHRegionInfo {
40    @Test
41    public void testCreateHRegionInfoName() throws Exception {
42      String tableName = "tablename";
43      final byte [] tn = Bytes.toBytes(tableName);
44      String startKey = "startkey";
45      final byte [] sk = Bytes.toBytes(startKey);
46      String id = "id";
47  
48      // old format region name
49      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
50      String nameStr = Bytes.toString(name);
51      assertEquals(tableName + "," + startKey + "," + id, nameStr);
52  
53  
54      // new format region name.
55      String md5HashInHex = MD5Hash.getMD5AsHex(name);
56      assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
57      name = HRegionInfo.createRegionName(tn, sk, id, true);
58      nameStr = Bytes.toString(name);
59      assertEquals(tableName + "," + startKey + ","
60                   + id + "." + md5HashInHex + ".",
61                   nameStr);
62    }
63    
64    @Test
65    public void testGetSetOfHTD() throws IOException {
66      HBaseTestingUtility HTU = new HBaseTestingUtility();
67          final String tablename = "testGetSetOfHTD";
68  
69      // Delete the temporary table directory that might still be there from the
70      // previous test run.
71      FSTableDescriptors.deleteTableDescriptorIfExists(tablename,
72          HTU.getConfiguration());
73  
74      HTableDescriptor htd = new HTableDescriptor(tablename);
75      FSTableDescriptors.createTableDescriptor(htd, HTU.getConfiguration());
76      HRegionInfo hri = new HRegionInfo(Bytes.toBytes("testGetSetOfHTD"),
77          HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
78      HTableDescriptor htd2 = hri.getTableDesc();
79      assertTrue(htd.equals(htd2));
80      final String key = "SOME_KEY";
81      assertNull(htd.getValue(key));
82      final String value = "VALUE";
83      htd.setValue(key, value);
84      hri.setTableDesc(htd);
85      HTableDescriptor htd3 = hri.getTableDesc();
86      assertTrue(htd.equals(htd3));
87    }
88    
89    @Test
90    public void testContainsRange() {
91      HTableDescriptor tableDesc = new HTableDescriptor("testtable");
92      HRegionInfo hri = new HRegionInfo(
93          tableDesc.getName(), Bytes.toBytes("a"), Bytes.toBytes("g"));
94      // Single row range at start of region
95      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
96      // Fully contained range
97      assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
98      // Range overlapping start of region
99      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
100     // Fully contained single-row range
101     assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
102     // Range that overlaps end key and hence doesn't fit
103     assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
104     // Single row range on end key
105     assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
106     // Single row range entirely outside
107     assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
108     
109     // Degenerate range
110     try {
111       hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
112       fail("Invalid range did not throw IAE");
113     } catch (IllegalArgumentException iae) {
114     }
115   }
116 
117   @Test
118   public void testLastRegionCompare() {
119     HTableDescriptor tableDesc = new HTableDescriptor("testtable");
120     HRegionInfo hrip = new HRegionInfo(
121         tableDesc.getName(), Bytes.toBytes("a"), new byte[0]);
122     HRegionInfo hric = new HRegionInfo(
123         tableDesc.getName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
124     assertTrue(hrip.compareTo(hric) > 0);
125   }
126 
127   @Test
128   public void testMetaTables() {
129     assertTrue(HRegionInfo.ROOT_REGIONINFO.isMetaTable());
130     assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
131   }
132 
133   @Test
134   public void testComparator() {
135     byte[] tablename = Bytes.toBytes("comparatorTablename");
136     byte[] empty = new byte[0];
137     HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L); 
138     HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L); 
139     assertTrue(older.compareTo(newer) < 0);
140     assertTrue(newer.compareTo(older) > 0);
141     assertTrue(older.compareTo(older) == 0);
142     assertTrue(newer.compareTo(newer) == 0);
143   }
144   
145   @org.junit.Rule
146   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
147     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
148 }
149