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