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.TableName;
34  import org.apache.hadoop.hbase.exceptions.DeserializationException;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.FSTableDescriptors;
38  import org.apache.hadoop.hbase.util.MD5Hash;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  
42  @Category(SmallTests.class)
43  public class TestHRegionInfo {
44    @Test
45    public void testPb() throws DeserializationException {
46      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
47      byte [] bytes = hri.toByteArray();
48      HRegionInfo pbhri = HRegionInfo.parseFrom(bytes);
49      assertTrue(hri.equals(pbhri));
50    }
51  
52    @Test
53    public void testReadAndWriteHRegionInfoFile() throws IOException, InterruptedException {
54      HBaseTestingUtility htu = new HBaseTestingUtility();
55      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
56      Path basedir = htu.getDataTestDir();
57      FSTableDescriptors fsTableDescriptors = new FSTableDescriptors(htu.getConfiguration());
58      // Create a region.  That'll write the .regioninfo file.
59      HRegion r = HRegion.createHRegion(hri, basedir, htu.getConfiguration(),
60        fsTableDescriptors.get(TableName.META_TABLE_NAME));
61      // Get modtime on the file.
62      long modtime = getModTime(r);
63      HRegion.closeHRegion(r);
64      Thread.sleep(1001);
65      r = HRegion.openHRegion(basedir, hri, fsTableDescriptors.get(TableName.META_TABLE_NAME),
66        null, htu.getConfiguration());
67      // Ensure the file is not written for a second time.
68      long modtime2 = getModTime(r);
69      assertEquals(modtime, modtime2);
70      // Now load the file.
71      HRegionInfo deserializedHri = HRegionFileSystem.loadRegionInfoFileContent(
72          r.getRegionFileSystem().getFileSystem(), r.getRegionFileSystem().getRegionDir());
73      assertTrue(hri.equals(deserializedHri));
74    }
75  
76    long getModTime(final HRegion r) throws IOException {
77      FileStatus[] statuses = r.getRegionFileSystem().getFileSystem().listStatus(
78        new Path(r.getRegionFileSystem().getRegionDir(), HRegionFileSystem.REGION_INFO_FILE));
79      assertTrue(statuses != null && statuses.length == 1);
80      return statuses[0].getModificationTime();
81    }
82  
83    @Test
84    public void testCreateHRegionInfoName() throws Exception {
85      String tableName = "tablename";
86      final TableName tn = TableName.valueOf(tableName);
87      String startKey = "startkey";
88      final byte[] sk = Bytes.toBytes(startKey);
89      String id = "id";
90  
91      // old format region name
92      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
93      String nameStr = Bytes.toString(name);
94      assertEquals(tableName + "," + startKey + "," + id, nameStr);
95  
96  
97      // new format region name.
98      String md5HashInHex = MD5Hash.getMD5AsHex(name);
99      assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
100     name = HRegionInfo.createRegionName(tn, sk, id, true);
101     nameStr = Bytes.toString(name);
102     assertEquals(tableName + "," + startKey + ","
103                  + id + "." + md5HashInHex + ".",
104                  nameStr);
105   }
106   
107   @Test
108   public void testContainsRange() {
109     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
110     HRegionInfo hri = new HRegionInfo(
111         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("g"));
112     // Single row range at start of region
113     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
114     // Fully contained range
115     assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
116     // Range overlapping start of region
117     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
118     // Fully contained single-row range
119     assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
120     // Range that overlaps end key and hence doesn't fit
121     assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
122     // Single row range on end key
123     assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
124     // Single row range entirely outside
125     assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
126     
127     // Degenerate range
128     try {
129       hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
130       fail("Invalid range did not throw IAE");
131     } catch (IllegalArgumentException iae) {
132     }
133   }
134 
135   @Test
136   public void testLastRegionCompare() {
137     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
138     HRegionInfo hrip = new HRegionInfo(
139         tableDesc.getTableName(), Bytes.toBytes("a"), new byte[0]);
140     HRegionInfo hric = new HRegionInfo(
141         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
142     assertTrue(hrip.compareTo(hric) > 0);
143   }
144 
145   @Test
146   public void testMetaTables() {
147     assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
148   }
149 
150   @Test
151   public void testComparator() {
152     TableName tablename = TableName.valueOf("comparatorTablename");
153     byte[] empty = new byte[0];
154     HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L); 
155     HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L); 
156     assertTrue(older.compareTo(newer) < 0);
157     assertTrue(newer.compareTo(older) > 0);
158     assertTrue(older.compareTo(older) == 0);
159     assertTrue(newer.compareTo(newer) == 0);
160   }
161   
162 }
163