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 org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.master.RegionState;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Utility used composing RegionInfo for 'display'; e.g. on the web UI
030 */
031@InterfaceAudience.Private
032public class RegionInfoDisplay {
033  public final static String DISPLAY_KEYS_KEY = "hbase.display.keys";
034  public final static byte[] HIDDEN_END_KEY = Bytes.toBytes("hidden-end-key");
035  public final static byte[] HIDDEN_START_KEY = Bytes.toBytes("hidden-start-key");
036
037  /**
038   * Get the descriptive name as {@link RegionState} does it but with hidden startkey optionally
039   * @return descriptive string
040   */
041  public static String getDescriptiveNameFromRegionStateForDisplay(RegionState state,
042    Configuration conf) {
043    if (conf.getBoolean(DISPLAY_KEYS_KEY, true)) return state.toDescriptiveString();
044    String descriptiveStringFromState = state.toDescriptiveString();
045    int idx = descriptiveStringFromState.lastIndexOf(" state=");
046    String regionName = getRegionNameAsStringForDisplay(
047      RegionInfoBuilder.newBuilder(state.getRegion()).build(), conf);
048    return regionName + descriptiveStringFromState.substring(idx);
049  }
050
051  /**
052   * Get the end key for display. Optionally hide the real end key.
053   * @return the endkey
054   */
055  public static byte[] getEndKeyForDisplay(RegionInfo ri, Configuration conf) {
056    boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
057    if (displayKey) return ri.getEndKey();
058    return HIDDEN_END_KEY;
059  }
060
061  /**
062   * Get the start key for display. Optionally hide the real start key. nn * @return the startkey
063   */
064  public static byte[] getStartKeyForDisplay(RegionInfo ri, Configuration conf) {
065    boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
066    if (displayKey) return ri.getStartKey();
067    return HIDDEN_START_KEY;
068  }
069
070  /**
071   * Get the region name for display. Optionally hide the start key. nn * @return region name as
072   * String
073   */
074  public static String getRegionNameAsStringForDisplay(RegionInfo ri, Configuration conf) {
075    return Bytes.toStringBinary(getRegionNameForDisplay(ri, conf));
076  }
077
078  /**
079   * Get the region name for display. Optionally hide the start key. nn * @return region name bytes
080   */
081  public static byte[] getRegionNameForDisplay(RegionInfo ri, Configuration conf) {
082    boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
083    if (displayKey || ri.getTable().equals(TableName.META_TABLE_NAME)) {
084      return ri.getRegionName();
085    } else {
086      // create a modified regionname with the startkey replaced but preserving
087      // the other parts including the encodedname.
088      try {
089        byte[][] regionNameParts = RegionInfo.parseRegionName(ri.getRegionName());
090        regionNameParts[1] = HIDDEN_START_KEY; // replace the real startkey
091        int len = 0;
092        // get the total length
093        for (byte[] b : regionNameParts) {
094          len += b.length;
095        }
096        byte[] encodedRegionName = Bytes.toBytes(RegionInfo.encodeRegionName(ri.getRegionName()));
097        len += encodedRegionName.length;
098        // allocate some extra bytes for the delimiters and the last '.'
099        byte[] modifiedName = new byte[len + regionNameParts.length + 1];
100        int lengthSoFar = 0;
101        int loopCount = 0;
102        for (byte[] b : regionNameParts) {
103          System.arraycopy(b, 0, modifiedName, lengthSoFar, b.length);
104          lengthSoFar += b.length;
105          if (loopCount++ == 2) modifiedName[lengthSoFar++] = RegionInfo.REPLICA_ID_DELIMITER;
106          else modifiedName[lengthSoFar++] = HConstants.DELIMITER;
107        }
108        // replace the last comma with '.'
109        modifiedName[lengthSoFar - 1] = RegionInfo.ENC_SEPARATOR;
110        System.arraycopy(encodedRegionName, 0, modifiedName, lengthSoFar, encodedRegionName.length);
111        lengthSoFar += encodedRegionName.length;
112        modifiedName[lengthSoFar] = RegionInfo.ENC_SEPARATOR;
113        return modifiedName;
114      } catch (IOException e) {
115        // LOG.warn("Encountered exception " + e);
116        throw new RuntimeException(e);
117      }
118    }
119  }
120}