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.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 public TestName name = new TestName();
045
046  @Test
047  public void testRegionDetailsForDisplay() throws IOException {
048    byte[] startKey = new byte[] {0x01, 0x01, 0x02, 0x03};
049    byte[] endKey = new byte[] {0x01, 0x01, 0x02, 0x04};
050    Configuration conf = new Configuration();
051    conf.setBoolean("hbase.display.keys", false);
052    RegionInfo ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
053      .setStartKey(startKey).setEndKey(endKey).build();
054    checkEquality(ri, conf);
055    // check HRIs with non-default replicaId
056    ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
057    .setStartKey(startKey)
058    .setEndKey(endKey)
059    .setSplit(false)
060    .setRegionId(System.currentTimeMillis())
061    .setReplicaId(1).build();
062    checkEquality(ri, conf);
063    Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY,
064        RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
065    Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY,
066        RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
067
068    RegionState state = RegionState.createForTesting(convert(ri), RegionState.State.OPEN);
069    String descriptiveNameForDisplay =
070        RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf);
071    String originalDescriptive = state.toDescriptiveString();
072    checkDescriptiveNameEquality(descriptiveNameForDisplay, originalDescriptive, startKey);
073
074    conf.setBoolean("hbase.display.keys", true);
075    Assert.assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
076    Assert.assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
077    Assert.assertEquals(originalDescriptive,
078        RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf));
079  }
080
081  private void checkDescriptiveNameEquality(String descriptiveNameForDisplay, String origDesc,
082      byte[] startKey) {
083    // except for the "hidden-start-key" substring everything else should exactly match
084    String firstPart = descriptiveNameForDisplay.substring(0,
085        descriptiveNameForDisplay.indexOf(
086        new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)));
087    String secondPart = descriptiveNameForDisplay.substring(
088        descriptiveNameForDisplay.indexOf(
089        new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)) +
090            RegionInfoDisplay.HIDDEN_START_KEY.length);
091    String firstPartOrig = origDesc.substring(0, origDesc.indexOf(Bytes.toStringBinary(startKey)));
092    String secondPartOrig = origDesc.substring(
093        origDesc.indexOf(Bytes.toStringBinary(startKey)) +
094            Bytes.toStringBinary(startKey).length());
095    assert(firstPart.equals(firstPartOrig));
096    assert(secondPart.equals(secondPartOrig));
097  }
098
099  private void checkEquality(RegionInfo ri, Configuration conf) throws IOException {
100    byte[] modifiedRegionName = RegionInfoDisplay.getRegionNameForDisplay(ri, conf);
101    System.out.println(Bytes.toString(modifiedRegionName) + " " + ri.toString());
102    byte[][] modifiedRegionNameParts = RegionInfo.parseRegionName(modifiedRegionName);
103    byte[][] regionNameParts = RegionInfo.parseRegionName(ri.getRegionName());
104
105    //same number of parts
106    assert(modifiedRegionNameParts.length == regionNameParts.length);
107    for (int i = 0; i < regionNameParts.length; i++) {
108      // all parts should match except for [1] where in the modified one,
109      // we should have "hidden_start_key"
110      if (i != 1) {
111        System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " +
112          Bytes.toString(modifiedRegionNameParts[i]));
113        Assert.assertArrayEquals(regionNameParts[i], modifiedRegionNameParts[i]);
114      } else {
115        System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " +
116          Bytes.toString(modifiedRegionNameParts[i]));
117        Assert.assertNotEquals(regionNameParts[i], modifiedRegionNameParts[i]);
118        Assert.assertArrayEquals(modifiedRegionNameParts[1],
119          RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
120      }
121    }
122  }
123
124  private HRegionInfo convert(RegionInfo ri) {
125    HRegionInfo hri =new HRegionInfo(ri.getTable(), ri.getStartKey(), ri.getEndKey(),
126        ri.isSplit(), ri.getRegionId());
127    hri.setOffline(ri.isOffline());
128    return hri;
129  }
130}