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