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.TableName; 025import org.apache.hadoop.hbase.master.RegionState; 026import org.apache.hadoop.hbase.testclassification.MasterTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.hadoop.hbase.util.Bytes; 029import org.junit.Assert; 030import org.junit.ClassRule; 031import org.junit.Rule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034import org.junit.rules.TestName; 035 036@Category({MasterTests.class, SmallTests.class}) 037public class TestRegionInfoDisplay { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestRegionInfoDisplay.class); 042 043 @Rule public TestName name = new TestName(); 044 045 @Test 046 public void testRegionDetailsForDisplay() throws IOException { 047 byte[] startKey = new byte[] {0x01, 0x01, 0x02, 0x03}; 048 byte[] endKey = new byte[] {0x01, 0x01, 0x02, 0x04}; 049 Configuration conf = new Configuration(); 050 conf.setBoolean("hbase.display.keys", false); 051 RegionInfo ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())) 052 .setStartKey(startKey).setEndKey(endKey).build(); 053 checkEquality(ri, conf); 054 // check HRIs with non-default replicaId 055 ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())) 056 .setStartKey(startKey) 057 .setEndKey(endKey) 058 .setSplit(false) 059 .setRegionId(System.currentTimeMillis()) 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(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, 084 descriptiveNameForDisplay.indexOf( 085 new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8))); 086 String secondPart = descriptiveNameForDisplay.substring( 087 descriptiveNameForDisplay.indexOf( 088 new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)) + 089 RegionInfoDisplay.HIDDEN_START_KEY.length); 090 String firstPartOrig = origDesc.substring(0, origDesc.indexOf(Bytes.toStringBinary(startKey))); 091 String secondPartOrig = origDesc.substring( 092 origDesc.indexOf(Bytes.toStringBinary(startKey)) + 093 Bytes.toStringBinary(startKey).length()); 094 assert(firstPart.equals(firstPartOrig)); 095 assert(secondPart.equals(secondPartOrig)); 096 } 097 098 private void checkEquality(RegionInfo ri, Configuration conf) throws IOException { 099 byte[] modifiedRegionName = RegionInfoDisplay.getRegionNameForDisplay(ri, conf); 100 System.out.println(Bytes.toString(modifiedRegionName) + " " + ri.toString()); 101 byte[][] modifiedRegionNameParts = RegionInfo.parseRegionName(modifiedRegionName); 102 byte[][] regionNameParts = RegionInfo.parseRegionName(ri.getRegionName()); 103 104 //same number of parts 105 assert(modifiedRegionNameParts.length == regionNameParts.length); 106 for (int i = 0; i < regionNameParts.length; i++) { 107 // all parts should match except for [1] where in the modified one, 108 // we should have "hidden_start_key" 109 if (i != 1) { 110 System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " + 111 Bytes.toString(modifiedRegionNameParts[i])); 112 Assert.assertArrayEquals(regionNameParts[i], modifiedRegionNameParts[i]); 113 } else { 114 System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " + 115 Bytes.toString(modifiedRegionNameParts[i])); 116 Assert.assertNotEquals(regionNameParts[i], modifiedRegionNameParts[i]); 117 Assert.assertArrayEquals(modifiedRegionNameParts[1], 118 RegionInfoDisplay.getStartKeyForDisplay(ri, conf)); 119 } 120 } 121 } 122}