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