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.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.hbase.util.Addressing;
24  
25  /**
26   * Data structure to hold HRegionInfo and the address for the hosting
27   * HRegionServer.  Immutable.  Comparable, but we compare the 'location' only:
28   * i.e. the hostname and port, and *not* the regioninfo.  This means two
29   * instances are the same if they refer to the same 'location' (the same
30   * hostname and port), though they may be carrying different regions.
31   *
32   * On a big cluster, each client will have thousands of instances of this object, often
33   *  100 000 of them if not million. It's important to keep the object size as small
34   *  as possible.
35   *
36   * <br>This interface has been marked InterfaceAudience.Public in 0.96 and 0.98.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Evolving
40  public class HRegionLocation implements Comparable<HRegionLocation> {
41    private final HRegionInfo regionInfo;
42    private final ServerName serverName;
43    private final long seqNum;
44  
45    public HRegionLocation(HRegionInfo regionInfo, ServerName serverName) {
46      this(regionInfo, serverName, HConstants.NO_SEQNUM);
47    }
48  
49    public HRegionLocation(HRegionInfo regionInfo, ServerName serverName, long seqNum) {
50      this.regionInfo = regionInfo;
51      this.serverName = serverName;
52      this.seqNum = seqNum;
53    }
54  
55    /**
56     * @see java.lang.Object#toString()
57     */
58    @Override
59    public String toString() {
60      return "region=" + (this.regionInfo == null ? "null" : this.regionInfo.getRegionNameAsString())
61          + ", hostname=" + this.serverName + ", seqNum=" + seqNum;
62    }
63  
64    /**
65     * @see java.lang.Object#equals(java.lang.Object)
66     */
67    @Override
68    public boolean equals(Object o) {
69      if (this == o) {
70        return true;
71      }
72      if (o == null) {
73        return false;
74      }
75      if (!(o instanceof HRegionLocation)) {
76        return false;
77      }
78      return this.compareTo((HRegionLocation)o) == 0;
79    }
80  
81    /**
82     * @see java.lang.Object#hashCode()
83     */
84    @Override
85    public int hashCode() {
86      if (serverName == null) {
87        return System.identityHashCode(this);
88      }
89      return this.serverName.hashCode();
90    }
91  
92    /** @return HRegionInfo */
93    public HRegionInfo getRegionInfo(){
94      return regionInfo;
95    }
96  
97    public String getHostname() {
98      return this.serverName.getHostname();
99    }
100 
101   public int getPort() {
102     return this.serverName.getPort();
103   }
104 
105   public long getSeqNum() {
106     return seqNum;
107   }
108 
109   /**
110    * @return String made of hostname and port formatted as per {@link Addressing#createHostAndPortStr(String, int)}
111    */
112   public String getHostnamePort() {
113     return Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
114   }
115 
116   public ServerName getServerName() {
117     return serverName;
118   }
119 
120   @Override
121   public int compareTo(HRegionLocation o) {
122     if (serverName == null) {
123       if (o.serverName != null) {
124         return 1;
125       }
126       return 0;
127     } else {
128       return serverName.compareTo(o.getServerName());
129     }
130   }
131 }