View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.io.IOException;
27  
28  import org.apache.hadoop.fs.FileStatus;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.SmallTests;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.exceptions.DeserializationException;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.MD5Hash;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category(SmallTests.class)
42  public class TestHRegionInfo {
43    @Test
44    public void testPb() throws DeserializationException {
45      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
46      byte [] bytes = hri.toByteArray();
47      HRegionInfo pbhri = HRegionInfo.parseFrom(bytes);
48      assertTrue(hri.equals(pbhri));
49    }
50  
51    @Test
52    public void testReadAndWriteHRegionInfoFile() throws IOException, InterruptedException {
53      HBaseTestingUtility htu = new HBaseTestingUtility();
54      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
55      Path basedir = htu.getDataTestDir();
56      // Create a region.  That'll write the .regioninfo file.
57      HRegion r = HRegion.createHRegion(hri, basedir, htu.getConfiguration(),
58        HTableDescriptor.META_TABLEDESC);
59      // Get modtime on the file.
60      long modtime = getModTime(r);
61      HRegion.closeHRegion(r);
62      Thread.sleep(1001);
63      r = HRegion.openHRegion(basedir, hri, HTableDescriptor.META_TABLEDESC,
64          null, htu.getConfiguration());
65      // Ensure the file is not written for a second time.
66      long modtime2 = getModTime(r);
67      assertEquals(modtime, modtime2);
68      // Now load the file.
69      HRegionInfo deserializedHri = HRegionFileSystem.loadRegionInfoFileContent(
70          r.getRegionFileSystem().getFileSystem(), r.getRegionFileSystem().getRegionDir());
71      assertTrue(hri.equals(deserializedHri));
72    }
73  
74    long getModTime(final HRegion r) throws IOException {
75      FileStatus[] statuses = r.getRegionFileSystem().getFileSystem().listStatus(
76        new Path(r.getRegionFileSystem().getRegionDir(), HRegionFileSystem.REGION_INFO_FILE));
77      assertTrue(statuses != null && statuses.length == 1);
78      return statuses[0].getModificationTime();
79    }
80  
81    @Test
82    public void testCreateHRegionInfoName() throws Exception {
83      String tableName = "tablename";
84      final TableName tn = TableName.valueOf(tableName);
85      String startKey = "startkey";
86      final byte[] sk = Bytes.toBytes(startKey);
87      String id = "id";
88  
89      // old format region name
90      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
91      String nameStr = Bytes.toString(name);
92      assertEquals(tableName + "," + startKey + "," + id, nameStr);
93  
94  
95      // new format region name.
96      String md5HashInHex = MD5Hash.getMD5AsHex(name);
97      assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
98      name = HRegionInfo.createRegionName(tn, sk, id, true);
99      nameStr = Bytes.toString(name);
100     assertEquals(tableName + "," + startKey + ","
101                  + id + "." + md5HashInHex + ".",
102                  nameStr);
103   }
104   
105   @Test
106   public void testContainsRange() {
107     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
108     HRegionInfo hri = new HRegionInfo(
109         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("g"));
110     // Single row range at start of region
111     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
112     // Fully contained range
113     assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
114     // Range overlapping start of region
115     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
116     // Fully contained single-row range
117     assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
118     // Range that overlaps end key and hence doesn't fit
119     assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
120     // Single row range on end key
121     assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
122     // Single row range entirely outside
123     assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
124     
125     // Degenerate range
126     try {
127       hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
128       fail("Invalid range did not throw IAE");
129     } catch (IllegalArgumentException iae) {
130     }
131   }
132 
133   @Test
134   public void testLastRegionCompare() {
135     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
136     HRegionInfo hrip = new HRegionInfo(
137         tableDesc.getTableName(), Bytes.toBytes("a"), new byte[0]);
138     HRegionInfo hric = new HRegionInfo(
139         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
140     assertTrue(hrip.compareTo(hric) > 0);
141   }
142 
143   @Test
144   public void testMetaTables() {
145     assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
146   }
147 
148   @Test
149   public void testComparator() {
150     TableName tablename = TableName.valueOf("comparatorTablename");
151     byte[] empty = new byte[0];
152     HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L); 
153     HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L); 
154     assertTrue(older.compareTo(newer) < 0);
155     assertTrue(newer.compareTo(older) > 0);
156     assertTrue(older.compareTo(older) == 0);
157     assertTrue(newer.compareTo(newer) == 0);
158   }
159   
160 }
161