001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertFalse; 023import static org.junit.Assert.assertNotEquals; 024import static org.junit.Assert.assertTrue; 025import static org.junit.Assert.fail; 026 027import java.io.IOException; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.fs.FileStatus; 030import org.apache.hadoop.fs.Path; 031import org.apache.hadoop.hbase.HBaseClassTestRule; 032import org.apache.hadoop.hbase.HBaseTestingUtil; 033import org.apache.hadoop.hbase.HConstants; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.client.RegionInfo; 036import org.apache.hadoop.hbase.client.RegionInfoBuilder; 037import org.apache.hadoop.hbase.client.RegionInfoDisplay; 038import org.apache.hadoop.hbase.client.TableDescriptor; 039import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 040import org.apache.hadoop.hbase.exceptions.DeserializationException; 041import org.apache.hadoop.hbase.master.RegionState; 042import org.apache.hadoop.hbase.testclassification.RegionServerTests; 043import org.apache.hadoop.hbase.testclassification.SmallTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 046import org.apache.hadoop.hbase.util.FSTableDescriptors; 047import org.apache.hadoop.hbase.util.MD5Hash; 048import org.junit.Assert; 049import org.junit.ClassRule; 050import org.junit.Rule; 051import org.junit.Test; 052import org.junit.experimental.categories.Category; 053import org.junit.rules.TestName; 054 055import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations; 056 057import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 058import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos; 059 060@Category({ RegionServerTests.class, SmallTests.class }) 061public class TestRegionInfo { 062 063 @ClassRule 064 public static final HBaseClassTestRule CLASS_RULE = 065 HBaseClassTestRule.forClass(TestRegionInfo.class); 066 067 @Rule 068 public TestName name = new TestName(); 069 070 @Test 071 public void testIsStart() { 072 assertTrue(RegionInfoBuilder.FIRST_META_REGIONINFO.isFirst()); 073 org.apache.hadoop.hbase.client.RegionInfo ri = org.apache.hadoop.hbase.client.RegionInfoBuilder 074 .newBuilder(TableName.META_TABLE_NAME).setStartKey(Bytes.toBytes("not_start")).build(); 075 assertFalse(ri.isFirst()); 076 } 077 078 @Test 079 public void testIsEnd() { 080 assertTrue(RegionInfoBuilder.FIRST_META_REGIONINFO.isFirst()); 081 org.apache.hadoop.hbase.client.RegionInfo ri = org.apache.hadoop.hbase.client.RegionInfoBuilder 082 .newBuilder(TableName.META_TABLE_NAME).setEndKey(Bytes.toBytes("not_end")).build(); 083 assertFalse(ri.isLast()); 084 } 085 086 @Test 087 public void testIsNext() { 088 byte[] bytes = Bytes.toBytes("row"); 089 org.apache.hadoop.hbase.client.RegionInfo ri = org.apache.hadoop.hbase.client.RegionInfoBuilder 090 .newBuilder(TableName.META_TABLE_NAME).setEndKey(bytes).build(); 091 org.apache.hadoop.hbase.client.RegionInfo ri2 = org.apache.hadoop.hbase.client.RegionInfoBuilder 092 .newBuilder(TableName.META_TABLE_NAME).setStartKey(bytes).build(); 093 assertFalse(ri.isNext(RegionInfoBuilder.FIRST_META_REGIONINFO)); 094 assertTrue(ri.isNext(ri2)); 095 } 096 097 @Test 098 public void testIsOverlap() { 099 byte[] a = Bytes.toBytes("a"); 100 byte[] b = Bytes.toBytes("b"); 101 byte[] c = Bytes.toBytes("c"); 102 byte[] d = Bytes.toBytes("d"); 103 org.apache.hadoop.hbase.client.RegionInfo all = RegionInfoBuilder.FIRST_META_REGIONINFO; 104 org.apache.hadoop.hbase.client.RegionInfo ari = org.apache.hadoop.hbase.client.RegionInfoBuilder 105 .newBuilder(TableName.META_TABLE_NAME).setEndKey(a).build(); 106 org.apache.hadoop.hbase.client.RegionInfo abri = 107 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 108 .setStartKey(a).setEndKey(b).build(); 109 org.apache.hadoop.hbase.client.RegionInfo adri = 110 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 111 .setStartKey(a).setEndKey(d).build(); 112 org.apache.hadoop.hbase.client.RegionInfo cdri = 113 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 114 .setStartKey(c).setEndKey(d).build(); 115 org.apache.hadoop.hbase.client.RegionInfo dri = org.apache.hadoop.hbase.client.RegionInfoBuilder 116 .newBuilder(TableName.META_TABLE_NAME).setStartKey(d).build(); 117 assertTrue(all.isOverlap(all)); 118 assertTrue(all.isOverlap(abri)); 119 assertFalse(abri.isOverlap(cdri)); 120 assertTrue(all.isOverlap(ari)); 121 assertFalse(ari.isOverlap(abri)); 122 assertFalse(ari.isOverlap(abri)); 123 assertTrue(ari.isOverlap(all)); 124 assertTrue(dri.isOverlap(all)); 125 assertTrue(abri.isOverlap(adri)); 126 assertFalse(dri.isOverlap(ari)); 127 assertTrue(abri.isOverlap(adri)); 128 assertTrue(adri.isOverlap(abri)); 129 } 130 131 /** 132 * Tests {@link RegionInfo#isOverlap(RegionInfo)} 133 */ 134 @Test 135 public void testIsOverlaps() { 136 byte[] a = Bytes.toBytes("a"); 137 byte[] b = Bytes.toBytes("b"); 138 byte[] c = Bytes.toBytes("c"); 139 byte[] d = Bytes.toBytes("d"); 140 byte[] e = Bytes.toBytes("e"); 141 byte[] f = Bytes.toBytes("f"); 142 org.apache.hadoop.hbase.client.RegionInfo ari = org.apache.hadoop.hbase.client.RegionInfoBuilder 143 .newBuilder(TableName.META_TABLE_NAME).setEndKey(a).build(); 144 org.apache.hadoop.hbase.client.RegionInfo abri = 145 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 146 .setStartKey(a).setEndKey(b).build(); 147 org.apache.hadoop.hbase.client.RegionInfo eri = org.apache.hadoop.hbase.client.RegionInfoBuilder 148 .newBuilder(TableName.META_TABLE_NAME).setEndKey(e).build(); 149 org.apache.hadoop.hbase.client.RegionInfo cdri = 150 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 151 .setStartKey(c).setEndKey(d).build(); 152 org.apache.hadoop.hbase.client.RegionInfo efri = 153 org.apache.hadoop.hbase.client.RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME) 154 .setStartKey(e).setEndKey(f).build(); 155 assertFalse(ari.isOverlap(abri)); 156 assertTrue(abri.isOverlap(eri)); 157 assertFalse(cdri.isOverlap(efri)); 158 assertTrue(eri.isOverlap(ari)); 159 } 160 161 @Test 162 public void testPb() throws DeserializationException { 163 RegionInfo hri = RegionInfoBuilder.FIRST_META_REGIONINFO; 164 byte[] bytes = RegionInfo.toByteArray(hri); 165 RegionInfo pbhri = RegionInfo.parseFrom(bytes); 166 assertTrue(hri.equals(pbhri)); 167 } 168 169 @Test 170 public void testReadAndWriteHRegionInfoFile() throws IOException, InterruptedException { 171 HBaseTestingUtil htu = new HBaseTestingUtil(); 172 RegionInfo hri = RegionInfoBuilder.FIRST_META_REGIONINFO; 173 Path basedir = htu.getDataTestDir(); 174 // Create a region. That'll write the .regioninfo file. 175 FSTableDescriptors fsTableDescriptors = new FSTableDescriptors(htu.getConfiguration()); 176 FSTableDescriptors.tryUpdateMetaTableDescriptor(htu.getConfiguration()); 177 HRegion r = HBaseTestingUtil.createRegionAndWAL(hri, basedir, htu.getConfiguration(), 178 fsTableDescriptors.get(TableName.META_TABLE_NAME)); 179 // Get modtime on the file. 180 long modtime = getModTime(r); 181 HBaseTestingUtil.closeRegionAndWAL(r); 182 Thread.sleep(1001); 183 r = HRegion.openHRegion(basedir, hri, fsTableDescriptors.get(TableName.META_TABLE_NAME), null, 184 htu.getConfiguration()); 185 // Ensure the file is not written for a second time. 186 long modtime2 = getModTime(r); 187 assertEquals(modtime, modtime2); 188 // Now load the file. 189 org.apache.hadoop.hbase.client.RegionInfo deserializedHri = 190 HRegionFileSystem.loadRegionInfoFileContent(r.getRegionFileSystem().getFileSystem(), 191 r.getRegionFileSystem().getRegionDir()); 192 assertEquals(0, 193 org.apache.hadoop.hbase.client.RegionInfo.COMPARATOR.compare(hri, deserializedHri)); 194 HBaseTestingUtil.closeRegionAndWAL(r); 195 } 196 197 long getModTime(final HRegion r) throws IOException { 198 FileStatus[] statuses = r.getRegionFileSystem().getFileSystem().listStatus( 199 new Path(r.getRegionFileSystem().getRegionDir(), HRegionFileSystem.REGION_INFO_FILE)); 200 assertTrue(statuses != null && statuses.length == 1); 201 return statuses[0].getModificationTime(); 202 } 203 204 @Test 205 public void testCreateHRegionInfoName() throws Exception { 206 final String tableName = name.getMethodName(); 207 final TableName tn = TableName.valueOf(tableName); 208 String startKey = "startkey"; 209 final byte[] sk = Bytes.toBytes(startKey); 210 String id = "id"; 211 212 // old format region name 213 byte[] name = RegionInfo.createRegionName(tn, sk, id, false); 214 String nameStr = Bytes.toString(name); 215 assertEquals(tableName + "," + startKey + "," + id, nameStr); 216 217 // new format region name. 218 String md5HashInHex = MD5Hash.getMD5AsHex(name); 219 assertEquals(RegionInfo.MD5_HEX_LENGTH, md5HashInHex.length()); 220 name = RegionInfo.createRegionName(tn, sk, id, true); 221 nameStr = Bytes.toString(name); 222 assertEquals(tableName + "," + startKey + "," + id + "." + md5HashInHex + ".", nameStr); 223 } 224 225 @Test 226 public void testContainsRange() { 227 TableDescriptor tableDesc = 228 TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName())).build(); 229 RegionInfo hri = RegionInfoBuilder.newBuilder(tableDesc.getTableName()) 230 .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("g")).build(); 231 // Single row range at start of region 232 assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a"))); 233 // Fully contained range 234 assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c"))); 235 // Range overlapping start of region 236 assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c"))); 237 // Fully contained single-row range 238 assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c"))); 239 // Range that overlaps end key and hence doesn't fit 240 assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g"))); 241 // Single row range on end key 242 assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g"))); 243 // Single row range entirely outside 244 assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z"))); 245 246 // Degenerate range 247 try { 248 hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a")); 249 fail("Invalid range did not throw IAE"); 250 } catch (IllegalArgumentException iae) { 251 } 252 } 253 254 @Test 255 public void testContainsRangeForMetaTable() { 256 TableDescriptor tableDesc = 257 TableDescriptorBuilder.newBuilder(TableName.META_TABLE_NAME).build(); 258 RegionInfo hri = RegionInfoBuilder.newBuilder(tableDesc.getTableName()).build(); 259 byte[] startRow = HConstants.EMPTY_START_ROW; 260 byte[] row1 = Bytes.toBytes("a,a,0"); 261 byte[] row2 = Bytes.toBytes("aaaaa,,1"); 262 byte[] row3 = Bytes.toBytes("aaaaa,\u0000\u0000,2"); 263 byte[] row4 = Bytes.toBytes("aaaaa,\u0001,3"); 264 byte[] row5 = Bytes.toBytes("aaaaa,a,4"); 265 byte[] row6 = Bytes.toBytes("aaaaa,\u1000,5"); 266 267 // Single row range at start of region 268 assertTrue(hri.containsRange(startRow, startRow)); 269 // Fully contained range 270 assertTrue(hri.containsRange(row1, row2)); 271 assertTrue(hri.containsRange(row2, row3)); 272 assertTrue(hri.containsRange(row3, row4)); 273 assertTrue(hri.containsRange(row4, row5)); 274 assertTrue(hri.containsRange(row5, row6)); 275 // Range overlapping start of region 276 assertTrue(hri.containsRange(startRow, row2)); 277 // Fully contained single-row range 278 assertTrue(hri.containsRange(row1, row1)); 279 // Degenerate range 280 try { 281 hri.containsRange(row3, row2); 282 fail("Invalid range did not throw IAE"); 283 } catch (IllegalArgumentException iae) { 284 } 285 } 286 287 @Test 288 public void testLastRegionCompare() { 289 TableDescriptor tableDesc = 290 TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName())).build(); 291 RegionInfo hrip = RegionInfoBuilder.newBuilder(tableDesc.getTableName()) 292 .setStartKey(Bytes.toBytes("a")).build(); 293 RegionInfo hric = RegionInfoBuilder.newBuilder(tableDesc.getTableName()) 294 .setStartKey(Bytes.toBytes("a")).setEndKey(Bytes.toBytes("b")).build(); 295 assertTrue(hrip.compareTo(hric) > 0); 296 } 297 298 @Test 299 public void testMetaTables() { 300 assertTrue(RegionInfoBuilder.FIRST_META_REGIONINFO.isMetaRegion()); 301 } 302 303 @SuppressWarnings("SelfComparison") 304 @Test 305 public void testComparator() { 306 final TableName tableName = TableName.valueOf(name.getMethodName()); 307 RegionInfo older = RegionInfoBuilder.newBuilder(tableName).setRegionId(0).build(); 308 RegionInfo newer = RegionInfoBuilder.newBuilder(tableName).setRegionId(1).build(); 309 assertTrue(older.compareTo(newer) < 0); 310 assertTrue(newer.compareTo(older) > 0); 311 assertEquals(0, older.compareTo(older)); 312 assertEquals(0, newer.compareTo(newer)); 313 314 RegionInfo a = RegionInfoBuilder.newBuilder(TableName.valueOf("a")).build(); 315 RegionInfo b = RegionInfoBuilder.newBuilder(TableName.valueOf("b")).build(); 316 assertNotEquals(0, a.compareTo(b)); 317 TableName t = TableName.valueOf("t"); 318 byte[] midway = Bytes.toBytes("midway"); 319 a = RegionInfoBuilder.newBuilder(t).setEndKey(midway).build(); 320 b = RegionInfoBuilder.newBuilder(t).setStartKey(midway).build(); 321 assertTrue(a.compareTo(b) < 0); 322 assertTrue(b.compareTo(a) > 0); 323 assertTrue(a.equals(a)); 324 assertEquals(0, a.compareTo(a)); 325 a = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("a")) 326 .setEndKey(Bytes.toBytes("d")).build(); 327 b = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("e")) 328 .setEndKey(Bytes.toBytes("g")).build(); 329 assertTrue(a.compareTo(b) < 0); 330 a = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("aaaa")) 331 .setEndKey(Bytes.toBytes("dddd")).build(); 332 b = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("e")) 333 .setEndKey(Bytes.toBytes("g")).build(); 334 assertTrue(a.compareTo(b) < 0); 335 a = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("aaaa")) 336 .setEndKey(Bytes.toBytes("dddd")).build(); 337 b = RegionInfoBuilder.newBuilder(t).setStartKey(Bytes.toBytes("aaaa")) 338 .setEndKey(Bytes.toBytes("eeee")).build(); 339 assertTrue(a.compareTo(b) < 0); 340 341 } 342 343 @Test 344 public void testRegionNameForRegionReplicas() throws Exception { 345 String tableName = name.getMethodName(); 346 final TableName tn = TableName.valueOf(tableName); 347 String startKey = "startkey"; 348 final byte[] sk = Bytes.toBytes(startKey); 349 String id = "id"; 350 351 // assert with only the region name without encoding 352 353 // primary, replicaId = 0 354 byte[] name = RegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0, false); 355 String nameStr = Bytes.toString(name); 356 assertEquals(tableName + "," + startKey + "," + id, nameStr); 357 358 // replicaId = 1 359 name = RegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 1, false); 360 nameStr = Bytes.toString(name); 361 assertEquals( 362 tableName + "," + startKey + "," + id + "_" + String.format(RegionInfo.REPLICA_ID_FORMAT, 1), 363 nameStr); 364 365 // replicaId = max 366 name = RegionInfo.createRegionName(tn, sk, Bytes.toBytes(id), 0xFFFF, false); 367 nameStr = Bytes.toString(name); 368 assertEquals(tableName + "," + startKey + "," + id + "_" 369 + String.format(RegionInfo.REPLICA_ID_FORMAT, 0xFFFF), nameStr); 370 } 371 372 @Test 373 public void testParseName() throws IOException { 374 final TableName tableName = TableName.valueOf(name.getMethodName()); 375 byte[] startKey = Bytes.toBytes("startKey"); 376 long regionId = EnvironmentEdgeManager.currentTime(); 377 int replicaId = 42; 378 379 // test without replicaId 380 byte[] regionName = RegionInfo.createRegionName(tableName, startKey, regionId, false); 381 382 byte[][] fields = RegionInfo.parseRegionName(regionName); 383 assertArrayEquals(Bytes.toString(fields[0]), tableName.getName(), fields[0]); 384 assertArrayEquals(Bytes.toString(fields[1]), startKey, fields[1]); 385 assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)), fields[2]); 386 assertEquals(3, fields.length); 387 388 // test with replicaId 389 regionName = RegionInfo.createRegionName(tableName, startKey, regionId, replicaId, false); 390 391 fields = RegionInfo.parseRegionName(regionName); 392 assertArrayEquals(Bytes.toString(fields[0]), tableName.getName(), fields[0]); 393 assertArrayEquals(Bytes.toString(fields[1]), startKey, fields[1]); 394 assertArrayEquals(Bytes.toString(fields[2]), Bytes.toBytes(Long.toString(regionId)), fields[2]); 395 assertArrayEquals(Bytes.toString(fields[3]), 396 Bytes.toBytes(String.format(RegionInfo.REPLICA_ID_FORMAT, replicaId)), fields[3]); 397 } 398 399 @Test 400 public void testConvert() { 401 final TableName tableName = TableName.valueOf("ns1:" + name.getMethodName()); 402 byte[] startKey = Bytes.toBytes("startKey"); 403 byte[] endKey = Bytes.toBytes("endKey"); 404 boolean split = false; 405 long regionId = EnvironmentEdgeManager.currentTime(); 406 int replicaId = 42; 407 408 RegionInfo hri = RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(endKey) 409 .setSplit(split).setRegionId(regionId).setReplicaId(replicaId).build(); 410 411 // convert two times, compare 412 RegionInfo convertedHri = ProtobufUtil.toRegionInfo(ProtobufUtil.toRegionInfo(hri)); 413 414 assertEquals(hri, convertedHri); 415 416 // test convert RegionInfo without replicaId 417 HBaseProtos.RegionInfo info = HBaseProtos.RegionInfo.newBuilder() 418 .setTableName(HBaseProtos.TableName.newBuilder() 419 .setQualifier(UnsafeByteOperations.unsafeWrap(tableName.getQualifier())) 420 .setNamespace(UnsafeByteOperations.unsafeWrap(tableName.getNamespace())).build()) 421 .setStartKey(UnsafeByteOperations.unsafeWrap(startKey)) 422 .setEndKey(UnsafeByteOperations.unsafeWrap(endKey)).setSplit(split).setRegionId(regionId) 423 .build(); 424 425 convertedHri = ProtobufUtil.toRegionInfo(info); 426 // expecting default replicaId 427 RegionInfo expectedHri = RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey) 428 .setEndKey(endKey).setSplit(split).setRegionId(regionId).setReplicaId(0).build(); 429 430 assertEquals(expectedHri, convertedHri); 431 } 432 433 @Test 434 public void testRegionDetailsForDisplay() throws IOException { 435 byte[] startKey = new byte[] { 0x01, 0x01, 0x02, 0x03 }; 436 byte[] endKey = new byte[] { 0x01, 0x01, 0x02, 0x04 }; 437 Configuration conf = new Configuration(); 438 conf.setBoolean("hbase.display.keys", false); 439 RegionInfo h = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())) 440 .setStartKey(startKey).setEndKey(endKey).build(); 441 checkEquality(h, conf); 442 // check HRIs with non-default replicaId 443 h = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).setStartKey(startKey) 444 .setEndKey(endKey).setRegionId(EnvironmentEdgeManager.currentTime()).setReplicaId(1).build(); 445 checkEquality(h, conf); 446 assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY, 447 RegionInfoDisplay.getEndKeyForDisplay(h, conf)); 448 assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY, 449 RegionInfoDisplay.getStartKeyForDisplay(h, conf)); 450 451 RegionState state = RegionState.createForTesting(h, RegionState.State.OPEN); 452 String descriptiveNameForDisplay = 453 RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf); 454 checkDescriptiveNameEquality(descriptiveNameForDisplay, state.toDescriptiveString(), startKey); 455 456 conf.setBoolean("hbase.display.keys", true); 457 assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(h, conf)); 458 assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(h, conf)); 459 assertEquals(state.toDescriptiveString(), 460 RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf)); 461 } 462 463 private void checkDescriptiveNameEquality(String descriptiveNameForDisplay, String origDesc, 464 byte[] startKey) { 465 // except for the "hidden-start-key" substring everything else should exactly match 466 String firstPart = descriptiveNameForDisplay.substring(0, 467 descriptiveNameForDisplay.indexOf(new String(RegionInfoDisplay.HIDDEN_START_KEY))); 468 String secondPart = descriptiveNameForDisplay 469 .substring(descriptiveNameForDisplay.indexOf(new String(RegionInfoDisplay.HIDDEN_START_KEY)) 470 + RegionInfoDisplay.HIDDEN_START_KEY.length); 471 String firstPartOrig = origDesc.substring(0, origDesc.indexOf(Bytes.toStringBinary(startKey))); 472 String secondPartOrig = origDesc.substring( 473 origDesc.indexOf(Bytes.toStringBinary(startKey)) + Bytes.toStringBinary(startKey).length()); 474 assert (firstPart.equals(firstPartOrig)); 475 assert (secondPart.equals(secondPartOrig)); 476 } 477 478 private void checkEquality(RegionInfo h, Configuration conf) throws IOException { 479 byte[] modifiedRegionName = RegionInfoDisplay.getRegionNameForDisplay(h, conf); 480 byte[][] modifiedRegionNameParts = RegionInfo.parseRegionName(modifiedRegionName); 481 byte[][] regionNameParts = RegionInfo.parseRegionName(h.getRegionName()); 482 483 // same number of parts 484 assert (modifiedRegionNameParts.length == regionNameParts.length); 485 486 for (int i = 0; i < regionNameParts.length; i++) { 487 // all parts should match except for [1] where in the modified one, 488 // we should have "hidden_start_key" 489 if (i != 1) { 490 Assert.assertArrayEquals(regionNameParts[i], modifiedRegionNameParts[i]); 491 } else { 492 assertNotEquals(regionNameParts[i][0], modifiedRegionNameParts[i][0]); 493 Assert.assertArrayEquals(modifiedRegionNameParts[1], 494 RegionInfoDisplay.getStartKeyForDisplay(h, conf)); 495 } 496 } 497 } 498}