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.client; 019 020import java.io.IOException; 021import java.nio.charset.StandardCharsets; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.HRegionInfo; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.master.RegionState; 027import org.apache.hadoop.hbase.testclassification.MasterTests; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 031import org.junit.Assert; 032import org.junit.ClassRule; 033import org.junit.Rule; 034import org.junit.Test; 035import org.junit.experimental.categories.Category; 036import org.junit.rules.TestName; 037 038@Category({ MasterTests.class, SmallTests.class }) 039public class TestRegionInfoDisplay { 040 041 @ClassRule 042 public static final HBaseClassTestRule CLASS_RULE = 043 HBaseClassTestRule.forClass(TestRegionInfoDisplay.class); 044 045 @Rule 046 public TestName name = new TestName(); 047 048 @Test 049 public void testRegionDetailsForDisplay() throws IOException { 050 byte[] startKey = new byte[] { 0x01, 0x01, 0x02, 0x03 }; 051 byte[] endKey = new byte[] { 0x01, 0x01, 0x02, 0x04 }; 052 Configuration conf = new Configuration(); 053 conf.setBoolean("hbase.display.keys", false); 054 RegionInfo ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())) 055 .setStartKey(startKey).setEndKey(endKey).build(); 056 checkEquality(ri, conf); 057 // check HRIs with non-default replicaId 058 ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).setStartKey(startKey) 059 .setEndKey(endKey).setSplit(false).setRegionId(EnvironmentEdgeManager.currentTime()) 060 .setReplicaId(1).build(); 061 checkEquality(ri, conf); 062 Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY, 063 RegionInfoDisplay.getEndKeyForDisplay(ri, conf)); 064 Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY, 065 RegionInfoDisplay.getStartKeyForDisplay(ri, conf)); 066 067 RegionState state = RegionState.createForTesting(convert(ri), RegionState.State.OPEN); 068 String descriptiveNameForDisplay = 069 RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf); 070 String originalDescriptive = state.toDescriptiveString(); 071 checkDescriptiveNameEquality(descriptiveNameForDisplay, originalDescriptive, startKey); 072 073 conf.setBoolean("hbase.display.keys", true); 074 Assert.assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(ri, conf)); 075 Assert.assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(ri, conf)); 076 Assert.assertEquals(originalDescriptive, 077 RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf)); 078 } 079 080 private void checkDescriptiveNameEquality(String descriptiveNameForDisplay, String origDesc, 081 byte[] startKey) { 082 // except for the "hidden-start-key" substring everything else should exactly match 083 String firstPart = descriptiveNameForDisplay.substring(0, descriptiveNameForDisplay 084 .indexOf(new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8))); 085 String secondPart = descriptiveNameForDisplay.substring(descriptiveNameForDisplay 086 .indexOf(new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)) 087 + RegionInfoDisplay.HIDDEN_START_KEY.length); 088 String firstPartOrig = origDesc.substring(0, origDesc.indexOf(Bytes.toStringBinary(startKey))); 089 String secondPartOrig = origDesc.substring( 090 origDesc.indexOf(Bytes.toStringBinary(startKey)) + Bytes.toStringBinary(startKey).length()); 091 Assert.assertTrue(firstPart.equals(firstPartOrig)); 092 Assert.assertTrue(secondPart.equals(secondPartOrig)); 093 } 094 095 private void checkEquality(RegionInfo ri, Configuration conf) throws IOException { 096 byte[] modifiedRegionName = RegionInfoDisplay.getRegionNameForDisplay(ri, conf); 097 System.out.println(Bytes.toString(modifiedRegionName) + " " + ri.toString()); 098 byte[][] modifiedRegionNameParts = RegionInfo.parseRegionName(modifiedRegionName); 099 byte[][] regionNameParts = RegionInfo.parseRegionName(ri.getRegionName()); 100 101 // same number of parts 102 assert (modifiedRegionNameParts.length == regionNameParts.length); 103 for (int i = 0; i < regionNameParts.length; i++) { 104 // all parts should match except for [1] where in the modified one, 105 // we should have "hidden_start_key" 106 if (i != 1) { 107 System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " 108 + Bytes.toString(modifiedRegionNameParts[i])); 109 Assert.assertArrayEquals(regionNameParts[i], modifiedRegionNameParts[i]); 110 } else { 111 System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " 112 + Bytes.toString(modifiedRegionNameParts[i])); 113 Assert.assertNotEquals(regionNameParts[i], modifiedRegionNameParts[i]); 114 Assert.assertArrayEquals(modifiedRegionNameParts[1], 115 RegionInfoDisplay.getStartKeyForDisplay(ri, conf)); 116 } 117 } 118 } 119 120 private HRegionInfo convert(RegionInfo ri) { 121 HRegionInfo hri = new HRegionInfo(ri.getTable(), ri.getStartKey(), ri.getEndKey(), ri.isSplit(), 122 ri.getRegionId()); 123 hri.setOffline(ri.isOffline()); 124 return hri; 125 } 126}