001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase;
020
021import java.io.DataInputStream;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026import java.util.stream.Collectors;
027
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.KeyValue.KVComparator;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.client.RegionInfoBuilder;
032import org.apache.hadoop.hbase.client.RegionInfoDisplay;
033import org.apache.hadoop.hbase.exceptions.DeserializationException;
034import org.apache.hadoop.hbase.master.RegionState;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.io.DataInputBuffer;
037import org.apache.yetus.audience.InterfaceAudience;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
041import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
042
043/**
044 * Information about a region. A region is a range of keys in the whole keyspace of a table, an
045 * identifier (a timestamp) for differentiating between subset ranges (after region split)
046 * and a replicaId for differentiating the instance for the same range and some status information
047 * about the region.
048 *
049 * The region has a unique name which consists of the following fields:
050 * <ul>
051 * <li> tableName   : The name of the table </li>
052 * <li> startKey    : The startKey for the region. </li>
053 * <li> regionId    : A timestamp when the region is created. </li>
054 * <li> replicaId   : An id starting from 0 to differentiate replicas of the same region range
055 * but hosted in separated servers. The same region range can be hosted in multiple locations.</li>
056 * <li> encodedName : An MD5 encoded string for the region name.</li>
057 * </ul>
058 *
059 * <br> Other than the fields in the region name, region info contains:
060 * <ul>
061 * <li> endKey      : the endKey for the region (exclusive) </li>
062 * <li> split       : Whether the region is split </li>
063 * <li> offline     : Whether the region is offline </li>
064 * </ul>
065 *
066 * In 0.98 or before, a list of table's regions would fully cover the total keyspace, and at any
067 * point in time, a row key always belongs to a single region, which is hosted in a single server.
068 * In 0.99+, a region can have multiple instances (called replicas), and thus a range (or row) can
069 * correspond to multiple HRegionInfo's. These HRI's share the same fields however except the
070 * replicaId field. If the replicaId is not set, it defaults to 0, which is compatible with the
071 * previous behavior of a range corresponding to 1 region.
072 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0.
073 *             use {@link RegionInfoBuilder} to build {@link RegionInfo}.
074 */
075@Deprecated
076@InterfaceAudience.Public
077public class HRegionInfo implements RegionInfo {
078  private static final Logger LOG = LoggerFactory.getLogger(HRegionInfo.class);
079
080  /**
081   * The new format for a region name contains its encodedName at the end.
082   * The encoded name also serves as the directory name for the region
083   * in the filesystem.
084   *
085   * New region name format:
086   *    &lt;tablename>,,&lt;startkey>,&lt;regionIdTimestamp>.&lt;encodedName>.
087   * where,
088   *    &lt;encodedName> is a hex version of the MD5 hash of
089   *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
090   *
091   * The old region name format:
092   *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
093   * For region names in the old format, the encoded name is a 32-bit
094   * JenkinsHash integer value (in its decimal notation, string form).
095   *<p>
096   * **NOTE**
097   *
098   * The first hbase:meta region, and regions created by an older
099   * version of HBase (0.20 or prior) will continue to use the
100   * old region name format.
101   */
102
103  /** A non-capture group so that this can be embedded. */
104  public static final String ENCODED_REGION_NAME_REGEX = RegionInfoBuilder.ENCODED_REGION_NAME_REGEX;
105
106  private static final int MAX_REPLICA_ID = 0xFFFF;
107
108  /**
109   * @param regionName
110   * @return the encodedName
111   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
112   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#encodeRegionName(byte[])}.
113   */
114  @Deprecated
115  public static String encodeRegionName(final byte [] regionName) {
116    return RegionInfo.encodeRegionName(regionName);
117  }
118
119  /**
120   * @return Return a short, printable name for this region (usually encoded name) for us logging.
121   */
122  @Override
123  public String getShortNameToLog() {
124    return prettyPrint(this.getEncodedName());
125  }
126
127  /**
128   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
129   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#getShortNameToLog(RegionInfo...)}.
130   */
131  @Deprecated
132  public static String getShortNameToLog(HRegionInfo...hris) {
133    return RegionInfo.getShortNameToLog(Arrays.asList(hris));
134  }
135
136  /**
137   * @return Return a String of short, printable names for <code>hris</code>
138   * (usually encoded name) for us logging.
139   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
140   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#getShortNameToLog(List)})}.
141   */
142  @Deprecated
143  public static String getShortNameToLog(final List<HRegionInfo> hris) {
144    return RegionInfo.getShortNameToLog(hris.stream().collect(Collectors.toList()));
145  }
146
147  /**
148   * Use logging.
149   * @param encodedRegionName The encoded regionname.
150   * @return <code>hbase:meta</code> if passed <code>1028785192</code> else returns
151   * <code>encodedRegionName</code>
152   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
153   *             Use {@link RegionInfo#prettyPrint(String)}.
154   */
155  @Deprecated
156  @InterfaceAudience.Private
157  public static String prettyPrint(final String encodedRegionName) {
158    return RegionInfo.prettyPrint(encodedRegionName);
159  }
160
161  private byte [] endKey = HConstants.EMPTY_BYTE_ARRAY;
162  // This flag is in the parent of a split while the parent is still referenced by daughter regions.
163  // We USED to set this flag when we disabled a table but now table state is kept up in zookeeper
164  // as of 0.90.0 HBase. And now in DisableTableProcedure, finally we will create bunch of
165  // UnassignProcedures and at the last of the procedure we will set the region state to CLOSED, and
166  // will not change the offLine flag.
167  private boolean offLine = false;
168  private long regionId = -1;
169  private transient byte [] regionName = HConstants.EMPTY_BYTE_ARRAY;
170  private boolean split = false;
171  private byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
172  private int hashCode = -1;
173  //TODO: Move NO_HASH to HStoreFile which is really the only place it is used.
174  public static final String NO_HASH = null;
175  private String encodedName = null;
176  private byte [] encodedNameAsBytes = null;
177  private int replicaId = DEFAULT_REPLICA_ID;
178
179  // Current TableName
180  private TableName tableName = null;
181
182  // Duplicated over in RegionInfoDisplay
183  final static String DISPLAY_KEYS_KEY = RegionInfoDisplay.DISPLAY_KEYS_KEY;
184  public final static byte[] HIDDEN_END_KEY = RegionInfoDisplay.HIDDEN_END_KEY;
185  public final static byte[] HIDDEN_START_KEY = RegionInfoDisplay.HIDDEN_START_KEY;
186
187  /** HRegionInfo for first meta region */
188  // TODO: How come Meta regions still do not have encoded region names? Fix.
189  public static final HRegionInfo FIRST_META_REGIONINFO =
190      new HRegionInfo(1L, TableName.META_TABLE_NAME);
191
192  private void setHashCode() {
193    int result = Arrays.hashCode(this.regionName);
194    result = (int) (result ^ this.regionId);
195    result ^= Arrays.hashCode(this.startKey);
196    result ^= Arrays.hashCode(this.endKey);
197    result ^= Boolean.valueOf(this.offLine).hashCode();
198    result ^= Arrays.hashCode(this.tableName.getName());
199    result ^= this.replicaId;
200    this.hashCode = result;
201  }
202
203  /**
204   * Private constructor used constructing HRegionInfo for the
205   * first meta regions
206   */
207  private HRegionInfo(long regionId, TableName tableName) {
208    this(regionId, tableName, DEFAULT_REPLICA_ID);
209  }
210
211  public HRegionInfo(long regionId, TableName tableName, int replicaId) {
212    super();
213    this.regionId = regionId;
214    this.tableName = tableName;
215    this.replicaId = replicaId;
216    // Note: First Meta region replicas names are in old format
217    this.regionName = createRegionName(tableName, null, regionId, replicaId, false);
218    setHashCode();
219  }
220
221  public HRegionInfo(final TableName tableName) {
222    this(tableName, null, null);
223  }
224
225  /**
226   * Construct HRegionInfo with explicit parameters
227   *
228   * @param tableName the table name
229   * @param startKey first key in region
230   * @param endKey end of key range
231   * @throws IllegalArgumentException
232   */
233  public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey)
234  throws IllegalArgumentException {
235    this(tableName, startKey, endKey, false);
236  }
237
238  /**
239   * Construct HRegionInfo with explicit parameters
240   *
241   * @param tableName the table descriptor
242   * @param startKey first key in region
243   * @param endKey end of key range
244   * @param split true if this region has split and we have daughter regions
245   * regions that may or may not hold references to this region.
246   * @throws IllegalArgumentException
247   */
248  public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey,
249      final boolean split)
250  throws IllegalArgumentException {
251    this(tableName, startKey, endKey, split, System.currentTimeMillis());
252  }
253
254  /**
255   * Construct HRegionInfo with explicit parameters
256   *
257   * @param tableName the table descriptor
258   * @param startKey first key in region
259   * @param endKey end of key range
260   * @param split true if this region has split and we have daughter regions
261   * regions that may or may not hold references to this region.
262   * @param regionid Region id to use.
263   * @throws IllegalArgumentException
264   */
265  public HRegionInfo(final TableName tableName, final byte[] startKey,
266                     final byte[] endKey, final boolean split, final long regionid)
267  throws IllegalArgumentException {
268    this(tableName, startKey, endKey, split, regionid, DEFAULT_REPLICA_ID);
269  }
270
271  /**
272   * Construct HRegionInfo with explicit parameters
273   *
274   * @param tableName the table descriptor
275   * @param startKey first key in region
276   * @param endKey end of key range
277   * @param split true if this region has split and we have daughter regions
278   * regions that may or may not hold references to this region.
279   * @param regionid Region id to use.
280   * @param replicaId the replicaId to use
281   * @throws IllegalArgumentException
282   */
283  public HRegionInfo(final TableName tableName, final byte[] startKey,
284                     final byte[] endKey, final boolean split, final long regionid,
285                     final int replicaId)
286    throws IllegalArgumentException {
287    super();
288    if (tableName == null) {
289      throw new IllegalArgumentException("TableName cannot be null");
290    }
291    this.tableName = tableName;
292    this.offLine = false;
293    this.regionId = regionid;
294    this.replicaId = replicaId;
295    if (this.replicaId > MAX_REPLICA_ID) {
296      throw new IllegalArgumentException("ReplicaId cannot be greater than" + MAX_REPLICA_ID);
297    }
298
299    this.regionName = createRegionName(this.tableName, startKey, regionId, replicaId, true);
300
301    this.split = split;
302    this.endKey = endKey == null? HConstants.EMPTY_END_ROW: endKey.clone();
303    this.startKey = startKey == null?
304      HConstants.EMPTY_START_ROW: startKey.clone();
305    this.tableName = tableName;
306    setHashCode();
307  }
308
309  /**
310   * Costruct a copy of another HRegionInfo
311   *
312   * @param other
313   */
314  public HRegionInfo(RegionInfo other) {
315    super();
316    this.endKey = other.getEndKey();
317    this.offLine = other.isOffline();
318    this.regionId = other.getRegionId();
319    this.regionName = other.getRegionName();
320    this.split = other.isSplit();
321    this.startKey = other.getStartKey();
322    this.hashCode = other.hashCode();
323    this.encodedName = other.getEncodedName();
324    this.tableName = other.getTable();
325    this.replicaId = other.getReplicaId();
326  }
327
328  public HRegionInfo(HRegionInfo other, int replicaId) {
329    this(other);
330    this.replicaId = replicaId;
331    this.setHashCode();
332  }
333
334  /**
335   * Make a region name of passed parameters.
336   * @param tableName
337   * @param startKey Can be null
338   * @param regionid Region id (Usually timestamp from when region was created).
339   * @param newFormat should we create the region name in the new format
340   *                  (such that it contains its encoded name?).
341   * @return Region name made of passed tableName, startKey and id
342   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
343   *             Use {@link RegionInfo#createRegionName(TableName, byte[], long, boolean)}.
344   */
345  @Deprecated
346  @InterfaceAudience.Private
347  public static byte [] createRegionName(final TableName tableName,
348      final byte [] startKey, final long regionid, boolean newFormat) {
349    return RegionInfo.createRegionName(tableName, startKey, Long.toString(regionid), newFormat);
350  }
351
352  /**
353   * Make a region name of passed parameters.
354   * @param tableName
355   * @param startKey Can be null
356   * @param id Region id (Usually timestamp from when region was created).
357   * @param newFormat should we create the region name in the new format
358   *                  (such that it contains its encoded name?).
359   * @return Region name made of passed tableName, startKey and id
360   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
361   *             Use {@link RegionInfo#createRegionName(TableName, byte[], String, boolean)}.
362   */
363  @Deprecated
364  @InterfaceAudience.Private
365  public static byte [] createRegionName(final TableName tableName,
366      final byte [] startKey, final String id, boolean newFormat) {
367    return RegionInfo.createRegionName(tableName, startKey, Bytes.toBytes(id), newFormat);
368  }
369
370  /**
371   * Make a region name of passed parameters.
372   * @param tableName
373   * @param startKey Can be null
374   * @param regionid Region id (Usually timestamp from when region was created).
375   * @param replicaId
376   * @param newFormat should we create the region name in the new format
377   *                  (such that it contains its encoded name?).
378   * @return Region name made of passed tableName, startKey, id and replicaId
379   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
380   *             Use {@link RegionInfo#createRegionName(TableName, byte[], long, int, boolean)}.
381   */
382  @Deprecated
383  @InterfaceAudience.Private
384  public static byte [] createRegionName(final TableName tableName,
385      final byte [] startKey, final long regionid, int replicaId, boolean newFormat) {
386    return RegionInfo.createRegionName(tableName, startKey, Bytes.toBytes(Long.toString(regionid)),
387        replicaId, newFormat);
388  }
389
390  /**
391   * Make a region name of passed parameters.
392   * @param tableName
393   * @param startKey Can be null
394   * @param id Region id (Usually timestamp from when region was created).
395   * @param newFormat should we create the region name in the new format
396   *                  (such that it contains its encoded name?).
397   * @return Region name made of passed tableName, startKey and id
398   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
399   *             Use {@link RegionInfo#createRegionName(TableName, byte[], byte[], boolean)}.
400   */
401  @Deprecated
402  @InterfaceAudience.Private
403  public static byte [] createRegionName(final TableName tableName,
404      final byte [] startKey, final byte [] id, boolean newFormat) {
405    return RegionInfo.createRegionName(tableName, startKey, id, DEFAULT_REPLICA_ID, newFormat);
406  }
407  /**
408   * Make a region name of passed parameters.
409   * @param tableName
410   * @param startKey Can be null
411   * @param id Region id (Usually timestamp from when region was created).
412   * @param replicaId
413   * @param newFormat should we create the region name in the new format
414   * @return Region name made of passed tableName, startKey, id and replicaId
415   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
416   *             Use {@link RegionInfo#createRegionName(TableName, byte[], byte[], int, boolean)}.
417   */
418  @Deprecated
419  @InterfaceAudience.Private
420  public static byte [] createRegionName(final TableName tableName,
421      final byte [] startKey, final byte [] id, final int replicaId, boolean newFormat) {
422    return RegionInfo.createRegionName(tableName, startKey, id, replicaId, newFormat);
423  }
424
425  /**
426   * Gets the table name from the specified region name.
427   * @param regionName to extract the table name from
428   * @return Table name
429   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
430   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#getTable(byte[])}.
431   */
432  @Deprecated
433  public static TableName getTable(final byte [] regionName) {
434    return RegionInfo.getTable(regionName);
435  }
436
437  /**
438   * Gets the start key from the specified region name.
439   * @param regionName
440   * @return Start key.
441   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
442   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#getStartKey(byte[])}.
443   */
444  @Deprecated
445  public static byte[] getStartKey(final byte[] regionName) throws IOException {
446    return RegionInfo.getStartKey(regionName);
447  }
448
449  /**
450   * Separate elements of a regionName.
451   * @param regionName
452   * @return Array of byte[] containing tableName, startKey and id
453   * @throws IOException
454   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
455   *             Use {@link RegionInfo#parseRegionName(byte[])}.
456   */
457  @Deprecated
458  @InterfaceAudience.Private
459  public static byte [][] parseRegionName(final byte [] regionName) throws IOException {
460    return RegionInfo.parseRegionName(regionName);
461  }
462
463  /**
464   *
465   * @param regionName
466   * @return if region name is encoded.
467   * @throws IOException
468   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
469   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#isEncodedRegionName(byte[])}.
470   */
471  @Deprecated
472  public static boolean isEncodedRegionName(byte[] regionName) throws IOException {
473    return RegionInfo.isEncodedRegionName(regionName);
474  }
475
476  /** @return the regionId */
477  @Override
478  public long getRegionId(){
479    return regionId;
480  }
481
482  /**
483   * @return the regionName as an array of bytes.
484   * @see #getRegionNameAsString()
485   */
486  @Override
487  public byte [] getRegionName(){
488    return regionName;
489  }
490
491  /**
492   * @return Region name as a String for use in logging, etc.
493   */
494  @Override
495  public String getRegionNameAsString() {
496    if (RegionInfo.hasEncodedName(this.regionName)) {
497      // new format region names already have their encoded name.
498      return Bytes.toStringBinary(this.regionName);
499    }
500
501    // old format. regionNameStr doesn't have the region name.
502    //
503    //
504    return Bytes.toStringBinary(this.regionName) + "." + this.getEncodedName();
505  }
506
507  /**
508   * @return the encoded region name
509   */
510  @Override
511  public synchronized String getEncodedName() {
512    if (this.encodedName == null) {
513      this.encodedName = RegionInfo.encodeRegionName(this.regionName);
514    }
515    return this.encodedName;
516  }
517
518  @Override
519  public synchronized byte [] getEncodedNameAsBytes() {
520    if (this.encodedNameAsBytes == null) {
521      this.encodedNameAsBytes = Bytes.toBytes(getEncodedName());
522    }
523    return this.encodedNameAsBytes;
524  }
525
526  /**
527   * @return the startKey
528   */
529  @Override
530  public byte [] getStartKey(){
531    return startKey;
532  }
533
534  /**
535   * @return the endKey
536   */
537  @Override
538  public byte [] getEndKey(){
539    return endKey;
540  }
541
542  /**
543   * Get current table name of the region
544   * @return TableName
545   */
546  @Override
547  public TableName getTable() {
548    // This method name should be getTableName but there was already a method getTableName
549    // that returned a byte array.  It is unfortunate given everywhere else, getTableName returns
550    // a TableName instance.
551    if (tableName == null || tableName.getName().length == 0) {
552      tableName = getTable(getRegionName());
553    }
554    return this.tableName;
555  }
556
557  /**
558   * Returns true if the given inclusive range of rows is fully contained
559   * by this region. For example, if the region is foo,a,g and this is
560   * passed ["b","c"] or ["a","c"] it will return true, but if this is passed
561   * ["b","z"] it will return false.
562   * @throws IllegalArgumentException if the range passed is invalid (ie. end &lt; start)
563   */
564  @Override
565  public boolean containsRange(byte[] rangeStartKey, byte[] rangeEndKey) {
566    if (Bytes.compareTo(rangeStartKey, rangeEndKey) > 0) {
567      throw new IllegalArgumentException(
568      "Invalid range: " + Bytes.toStringBinary(rangeStartKey) +
569      " > " + Bytes.toStringBinary(rangeEndKey));
570    }
571
572    boolean firstKeyInRange = Bytes.compareTo(rangeStartKey, startKey) >= 0;
573    boolean lastKeyInRange =
574      Bytes.compareTo(rangeEndKey, endKey) < 0 ||
575      Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY);
576    return firstKeyInRange && lastKeyInRange;
577  }
578
579  /**
580   * @return true if the given row falls in this region.
581   */
582  @Override
583  public boolean containsRow(byte[] row) {
584    return Bytes.compareTo(row, startKey) >= 0 &&
585      (Bytes.compareTo(row, endKey) < 0 ||
586       Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY));
587  }
588
589  /**
590   * @return true if this region is from hbase:meta
591   */
592  public boolean isMetaTable() {
593    return isMetaRegion();
594  }
595
596  /**
597   * @return true if this region is a meta region
598   */
599  @Override
600  public boolean isMetaRegion() {
601     return tableName.equals(HRegionInfo.FIRST_META_REGIONINFO.getTable());
602  }
603
604  /**
605   * @return true if this region is from a system table
606   */
607  public boolean isSystemTable() {
608    return tableName.isSystemTable();
609  }
610
611  /**
612   * @return true if has been split and has daughters.
613   */
614  @Override
615  public boolean isSplit() {
616    return this.split;
617  }
618
619  /**
620   * @param split set split status
621   */
622  public void setSplit(boolean split) {
623    this.split = split;
624  }
625
626  /**
627   * @return true if this region is offline.
628   */
629  @Override
630  public boolean isOffline() {
631    return this.offLine;
632  }
633
634  /**
635   * The parent of a region split is offline while split daughters hold
636   * references to the parent. Offlined regions are closed.
637   * @param offLine Set online/offline status.
638   */
639  public void setOffline(boolean offLine) {
640    this.offLine = offLine;
641  }
642
643  /**
644   * @return true if this is a split parent region.
645   */
646  @Override
647  public boolean isSplitParent() {
648    if (!isSplit()) return false;
649    if (!isOffline()) {
650      LOG.warn("Region is split but NOT offline: " + getRegionNameAsString());
651    }
652    return true;
653  }
654
655  /**
656   * Returns the region replica id
657   * @return returns region replica id
658   */
659  @Override
660  public int getReplicaId() {
661    return replicaId;
662  }
663
664  /**
665   * @see java.lang.Object#toString()
666   */
667  @Override
668  public String toString() {
669    return "{ENCODED => " + getEncodedName() + ", " +
670      HConstants.NAME + " => '" + Bytes.toStringBinary(this.regionName)
671      + "', STARTKEY => '" +
672      Bytes.toStringBinary(this.startKey) + "', ENDKEY => '" +
673      Bytes.toStringBinary(this.endKey) + "'" +
674      (isOffline()? ", OFFLINE => true": "") +
675      (isSplit()? ", SPLIT => true": "") +
676      ((replicaId > 0)? ", REPLICA_ID => " + replicaId : "") + "}";
677  }
678
679  /**
680   * @see java.lang.Object#equals(java.lang.Object)
681   */
682  @Override
683  public boolean equals(Object o) {
684    if (this == o) {
685      return true;
686    }
687    if (o == null) {
688      return false;
689    }
690    if (!(o instanceof HRegionInfo)) {
691      return false;
692    }
693    return this.compareTo((HRegionInfo)o) == 0;
694  }
695
696  /**
697   * @see java.lang.Object#hashCode()
698   */
699  @Override
700  public int hashCode() {
701    return this.hashCode;
702  }
703
704  /**
705   * @return Comparator to use comparing {@link KeyValue}s.
706   * @deprecated Use Region#getCellComparator().  deprecated for hbase 2.0, remove for hbase 3.0
707   */
708  @Deprecated
709  public KVComparator getComparator() {
710    return isMetaRegion()?
711        KeyValue.META_COMPARATOR: KeyValue.COMPARATOR;
712  }
713
714  /**
715   * Convert a HRegionInfo to the protobuf RegionInfo
716   *
717   * @return the converted RegionInfo
718   */
719  HBaseProtos.RegionInfo convert() {
720    return convert(this);
721  }
722
723  /**
724   * Convert a HRegionInfo to a RegionInfo
725   *
726   * @param info the HRegionInfo to convert
727   * @return the converted RegionInfo
728   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
729   *             Use toRegionInfo(org.apache.hadoop.hbase.client.RegionInfo)
730   *             in org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil.
731   */
732  @Deprecated
733  @InterfaceAudience.Private
734  public static HBaseProtos.RegionInfo convert(final HRegionInfo info) {
735    return ProtobufUtil.toRegionInfo(info);
736  }
737
738  /**
739   * Convert a RegionInfo to a HRegionInfo
740   *
741   * @param proto the RegionInfo to convert
742   * @return the converted HRegionInfo
743   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
744   *             Use toRegionInfo(HBaseProtos.RegionInfo)
745   *             in org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil.
746   */
747  @Deprecated
748  @InterfaceAudience.Private
749  public static HRegionInfo convert(final HBaseProtos.RegionInfo proto) {
750    RegionInfo ri = ProtobufUtil.toRegionInfo(proto);
751    // This is hack of what is in RegionReplicaUtil but it is doing translation of
752    // RegionInfo into HRegionInfo which is what is wanted here.
753    HRegionInfo hri;
754    if (ri.isMetaRegion()) {
755      hri = ri.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID ?
756      HRegionInfo.FIRST_META_REGIONINFO :
757      new HRegionInfo(ri.getRegionId(), ri.getTable(), ri.getReplicaId());
758    } else {
759      hri = new HRegionInfo(
760        ri.getTable(),
761        ri.getStartKey(),
762        ri.getEndKey(),
763        ri.isSplit(),
764        ri.getRegionId(),
765        ri.getReplicaId());
766      if (proto.hasOffline()) {
767        hri.setOffline(proto.getOffline());
768      }
769    }
770    return hri;
771  }
772
773  /**
774   * @return This instance serialized as protobuf w/ a magic pb prefix.
775   * @see #parseFrom(byte[])
776   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
777   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#toByteArray(RegionInfo)}.
778   */
779  @Deprecated
780  public byte [] toByteArray() {
781    return RegionInfo.toByteArray(this);
782  }
783
784  /**
785   * @return A deserialized {@link HRegionInfo}
786   * or null if we failed deserialize or passed bytes null
787   * @see #toByteArray()
788   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
789   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFromOrNull(byte[])}.
790   */
791  @Deprecated
792  public static HRegionInfo parseFromOrNull(final byte [] bytes) {
793    if (bytes == null) return null;
794    return parseFromOrNull(bytes, 0, bytes.length);
795  }
796
797  /**
798   * @return A deserialized {@link HRegionInfo} or null
799   *  if we failed deserialize or passed bytes null
800   * @see #toByteArray()
801   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
802   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFromOrNull(byte[], int, int)}.
803   */
804  @Deprecated
805  public static HRegionInfo parseFromOrNull(final byte [] bytes, int offset, int len) {
806    if (bytes == null || len <= 0) return null;
807    try {
808      return parseFrom(bytes, offset, len);
809    } catch (DeserializationException e) {
810      return null;
811    }
812  }
813
814  /**
815   * @param bytes A pb RegionInfo serialized with a pb magic prefix.
816   * @return A deserialized {@link HRegionInfo}
817   * @throws DeserializationException
818   * @see #toByteArray()
819   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
820   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFrom(byte[])}.
821   */
822  public static HRegionInfo parseFrom(final byte [] bytes) throws DeserializationException {
823    if (bytes == null) return null;
824    return parseFrom(bytes, 0, bytes.length);
825  }
826
827  /**
828   * @param bytes A pb RegionInfo serialized with a pb magic prefix.
829   * @param offset starting point in the byte array
830   * @param len length to read on the byte array
831   * @return A deserialized {@link HRegionInfo}
832   * @throws DeserializationException
833   * @see #toByteArray()
834   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
835   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#parseFrom(byte[], int, int)}.
836   */
837  @Deprecated
838  public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len)
839      throws DeserializationException {
840    if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) {
841      int pblen = ProtobufUtil.lengthOfPBMagic();
842      try {
843        HBaseProtos.RegionInfo.Builder builder = HBaseProtos.RegionInfo.newBuilder();
844        ProtobufUtil.mergeFrom(builder, bytes, pblen + offset, len - pblen);
845        HBaseProtos.RegionInfo ri = builder.build();
846        return convert(ri);
847      } catch (IOException e) {
848        throw new DeserializationException(e);
849      }
850    } else {
851      throw new DeserializationException("PB encoded HRegionInfo expected");
852    }
853  }
854
855  /**
856   * Use this instead of {@link #toByteArray()} when writing to a stream and you want to use
857   * the pb mergeDelimitedFrom (w/o the delimiter, pb reads to EOF which may not be what you want).
858   * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
859   * @throws IOException
860   * @see #toByteArray()
861   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
862   *             Use {@link RegionInfo#toDelimitedByteArray(RegionInfo)}.
863   */
864  @Deprecated
865  public byte [] toDelimitedByteArray() throws IOException {
866    return RegionInfo.toDelimitedByteArray(this);
867  }
868
869  /**
870   * Get the descriptive name as {@link RegionState} does it but with hidden
871   * startkey optionally
872   * @param state
873   * @param conf
874   * @return descriptive string
875   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
876   *             Use RegionInfoDisplay#getDescriptiveNameFromRegionStateForDisplay(RegionState, Configuration)
877   *             over in hbase-server module.
878   */
879  @Deprecated
880  @InterfaceAudience.Private
881  public static String getDescriptiveNameFromRegionStateForDisplay(RegionState state,
882      Configuration conf) {
883    return RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf);
884  }
885
886  /**
887   * Get the end key for display. Optionally hide the real end key.
888   * @param hri
889   * @param conf
890   * @return the endkey
891   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
892   *             Use RegionInfoDisplay#getEndKeyForDisplay(RegionInfo, Configuration)
893   *             over in hbase-server module.
894   */
895  @Deprecated
896  @InterfaceAudience.Private
897  public static byte[] getEndKeyForDisplay(HRegionInfo hri, Configuration conf) {
898    return RegionInfoDisplay.getEndKeyForDisplay(hri, conf);
899  }
900
901  /**
902   * Get the start key for display. Optionally hide the real start key.
903   * @param hri
904   * @param conf
905   * @return the startkey
906   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
907   *             Use RegionInfoDisplay#getStartKeyForDisplay(RegionInfo, Configuration)
908   *             over in hbase-server module.
909   */
910  @Deprecated
911  @InterfaceAudience.Private
912  public static byte[] getStartKeyForDisplay(HRegionInfo hri, Configuration conf) {
913    return RegionInfoDisplay.getStartKeyForDisplay(hri, conf);
914  }
915
916  /**
917   * Get the region name for display. Optionally hide the start key.
918   * @param hri
919   * @param conf
920   * @return region name as String
921   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
922   *             Use RegionInfoDisplay#getRegionNameAsStringForDisplay(RegionInfo, Configuration)
923   *             over in hbase-server module.
924   */
925  @Deprecated
926  @InterfaceAudience.Private
927  public static String getRegionNameAsStringForDisplay(HRegionInfo hri, Configuration conf) {
928    return RegionInfoDisplay.getRegionNameAsStringForDisplay(hri, conf);
929  }
930
931  /**
932   * Get the region name for display. Optionally hide the start key.
933   * @param hri
934   * @param conf
935   * @return region name bytes
936   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
937   *             Use RegionInfoDisplay#getRegionNameForDisplay(RegionInfo, Configuration)
938   *             over in hbase-server module.
939   */
940  @Deprecated
941  @InterfaceAudience.Private
942  public static byte[] getRegionNameForDisplay(HRegionInfo hri, Configuration conf) {
943    return RegionInfoDisplay.getRegionNameForDisplay(hri, conf);
944  }
945
946  /**
947   * Parses an HRegionInfo instance from the passed in stream.  Presumes the HRegionInfo was
948   * serialized to the stream with {@link #toDelimitedByteArray()}
949   * @param in
950   * @return An instance of HRegionInfo.
951   * @throws IOException
952   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
953   *             Use {@link RegionInfo#parseFrom(DataInputStream)}.
954   */
955  @Deprecated
956  @InterfaceAudience.Private
957  public static HRegionInfo parseFrom(final DataInputStream in) throws IOException {
958    // I need to be able to move back in the stream if this is not a pb serialization so I can
959    // do the Writable decoding instead.
960    int pblen = ProtobufUtil.lengthOfPBMagic();
961    byte [] pbuf = new byte[pblen];
962    if (in.markSupported()) { //read it with mark()
963      in.mark(pblen);
964    }
965
966    //assumption: if Writable serialization, it should be longer than pblen.
967    int read = in.read(pbuf);
968    if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
969    if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
970      return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
971    } else {
972      throw new IOException("PB encoded HRegionInfo expected");
973    }
974  }
975
976  /**
977   * Serializes given HRegionInfo's as a byte array. Use this instead of {@link #toByteArray()} when
978   * writing to a stream and you want to use the pb mergeDelimitedFrom (w/o the delimiter, pb reads
979   * to EOF which may not be what you want). {@link #parseDelimitedFrom(byte[], int, int)} can
980   * be used to read back the instances.
981   * @param infos HRegionInfo objects to serialize
982   * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
983   * @throws IOException
984   * @see #toByteArray()
985   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
986   *             Use {@link RegionInfo#toDelimitedByteArray(RegionInfo...)}.
987   */
988  @Deprecated
989  @InterfaceAudience.Private
990  public static byte[] toDelimitedByteArray(HRegionInfo... infos) throws IOException {
991    return RegionInfo.toDelimitedByteArray(infos);
992  }
993
994  /**
995   * Parses all the HRegionInfo instances from the passed in stream until EOF. Presumes the
996   * HRegionInfo's were serialized to the stream with {@link #toDelimitedByteArray()}
997   * @param bytes serialized bytes
998   * @param offset the start offset into the byte[] buffer
999   * @param length how far we should read into the byte[] buffer
1000   * @return All the hregioninfos that are in the byte array. Keeps reading till we hit the end.
1001   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
1002   *             Use {@link RegionInfo#parseDelimitedFrom(byte[], int, int)}.
1003   */
1004  @Deprecated
1005  public static List<HRegionInfo> parseDelimitedFrom(final byte[] bytes, final int offset,
1006      final int length) throws IOException {
1007    if (bytes == null) {
1008      throw new IllegalArgumentException("Can't build an object with empty bytes array");
1009    }
1010    DataInputBuffer in = new DataInputBuffer();
1011    List<HRegionInfo> hris = new ArrayList<>();
1012    try {
1013      in.reset(bytes, offset, length);
1014      while (in.available() > 0) {
1015        HRegionInfo hri = parseFrom(in);
1016        hris.add(hri);
1017      }
1018    } finally {
1019      in.close();
1020    }
1021    return hris;
1022  }
1023
1024  /**
1025   * Check whether two regions are adjacent
1026   * @param regionA
1027   * @param regionB
1028   * @return true if two regions are adjacent
1029   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0
1030   *             Use {@link org.apache.hadoop.hbase.client.RegionInfo#areAdjacent(RegionInfo, RegionInfo)}.
1031   */
1032  @Deprecated
1033  public static boolean areAdjacent(HRegionInfo regionA, HRegionInfo regionB) {
1034    return RegionInfo.areAdjacent(regionA, regionB);
1035  }
1036}