View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.util.Addressing;
23  
24  /**
25   * Data structure to hold HRegionInfo and the address for the hosting
26   * HRegionServer.  Immutable.  Comparable, but we compare the 'location' only:
27   * i.e. the hostname and port, and *not* the regioninfo.  This means two
28   * instances are the same if they refer to the same 'location' (the same
29   * hostname and port), though they may be carrying different regions.
30   *
31   * On a big cluster, each client will have thousands of instances of this object, often
32   *  100 000 of them if not million. It's important to keep the object size as small
33   *  as possible.
34   * <br>This interface has been marked InterfaceAudience.Public in 0.96 and 0.98, it is
35   * no longer the case.
36   */
37  @InterfaceAudience.Private
38  public class HRegionLocation implements Comparable<HRegionLocation> {
39    private final HRegionInfo regionInfo;
40    private final ServerName serverName;
41    private final long seqNum;
42  
43    public HRegionLocation(HRegionInfo regionInfo, ServerName serverName) {
44      this(regionInfo, serverName, HConstants.NO_SEQNUM);
45    }
46  
47    public HRegionLocation(HRegionInfo regionInfo, ServerName serverName, long seqNum) {
48      this.regionInfo = regionInfo;
49      this.serverName = serverName;
50      this.seqNum = seqNum;
51    }
52  
53    /**
54     * @see java.lang.Object#toString()
55     */
56    @Override
57    public String toString() {
58      return "region=" + (this.regionInfo == null ? "null" : this.regionInfo.getRegionNameAsString())
59          + ", hostname=" + this.serverName + ", seqNum=" + seqNum;
60    }
61  
62    /**
63     * @see java.lang.Object#equals(java.lang.Object)
64     */
65    @Override
66    public boolean equals(Object o) {
67      if (this == o) {
68        return true;
69      }
70      if (o == null) {
71        return false;
72      }
73      if (!(o instanceof HRegionLocation)) {
74        return false;
75      }
76      return this.compareTo((HRegionLocation)o) == 0;
77    }
78  
79    /**
80     * @see java.lang.Object#hashCode()
81     */
82    @Override
83    public int hashCode() {
84      return this.serverName.hashCode();
85    }
86  
87    /** @return HRegionInfo */
88    public HRegionInfo getRegionInfo(){
89      return regionInfo;
90    }
91  
92    public String getHostname() {
93      return this.serverName.getHostname();
94    }
95  
96    public int getPort() {
97      return this.serverName.getPort();
98    }
99  
100   public long getSeqNum() {
101     return seqNum;
102   }
103 
104   /**
105    * @return String made of hostname and port formatted as per {@link Addressing#createHostAndPortStr(String, int)}
106    */
107   public String getHostnamePort() {
108     return Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
109   }
110 
111   public ServerName getServerName() {
112     return serverName;
113   }
114 
115   @Override
116   public int compareTo(HRegionLocation o) {
117     return serverName.compareTo(o.getServerName());
118   }
119 }