View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import java.io.DataInputStream;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceStability;
32  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
33  import org.apache.hadoop.hbase.KeyValue.KVComparator;
34  import org.apache.hadoop.hbase.exceptions.DeserializationException;
35  import org.apache.hadoop.hbase.master.RegionState;
36  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
38  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionInfo;
39  import org.apache.hadoop.hbase.util.ByteStringer;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.JenkinsHash;
42  import org.apache.hadoop.hbase.util.MD5Hash;
43  import org.apache.hadoop.io.DataInputBuffer;
44  
45  /**
46   * Information about a region. A region is a range of keys in the whole keyspace of a table, an
47   * identifier (a timestamp) for differentiating between subset ranges (after region split)
48   * and a replicaId for differentiating the instance for the same range and some status information
49   * about the region.
50   *
51   * The region has a unique name which consists of the following fields:
52   * <ul>
53   * <li> tableName   : The name of the table </li>
54   * <li> startKey    : The startKey for the region. </li>
55   * <li> regionId    : A timestamp when the region is created. </li>
56   * <li> replicaId   : An id starting from 0 to differentiate replicas of the same region range
57   * but hosted in separated servers. The same region range can be hosted in multiple locations.</li>
58   * <li> encodedName : An MD5 encoded string for the region name.</li>
59   * </ul>
60   *
61   * <br> Other than the fields in the region name, region info contains:
62   * <ul>
63   * <li> endKey      : the endKey for the region (exclusive) </li>
64   * <li> split       : Whether the region is split </li>
65   * <li> offline     : Whether the region is offline </li>
66   * </ul>
67   *
68   * In 0.98 or before, a list of table's regions would fully cover the total keyspace, and at any
69   * point in time, a row key always belongs to a single region, which is hosted in a single server.
70   * In 0.99+, a region can have multiple instances (called replicas), and thus a range (or row) can
71   * correspond to multiple HRegionInfo's. These HRI's share the same fields however except the
72   * replicaId field. If the replicaId is not set, it defaults to 0, which is compatible with the
73   * previous behavior of a range corresponding to 1 region.
74   */
75  @InterfaceAudience.Public
76  @InterfaceStability.Evolving
77  public class HRegionInfo implements Comparable<HRegionInfo> {
78  
79    private static final Log LOG = LogFactory.getLog(HRegionInfo.class);
80  
81    /**
82     * The new format for a region name contains its encodedName at the end.
83     * The encoded name also serves as the directory name for the region
84     * in the filesystem.
85     *
86     * New region name format:
87     *    &lt;tablename>,,&lt;startkey>,&lt;regionIdTimestamp>.&lt;encodedName>.
88     * where,
89     *    &lt;encodedName> is a hex version of the MD5 hash of
90     *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
91     *
92     * The old region name format:
93     *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
94     * For region names in the old format, the encoded name is a 32-bit
95     * JenkinsHash integer value (in its decimal notation, string form).
96     *<p>
97     * **NOTE**
98     *
99     * The first hbase:meta region, and regions created by an older
100    * version of HBase (0.20 or prior) will continue to use the
101    * old region name format.
102    */
103 
104   /** Separator used to demarcate the encodedName in a region name
105    * in the new format. See description on new format above.
106    */
107   private static final int ENC_SEPARATOR = '.';
108   public  static final int MD5_HEX_LENGTH   = 32;
109 
110   /** A non-capture group so that this can be embedded. */
111   public static final String ENCODED_REGION_NAME_REGEX = "(?:[a-f0-9]+)";
112 
113   // to keep appended int's sorted in string format. Only allows 2 bytes to be
114   // sorted for replicaId
115   public static final String REPLICA_ID_FORMAT = "%04X";
116 
117   public static final byte REPLICA_ID_DELIMITER = (byte)'_';
118 
119   private static final int MAX_REPLICA_ID = 0xFFFF;
120   public static final int DEFAULT_REPLICA_ID = 0;
121 
122   public static final String INVALID_REGION_NAME_FORMAT_MESSAGE = "Invalid regionName format";
123 
124   /**
125    * Does region name contain its encoded name?
126    * @param regionName region name
127    * @return boolean indicating if this a new format region
128    *         name which contains its encoded name.
129    */
130   private static boolean hasEncodedName(final byte[] regionName) {
131     // check if region name ends in ENC_SEPARATOR
132     if ((regionName.length >= 1)
133         && (regionName[regionName.length - 1] == ENC_SEPARATOR)) {
134       // region name is new format. it contains the encoded name.
135       return true;
136     }
137     return false;
138   }
139 
140   /**
141    * @param regionName
142    * @return the encodedName
143    */
144   public static String encodeRegionName(final byte [] regionName) {
145     String encodedName;
146     if (hasEncodedName(regionName)) {
147       // region is in new format:
148       // <tableName>,<startKey>,<regionIdTimeStamp>/encodedName/
149       encodedName = Bytes.toString(regionName,
150           regionName.length - MD5_HEX_LENGTH - 1,
151           MD5_HEX_LENGTH);
152     } else {
153       // old format region name. First hbase:meta region also
154       // use this format.EncodedName is the JenkinsHash value.
155       int hashVal = Math.abs(JenkinsHash.getInstance().hash(regionName,
156         regionName.length, 0));
157       encodedName = String.valueOf(hashVal);
158     }
159     return encodedName;
160   }
161 
162   /**
163    * @return Return a short, printable name for this region (usually encoded name) for us logging.
164    */
165   public String getShortNameToLog() {
166     return prettyPrint(this.getEncodedName());
167   }
168 
169   /**
170    * Use logging.
171    * @param encodedRegionName The encoded regionname.
172    * @return <code>hbase:meta</code> if passed <code>1028785192</code> else returns
173    * <code>encodedRegionName</code>
174    */
175   public static String prettyPrint(final String encodedRegionName) {
176     if (encodedRegionName.equals("1028785192")) {
177       return encodedRegionName + "/hbase:meta";
178     }
179     return encodedRegionName;
180   }
181 
182   private byte [] endKey = HConstants.EMPTY_BYTE_ARRAY;
183   // This flag is in the parent of a split while the parent is still referenced
184   // by daughter regions.  We USED to set this flag when we disabled a table
185   // but now table state is kept up in zookeeper as of 0.90.0 HBase.
186   private boolean offLine = false;
187   private long regionId = -1;
188   private transient byte [] regionName = HConstants.EMPTY_BYTE_ARRAY;
189   private boolean split = false;
190   private byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
191   private int hashCode = -1;
192   //TODO: Move NO_HASH to HStoreFile which is really the only place it is used.
193   public static final String NO_HASH = null;
194   private String encodedName = null;
195   private byte [] encodedNameAsBytes = null;
196   private int replicaId = DEFAULT_REPLICA_ID;
197 
198   // Current TableName
199   private TableName tableName = null;
200   final static String DISPLAY_KEYS_KEY = "hbase.display.keys";
201   public final static byte[] HIDDEN_END_KEY = Bytes.toBytes("hidden-end-key");
202   public final static byte[] HIDDEN_START_KEY = Bytes.toBytes("hidden-start-key");
203 
204   /** HRegionInfo for first meta region */
205   // TODO: How come Meta regions still do not have encoded region names? Fix.
206   public static final HRegionInfo FIRST_META_REGIONINFO =
207       new HRegionInfo(1L, TableName.META_TABLE_NAME);
208 
209   private void setHashCode() {
210     int result = Arrays.hashCode(this.regionName);
211     result ^= this.regionId;
212     result ^= Arrays.hashCode(this.startKey);
213     result ^= Arrays.hashCode(this.endKey);
214     result ^= Boolean.valueOf(this.offLine).hashCode();
215     result ^= Arrays.hashCode(this.tableName.getName());
216     result ^= this.replicaId;
217     this.hashCode = result;
218   }
219 
220 
221   /**
222    * Private constructor used constructing HRegionInfo for the
223    * first meta regions
224    */
225   private HRegionInfo(long regionId, TableName tableName) {
226     this(regionId, tableName, DEFAULT_REPLICA_ID);
227   }
228 
229   public HRegionInfo(long regionId, TableName tableName, int replicaId) {
230     super();
231     this.regionId = regionId;
232     this.tableName = tableName;
233     this.replicaId = replicaId;
234     // Note: First Meta region replicas names are in old format
235     this.regionName = createRegionName(tableName, null, regionId, replicaId, false);
236     setHashCode();
237   }
238 
239   public HRegionInfo(final TableName tableName) {
240     this(tableName, null, null);
241   }
242 
243   /**
244    * Construct HRegionInfo with explicit parameters
245    *
246    * @param tableName the table name
247    * @param startKey first key in region
248    * @param endKey end of key range
249    * @throws IllegalArgumentException
250    */
251   public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey)
252   throws IllegalArgumentException {
253     this(tableName, startKey, endKey, false);
254   }
255 
256   /**
257    * Construct HRegionInfo with explicit parameters
258    *
259    * @param tableName the table descriptor
260    * @param startKey first key in region
261    * @param endKey end of key range
262    * @param split true if this region has split and we have daughter regions
263    * regions that may or may not hold references to this region.
264    * @throws IllegalArgumentException
265    */
266   public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey,
267       final boolean split)
268   throws IllegalArgumentException {
269     this(tableName, startKey, endKey, split, System.currentTimeMillis());
270   }
271 
272   /**
273    * Construct HRegionInfo with explicit parameters
274    *
275    * @param tableName the table descriptor
276    * @param startKey first key in region
277    * @param endKey end of key range
278    * @param split true if this region has split and we have daughter regions
279    * regions that may or may not hold references to this region.
280    * @param regionid Region id 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   throws IllegalArgumentException {
286     this(tableName, startKey, endKey, split, regionid, DEFAULT_REPLICA_ID);
287   }
288 
289   /**
290    * Construct HRegionInfo with explicit parameters
291    *
292    * @param tableName the table descriptor
293    * @param startKey first key in region
294    * @param endKey end of key range
295    * @param split true if this region has split and we have daughter regions
296    * regions that may or may not hold references to this region.
297    * @param regionid Region id to use.
298    * @param replicaId the replicaId to use
299    * @throws IllegalArgumentException
300    */
301   public HRegionInfo(final TableName tableName, final byte[] startKey,
302                      final byte[] endKey, final boolean split, final long regionid,
303                      final int replicaId)
304     throws IllegalArgumentException {
305     super();
306     if (tableName == null) {
307       throw new IllegalArgumentException("TableName cannot be null");
308     }
309     this.tableName = tableName;
310     this.offLine = false;
311     this.regionId = regionid;
312     this.replicaId = replicaId;
313     if (this.replicaId > MAX_REPLICA_ID) {
314       throw new IllegalArgumentException("ReplicaId cannot be greater than" + MAX_REPLICA_ID);
315     }
316 
317     this.regionName = createRegionName(this.tableName, startKey, regionId, replicaId, true);
318 
319     this.split = split;
320     this.endKey = endKey == null? HConstants.EMPTY_END_ROW: endKey.clone();
321     this.startKey = startKey == null?
322       HConstants.EMPTY_START_ROW: startKey.clone();
323     this.tableName = tableName;
324     setHashCode();
325   }
326 
327   /**
328    * Costruct a copy of another HRegionInfo
329    *
330    * @param other
331    */
332   public HRegionInfo(HRegionInfo other) {
333     super();
334     this.endKey = other.getEndKey();
335     this.offLine = other.isOffline();
336     this.regionId = other.getRegionId();
337     this.regionName = other.getRegionName();
338     this.split = other.isSplit();
339     this.startKey = other.getStartKey();
340     this.hashCode = other.hashCode();
341     this.encodedName = other.getEncodedName();
342     this.tableName = other.tableName;
343     this.replicaId = other.replicaId;
344   }
345 
346   public HRegionInfo(HRegionInfo other, int replicaId) {
347     this(other);
348     this.replicaId = replicaId;
349     this.setHashCode();
350   }
351 
352   /**
353    * Make a region name of passed parameters.
354    * @param tableName
355    * @param startKey Can be null
356    * @param regionid 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    */
361   public static byte [] createRegionName(final TableName tableName,
362       final byte [] startKey, final long regionid, boolean newFormat) {
363     return createRegionName(tableName, startKey, Long.toString(regionid), newFormat);
364   }
365 
366   /**
367    * Make a region name of passed parameters.
368    * @param tableName
369    * @param startKey Can be null
370    * @param id Region id (Usually timestamp from when region was created).
371    * @param newFormat should we create the region name in the new format
372    *                  (such that it contains its encoded name?).
373    * @return Region name made of passed tableName, startKey and id
374    */
375   public static byte [] createRegionName(final TableName tableName,
376       final byte [] startKey, final String id, boolean newFormat) {
377     return createRegionName(tableName, startKey, Bytes.toBytes(id), newFormat);
378   }
379 
380   /**
381    * Make a region name of passed parameters.
382    * @param tableName
383    * @param startKey Can be null
384    * @param regionid Region id (Usually timestamp from when region was created).
385    * @param replicaId
386    * @param newFormat should we create the region name in the new format
387    *                  (such that it contains its encoded name?).
388    * @return Region name made of passed tableName, startKey, id and replicaId
389    */
390   public static byte [] createRegionName(final TableName tableName,
391       final byte [] startKey, final long regionid, int replicaId, boolean newFormat) {
392     return createRegionName(tableName, startKey, Bytes.toBytes(Long.toString(regionid)),
393         replicaId, newFormat);
394   }
395 
396   /**
397    * Make a region name of passed parameters.
398    * @param tableName
399    * @param startKey Can be null
400    * @param id Region id (Usually timestamp from when region was created).
401    * @param newFormat should we create the region name in the new format
402    *                  (such that it contains its encoded name?).
403    * @return Region name made of passed tableName, startKey and id
404    */
405   public static byte [] createRegionName(final TableName tableName,
406       final byte [] startKey, final byte [] id, boolean newFormat) {
407     return createRegionName(tableName, startKey, id, DEFAULT_REPLICA_ID, newFormat);
408   }
409   /**
410    * Make a region name of passed parameters.
411    * @param tableName
412    * @param startKey Can be null
413    * @param id Region id (Usually timestamp from when region was created).
414    * @param replicaId
415    * @param newFormat should we create the region name in the new format
416    * @return Region name made of passed tableName, startKey, id and replicaId
417    */
418   public static byte [] createRegionName(final TableName tableName,
419       final byte [] startKey, final byte [] id, final int replicaId, boolean newFormat) {
420     int len = tableName.getName().length + 2 + id.length +
421         (startKey == null? 0: startKey.length);
422     if (newFormat) {
423       len += MD5_HEX_LENGTH + 2;
424     }
425     byte[] replicaIdBytes = null;
426     // Special casing: replicaId is only appended if replicaId is greater than
427     // 0. This is because all regions in meta would have to be migrated to the new
428     // name otherwise
429     if (replicaId > 0) {
430       // use string representation for replica id
431       replicaIdBytes = Bytes.toBytes(String.format(REPLICA_ID_FORMAT, replicaId));
432       len += 1 + replicaIdBytes.length;
433     }
434 
435     byte [] b = new byte [len];
436 
437     int offset = tableName.getName().length;
438     System.arraycopy(tableName.getName(), 0, b, 0, offset);
439     b[offset++] = HConstants.DELIMITER;
440     if (startKey != null && startKey.length > 0) {
441       System.arraycopy(startKey, 0, b, offset, startKey.length);
442       offset += startKey.length;
443     }
444     b[offset++] = HConstants.DELIMITER;
445     System.arraycopy(id, 0, b, offset, id.length);
446     offset += id.length;
447 
448     if (replicaIdBytes != null) {
449       b[offset++] = REPLICA_ID_DELIMITER;
450       System.arraycopy(replicaIdBytes, 0, b, offset, replicaIdBytes.length);
451       offset += replicaIdBytes.length;
452     }
453 
454     if (newFormat) {
455       //
456       // Encoded name should be built into the region name.
457       //
458       // Use the region name thus far (namely, <tablename>,<startKey>,<id>_<replicaId>)
459       // to compute a MD5 hash to be used as the encoded name, and append
460       // it to the byte buffer.
461       //
462       String md5Hash = MD5Hash.getMD5AsHex(b, 0, offset);
463       byte [] md5HashBytes = Bytes.toBytes(md5Hash);
464 
465       if (md5HashBytes.length != MD5_HEX_LENGTH) {
466         LOG.error("MD5-hash length mismatch: Expected=" + MD5_HEX_LENGTH +
467                   "; Got=" + md5HashBytes.length);
468       }
469 
470       // now append the bytes '.<encodedName>.' to the end
471       b[offset++] = ENC_SEPARATOR;
472       System.arraycopy(md5HashBytes, 0, b, offset, MD5_HEX_LENGTH);
473       offset += MD5_HEX_LENGTH;
474       b[offset++] = ENC_SEPARATOR;
475     }
476 
477     return b;
478   }
479 
480   /**
481    * Gets the table name from the specified region name.
482    * @param regionName to extract the table name from
483    * @return Table name
484    */
485   public static TableName getTable(final byte [] regionName) {
486     int offset = -1;
487     for (int i = 0; i < regionName.length; i++) {
488       if (regionName[i] == HConstants.DELIMITER) {
489         offset = i;
490         break;
491       }
492     }
493     byte[] buff  = new byte[offset];
494     System.arraycopy(regionName, 0, buff, 0, offset);
495     return TableName.valueOf(buff);
496   }
497 
498   /**
499    * Gets the start key from the specified region name.
500    * @param regionName
501    * @return Start key.
502    */
503   public static byte[] getStartKey(final byte[] regionName) throws IOException {
504     return parseRegionName(regionName)[1];
505   }
506 
507   /**
508    * Separate elements of a regionName.
509    * @param regionName
510    * @return Array of byte[] containing tableName, startKey and id
511    * @throws IOException
512    */
513   public static byte [][] parseRegionName(final byte [] regionName)
514   throws IOException {
515     // Region name is of the format:
516     // tablename,startkey,regionIdTimestamp[_replicaId][.encodedName.]
517     // startkey can contain the delimiter (',') so we parse from the start and end
518 
519     // parse from start
520     int offset = -1;
521     for (int i = 0; i < regionName.length; i++) {
522       if (regionName[i] == HConstants.DELIMITER) {
523         offset = i;
524         break;
525       }
526     }
527     if (offset == -1) {
528       throw new IOException(INVALID_REGION_NAME_FORMAT_MESSAGE
529         + ": " + Bytes.toStringBinary(regionName));
530     }
531     byte[] tableName = new byte[offset];
532     System.arraycopy(regionName, 0, tableName, 0, offset);
533     offset = -1;
534 
535     int endOffset = regionName.length;
536     // check whether regionName contains encodedName
537     if (regionName.length > MD5_HEX_LENGTH + 2
538         && regionName[regionName.length-1] == ENC_SEPARATOR
539         && regionName[regionName.length-MD5_HEX_LENGTH-2] == ENC_SEPARATOR) {
540       endOffset = endOffset - MD5_HEX_LENGTH - 2;
541     }
542 
543     // parse from end
544     byte[] replicaId = null;
545     int idEndOffset = endOffset;
546     for (int i = endOffset - 1; i > 0; i--) {
547       if (regionName[i] == REPLICA_ID_DELIMITER) { //replicaId may or may not be present
548         replicaId = new byte[endOffset - i - 1];
549         System.arraycopy(regionName, i + 1, replicaId, 0,
550           endOffset - i - 1);
551         idEndOffset = i;
552         // do not break, continue to search for id
553       }
554       if (regionName[i] == HConstants.DELIMITER) {
555         offset = i;
556         break;
557       }
558     }
559     if (offset == -1) {
560       throw new IOException(INVALID_REGION_NAME_FORMAT_MESSAGE
561         + ": " + Bytes.toStringBinary(regionName));
562     }
563     byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
564     if(offset != tableName.length + 1) {
565       startKey = new byte[offset - tableName.length - 1];
566       System.arraycopy(regionName, tableName.length + 1, startKey, 0,
567           offset - tableName.length - 1);
568     }
569     byte [] id = new byte[idEndOffset - offset - 1];
570     System.arraycopy(regionName, offset + 1, id, 0,
571       idEndOffset - offset - 1);
572     byte [][] elements = new byte[replicaId == null ? 3 : 4][];
573     elements[0] = tableName;
574     elements[1] = startKey;
575     elements[2] = id;
576     if (replicaId != null) {
577       elements[3] = replicaId;
578     }
579 
580     return elements;
581   }
582 
583   /** @return the regionId */
584   public long getRegionId(){
585     return regionId;
586   }
587 
588   /**
589    * @return the regionName as an array of bytes.
590    * @see #getRegionNameAsString()
591    */
592   public byte [] getRegionName(){
593     return regionName;
594   }
595 
596   /**
597    * @return Region name as a String for use in logging, etc.
598    */
599   public String getRegionNameAsString() {
600     if (hasEncodedName(this.regionName)) {
601       // new format region names already have their encoded name.
602       return Bytes.toStringBinary(this.regionName);
603     }
604 
605     // old format. regionNameStr doesn't have the region name.
606     //
607     //
608     return Bytes.toStringBinary(this.regionName) + "." + this.getEncodedName();
609   }
610 
611   /** @return the encoded region name */
612   public synchronized String getEncodedName() {
613     if (this.encodedName == null) {
614       this.encodedName = encodeRegionName(this.regionName);
615     }
616     return this.encodedName;
617   }
618 
619   public synchronized byte [] getEncodedNameAsBytes() {
620     if (this.encodedNameAsBytes == null) {
621       this.encodedNameAsBytes = Bytes.toBytes(getEncodedName());
622     }
623     return this.encodedNameAsBytes;
624   }
625 
626   /** @return the startKey */
627   public byte [] getStartKey(){
628     return startKey;
629   }
630 
631   /** @return the endKey */
632   public byte [] getEndKey(){
633     return endKey;
634   }
635 
636   /**
637    * Get current table name of the region
638    * @return TableName
639    */
640   public TableName getTable() {
641     // This method name should be getTableName but there was already a method getTableName
642     // that returned a byte array.  It is unfortunate given everywhere else, getTableName returns
643     // a TableName instance.
644     if (tableName == null || tableName.getName().length == 0) {
645       tableName = getTable(getRegionName());
646     }
647     return this.tableName;
648   }
649 
650   /**
651    * Returns true if the given inclusive range of rows is fully contained
652    * by this region. For example, if the region is foo,a,g and this is
653    * passed ["b","c"] or ["a","c"] it will return true, but if this is passed
654    * ["b","z"] it will return false.
655    * @throws IllegalArgumentException if the range passed is invalid (ie. end &lt; start)
656    */
657   public boolean containsRange(byte[] rangeStartKey, byte[] rangeEndKey) {
658     if (Bytes.compareTo(rangeStartKey, rangeEndKey) > 0) {
659       throw new IllegalArgumentException(
660       "Invalid range: " + Bytes.toStringBinary(rangeStartKey) +
661       " > " + Bytes.toStringBinary(rangeEndKey));
662     }
663 
664     boolean firstKeyInRange = Bytes.compareTo(rangeStartKey, startKey) >= 0;
665     boolean lastKeyInRange =
666       Bytes.compareTo(rangeEndKey, endKey) < 0 ||
667       Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY);
668     return firstKeyInRange && lastKeyInRange;
669   }
670 
671   /**
672    * Return true if the given row falls in this region.
673    */
674   public boolean containsRow(byte[] row) {
675     return Bytes.compareTo(row, startKey) >= 0 &&
676       (Bytes.compareTo(row, endKey) < 0 ||
677        Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY));
678   }
679 
680   /**
681    * @return true if this region is from hbase:meta
682    */
683   public boolean isMetaTable() {
684     return isMetaRegion();
685   }
686 
687   /** @return true if this region is a meta region */
688   public boolean isMetaRegion() {
689      return tableName.equals(HRegionInfo.FIRST_META_REGIONINFO.getTable());
690   }
691 
692   /**
693    * @return true if this region is from a system table
694    */
695   public boolean isSystemTable() {
696     return tableName.isSystemTable();
697   }
698 
699   /**
700    * @return True if has been split and has daughters.
701    */
702   public boolean isSplit() {
703     return this.split;
704   }
705 
706   /**
707    * @param split set split status
708    */
709   public void setSplit(boolean split) {
710     this.split = split;
711   }
712 
713   /**
714    * @return True if this region is offline.
715    */
716   public boolean isOffline() {
717     return this.offLine;
718   }
719 
720   /**
721    * The parent of a region split is offline while split daughters hold
722    * references to the parent. Offlined regions are closed.
723    * @param offLine Set online/offline status.
724    */
725   public void setOffline(boolean offLine) {
726     this.offLine = offLine;
727   }
728 
729   /**
730    * @return True if this is a split parent region.
731    */
732   public boolean isSplitParent() {
733     if (!isSplit()) return false;
734     if (!isOffline()) {
735       LOG.warn("Region is split but NOT offline: " + getRegionNameAsString());
736     }
737     return true;
738   }
739 
740   /**
741    * Returns the region replica id
742    * @return returns region replica id
743    */
744   public int getReplicaId() {
745     return replicaId;
746   }
747 
748   /**
749    * @see java.lang.Object#toString()
750    */
751   @Override
752   public String toString() {
753     return "{ENCODED => " + getEncodedName() + ", " +
754       HConstants.NAME + " => '" + Bytes.toStringBinary(this.regionName)
755       + "', STARTKEY => '" +
756       Bytes.toStringBinary(this.startKey) + "', ENDKEY => '" +
757       Bytes.toStringBinary(this.endKey) + "'" +
758       (isOffline()? ", OFFLINE => true": "") +
759       (isSplit()? ", SPLIT => true": "") +
760       ((replicaId > 0)? ", REPLICA_ID => " + replicaId : "") + "}";
761   }
762 
763   /**
764    * @see java.lang.Object#equals(java.lang.Object)
765    */
766   @Override
767   public boolean equals(Object o) {
768     if (this == o) {
769       return true;
770     }
771     if (o == null) {
772       return false;
773     }
774     if (!(o instanceof HRegionInfo)) {
775       return false;
776     }
777     return this.compareTo((HRegionInfo)o) == 0;
778   }
779 
780   /**
781    * @see java.lang.Object#hashCode()
782    */
783   @Override
784   public int hashCode() {
785     return this.hashCode;
786   }
787 
788   //
789   // Comparable
790   //
791 
792   @Override
793   public int compareTo(HRegionInfo o) {
794     if (o == null) {
795       return 1;
796     }
797 
798     // Are regions of same table?
799     int result = this.tableName.compareTo(o.tableName);
800     if (result != 0) {
801       return result;
802     }
803 
804     // Compare start keys.
805     result = Bytes.compareTo(this.startKey, o.startKey);
806     if (result != 0) {
807       return result;
808     }
809 
810     // Compare end keys.
811     result = Bytes.compareTo(this.endKey, o.endKey);
812 
813     if (result != 0) {
814       if (this.getStartKey().length != 0
815               && this.getEndKey().length == 0) {
816           return 1; // this is last region
817       }
818       if (o.getStartKey().length != 0
819               && o.getEndKey().length == 0) {
820           return -1; // o is the last region
821       }
822       return result;
823     }
824 
825     // regionId is usually milli timestamp -- this defines older stamps
826     // to be "smaller" than newer stamps in sort order.
827     if (this.regionId > o.regionId) {
828       return 1;
829     } else if (this.regionId < o.regionId) {
830       return -1;
831     }
832 
833     int replicaDiff = this.getReplicaId() - o.getReplicaId();
834     if (replicaDiff != 0) return replicaDiff;
835 
836     if (this.offLine == o.offLine)
837       return 0;
838     if (this.offLine == true) return -1;
839 
840     return 1;
841   }
842 
843   /**
844    * @return Comparator to use comparing {@link KeyValue}s.
845    * @deprecated Use Region#getCellComparator().  deprecated for hbase 2.0, remove for hbase 3.0
846    */
847   @Deprecated
848   public KVComparator getComparator() {
849     return isMetaRegion()?
850         KeyValue.META_COMPARATOR: KeyValue.COMPARATOR;
851   }
852 
853   /**
854    * Convert a HRegionInfo to the protobuf RegionInfo
855    *
856    * @return the converted RegionInfo
857    */
858   RegionInfo convert() {
859     return convert(this);
860   }
861 
862   /**
863    * Convert a HRegionInfo to a RegionInfo
864    *
865    * @param info the HRegionInfo to convert
866    * @return the converted RegionInfo
867    */
868   public static RegionInfo convert(final HRegionInfo info) {
869     if (info == null) return null;
870     RegionInfo.Builder builder = RegionInfo.newBuilder();
871     builder.setTableName(ProtobufUtil.toProtoTableName(info.getTable()));
872     builder.setRegionId(info.getRegionId());
873     if (info.getStartKey() != null) {
874       builder.setStartKey(ByteStringer.wrap(info.getStartKey()));
875     }
876     if (info.getEndKey() != null) {
877       builder.setEndKey(ByteStringer.wrap(info.getEndKey()));
878     }
879     builder.setOffline(info.isOffline());
880     builder.setSplit(info.isSplit());
881     builder.setReplicaId(info.getReplicaId());
882     return builder.build();
883   }
884 
885   /**
886    * Convert a RegionInfo to a HRegionInfo
887    *
888    * @param proto the RegionInfo to convert
889    * @return the converted HRegionInfho
890    */
891   public static HRegionInfo convert(final RegionInfo proto) {
892     if (proto == null) return null;
893     TableName tableName =
894         ProtobufUtil.toTableName(proto.getTableName());
895     if (tableName.equals(TableName.META_TABLE_NAME)) {
896       return RegionReplicaUtil.getRegionInfoForReplica(FIRST_META_REGIONINFO,
897           proto.getReplicaId());
898     }
899     long regionId = proto.getRegionId();
900     int replicaId = proto.hasReplicaId() ? proto.getReplicaId() : DEFAULT_REPLICA_ID;
901     byte[] startKey = null;
902     byte[] endKey = null;
903     if (proto.hasStartKey()) {
904       startKey = proto.getStartKey().toByteArray();
905     }
906     if (proto.hasEndKey()) {
907       endKey = proto.getEndKey().toByteArray();
908     }
909     boolean split = false;
910     if (proto.hasSplit()) {
911       split = proto.getSplit();
912     }
913     HRegionInfo hri = new HRegionInfo(
914         tableName,
915         startKey,
916         endKey, split, regionId, replicaId);
917     if (proto.hasOffline()) {
918       hri.setOffline(proto.getOffline());
919     }
920     return hri;
921   }
922 
923   /**
924    * @return This instance serialized as protobuf w/ a magic pb prefix.
925    * @see #parseFrom(byte[])
926    */
927   public byte [] toByteArray() {
928     byte [] bytes = convert().toByteArray();
929     return ProtobufUtil.prependPBMagic(bytes);
930   }
931 
932   /**
933    * @return A deserialized {@link HRegionInfo}
934    * or null if we failed deserialize or passed bytes null
935    * @see #toByteArray()
936    */
937   public static HRegionInfo parseFromOrNull(final byte [] bytes) {
938     if (bytes == null) return null;
939     return parseFromOrNull(bytes, 0, bytes.length);
940   }
941 
942   /**
943    * @return A deserialized {@link HRegionInfo} or null
944    *  if we failed deserialize or passed bytes null
945    * @see #toByteArray()
946    */
947   public static HRegionInfo parseFromOrNull(final byte [] bytes, int offset, int len) {
948     if (bytes == null || len <= 0) return null;
949     try {
950       return parseFrom(bytes, offset, len);
951     } catch (DeserializationException e) {
952       return null;
953     }
954   }
955 
956   /**
957    * @param bytes A pb RegionInfo serialized with a pb magic prefix.
958    * @return A deserialized {@link HRegionInfo}
959    * @throws DeserializationException
960    * @see #toByteArray()
961    */
962   public static HRegionInfo parseFrom(final byte [] bytes) throws DeserializationException {
963     if (bytes == null) return null;
964     return parseFrom(bytes, 0, bytes.length);
965   }
966 
967   /**
968    * @param bytes A pb RegionInfo serialized with a pb magic prefix.
969    * @param offset starting point in the byte array
970    * @param len length to read on the byte array
971    * @return A deserialized {@link HRegionInfo}
972    * @throws DeserializationException
973    * @see #toByteArray()
974    */
975   public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len)
976       throws DeserializationException {
977     if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) {
978       int pblen = ProtobufUtil.lengthOfPBMagic();
979       try {
980         HBaseProtos.RegionInfo.Builder builder = HBaseProtos.RegionInfo.newBuilder();
981         ProtobufUtil.mergeFrom(builder, bytes, pblen + offset, len - pblen);
982         HBaseProtos.RegionInfo ri = builder.build();
983         return convert(ri);
984       } catch (IOException e) {
985         throw new DeserializationException(e);
986       }
987     } else {
988       throw new DeserializationException("PB encoded HRegionInfo expected");
989     }
990   }
991 
992   /**
993    * Use this instead of {@link #toByteArray()} when writing to a stream and you want to use
994    * the pb mergeDelimitedFrom (w/o the delimiter, pb reads to EOF which may not be what you want).
995    * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
996    * @throws IOException
997    * @see #toByteArray()
998    */
999   public byte [] toDelimitedByteArray() throws IOException {
1000     return ProtobufUtil.toDelimitedByteArray(convert());
1001   }
1002 
1003   /**
1004    * Get the descriptive name as {@link RegionState} does it but with hidden
1005    * startkey optionally
1006    * @param state
1007    * @param conf
1008    * @return descriptive string
1009    */
1010   public static String getDescriptiveNameFromRegionStateForDisplay(RegionState state,
1011       Configuration conf) {
1012     if (conf.getBoolean(DISPLAY_KEYS_KEY, true)) return state.toDescriptiveString();
1013     String descriptiveStringFromState = state.toDescriptiveString();
1014     int idx = descriptiveStringFromState.lastIndexOf(" state=");
1015     String regionName = getRegionNameAsStringForDisplay(state.getRegion(), conf);
1016     return regionName + descriptiveStringFromState.substring(idx);
1017   }
1018 
1019   /**
1020    * Get the end key for display. Optionally hide the real end key.
1021    * @param hri
1022    * @param conf
1023    * @return the endkey
1024    */
1025   public static byte[] getEndKeyForDisplay(HRegionInfo hri, Configuration conf) {
1026     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1027     if (displayKey) return hri.getEndKey();
1028     return HIDDEN_END_KEY;
1029   }
1030 
1031   /**
1032    * Get the start key for display. Optionally hide the real start key.
1033    * @param hri
1034    * @param conf
1035    * @return the startkey
1036    */
1037   public static byte[] getStartKeyForDisplay(HRegionInfo hri, Configuration conf) {
1038     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1039     if (displayKey) return hri.getStartKey();
1040     return HIDDEN_START_KEY;
1041   }
1042 
1043   /**
1044    * Get the region name for display. Optionally hide the start key.
1045    * @param hri
1046    * @param conf
1047    * @return region name as String
1048    */
1049   public static String getRegionNameAsStringForDisplay(HRegionInfo hri, Configuration conf) {
1050     return Bytes.toStringBinary(getRegionNameForDisplay(hri, conf));
1051   }
1052 
1053   /**
1054    * Get the region name for display. Optionally hide the start key.
1055    * @param hri
1056    * @param conf
1057    * @return region name bytes
1058    */
1059   public static byte[] getRegionNameForDisplay(HRegionInfo hri, Configuration conf) {
1060     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1061     if (displayKey || hri.getTable().equals(TableName.META_TABLE_NAME)) {
1062       return hri.getRegionName();
1063     } else {
1064       // create a modified regionname with the startkey replaced but preserving
1065       // the other parts including the encodedname.
1066       try {
1067         byte[][]regionNameParts = parseRegionName(hri.getRegionName());
1068         regionNameParts[1] = HIDDEN_START_KEY; //replace the real startkey
1069         int len = 0;
1070         // get the total length
1071         for (byte[] b : regionNameParts) {
1072           len += b.length;
1073         }
1074         byte[] encodedRegionName =
1075             Bytes.toBytes(encodeRegionName(hri.getRegionName()));
1076         len += encodedRegionName.length;
1077         //allocate some extra bytes for the delimiters and the last '.'
1078         byte[] modifiedName = new byte[len + regionNameParts.length + 1];
1079         int lengthSoFar = 0;
1080         int loopCount = 0;
1081         for (byte[] b : regionNameParts) {
1082           System.arraycopy(b, 0, modifiedName, lengthSoFar, b.length);
1083           lengthSoFar += b.length;
1084           if (loopCount++ == 2) modifiedName[lengthSoFar++] = REPLICA_ID_DELIMITER;
1085           else  modifiedName[lengthSoFar++] = HConstants.DELIMITER;
1086         }
1087         // replace the last comma with '.'
1088         modifiedName[lengthSoFar - 1] = ENC_SEPARATOR;
1089         System.arraycopy(encodedRegionName, 0, modifiedName, lengthSoFar,
1090             encodedRegionName.length);
1091         lengthSoFar += encodedRegionName.length;
1092         modifiedName[lengthSoFar] = ENC_SEPARATOR;
1093         return modifiedName;
1094       } catch (IOException e) {
1095         //LOG.warn("Encountered exception " + e);
1096         throw new RuntimeException(e);
1097       }
1098     }
1099   }
1100 
1101   /**
1102    * Parses an HRegionInfo instance from the passed in stream.  Presumes the HRegionInfo was
1103    * serialized to the stream with {@link #toDelimitedByteArray()}
1104    * @param in
1105    * @return An instance of HRegionInfo.
1106    * @throws IOException
1107    */
1108   public static HRegionInfo parseFrom(final DataInputStream in) throws IOException {
1109     // I need to be able to move back in the stream if this is not a pb serialization so I can
1110     // do the Writable decoding instead.
1111     int pblen = ProtobufUtil.lengthOfPBMagic();
1112     byte [] pbuf = new byte[pblen];
1113     if (in.markSupported()) { //read it with mark()
1114       in.mark(pblen);
1115     }
1116 
1117     //assumption: if Writable serialization, it should be longer than pblen.
1118     int read = in.read(pbuf);
1119     if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
1120     if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
1121       return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
1122     } else {
1123       throw new IOException("PB encoded HRegionInfo expected");
1124     }
1125   }
1126 
1127   /**
1128    * Serializes given HRegionInfo's as a byte array. Use this instead of {@link #toByteArray()} when
1129    * writing to a stream and you want to use the pb mergeDelimitedFrom (w/o the delimiter, pb reads
1130    * to EOF which may not be what you want). {@link #parseDelimitedFrom(byte[], int, int)} can
1131    * be used to read back the instances.
1132    * @param infos HRegionInfo objects to serialize
1133    * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
1134    * @throws IOException
1135    * @see #toByteArray()
1136    */
1137   public static byte[] toDelimitedByteArray(HRegionInfo... infos) throws IOException {
1138     byte[][] bytes = new byte[infos.length][];
1139     int size = 0;
1140     for (int i = 0; i < infos.length; i++) {
1141       bytes[i] = infos[i].toDelimitedByteArray();
1142       size += bytes[i].length;
1143     }
1144 
1145     byte[] result = new byte[size];
1146     int offset = 0;
1147     for (byte[] b : bytes) {
1148       System.arraycopy(b, 0, result, offset, b.length);
1149       offset += b.length;
1150     }
1151     return result;
1152   }
1153 
1154   /**
1155    * Parses all the HRegionInfo instances from the passed in stream until EOF. Presumes the
1156    * HRegionInfo's were serialized to the stream with {@link #toDelimitedByteArray()}
1157    * @param bytes serialized bytes
1158    * @param offset the start offset into the byte[] buffer
1159    * @param length how far we should read into the byte[] buffer
1160    * @return All the hregioninfos that are in the byte array. Keeps reading till we hit the end.
1161    */
1162   public static List<HRegionInfo> parseDelimitedFrom(final byte[] bytes, final int offset,
1163       final int length) throws IOException {
1164     if (bytes == null) {
1165       throw new IllegalArgumentException("Can't build an object with empty bytes array");
1166     }
1167     DataInputBuffer in = new DataInputBuffer();
1168     List<HRegionInfo> hris = new ArrayList<HRegionInfo>();
1169     try {
1170       in.reset(bytes, offset, length);
1171       while (in.available() > 0) {
1172         HRegionInfo hri = parseFrom(in);
1173         hris.add(hri);
1174       }
1175     } finally {
1176       in.close();
1177     }
1178     return hris;
1179   }
1180 
1181   /**
1182    * Check whether two regions are adjacent
1183    * @param regionA
1184    * @param regionB
1185    * @return true if two regions are adjacent
1186    */
1187   public static boolean areAdjacent(HRegionInfo regionA, HRegionInfo regionB) {
1188     if (regionA == null || regionB == null) {
1189       throw new IllegalArgumentException(
1190           "Can't check whether adjacent for null region");
1191     }
1192     HRegionInfo a = regionA;
1193     HRegionInfo b = regionB;
1194     if (Bytes.compareTo(a.getStartKey(), b.getStartKey()) > 0) {
1195       a = regionB;
1196       b = regionA;
1197     }
1198     if (Bytes.compareTo(a.getEndKey(), b.getStartKey()) == 0) {
1199       return true;
1200     }
1201     return false;
1202   }
1203 }