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.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  @InterfaceAudience.Public
33  @InterfaceStability.Evolving
34  public class HRegionLocation implements Comparable<HRegionLocation> {
35    private final HRegionInfo regionInfo;
36    private final ServerName serverName;
37    private final long seqNum;
38    // Cache of the 'toString' result.
39    private String cachedString = null;
40    // Cache of the hostname + port
41    private String cachedHostnamePort;
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 synchronized String toString() {
58      if (this.cachedString == null) {
59        this.cachedString = "region=" + this.regionInfo.getRegionNameAsString() +
60        ", hostname=" + this.serverName + ", seqNum=" + seqNum;
61      }
62      return this.cachedString;
63    }
64  
65    /**
66     * @see java.lang.Object#equals(java.lang.Object)
67     */
68    @Override
69    public boolean equals(Object o) {
70      if (this == o) {
71        return true;
72      }
73      if (o == null) {
74        return false;
75      }
76      if (!(o instanceof HRegionLocation)) {
77        return false;
78      }
79      return this.compareTo((HRegionLocation)o) == 0;
80    }
81  
82    /**
83     * @see java.lang.Object#hashCode()
84     */
85    @Override
86    public int hashCode() {
87      return this.serverName.hashCode();
88    }
89  
90    /** @return HRegionInfo */
91    public HRegionInfo getRegionInfo(){
92      return regionInfo;
93    }
94  
95    public String getHostname() {
96      return this.serverName.getHostname();
97    }
98  
99    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 per {@link Addressing#createHostAndPortStr(String, int)}
109    */
110   public synchronized String getHostnamePort() {
111     if (this.cachedHostnamePort == null) {
112       this.cachedHostnamePort =
113         Addressing.createHostAndPortStr(this.getHostname(), this.getPort());
114     }
115     return this.cachedHostnamePort;
116   }
117 
118   public ServerName getServerName() {
119     return serverName;
120   }
121 
122   public int compareTo(HRegionLocation o) {
123     return serverName.compareTo(o.getServerName());
124   }
125 }