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   public static final HRegionInfo FIRST_META_REGIONINFO =
206       new HRegionInfo(1L, TableName.META_TABLE_NAME);
207 
208   private void setHashCode() {
209     int result = Arrays.hashCode(this.regionName);
210     result ^= this.regionId;
211     result ^= Arrays.hashCode(this.startKey);
212     result ^= Arrays.hashCode(this.endKey);
213     result ^= Boolean.valueOf(this.offLine).hashCode();
214     result ^= Arrays.hashCode(this.tableName.getName());
215     result ^= this.replicaId;
216     this.hashCode = result;
217   }
218 
219 
220   /**
221    * Private constructor used constructing HRegionInfo for the
222    * first meta regions
223    */
224   private HRegionInfo(long regionId, TableName tableName) {
225     this(regionId, tableName, DEFAULT_REPLICA_ID);
226   }
227 
228   public HRegionInfo(long regionId, TableName tableName, int replicaId) {
229     super();
230     this.regionId = regionId;
231     this.tableName = tableName;
232     this.replicaId = replicaId;
233     // Note: First Meta region replicas names are in old format
234     this.regionName = createRegionName(tableName, null, regionId, replicaId, false);
235     setHashCode();
236   }
237 
238   public HRegionInfo(final TableName tableName) {
239     this(tableName, null, null);
240   }
241 
242   /**
243    * Construct HRegionInfo with explicit parameters
244    *
245    * @param tableName the table name
246    * @param startKey first key in region
247    * @param endKey end of key range
248    * @throws IllegalArgumentException
249    */
250   public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey)
251   throws IllegalArgumentException {
252     this(tableName, startKey, endKey, false);
253   }
254 
255   /**
256    * Construct HRegionInfo with explicit parameters
257    *
258    * @param tableName the table descriptor
259    * @param startKey first key in region
260    * @param endKey end of key range
261    * @param split true if this region has split and we have daughter regions
262    * regions that may or may not hold references to this region.
263    * @throws IllegalArgumentException
264    */
265   public HRegionInfo(final TableName tableName, final byte[] startKey, final byte[] endKey,
266       final boolean split)
267   throws IllegalArgumentException {
268     this(tableName, startKey, endKey, split, System.currentTimeMillis());
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    * @throws IllegalArgumentException
281    */
282   public HRegionInfo(final TableName tableName, final byte[] startKey,
283                      final byte[] endKey, final boolean split, final long regionid)
284   throws IllegalArgumentException {
285     this(tableName, startKey, endKey, split, regionid, DEFAULT_REPLICA_ID);
286   }
287 
288   /**
289    * Construct HRegionInfo with explicit parameters
290    *
291    * @param tableName the table descriptor
292    * @param startKey first key in region
293    * @param endKey end of key range
294    * @param split true if this region has split and we have daughter regions
295    * regions that may or may not hold references to this region.
296    * @param regionid Region id to use.
297    * @param replicaId the replicaId to use
298    * @throws IllegalArgumentException
299    */
300   public HRegionInfo(final TableName tableName, final byte[] startKey,
301                      final byte[] endKey, final boolean split, final long regionid,
302                      final int replicaId)
303     throws IllegalArgumentException {
304     super();
305     if (tableName == null) {
306       throw new IllegalArgumentException("TableName cannot be null");
307     }
308     this.tableName = tableName;
309     this.offLine = false;
310     this.regionId = regionid;
311     this.replicaId = replicaId;
312     if (this.replicaId > MAX_REPLICA_ID) {
313       throw new IllegalArgumentException("ReplicaId cannot be greater than" + MAX_REPLICA_ID);
314     }
315 
316     this.regionName = createRegionName(this.tableName, startKey, regionId, replicaId, true);
317 
318     this.split = split;
319     this.endKey = endKey == null? HConstants.EMPTY_END_ROW: endKey.clone();
320     this.startKey = startKey == null?
321       HConstants.EMPTY_START_ROW: startKey.clone();
322     this.tableName = tableName;
323     setHashCode();
324   }
325 
326   /**
327    * Costruct a copy of another HRegionInfo
328    *
329    * @param other
330    */
331   public HRegionInfo(HRegionInfo other) {
332     super();
333     this.endKey = other.getEndKey();
334     this.offLine = other.isOffline();
335     this.regionId = other.getRegionId();
336     this.regionName = other.getRegionName();
337     this.split = other.isSplit();
338     this.startKey = other.getStartKey();
339     this.hashCode = other.hashCode();
340     this.encodedName = other.getEncodedName();
341     this.tableName = other.tableName;
342     this.replicaId = other.replicaId;
343   }
344 
345   public HRegionInfo(HRegionInfo other, int replicaId) {
346     this(other);
347     this.replicaId = replicaId;
348     this.setHashCode();
349   }
350 
351   /**
352    * Make a region name of passed parameters.
353    * @param tableName
354    * @param startKey Can be null
355    * @param regionid Region id (Usually timestamp from when region was created).
356    * @param newFormat should we create the region name in the new format
357    *                  (such that it contains its encoded name?).
358    * @return Region name made of passed tableName, startKey and id
359    */
360   public static byte [] createRegionName(final TableName tableName,
361       final byte [] startKey, final long regionid, boolean newFormat) {
362     return createRegionName(tableName, startKey, Long.toString(regionid), newFormat);
363   }
364 
365   /**
366    * Make a region name of passed parameters.
367    * @param tableName
368    * @param startKey Can be null
369    * @param id Region id (Usually timestamp from when region was created).
370    * @param newFormat should we create the region name in the new format
371    *                  (such that it contains its encoded name?).
372    * @return Region name made of passed tableName, startKey and id
373    */
374   public static byte [] createRegionName(final TableName tableName,
375       final byte [] startKey, final String id, boolean newFormat) {
376     return createRegionName(tableName, startKey, Bytes.toBytes(id), newFormat);
377   }
378 
379   /**
380    * Make a region name of passed parameters.
381    * @param tableName
382    * @param startKey Can be null
383    * @param regionid Region id (Usually timestamp from when region was created).
384    * @param replicaId
385    * @param newFormat should we create the region name in the new format
386    *                  (such that it contains its encoded name?).
387    * @return Region name made of passed tableName, startKey, id and replicaId
388    */
389   public static byte [] createRegionName(final TableName tableName,
390       final byte [] startKey, final long regionid, int replicaId, boolean newFormat) {
391     return createRegionName(tableName, startKey, Bytes.toBytes(Long.toString(regionid)),
392         replicaId, newFormat);
393   }
394 
395   /**
396    * Make a region name of passed parameters.
397    * @param tableName
398    * @param startKey Can be null
399    * @param id Region id (Usually timestamp from when region was created).
400    * @param newFormat should we create the region name in the new format
401    *                  (such that it contains its encoded name?).
402    * @return Region name made of passed tableName, startKey and id
403    */
404   public static byte [] createRegionName(final TableName tableName,
405       final byte [] startKey, final byte [] id, boolean newFormat) {
406     return createRegionName(tableName, startKey, id, DEFAULT_REPLICA_ID, newFormat);
407   }
408   /**
409    * Make a region name of passed parameters.
410    * @param tableName
411    * @param startKey Can be null
412    * @param id Region id (Usually timestamp from when region was created).
413    * @param replicaId
414    * @param newFormat should we create the region name in the new format
415    * @return Region name made of passed tableName, startKey, id and replicaId
416    */
417   public static byte [] createRegionName(final TableName tableName,
418       final byte [] startKey, final byte [] id, final int replicaId, boolean newFormat) {
419     int len = tableName.getName().length + 2 + id.length +
420         (startKey == null? 0: startKey.length);
421     if (newFormat) {
422       len += MD5_HEX_LENGTH + 2;
423     }
424     byte[] replicaIdBytes = null;
425     // Special casing: replicaId is only appended if replicaId is greater than
426     // 0. This is because all regions in meta would have to be migrated to the new
427     // name otherwise
428     if (replicaId > 0) {
429       // use string representation for replica id
430       replicaIdBytes = Bytes.toBytes(String.format(REPLICA_ID_FORMAT, replicaId));
431       len += 1 + replicaIdBytes.length;
432     }
433 
434     byte [] b = new byte [len];
435 
436     int offset = tableName.getName().length;
437     System.arraycopy(tableName.getName(), 0, b, 0, offset);
438     b[offset++] = HConstants.DELIMITER;
439     if (startKey != null && startKey.length > 0) {
440       System.arraycopy(startKey, 0, b, offset, startKey.length);
441       offset += startKey.length;
442     }
443     b[offset++] = HConstants.DELIMITER;
444     System.arraycopy(id, 0, b, offset, id.length);
445     offset += id.length;
446 
447     if (replicaIdBytes != null) {
448       b[offset++] = REPLICA_ID_DELIMITER;
449       System.arraycopy(replicaIdBytes, 0, b, offset, replicaIdBytes.length);
450       offset += replicaIdBytes.length;
451     }
452 
453     if (newFormat) {
454       //
455       // Encoded name should be built into the region name.
456       //
457       // Use the region name thus far (namely, <tablename>,<startKey>,<id>_<replicaId>)
458       // to compute a MD5 hash to be used as the encoded name, and append
459       // it to the byte buffer.
460       //
461       String md5Hash = MD5Hash.getMD5AsHex(b, 0, offset);
462       byte [] md5HashBytes = Bytes.toBytes(md5Hash);
463 
464       if (md5HashBytes.length != MD5_HEX_LENGTH) {
465         LOG.error("MD5-hash length mismatch: Expected=" + MD5_HEX_LENGTH +
466                   "; Got=" + md5HashBytes.length);
467       }
468 
469       // now append the bytes '.<encodedName>.' to the end
470       b[offset++] = ENC_SEPARATOR;
471       System.arraycopy(md5HashBytes, 0, b, offset, MD5_HEX_LENGTH);
472       offset += MD5_HEX_LENGTH;
473       b[offset++] = ENC_SEPARATOR;
474     }
475 
476     return b;
477   }
478 
479   /**
480    * Gets the table name from the specified region name.
481    * @param regionName to extract the table name from
482    * @return Table name
483    */
484   public static TableName getTable(final byte [] regionName) {
485     int offset = -1;
486     for (int i = 0; i < regionName.length; i++) {
487       if (regionName[i] == HConstants.DELIMITER) {
488         offset = i;
489         break;
490       }
491     }
492     byte[] buff  = new byte[offset];
493     System.arraycopy(regionName, 0, buff, 0, offset);
494     return TableName.valueOf(buff);
495   }
496 
497   /**
498    * Gets the start key from the specified region name.
499    * @param regionName
500    * @return Start key.
501    */
502   public static byte[] getStartKey(final byte[] regionName) throws IOException {
503     return parseRegionName(regionName)[1];
504   }
505 
506   /**
507    * Separate elements of a regionName.
508    * @param regionName
509    * @return Array of byte[] containing tableName, startKey and id
510    * @throws IOException
511    */
512   public static byte [][] parseRegionName(final byte [] regionName)
513   throws IOException {
514     // Region name is of the format:
515     // tablename,startkey,regionIdTimestamp[_replicaId][.encodedName.]
516     // startkey can contain the delimiter (',') so we parse from the start and end
517 
518     // parse from start
519     int offset = -1;
520     for (int i = 0; i < regionName.length; i++) {
521       if (regionName[i] == HConstants.DELIMITER) {
522         offset = i;
523         break;
524       }
525     }
526     if (offset == -1) {
527       throw new IOException(INVALID_REGION_NAME_FORMAT_MESSAGE
528         + ": " + Bytes.toStringBinary(regionName));
529     }
530     byte[] tableName = new byte[offset];
531     System.arraycopy(regionName, 0, tableName, 0, offset);
532     offset = -1;
533 
534     int endOffset = regionName.length;
535     // check whether regionName contains encodedName
536     if (regionName.length > MD5_HEX_LENGTH + 2
537         && regionName[regionName.length-1] == ENC_SEPARATOR
538         && regionName[regionName.length-MD5_HEX_LENGTH-2] == ENC_SEPARATOR) {
539       endOffset = endOffset - MD5_HEX_LENGTH - 2;
540     }
541 
542     // parse from end
543     byte[] replicaId = null;
544     int idEndOffset = endOffset;
545     for (int i = endOffset - 1; i > 0; i--) {
546       if (regionName[i] == REPLICA_ID_DELIMITER) { //replicaId may or may not be present
547         replicaId = new byte[endOffset - i - 1];
548         System.arraycopy(regionName, i + 1, replicaId, 0,
549           endOffset - i - 1);
550         idEndOffset = i;
551         // do not break, continue to search for id
552       }
553       if (regionName[i] == HConstants.DELIMITER) {
554         offset = i;
555         break;
556       }
557     }
558     if (offset == -1) {
559       throw new IOException(INVALID_REGION_NAME_FORMAT_MESSAGE
560         + ": " + Bytes.toStringBinary(regionName));
561     }
562     byte [] startKey = HConstants.EMPTY_BYTE_ARRAY;
563     if(offset != tableName.length + 1) {
564       startKey = new byte[offset - tableName.length - 1];
565       System.arraycopy(regionName, tableName.length + 1, startKey, 0,
566           offset - tableName.length - 1);
567     }
568     byte [] id = new byte[idEndOffset - offset - 1];
569     System.arraycopy(regionName, offset + 1, id, 0,
570       idEndOffset - offset - 1);
571     byte [][] elements = new byte[replicaId == null ? 3 : 4][];
572     elements[0] = tableName;
573     elements[1] = startKey;
574     elements[2] = id;
575     if (replicaId != null) {
576       elements[3] = replicaId;
577     }
578 
579     return elements;
580   }
581 
582   /** @return the regionId */
583   public long getRegionId(){
584     return regionId;
585   }
586 
587   /**
588    * @return the regionName as an array of bytes.
589    * @see #getRegionNameAsString()
590    */
591   public byte [] getRegionName(){
592     return regionName;
593   }
594 
595   /**
596    * @return Region name as a String for use in logging, etc.
597    */
598   public String getRegionNameAsString() {
599     if (hasEncodedName(this.regionName)) {
600       // new format region names already have their encoded name.
601       return Bytes.toStringBinary(this.regionName);
602     }
603 
604     // old format. regionNameStr doesn't have the region name.
605     //
606     //
607     return Bytes.toStringBinary(this.regionName) + "." + this.getEncodedName();
608   }
609 
610   /** @return the encoded region name */
611   public synchronized String getEncodedName() {
612     if (this.encodedName == null) {
613       this.encodedName = encodeRegionName(this.regionName);
614     }
615     return this.encodedName;
616   }
617 
618   public synchronized byte [] getEncodedNameAsBytes() {
619     if (this.encodedNameAsBytes == null) {
620       this.encodedNameAsBytes = Bytes.toBytes(getEncodedName());
621     }
622     return this.encodedNameAsBytes;
623   }
624 
625   /** @return the startKey */
626   public byte [] getStartKey(){
627     return startKey;
628   }
629 
630   /** @return the endKey */
631   public byte [] getEndKey(){
632     return endKey;
633   }
634 
635   /**
636    * Get current table name of the region
637    * @return TableName
638    */
639   public TableName getTable() {
640     // This method name should be getTableName but there was already a method getTableName
641     // that returned a byte array.  It is unfortunate given everywhere else, getTableName returns
642     // a TableName instance.
643     if (tableName == null || tableName.getName().length == 0) {
644       tableName = getTable(getRegionName());
645     }
646     return this.tableName;
647   }
648 
649   /**
650    * Returns true if the given inclusive range of rows is fully contained
651    * by this region. For example, if the region is foo,a,g and this is
652    * passed ["b","c"] or ["a","c"] it will return true, but if this is passed
653    * ["b","z"] it will return false.
654    * @throws IllegalArgumentException if the range passed is invalid (ie. end &lt; start)
655    */
656   public boolean containsRange(byte[] rangeStartKey, byte[] rangeEndKey) {
657     if (Bytes.compareTo(rangeStartKey, rangeEndKey) > 0) {
658       throw new IllegalArgumentException(
659       "Invalid range: " + Bytes.toStringBinary(rangeStartKey) +
660       " > " + Bytes.toStringBinary(rangeEndKey));
661     }
662 
663     boolean firstKeyInRange = Bytes.compareTo(rangeStartKey, startKey) >= 0;
664     boolean lastKeyInRange =
665       Bytes.compareTo(rangeEndKey, endKey) < 0 ||
666       Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY);
667     return firstKeyInRange && lastKeyInRange;
668   }
669 
670   /**
671    * Return true if the given row falls in this region.
672    */
673   public boolean containsRow(byte[] row) {
674     return Bytes.compareTo(row, startKey) >= 0 &&
675       (Bytes.compareTo(row, endKey) < 0 ||
676        Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY));
677   }
678 
679   /**
680    * @return true if this region is from hbase:meta
681    */
682   public boolean isMetaTable() {
683     return isMetaRegion();
684   }
685 
686   /** @return true if this region is a meta region */
687   public boolean isMetaRegion() {
688      return tableName.equals(HRegionInfo.FIRST_META_REGIONINFO.getTable());
689   }
690 
691   /**
692    * @return true if this region is from a system table
693    */
694   public boolean isSystemTable() {
695     return tableName.isSystemTable();
696   }
697 
698   /**
699    * @return True if has been split and has daughters.
700    */
701   public boolean isSplit() {
702     return this.split;
703   }
704 
705   /**
706    * @param split set split status
707    */
708   public void setSplit(boolean split) {
709     this.split = split;
710   }
711 
712   /**
713    * @return True if this region is offline.
714    */
715   public boolean isOffline() {
716     return this.offLine;
717   }
718 
719   /**
720    * The parent of a region split is offline while split daughters hold
721    * references to the parent. Offlined regions are closed.
722    * @param offLine Set online/offline status.
723    */
724   public void setOffline(boolean offLine) {
725     this.offLine = offLine;
726   }
727 
728   /**
729    * @return True if this is a split parent region.
730    */
731   public boolean isSplitParent() {
732     if (!isSplit()) return false;
733     if (!isOffline()) {
734       LOG.warn("Region is split but NOT offline: " + getRegionNameAsString());
735     }
736     return true;
737   }
738 
739   /**
740    * Returns the region replica id
741    * @return returns region replica id
742    */
743   public int getReplicaId() {
744     return replicaId;
745   }
746 
747   /**
748    * @see java.lang.Object#toString()
749    */
750   @Override
751   public String toString() {
752     return "{ENCODED => " + getEncodedName() + ", " +
753       HConstants.NAME + " => '" + Bytes.toStringBinary(this.regionName)
754       + "', STARTKEY => '" +
755       Bytes.toStringBinary(this.startKey) + "', ENDKEY => '" +
756       Bytes.toStringBinary(this.endKey) + "'" +
757       (isOffline()? ", OFFLINE => true": "") +
758       (isSplit()? ", SPLIT => true": "") +
759       ((replicaId > 0)? ", REPLICA_ID => " + replicaId : "") + "}";
760   }
761 
762   /**
763    * @see java.lang.Object#equals(java.lang.Object)
764    */
765   @Override
766   public boolean equals(Object o) {
767     if (this == o) {
768       return true;
769     }
770     if (o == null) {
771       return false;
772     }
773     if (!(o instanceof HRegionInfo)) {
774       return false;
775     }
776     return this.compareTo((HRegionInfo)o) == 0;
777   }
778 
779   /**
780    * @see java.lang.Object#hashCode()
781    */
782   @Override
783   public int hashCode() {
784     return this.hashCode;
785   }
786 
787   //
788   // Comparable
789   //
790 
791   @Override
792   public int compareTo(HRegionInfo o) {
793     if (o == null) {
794       return 1;
795     }
796 
797     // Are regions of same table?
798     int result = this.tableName.compareTo(o.tableName);
799     if (result != 0) {
800       return result;
801     }
802 
803     // Compare start keys.
804     result = Bytes.compareTo(this.startKey, o.startKey);
805     if (result != 0) {
806       return result;
807     }
808 
809     // Compare end keys.
810     result = Bytes.compareTo(this.endKey, o.endKey);
811 
812     if (result != 0) {
813       if (this.getStartKey().length != 0
814               && this.getEndKey().length == 0) {
815           return 1; // this is last region
816       }
817       if (o.getStartKey().length != 0
818               && o.getEndKey().length == 0) {
819           return -1; // o is the last region
820       }
821       return result;
822     }
823 
824     // regionId is usually milli timestamp -- this defines older stamps
825     // to be "smaller" than newer stamps in sort order.
826     if (this.regionId > o.regionId) {
827       return 1;
828     } else if (this.regionId < o.regionId) {
829       return -1;
830     }
831 
832     int replicaDiff = this.getReplicaId() - o.getReplicaId();
833     if (replicaDiff != 0) return replicaDiff;
834 
835     if (this.offLine == o.offLine)
836       return 0;
837     if (this.offLine == true) return -1;
838 
839     return 1;
840   }
841 
842   /**
843    * @return Comparator to use comparing {@link KeyValue}s.
844    * @deprecated Use Region#getCellComparator().  deprecated for hbase 2.0, remove for hbase 3.0
845    */
846   @Deprecated
847   public KVComparator getComparator() {
848     return isMetaRegion()?
849         KeyValue.META_COMPARATOR: KeyValue.COMPARATOR;
850   }
851 
852   /**
853    * Convert a HRegionInfo to the protobuf RegionInfo
854    *
855    * @return the converted RegionInfo
856    */
857   RegionInfo convert() {
858     return convert(this);
859   }
860 
861   /**
862    * Convert a HRegionInfo to a RegionInfo
863    *
864    * @param info the HRegionInfo to convert
865    * @return the converted RegionInfo
866    */
867   public static RegionInfo convert(final HRegionInfo info) {
868     if (info == null) return null;
869     RegionInfo.Builder builder = RegionInfo.newBuilder();
870     builder.setTableName(ProtobufUtil.toProtoTableName(info.getTable()));
871     builder.setRegionId(info.getRegionId());
872     if (info.getStartKey() != null) {
873       builder.setStartKey(ByteStringer.wrap(info.getStartKey()));
874     }
875     if (info.getEndKey() != null) {
876       builder.setEndKey(ByteStringer.wrap(info.getEndKey()));
877     }
878     builder.setOffline(info.isOffline());
879     builder.setSplit(info.isSplit());
880     builder.setReplicaId(info.getReplicaId());
881     return builder.build();
882   }
883 
884   /**
885    * Convert a RegionInfo to a HRegionInfo
886    *
887    * @param proto the RegionInfo to convert
888    * @return the converted HRegionInfho
889    */
890   public static HRegionInfo convert(final RegionInfo proto) {
891     if (proto == null) return null;
892     TableName tableName =
893         ProtobufUtil.toTableName(proto.getTableName());
894     if (tableName.equals(TableName.META_TABLE_NAME)) {
895       return RegionReplicaUtil.getRegionInfoForReplica(FIRST_META_REGIONINFO,
896           proto.getReplicaId());
897     }
898     long regionId = proto.getRegionId();
899     int replicaId = proto.hasReplicaId() ? proto.getReplicaId() : DEFAULT_REPLICA_ID;
900     byte[] startKey = null;
901     byte[] endKey = null;
902     if (proto.hasStartKey()) {
903       startKey = proto.getStartKey().toByteArray();
904     }
905     if (proto.hasEndKey()) {
906       endKey = proto.getEndKey().toByteArray();
907     }
908     boolean split = false;
909     if (proto.hasSplit()) {
910       split = proto.getSplit();
911     }
912     HRegionInfo hri = new HRegionInfo(
913         tableName,
914         startKey,
915         endKey, split, regionId, replicaId);
916     if (proto.hasOffline()) {
917       hri.setOffline(proto.getOffline());
918     }
919     return hri;
920   }
921 
922   /**
923    * @return This instance serialized as protobuf w/ a magic pb prefix.
924    * @see #parseFrom(byte[])
925    */
926   public byte [] toByteArray() {
927     byte [] bytes = convert().toByteArray();
928     return ProtobufUtil.prependPBMagic(bytes);
929   }
930 
931   /**
932    * @return A deserialized {@link HRegionInfo}
933    * or null if we failed deserialize or passed bytes null
934    * @see #toByteArray()
935    */
936   public static HRegionInfo parseFromOrNull(final byte [] bytes) {
937     if (bytes == null) return null;
938     return parseFromOrNull(bytes, 0, bytes.length);
939   }
940 
941   /**
942    * @return A deserialized {@link HRegionInfo} or null
943    *  if we failed deserialize or passed bytes null
944    * @see #toByteArray()
945    */
946   public static HRegionInfo parseFromOrNull(final byte [] bytes, int offset, int len) {
947     if (bytes == null || len <= 0) return null;
948     try {
949       return parseFrom(bytes, offset, len);
950     } catch (DeserializationException e) {
951       return null;
952     }
953   }
954 
955   /**
956    * @param bytes A pb RegionInfo serialized with a pb magic prefix.
957    * @return A deserialized {@link HRegionInfo}
958    * @throws DeserializationException
959    * @see #toByteArray()
960    */
961   public static HRegionInfo parseFrom(final byte [] bytes) throws DeserializationException {
962     if (bytes == null) return null;
963     return parseFrom(bytes, 0, bytes.length);
964   }
965 
966   /**
967    * @param bytes A pb RegionInfo serialized with a pb magic prefix.
968    * @param offset starting point in the byte array
969    * @param len length to read on the byte array
970    * @return A deserialized {@link HRegionInfo}
971    * @throws DeserializationException
972    * @see #toByteArray()
973    */
974   public static HRegionInfo parseFrom(final byte [] bytes, int offset, int len)
975       throws DeserializationException {
976     if (ProtobufUtil.isPBMagicPrefix(bytes, offset, len)) {
977       int pblen = ProtobufUtil.lengthOfPBMagic();
978       try {
979         HBaseProtos.RegionInfo.Builder builder = HBaseProtos.RegionInfo.newBuilder();
980         ProtobufUtil.mergeFrom(builder, bytes, pblen + offset, len - pblen);
981         HBaseProtos.RegionInfo ri = builder.build();
982         return convert(ri);
983       } catch (IOException e) {
984         throw new DeserializationException(e);
985       }
986     } else {
987       throw new DeserializationException("PB encoded HRegionInfo expected");
988     }
989   }
990 
991   /**
992    * Use this instead of {@link #toByteArray()} when writing to a stream and you want to use
993    * the pb mergeDelimitedFrom (w/o the delimiter, pb reads to EOF which may not be what you want).
994    * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
995    * @throws IOException
996    * @see #toByteArray()
997    */
998   public byte [] toDelimitedByteArray() throws IOException {
999     return ProtobufUtil.toDelimitedByteArray(convert());
1000   }
1001 
1002   /**
1003    * Get the descriptive name as {@link RegionState} does it but with hidden
1004    * startkey optionally
1005    * @param state
1006    * @param conf
1007    * @return descriptive string
1008    */
1009   public static String getDescriptiveNameFromRegionStateForDisplay(RegionState state,
1010       Configuration conf) {
1011     if (conf.getBoolean(DISPLAY_KEYS_KEY, true)) return state.toDescriptiveString();
1012     String descriptiveStringFromState = state.toDescriptiveString();
1013     int idx = descriptiveStringFromState.lastIndexOf(" state=");
1014     String regionName = getRegionNameAsStringForDisplay(state.getRegion(), conf);
1015     return regionName + descriptiveStringFromState.substring(idx);
1016   }
1017 
1018   /**
1019    * Get the end key for display. Optionally hide the real end key.
1020    * @param hri
1021    * @param conf
1022    * @return the endkey
1023    */
1024   public static byte[] getEndKeyForDisplay(HRegionInfo hri, Configuration conf) {
1025     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1026     if (displayKey) return hri.getEndKey();
1027     return HIDDEN_END_KEY;
1028   }
1029 
1030   /**
1031    * Get the start key for display. Optionally hide the real start key.
1032    * @param hri
1033    * @param conf
1034    * @return the startkey
1035    */
1036   public static byte[] getStartKeyForDisplay(HRegionInfo hri, Configuration conf) {
1037     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1038     if (displayKey) return hri.getStartKey();
1039     return HIDDEN_START_KEY;
1040   }
1041 
1042   /**
1043    * Get the region name for display. Optionally hide the start key.
1044    * @param hri
1045    * @param conf
1046    * @return region name as String
1047    */
1048   public static String getRegionNameAsStringForDisplay(HRegionInfo hri, Configuration conf) {
1049     return Bytes.toStringBinary(getRegionNameForDisplay(hri, conf));
1050   }
1051 
1052   /**
1053    * Get the region name for display. Optionally hide the start key.
1054    * @param hri
1055    * @param conf
1056    * @return region name bytes
1057    */
1058   public static byte[] getRegionNameForDisplay(HRegionInfo hri, Configuration conf) {
1059     boolean displayKey = conf.getBoolean(DISPLAY_KEYS_KEY, true);
1060     if (displayKey || hri.getTable().equals(TableName.META_TABLE_NAME)) {
1061       return hri.getRegionName();
1062     } else {
1063       // create a modified regionname with the startkey replaced but preserving
1064       // the other parts including the encodedname.
1065       try {
1066         byte[][]regionNameParts = parseRegionName(hri.getRegionName());
1067         regionNameParts[1] = HIDDEN_START_KEY; //replace the real startkey
1068         int len = 0;
1069         // get the total length
1070         for (byte[] b : regionNameParts) {
1071           len += b.length;
1072         }
1073         byte[] encodedRegionName =
1074             Bytes.toBytes(encodeRegionName(hri.getRegionName()));
1075         len += encodedRegionName.length;
1076         //allocate some extra bytes for the delimiters and the last '.'
1077         byte[] modifiedName = new byte[len + regionNameParts.length + 1];
1078         int lengthSoFar = 0;
1079         int loopCount = 0;
1080         for (byte[] b : regionNameParts) {
1081           System.arraycopy(b, 0, modifiedName, lengthSoFar, b.length);
1082           lengthSoFar += b.length;
1083           if (loopCount++ == 2) modifiedName[lengthSoFar++] = REPLICA_ID_DELIMITER;
1084           else  modifiedName[lengthSoFar++] = HConstants.DELIMITER;
1085         }
1086         // replace the last comma with '.'
1087         modifiedName[lengthSoFar - 1] = ENC_SEPARATOR;
1088         System.arraycopy(encodedRegionName, 0, modifiedName, lengthSoFar,
1089             encodedRegionName.length);
1090         lengthSoFar += encodedRegionName.length;
1091         modifiedName[lengthSoFar] = ENC_SEPARATOR;
1092         return modifiedName;
1093       } catch (IOException e) {
1094         //LOG.warn("Encountered exception " + e);
1095         throw new RuntimeException(e);
1096       }
1097     }
1098   }
1099 
1100   /**
1101    * Parses an HRegionInfo instance from the passed in stream.  Presumes the HRegionInfo was
1102    * serialized to the stream with {@link #toDelimitedByteArray()}
1103    * @param in
1104    * @return An instance of HRegionInfo.
1105    * @throws IOException
1106    */
1107   public static HRegionInfo parseFrom(final DataInputStream in) throws IOException {
1108     // I need to be able to move back in the stream if this is not a pb serialization so I can
1109     // do the Writable decoding instead.
1110     int pblen = ProtobufUtil.lengthOfPBMagic();
1111     byte [] pbuf = new byte[pblen];
1112     if (in.markSupported()) { //read it with mark()
1113       in.mark(pblen);
1114     }
1115 
1116     //assumption: if Writable serialization, it should be longer than pblen.
1117     int read = in.read(pbuf);
1118     if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
1119     if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
1120       return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
1121     } else {
1122       throw new IOException("PB encoded HRegionInfo expected");
1123     }
1124   }
1125 
1126   /**
1127    * Serializes given HRegionInfo's as a byte array. Use this instead of {@link #toByteArray()} when
1128    * writing to a stream and you want to use the pb mergeDelimitedFrom (w/o the delimiter, pb reads
1129    * to EOF which may not be what you want). {@link #parseDelimitedFrom(byte[], int, int)} can
1130    * be used to read back the instances.
1131    * @param infos HRegionInfo objects to serialize
1132    * @return This instance serialized as a delimited protobuf w/ a magic pb prefix.
1133    * @throws IOException
1134    * @see #toByteArray()
1135    */
1136   public static byte[] toDelimitedByteArray(HRegionInfo... infos) throws IOException {
1137     byte[][] bytes = new byte[infos.length][];
1138     int size = 0;
1139     for (int i = 0; i < infos.length; i++) {
1140       bytes[i] = infos[i].toDelimitedByteArray();
1141       size += bytes[i].length;
1142     }
1143 
1144     byte[] result = new byte[size];
1145     int offset = 0;
1146     for (byte[] b : bytes) {
1147       System.arraycopy(b, 0, result, offset, b.length);
1148       offset += b.length;
1149     }
1150     return result;
1151   }
1152 
1153   /**
1154    * Parses all the HRegionInfo instances from the passed in stream until EOF. Presumes the
1155    * HRegionInfo's were serialized to the stream with {@link #toDelimitedByteArray()}
1156    * @param bytes serialized bytes
1157    * @param offset the start offset into the byte[] buffer
1158    * @param length how far we should read into the byte[] buffer
1159    * @return All the hregioninfos that are in the byte array. Keeps reading till we hit the end.
1160    */
1161   public static List<HRegionInfo> parseDelimitedFrom(final byte[] bytes, final int offset,
1162       final int length) throws IOException {
1163     if (bytes == null) {
1164       throw new IllegalArgumentException("Can't build an object with empty bytes array");
1165     }
1166     DataInputBuffer in = new DataInputBuffer();
1167     List<HRegionInfo> hris = new ArrayList<HRegionInfo>();
1168     try {
1169       in.reset(bytes, offset, length);
1170       while (in.available() > 0) {
1171         HRegionInfo hri = parseFrom(in);
1172         hris.add(hri);
1173       }
1174     } finally {
1175       in.close();
1176     }
1177     return hris;
1178   }
1179 
1180   /**
1181    * Check whether two regions are adjacent
1182    * @param regionA
1183    * @param regionB
1184    * @return true if two regions are adjacent
1185    */
1186   public static boolean areAdjacent(HRegionInfo regionA, HRegionInfo regionB) {
1187     if (regionA == null || regionB == null) {
1188       throw new IllegalArgumentException(
1189           "Can't check whether adjacent for null region");
1190     }
1191     HRegionInfo a = regionA;
1192     HRegionInfo b = regionB;
1193     if (Bytes.compareTo(a.getStartKey(), b.getStartKey()) > 0) {
1194       a = regionB;
1195       b = regionA;
1196     }
1197     if (Bytes.compareTo(a.getEndKey(), b.getStartKey()) == 0) {
1198       return true;
1199     }
1200     return false;
1201   }
1202 }