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