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.assertArrayEquals;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
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.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.apache.hadoop.hbase.TableName;
36  import org.apache.hadoop.hbase.exceptions.DeserializationException;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionInfo;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.FSTableDescriptors;
41  import org.apache.hadoop.hbase.util.MD5Hash;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  import com.google.protobuf.ByteString;
46  
47  @Category(SmallTests.class)
48  public class TestHRegionInfo {
49    @Test
50    public void testPb() throws DeserializationException {
51      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
52      byte [] bytes = hri.toByteArray();
53      HRegionInfo pbhri = HRegionInfo.parseFrom(bytes);
54      assertTrue(hri.equals(pbhri));
55    }
56  
57    @Test
58    public void testReadAndWriteHRegionInfoFile() throws IOException, InterruptedException {
59      HBaseTestingUtility htu = new HBaseTestingUtility();
60      HRegionInfo hri = HRegionInfo.FIRST_META_REGIONINFO;
61      Path basedir = htu.getDataTestDir();
62      FSTableDescriptors fsTableDescriptors = new FSTableDescriptors(htu.getConfiguration());
63      // Create a region.  That'll write the .regioninfo file.
64      HRegion r = HRegion.createHRegion(hri, basedir, htu.getConfiguration(),
65        fsTableDescriptors.get(TableName.META_TABLE_NAME));
66      // Get modtime on the file.
67      long modtime = getModTime(r);
68      HRegion.closeHRegion(r);
69      Thread.sleep(1001);
70      r = HRegion.openHRegion(basedir, hri, fsTableDescriptors.get(TableName.META_TABLE_NAME),
71        null, htu.getConfiguration());
72      // Ensure the file is not written for a second time.
73      long modtime2 = getModTime(r);
74      assertEquals(modtime, modtime2);
75      // Now load the file.
76      HRegionInfo deserializedHri = HRegionFileSystem.loadRegionInfoFileContent(
77          r.getRegionFileSystem().getFileSystem(), r.getRegionFileSystem().getRegionDir());
78      assertTrue(hri.equals(deserializedHri));
79    }
80  
81    long getModTime(final HRegion r) throws IOException {
82      FileStatus[] statuses = r.getRegionFileSystem().getFileSystem().listStatus(
83        new Path(r.getRegionFileSystem().getRegionDir(), HRegionFileSystem.REGION_INFO_FILE));
84      assertTrue(statuses != null && statuses.length == 1);
85      return statuses[0].getModificationTime();
86    }
87  
88    @Test
89    public void testCreateHRegionInfoName() throws Exception {
90      String tableName = "tablename";
91      final TableName tn = TableName.valueOf(tableName);
92      String startKey = "startkey";
93      final byte[] sk = Bytes.toBytes(startKey);
94      String id = "id";
95  
96      // old format region name
97      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
98      String nameStr = Bytes.toString(name);
99      assertEquals(tableName + "," + startKey + "," + id, nameStr);
100 
101 
102     // new format region name.
103     String md5HashInHex = MD5Hash.getMD5AsHex(name);
104     assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
105     name = HRegionInfo.createRegionName(tn, sk, id, true);
106     nameStr = Bytes.toString(name);
107     assertEquals(tableName + "," + startKey + ","
108                  + id + "." + md5HashInHex + ".",
109                  nameStr);
110   }
111 
112   @Test
113   public void testContainsRange() {
114     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
115     HRegionInfo hri = new HRegionInfo(
116         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("g"));
117     // Single row range at start of region
118     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
119     // Fully contained range
120     assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
121     // Range overlapping start of region
122     assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
123     // Fully contained single-row range
124     assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
125     // Range that overlaps end key and hence doesn't fit
126     assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
127     // Single row range on end key
128     assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
129     // Single row range entirely outside
130     assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
131 
132     // Degenerate range
133     try {
134       hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
135       fail("Invalid range did not throw IAE");
136     } catch (IllegalArgumentException iae) {
137     }
138   }
139 
140   @Test
141   public void testLastRegionCompare() {
142     HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("testtable"));
143     HRegionInfo hrip = new HRegionInfo(
144         tableDesc.getTableName(), Bytes.toBytes("a"), new byte[0]);
145     HRegionInfo hric = new HRegionInfo(
146         tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("b"));
147     assertTrue(hrip.compareTo(hric) > 0);
148   }
149 
150   @Test
151   public void testMetaTables() {
152     assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
153   }
154 
155   @Test
156   public void testComparator() {
157     TableName tablename = TableName.valueOf("comparatorTablename");
158     byte[] empty = new byte[0];
159     HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L);
160     HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L);
161     assertTrue(older.compareTo(newer) < 0);
162     assertTrue(newer.compareTo(older) > 0);
163     assertTrue(older.compareTo(older) == 0);
164     assertTrue(newer.compareTo(newer) == 0);
165   }
166 
167   @Test
168   public void testRegionNameForRegionReplicas() throws Exception {
169     String tableName = "tablename";
170     final TableName tn = TableName.valueOf(tableName);
171     String startKey = "startkey";
172     final byte[] sk = Bytes.toBytes(startKey);
173     String id = "id";
174 
175     // assert with only the region name without encoding
176 
177     // primary, replicaId = 0
178     byte [] name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0, false);
179     String nameStr = Bytes.toString(name);
180     assertEquals(tableName + "," + startKey + "," + id, nameStr);
181 
182     // replicaId = 1
183     name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 1, false);
184     nameStr = Bytes.toString(name);
185     assertEquals(tableName + "," + startKey + "," + id + "_" +
186       String.format(HRegionInfo.REPLICA_ID_FORMAT, 1), nameStr);
187 
188     // replicaId = max
189     name = HRegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0xFFFF, false);
190     nameStr = Bytes.toString(name);
191     assertEquals(tableName + "," + startKey + "," + id + "_" +
192         String.format(HRegionInfo.REPLICA_ID_FORMAT, 0xFFFF), nameStr);
193   }
194 
195   @Test
196   public void testParseName() throws IOException {
197     TableName tableName = TableName.valueOf("testParseName");
198     byte[] startKey = Bytes.toBytes("startKey");
199     long regionId = System.currentTimeMillis();
200     int replicaId = 42;
201 
202     // test without replicaId
203     byte[] regionName = HRegionInfo.createRegionName(tableName, startKey, regionId, false);
204 
205     byte[][] fields = HRegionInfo.parseRegionName(regionName);
206     assertArrayEquals(Bytes.toString(fields[0]),tableName.getName(), fields[0]);
207     assertArrayEquals(Bytes.toString(fields[1]),startKey, fields[1]);
208     assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)),fields[2]);
209     assertEquals(3, fields.length);
210 
211     // test with replicaId
212     regionName = HRegionInfo.createRegionName(tableName, startKey, regionId,
213       replicaId, false);
214 
215     fields = HRegionInfo.parseRegionName(regionName);
216     assertArrayEquals(Bytes.toString(fields[0]),tableName.getName(), fields[0]);
217     assertArrayEquals(Bytes.toString(fields[1]),startKey, fields[1]);
218     assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)),fields[2]);
219     assertArrayEquals(Bytes.toString(fields[3]), Bytes.toBytes(
220       String.format(HRegionInfo.REPLICA_ID_FORMAT, replicaId)), fields[3]);
221   }
222 
223   @Test
224   public void testConvert() {
225     TableName tableName = TableName.valueOf("ns1:table1");
226     byte[] startKey = Bytes.toBytes("startKey");
227     byte[] endKey = Bytes.toBytes("endKey");
228     boolean split = false;
229     long regionId = System.currentTimeMillis();
230     int replicaId = 42;
231 
232 
233     HRegionInfo hri = new HRegionInfo(tableName, startKey, endKey, split,
234       regionId, replicaId);
235 
236     // convert two times, compare
237     HRegionInfo convertedHri = HRegionInfo.convert(HRegionInfo.convert(hri));
238 
239     assertEquals(hri, convertedHri);
240 
241     // test convert RegionInfo without replicaId
242     RegionInfo info = RegionInfo.newBuilder()
243       .setTableName(HBaseProtos.TableName.newBuilder()
244         .setQualifier(ByteString.copyFrom(tableName.getQualifier()))
245         .setNamespace(ByteString.copyFrom(tableName.getNamespace()))
246         .build())
247       .setStartKey(ByteString.copyFrom(startKey))
248       .setEndKey(ByteString.copyFrom(endKey))
249       .setSplit(split)
250       .setRegionId(regionId)
251       .build();
252 
253     convertedHri = HRegionInfo.convert(info);
254     HRegionInfo expectedHri = new HRegionInfo(tableName, startKey, endKey, split,
255       regionId, 0); // expecting default replicaId
256 
257     assertEquals(expectedHri, convertedHri);
258   }
259 
260 }
261