001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.client;
020
021import java.util.Arrays;
022
023import org.apache.commons.lang3.ArrayUtils;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.util.Bytes;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031@InterfaceAudience.Private
032public class RegionInfoBuilder {
033  private static final Logger LOG = LoggerFactory.getLogger(RegionInfoBuilder.class);
034
035  /** A non-capture group so that this can be embedded. */
036  public static final String ENCODED_REGION_NAME_REGEX = "(?:[a-f0-9]+)";
037
038  private static final int MAX_REPLICA_ID = 0xFFFF;
039
040  //TODO: Move NO_HASH to HStoreFile which is really the only place it is used.
041  public static final String NO_HASH = null;
042
043  /**
044   * RegionInfo for first meta region
045   * You cannot use this builder to make an instance of the {@link #FIRST_META_REGIONINFO}.
046   * Just refer to this instance. Also, while the instance is actually a MutableRI, its type is
047   * just RI so the mutable methods are not available (unless you go casting); it appears
048   * as immutable (I tried adding Immutable type but it just makes a mess).
049   */
050  // TODO: How come Meta regions still do not have encoded region names? Fix.
051  // hbase:meta,,1.1588230740 should be the hbase:meta first region name.
052  public static final RegionInfo FIRST_META_REGIONINFO =
053    new MutableRegionInfo(1L, TableName.META_TABLE_NAME, RegionInfo.DEFAULT_REPLICA_ID);
054
055  private final TableName tableName;
056  private byte[] startKey = HConstants.EMPTY_START_ROW;
057  private byte[] endKey = HConstants.EMPTY_END_ROW;
058  private long regionId = System.currentTimeMillis();
059  private int replicaId = RegionInfo.DEFAULT_REPLICA_ID;
060  private boolean offLine = false;
061  private boolean split = false;
062  private byte[] regionName = null;
063  private String encodedName = null;
064
065  public static RegionInfoBuilder newBuilder(TableName tableName) {
066    return new RegionInfoBuilder(tableName);
067  }
068
069  public static RegionInfoBuilder newBuilder(RegionInfo regionInfo) {
070    return new RegionInfoBuilder(regionInfo);
071  }
072
073  private RegionInfoBuilder(TableName tableName) {
074    this.tableName = tableName;
075  }
076
077  private RegionInfoBuilder(RegionInfo regionInfo) {
078    this.tableName = regionInfo.getTable();
079    this.startKey = regionInfo.getStartKey();
080    this.endKey = regionInfo.getEndKey();
081    this.offLine = regionInfo.isOffline();
082    this.split = regionInfo.isSplit();
083    this.regionId = regionInfo.getRegionId();
084    this.replicaId = regionInfo.getReplicaId();
085    this.regionName = regionInfo.getRegionName();
086    this.encodedName = regionInfo.getEncodedName();
087  }
088
089  public RegionInfoBuilder setStartKey(byte[] startKey) {
090    this.startKey = startKey;
091    return this;
092  }
093
094  public RegionInfoBuilder setEndKey(byte[] endKey) {
095    this.endKey = endKey;
096    return this;
097  }
098
099  public RegionInfoBuilder setRegionId(long regionId) {
100    this.regionId = regionId;
101    return this;
102  }
103
104  public RegionInfoBuilder setReplicaId(int replicaId) {
105    this.replicaId = replicaId;
106    return this;
107  }
108
109  public RegionInfoBuilder setSplit(boolean split) {
110    this.split = split;
111    return this;
112  }
113
114  public RegionInfoBuilder setOffline(boolean offLine) {
115    this.offLine = offLine;
116    return this;
117  }
118
119  public RegionInfoBuilder setEncodedName(String encodedName) {
120    this.encodedName = encodedName;
121    return this;
122  }
123
124  public RegionInfo build() {
125    return new MutableRegionInfo(tableName, startKey, endKey, split,
126        regionId, replicaId, offLine, regionName, encodedName);
127  }
128
129  /**
130   * An implementation of RegionInfo that adds mutable methods so can build a RegionInfo instance.
131   */
132  @InterfaceAudience.Private
133  static class MutableRegionInfo implements RegionInfo, Comparable<RegionInfo> {
134    /**
135     * The new format for a region name contains its encodedName at the end.
136     * The encoded name also serves as the directory name for the region
137     * in the filesystem.
138     *
139     * New region name format:
140     *    &lt;tablename>,,&lt;startkey>,&lt;regionIdTimestamp>.&lt;encodedName>.
141     * where,
142     *    &lt;encodedName> is a hex version of the MD5 hash of
143     *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
144     *
145     * The old region name format:
146     *    &lt;tablename>,&lt;startkey>,&lt;regionIdTimestamp>
147     * For region names in the old format, the encoded name is a 32-bit
148     * JenkinsHash integer value (in its decimal notation, string form).
149     *<p>
150     * **NOTE**
151     *
152     * The first hbase:meta region, and regions created by an older
153     * version of HBase (0.20 or prior) will continue to use the
154     * old region name format.
155     */
156
157    // This flag is in the parent of a split while the parent is still referenced by daughter
158    // regions. We USED to set this flag when we disabled a table but now table state is kept up in
159    // zookeeper as of 0.90.0 HBase. And now in DisableTableProcedure, finally we will create bunch
160    // of UnassignProcedures and at the last of the procedure we will set the region state to
161    // CLOSED, and will not change the offLine flag.
162    private boolean offLine = false;
163    private boolean split = false;
164    private final long regionId;
165    private final int replicaId;
166    private final byte[] regionName;
167    private final byte[] startKey;
168    private final byte[] endKey;
169    private final int hashCode;
170    private final String encodedName;
171    private final byte[] encodedNameAsBytes;
172    private final TableName tableName;
173
174    private static int generateHashCode(final TableName tableName, final byte[] startKey,
175        final byte[] endKey, final long regionId,
176        final int replicaId, boolean offLine, byte[] regionName) {
177      int result = Arrays.hashCode(regionName);
178      result = (int) (result ^ regionId);
179      result ^= Arrays.hashCode(checkStartKey(startKey));
180      result ^= Arrays.hashCode(checkEndKey(endKey));
181      result ^= Boolean.valueOf(offLine).hashCode();
182      result ^= Arrays.hashCode(tableName.getName());
183      result ^= replicaId;
184      return result;
185    }
186
187    private static byte[] checkStartKey(byte[] startKey) {
188      return startKey == null? HConstants.EMPTY_START_ROW: startKey;
189    }
190
191    private static byte[] checkEndKey(byte[] endKey) {
192      return endKey == null? HConstants.EMPTY_END_ROW: endKey;
193    }
194
195    private static TableName checkTableName(TableName tableName) {
196      if (tableName == null) {
197        throw new IllegalArgumentException("TableName cannot be null");
198      }
199      return tableName;
200    }
201
202    private static int checkReplicaId(int regionId) {
203      if (regionId > MAX_REPLICA_ID) {
204        throw new IllegalArgumentException("ReplicaId cannot be greater than" + MAX_REPLICA_ID);
205      }
206      return regionId;
207    }
208
209    /**
210     * Private constructor used constructing MutableRegionInfo for the
211     * first meta regions
212     */
213    private MutableRegionInfo(long regionId, TableName tableName, int replicaId) {
214      this(tableName,
215          HConstants.EMPTY_START_ROW,
216          HConstants.EMPTY_END_ROW,
217          false,
218          regionId,
219          replicaId,
220          false,
221          RegionInfo.createRegionName(tableName, null, regionId, replicaId, false));
222    }
223
224    MutableRegionInfo(final TableName tableName, final byte[] startKey,
225        final byte[] endKey, final boolean split, final long regionId,
226        final int replicaId, boolean offLine, byte[] regionName) {
227      this(checkTableName(tableName),
228          checkStartKey(startKey),
229          checkEndKey(endKey),
230          split, regionId,
231          checkReplicaId(replicaId),
232          offLine,
233          regionName,
234          RegionInfo.encodeRegionName(regionName));
235    }
236
237    MutableRegionInfo(final TableName tableName, final byte[] startKey,
238        final byte[] endKey, final boolean split, final long regionId,
239        final int replicaId, boolean offLine, byte[] regionName, String encodedName) {
240      this.tableName = checkTableName(tableName);
241      this.startKey = checkStartKey(startKey);
242      this.endKey = checkEndKey(endKey);
243      this.split = split;
244      this.regionId = regionId;
245      this.replicaId = checkReplicaId(replicaId);
246      this.offLine = offLine;
247      if (ArrayUtils.isEmpty(regionName)) {
248        this.regionName = RegionInfo.createRegionName(this.tableName, this.startKey, this.regionId, this.replicaId,
249            !this.tableName.equals(TableName.META_TABLE_NAME));
250        this.encodedName = RegionInfo.encodeRegionName(this.regionName);
251      } else {
252        this.regionName = regionName;
253        this.encodedName = encodedName;
254      }
255      this.hashCode = generateHashCode(
256          this.tableName,
257          this.startKey,
258          this.endKey,
259          this.regionId,
260          this.replicaId,
261          this.offLine,
262          this.regionName);
263      this.encodedNameAsBytes = Bytes.toBytes(this.encodedName);
264    }
265    /**
266     * @return Return a short, printable name for this region
267     * (usually encoded name) for us logging.
268     */
269    @Override
270    public String getShortNameToLog() {
271      return RegionInfo.prettyPrint(this.getEncodedName());
272    }
273
274    /** @return the regionId */
275    @Override
276    public long getRegionId(){
277      return regionId;
278    }
279
280
281    /**
282     * @return the regionName as an array of bytes.
283     * @see #getRegionNameAsString()
284     */
285    @Override
286    public byte [] getRegionName(){
287      return regionName;
288    }
289
290    /**
291     * @return Region name as a String for use in logging, etc.
292     */
293    @Override
294    public String getRegionNameAsString() {
295      return RegionInfo.getRegionNameAsString(this, this.regionName);
296    }
297
298    /** @return the encoded region name */
299    @Override
300    public String getEncodedName() {
301      return this.encodedName;
302    }
303
304    @Override
305    public byte [] getEncodedNameAsBytes() {
306      return this.encodedNameAsBytes;
307    }
308
309    /** @return the startKey */
310    @Override
311    public byte [] getStartKey(){
312      return startKey;
313    }
314
315
316    /** @return the endKey */
317    @Override
318    public byte [] getEndKey(){
319      return endKey;
320    }
321
322    /**
323     * Get current table name of the region
324     * @return TableName
325     */
326    @Override
327    public TableName getTable() {
328      return this.tableName;
329    }
330
331    /**
332     * Returns true if the given inclusive range of rows is fully contained
333     * by this region. For example, if the region is foo,a,g and this is
334     * passed ["b","c"] or ["a","c"] it will return true, but if this is passed
335     * ["b","z"] it will return false.
336     * @throws IllegalArgumentException if the range passed is invalid (ie. end &lt; start)
337     */
338    @Override
339    public boolean containsRange(byte[] rangeStartKey, byte[] rangeEndKey) {
340      if (Bytes.compareTo(rangeStartKey, rangeEndKey) > 0) {
341        throw new IllegalArgumentException(
342        "Invalid range: " + Bytes.toStringBinary(rangeStartKey) +
343        " > " + Bytes.toStringBinary(rangeEndKey));
344      }
345
346      boolean firstKeyInRange = Bytes.compareTo(rangeStartKey, startKey) >= 0;
347      boolean lastKeyInRange =
348        Bytes.compareTo(rangeEndKey, endKey) < 0 ||
349        Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY);
350      return firstKeyInRange && lastKeyInRange;
351    }
352
353    /**
354     * Return true if the given row falls in this region.
355     */
356    @Override
357    public boolean containsRow(byte[] row) {
358      return Bytes.compareTo(row, startKey) >= 0 &&
359        (Bytes.compareTo(row, endKey) < 0 ||
360         Bytes.equals(endKey, HConstants.EMPTY_BYTE_ARRAY));
361    }
362
363    /** @return true if this region is a meta region */
364    @Override
365    public boolean isMetaRegion() {
366       return tableName.equals(FIRST_META_REGIONINFO.getTable());
367    }
368
369    /**
370     * @return True if has been split and has daughters.
371     */
372    @Override
373    public boolean isSplit() {
374      return this.split;
375    }
376
377    /**
378     * @param split set split status
379     * @return MutableRegionInfo
380     */
381    public MutableRegionInfo setSplit(boolean split) {
382      this.split = split;
383      return this;
384    }
385
386    /**
387     * @return True if this region is offline.
388     */
389    @Override
390    public boolean isOffline() {
391      return this.offLine;
392    }
393
394    /**
395     * The parent of a region split is offline while split daughters hold
396     * references to the parent. Offlined regions are closed.
397     * @param offLine Set online/offline status.
398     * @return MutableRegionInfo
399     */
400    public MutableRegionInfo setOffline(boolean offLine) {
401      this.offLine = offLine;
402      return this;
403    }
404
405    /**
406     * @return True if this is a split parent region.
407     */
408    @Override
409    public boolean isSplitParent() {
410      if (!isSplit()) return false;
411      if (!isOffline()) {
412        LOG.warn("Region is split but NOT offline: " + getRegionNameAsString());
413      }
414      return true;
415    }
416
417    /**
418     * Returns the region replica id
419     * @return returns region replica id
420     */
421    @Override
422    public int getReplicaId() {
423      return replicaId;
424    }
425
426    /**
427     * @see java.lang.Object#toString()
428     */
429    @Override
430    public String toString() {
431      return "{ENCODED => " + getEncodedName() + ", " +
432        HConstants.NAME + " => '" + Bytes.toStringBinary(this.regionName)
433        + "', STARTKEY => '" +
434        Bytes.toStringBinary(this.startKey) + "', ENDKEY => '" +
435        Bytes.toStringBinary(this.endKey) + "'" +
436        (isOffline()? ", OFFLINE => true": "") +
437        (isSplit()? ", SPLIT => true": "") +
438        ((replicaId > 0)? ", REPLICA_ID => " + replicaId : "") + "}";
439    }
440
441    /**
442     * @param o
443     * @see java.lang.Object#equals(java.lang.Object)
444     */
445    @Override
446    public boolean equals(Object o) {
447      if (this == o) {
448        return true;
449      }
450      if (o == null) {
451        return false;
452      }
453      if (!(o instanceof RegionInfo)) {
454        return false;
455      }
456      return this.compareTo((RegionInfo)o) == 0;
457    }
458
459    /**
460     * @see java.lang.Object#hashCode()
461     */
462    @Override
463    public int hashCode() {
464      return this.hashCode;
465    }
466
467    @Override
468    public int compareTo(RegionInfo other) {
469      return RegionInfo.COMPARATOR.compare(this, other);
470    }
471
472  }
473}