1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.RegionInfo;
38 import org.apache.hadoop.hbase.protobuf.generated.TableProtos;
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
64 HRegion r = HRegion.createHRegion(hri, basedir, htu.getConfiguration(),
65 fsTableDescriptors.get(TableName.META_TABLE_NAME));
66
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
73 long modtime2 = getModTime(r);
74 assertEquals(modtime, modtime2);
75
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
97 byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
98 String nameStr = Bytes.toString(name);
99 assertEquals(tableName + "," + startKey + "," + id, nameStr);
100
101
102
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
118 assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
119
120 assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
121
122 assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
123
124 assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
125
126 assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
127
128 assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
129
130 assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
131
132
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 @SuppressWarnings("SelfComparison")
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
177
178
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
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
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
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
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
238 HRegionInfo convertedHri = HRegionInfo.convert(HRegionInfo.convert(hri));
239
240 assertEquals(hri, convertedHri);
241
242
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);
257
258 assertEquals(expectedHri, convertedHri);
259 }
260
261 }
262