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