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