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