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
037import static org.junit.Assert.assertTrue;
038
039@Category({MasterTests.class, SmallTests.class})
040public class TestRegionInfoDisplay {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044      HBaseClassTestRule.forClass(TestRegionInfoDisplay.class);
045
046  @Rule public TestName name = new TestName();
047
048  @Test
049  public void testRegionDetailsForDisplay() throws IOException {
050    byte[] startKey = new byte[] {0x01, 0x01, 0x02, 0x03};
051    byte[] endKey = new byte[] {0x01, 0x01, 0x02, 0x04};
052    Configuration conf = new Configuration();
053    conf.setBoolean("hbase.display.keys", false);
054    RegionInfo ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
055      .setStartKey(startKey).setEndKey(endKey).build();
056    checkEquality(ri, conf);
057    // check HRIs with non-default replicaId
058    ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
059    .setStartKey(startKey)
060    .setEndKey(endKey)
061    .setSplit(false)
062    .setRegionId(System.currentTimeMillis())
063    .setReplicaId(1).build();
064    checkEquality(ri, conf);
065    Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY,
066        RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
067    Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY,
068        RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
069
070    RegionState state = RegionState.createForTesting(convert(ri), RegionState.State.OPEN);
071    String descriptiveNameForDisplay =
072        RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf);
073    checkDescriptiveNameEquality(descriptiveNameForDisplay,state.toDescriptiveString(), startKey);
074
075    conf.setBoolean("hbase.display.keys", true);
076    Assert.assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
077    Assert.assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
078    Assert.assertEquals(state.toDescriptiveString(),
079        RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf));
080  }
081
082  private void checkDescriptiveNameEquality(String descriptiveNameForDisplay, String origDesc,
083      byte[] startKey) {
084    // except for the "hidden-start-key" substring everything else should exactly match
085    String firstPart = descriptiveNameForDisplay.substring(0,
086        descriptiveNameForDisplay.indexOf(
087        new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)));
088    String secondPart = descriptiveNameForDisplay.substring(
089        descriptiveNameForDisplay.indexOf(
090        new String(RegionInfoDisplay.HIDDEN_START_KEY, StandardCharsets.UTF_8)) +
091            RegionInfoDisplay.HIDDEN_START_KEY.length);
092    String firstPartOrig = origDesc.substring(0, origDesc.indexOf(Bytes.toStringBinary(startKey)));
093    String secondPartOrig = origDesc.substring(
094        origDesc.indexOf(Bytes.toStringBinary(startKey)) +
095            Bytes.toStringBinary(startKey).length());
096    assert(firstPart.equals(firstPartOrig));
097    // The elapsed time may be different in the two Strings since they were calculated at different
098    // times... so, don't include that portion when we compare. It starts with a '('.
099    // Second part looks like this:
100    // ",1539385518431.9d15487c60247dc3876b8b2a842929ed. state=OPEN,
101    //   ts=Fri Oct 12 16:05:18 PDT 2018 (PT0S ago), server=null"
102    int indexOfElapsedTime = secondPart.indexOf("(");
103    assertTrue(indexOfElapsedTime > 0);
104    assertTrue(secondPart + " " + secondPartOrig,
105        secondPart.substring(0, indexOfElapsedTime).equals(secondPartOrig.
106            substring(0, indexOfElapsedTime)));
107  }
108
109  private void checkEquality(RegionInfo ri, Configuration conf) throws IOException {
110    byte[] modifiedRegionName = RegionInfoDisplay.getRegionNameForDisplay(ri, conf);
111    System.out.println(Bytes.toString(modifiedRegionName) + " " + ri.toString());
112    byte[][] modifiedRegionNameParts = RegionInfo.parseRegionName(modifiedRegionName);
113    byte[][] regionNameParts = RegionInfo.parseRegionName(ri.getRegionName());
114
115    //same number of parts
116    assert(modifiedRegionNameParts.length == regionNameParts.length);
117    for (int i = 0; i < regionNameParts.length; i++) {
118      // all parts should match except for [1] where in the modified one,
119      // we should have "hidden_start_key"
120      if (i != 1) {
121        System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " +
122          Bytes.toString(modifiedRegionNameParts[i]));
123        Assert.assertArrayEquals(regionNameParts[i], modifiedRegionNameParts[i]);
124      } else {
125        System.out.println("" + i + " " + Bytes.toString(regionNameParts[i]) + " " +
126          Bytes.toString(modifiedRegionNameParts[i]));
127        Assert.assertNotEquals(regionNameParts[i], modifiedRegionNameParts[i]);
128        Assert.assertArrayEquals(modifiedRegionNameParts[1],
129          RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
130      }
131    }
132  }
133
134  private HRegionInfo convert(RegionInfo ri) {
135    HRegionInfo hri =new HRegionInfo(ri.getTable(), ri.getStartKey(), ri.getEndKey(),
136        ri.isSplit(), ri.getRegionId());
137    hri.setOffline(ri.isOffline());
138    return hri;
139  }
140}