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    checkDescriptiveNameEquality(descriptiveNameForDisplay,state.toDescriptiveString(), 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(state.toDescriptiveString(),
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
123  private HRegionInfo convert(RegionInfo ri) {
124    HRegionInfo hri =new HRegionInfo(ri.getTable(), ri.getStartKey(), ri.getEndKey(),
125        ri.isSplit(), ri.getRegionId());
126    hri.setOffline(ri.isOffline());
127    return hri;
128  }
129}