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.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
027 * HRegionServer.  Immutable.  Comparable, but we compare the 'location' only:
028 * i.e. the hostname and port, and *not* the regioninfo.  This means two
029 * instances are the same if they refer to the same 'location' (the same
030 * hostname and port), though they may be carrying different regions.
031 *
032 * On a big cluster, each client will have thousands of instances of this object, often
033 *  100 000 of them if not million. It's important to keep the object size as small
034 *  as possible.
035 *
036 * <br>This interface has been marked InterfaceAudience.Public in 0.96 and 0.98.
037 */
038@InterfaceAudience.Public
039public class HRegionLocation implements Comparable<HRegionLocation> {
040  private final RegionInfo regionInfo;
041  private final ServerName serverName;
042  private final long seqNum;
043
044  public HRegionLocation(RegionInfo regionInfo, ServerName serverName) {
045    this(regionInfo, serverName, HConstants.NO_SEQNUM);
046  }
047
048  public HRegionLocation(RegionInfo regionInfo, ServerName serverName, long seqNum) {
049    this.regionInfo = regionInfo;
050    this.serverName = serverName;
051    this.seqNum = seqNum;
052  }
053
054  /**
055   * @see java.lang.Object#toString()
056   */
057  @Override
058  public String toString() {
059    return "region=" + (this.regionInfo == null ? "null" : this.regionInfo.getRegionNameAsString())
060        + ", hostname=" + this.serverName + ", seqNum=" + seqNum;
061  }
062
063  /**
064   * @see java.lang.Object#equals(java.lang.Object)
065   */
066  @Override
067  public boolean equals(Object o) {
068    if (this == o) {
069      return true;
070    }
071    if (o == null) {
072      return false;
073    }
074    if (!(o instanceof HRegionLocation)) {
075      return false;
076    }
077    return this.compareTo((HRegionLocation)o) == 0;
078  }
079
080  /**
081   * @see java.lang.Object#hashCode()
082   */
083  @Override
084  public int hashCode() {
085    return this.serverName.hashCode();
086  }
087
088  /**
089   * @return regionInfo
090   */
091  public RegionInfo getRegion(){
092    return regionInfo;
093  }
094
095  public String getHostname() {
096    return this.serverName.getHostname();
097  }
098
099  public int getPort() {
100    return this.serverName.getPort();
101  }
102
103  public long getSeqNum() {
104    return seqNum;
105  }
106
107  /**
108   * @return String made of hostname and port formatted as
109   * per {@link Addressing#createHostAndPortStr(String, int)}
110   */
111  public String getHostnamePort() {
112    return Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
113  }
114
115  public ServerName getServerName() {
116    return serverName;
117  }
118
119  @Override
120  public int compareTo(HRegionLocation o) {
121    return serverName.compareTo(o.getServerName());
122  }
123}