001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase;
020
021import org.apache.hadoop.hbase.client.ImmutableHRegionInfo;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.hadoop.hbase.util.Addressing;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Data structure to hold RegionInfo and the address for the hosting
028 * HRegionServer.  Immutable.  Comparable, but we compare the 'location' only:
029 * i.e. the hostname and port, and *not* the regioninfo.  This means two
030 * instances are the same if they refer to the same 'location' (the same
031 * hostname and port), though they may be carrying different regions.
032 *
033 * On a big cluster, each client will have thousands of instances of this object, often
034 *  100 000 of them if not million. It's important to keep the object size as small
035 *  as possible.
036 *
037 * <br>This interface has been marked InterfaceAudience.Public in 0.96 and 0.98.
038 */
039@InterfaceAudience.Public
040public class HRegionLocation implements Comparable<HRegionLocation> {
041  private final RegionInfo regionInfo;
042  private final ServerName serverName;
043  private final long seqNum;
044
045  public HRegionLocation(RegionInfo regionInfo, ServerName serverName) {
046    this(regionInfo, serverName, HConstants.NO_SEQNUM);
047  }
048
049  public HRegionLocation(RegionInfo regionInfo, ServerName serverName, long seqNum) {
050    this.regionInfo = regionInfo;
051    this.serverName = serverName;
052    this.seqNum = seqNum;
053  }
054
055  /**
056   * @see java.lang.Object#toString()
057   */
058  @Override
059  public String toString() {
060    return "region=" + (this.regionInfo == null ? "null" : this.regionInfo.getRegionNameAsString())
061        + ", hostname=" + this.serverName + ", seqNum=" + seqNum;
062  }
063
064  /**
065   * @see java.lang.Object#equals(java.lang.Object)
066   */
067  @Override
068  public boolean equals(Object o) {
069    if (this == o) {
070      return true;
071    }
072    if (o == null) {
073      return false;
074    }
075    if (!(o instanceof HRegionLocation)) {
076      return false;
077    }
078    return this.compareTo((HRegionLocation)o) == 0;
079  }
080
081  /**
082   * @see java.lang.Object#hashCode()
083   */
084  @Override
085  public int hashCode() {
086    return this.serverName.hashCode();
087  }
088
089  /**
090   *
091   * @return Immutable HRegionInfo
092   * @deprecated Since 2.0.0. Will remove in 3.0.0. Use {@link #getRegion()}} instead.
093   */
094  @Deprecated
095  public HRegionInfo getRegionInfo(){
096    return regionInfo == null ? null : new ImmutableHRegionInfo(regionInfo);
097  }
098
099  /**
100   * @return regionInfo
101   */
102  public RegionInfo getRegion(){
103    return regionInfo;
104  }
105
106  public String getHostname() {
107    return this.serverName.getHostname();
108  }
109
110  public int getPort() {
111    return this.serverName.getPort();
112  }
113
114  public long getSeqNum() {
115    return seqNum;
116  }
117
118  /**
119   * @return String made of hostname and port formatted as
120   * per {@link Addressing#createHostAndPortStr(String, int)}
121   */
122  public String getHostnamePort() {
123    return Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
124  }
125
126  public ServerName getServerName() {
127    return serverName;
128  }
129
130  @Override
131  public int compareTo(HRegionLocation o) {
132    return serverName.compareTo(o.getServerName());
133  }
134}